Skip to content

Commit

Permalink
Fix handling of emtpy sink configs
Browse files Browse the repository at this point in the history
Toml parsing library can return nil in case the object on the list
that is instantiated doesn't have any properties set. This change
fixes that by creating default emtpy object so we don't crash.
  • Loading branch information
p2004a committed Dec 6, 2023
1 parent e3ed12f commit 0ddd46d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func parseTLSConfig(config *fTLSConfig, defaultServerName string, basePath strin
}

func parseMQTTSink(basePath string, sinkID int, sink *fMQTTSink) (*MQTTSink, error) {
if sink == nil {
sink = &fMQTTSink{}
}
res := &MQTTSink{}
if sink.Name == "" {
res.Name = fmt.Sprintf("unnamed-mqtt-sink-%d", sinkID)
Expand Down Expand Up @@ -196,6 +199,9 @@ func parseMQTTSink(basePath string, sinkID int, sink *fMQTTSink) (*MQTTSink, err
}

func parseCloudPubSubSink(basePath string, sinkID int, sink *fCloudPubSubSink) (*CloudPubSubSink, error) {
if sink == nil {
sink = &fCloudPubSubSink{}
}
ctx := context.Background()
res := &CloudPubSubSink{}
if sink.Name == "" {
Expand Down Expand Up @@ -246,6 +252,9 @@ func parseCloudPubSubSink(basePath string, sinkID int, sink *fCloudPubSubSink) (
}

func parseStdoutSink(sinkID int, sink *fStdoutSink) (*StdoutSink, error) {
if sink == nil {
sink = &fStdoutSink{}
}
res := &StdoutSink{}
if sink.Name == "" {
res.Name = fmt.Sprintf("unnamed-stdout-sink-%d", sinkID)
Expand Down
19 changes: 19 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ func TestParsingEmpty(t *testing.T) {
Name: "default-sink",
},
},
SensorAllowlist: nil,

Check failure on line 132 in pkg/config/config_test.go

View workflow job for this annotation

GitHub Actions / build-test-stdgo

unknown field SensorAllowlist in struct literal of type Config
}
if diff := cmpConfig(config, expectedConfig); diff != "" {
t.Errorf("unexpected difference:\n%v", diff)
}
}

func TestParsingEmptySink(t *testing.T) {
config, err := Read("testdata/test2/config.toml")
if err != nil {
t.Fatalf("Failed to parse config: %v", err)
}
expectedConfig := &Config{
Adapter: "hci0",
Sinks: []Sink{
&StdoutSink{
Name: "unnamed-stdout-sink-0",
},
},
}
if diff := cmpConfig(config, expectedConfig); diff != "" {
t.Errorf("unexpected difference:\n%v", diff)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/testdata/test2/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[sinks.stdout]]

0 comments on commit 0ddd46d

Please sign in to comment.