config: make parsing configurations without usable values invalid (backport) (#4358)

This makes configuration files that are empty, or read and processed by
the parser but with no detected values now return an error.

Fixes #4343 
Backport from dev branch
(https://github.com/nats-io/nats-server/pull/4347)
This commit is contained in:
Waldemar Quevedo
2023-08-01 22:33:30 -07:00
committed by GitHub
2 changed files with 30 additions and 1 deletions

View File

@@ -146,7 +146,9 @@ func parse(data, fp string, pedantic bool) (p *parser, err error) {
return nil, err
}
}
if len(p.mapping) == 0 {
return nil, fmt.Errorf("config has no values or is empty")
}
return p, nil
}

View File

@@ -403,3 +403,30 @@ func TestParserNoInfiniteLoop(t *testing.T) {
}
}
}
func TestParseWithNoValues(t *testing.T) {
for _, test := range []string{
``,
`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
` aaaaaaaaaaaaaaaaaaaaaaaaaaa`,
` aaaaaaaaaaaaaaaaaaaaaaaaaaa `,
`
# just comments with no values
# is also invalid.
`,
`
# with comments and no spaces to create key values
# is also an invalid config.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
`,
`
a,a,a,a,a,a,a,a,a,a,a
`,
} {
if _, err := parse(test, "", true); err == nil {
t.Fatal("expected an error")
} else if !strings.Contains(err.Error(), "config has no values or is empty") {
t.Fatal("expected invalid conf error")
}
}
}