Skip to content

Commit ce69678

Browse files
mpfz0rbernd
authored andcommitted
Fix configuration parsing of lists (#328)
* Fix configuration parsing of lists Don't use `ucfg.Merge` because this will merge the contents of two slices by copying them on top of each other. Use a much simpler approach and just join the configuration strings together before parsing them. Fixes #327 * Handle configurations with yaml doc separators Yaml doc separators would get in the way of merging the configuration with the default. * Be more explicit about empty lists in the configuration
1 parent 400d97b commit ce69678

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

cfgfile/cfgfile.go

+16-18
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
package cfgfile
1717

1818
import (
19+
"bufio"
1920
"flag"
2021
"fmt"
21-
"io/ioutil"
2222
"os"
2323
"runtime"
2424
"strings"
@@ -39,24 +39,12 @@ func init() {
3939
validateConfiguration = flag.Bool("configtest", false, "Validate configuration file and exit.")
4040
}
4141

42-
func ConfigDefaults() *SidecarConfig {
43-
userConfig := SidecarConfig{}
44-
defaults, err := yaml.NewConfig([]byte(CommonDefaults), ucfg.PathSep("."))
45-
if err != nil {
46-
log.Fatal("Could not parse default config", err)
47-
}
42+
func ConfigDefaults() []byte {
43+
defaults := []byte(CommonDefaults)
4844
if runtime.GOOS == "windows" {
49-
winDefaults, err := yaml.NewConfig([]byte(WindowsDefaults), ucfg.PathSep("."))
50-
if err != nil {
51-
log.Fatal("Could not parse default config", err)
52-
}
53-
defaults.Merge(winDefaults)
45+
defaults = append(defaults, WindowsDefaults...)
5446
}
55-
err = defaults.Unpack(&userConfig)
56-
if err != nil {
57-
log.Fatal("Could not unpack default config", err)
58-
}
59-
return &userConfig
47+
return defaults
6048
}
6149

6250
// Read reads the configuration from a yaml file into the given interface structure.
@@ -67,10 +55,20 @@ func Read(out interface{}, path string) error {
6755
path = configurationFile
6856
}
6957

70-
filecontent, err := ioutil.ReadFile(path)
58+
configfile, err := os.Open(path)
7159
if err != nil {
7260
return fmt.Errorf("[ConfigFile] Failed to read %s: %v. Exiting.", path, err)
7361
}
62+
// start with configuration defaults
63+
filecontent := ConfigDefaults()
64+
65+
// append configuration, but strip away possible yaml doc separators
66+
scanner := bufio.NewScanner(configfile)
67+
for scanner.Scan() {
68+
if line := scanner.Text(); line != "---" {
69+
filecontent = append(filecontent, []byte(line+"\n")...)
70+
}
71+
}
7472
filecontent = expandEnv(filecontent)
7573

7674
config, err := yaml.NewConfig(filecontent, ucfg.PathSep("."))

cfgfile/schema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ node_id: "file:/etc/graylog/sidecar/node-id"
4141
update_interval: 10
4242
tls_skip_verify: false
4343
send_status: true
44-
list_log_files:
44+
list_log_files: []
4545
cache_path: "/var/cache/graylog-sidecar"
4646
log_path: "/var/log/graylog-sidecar"
4747
log_rotate_max_file_size: "10MiB"

context/context.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ type Ctx struct {
4040

4141
func NewContext() *Ctx {
4242
return &Ctx{
43-
Inventory: system.NewInventory(),
44-
UserConfig: cfgfile.ConfigDefaults(),
43+
Inventory: system.NewInventory(),
4544
}
4645
}
4746

sidecar-example.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ server_api_token: ""
4242
# - "/opt/app/logs"
4343
#
4444
# Default: empty list
45-
#list_log_files:
45+
#list_log_files: []
4646

4747
# Directory where the sidecar stores internal data.
4848
#cache_path: "/var/cache/graylog-sidecar"
@@ -67,7 +67,7 @@ server_api_token: ""
6767
# - "/opt/collectors/*"
6868
#
6969
# Example disable whitelisting:
70-
# collector_binaries_whitelist:
70+
# collector_binaries_whitelist: []
7171
#
7272
# Default:
7373
# collector_binaries_whitelist:

sidecar-windows-example.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ send_status: <SENDSTATUS>
4747
# - "/opt/app/logs"
4848
#
4949
# Default: empty list
50-
#list_log_files:
50+
#list_log_files: []
5151

5252
# Directory where the sidecar stores internal data.
5353
#cache_path: "C:\\Program Files\\Graylog\\sidecar\\cache"
@@ -72,7 +72,7 @@ send_status: <SENDSTATUS>
7272
# - "C:\\Program Files\\Filebeat\\filebeat.exe"
7373
#
7474
# Example disable whitelisting:
75-
# collector_binaries_whitelist:
75+
# collector_binaries_whitelist: []
7676
#
7777
# Default:
7878
# collector_binaries_whitelist:

0 commit comments

Comments
 (0)