Add validator for proxy log-level (#2256)

Add validator for proxy log-level

Follow-up to #2249

Signed-off-by: Alejandro Pedraza <alejandro@buoyant.io>
This commit is contained in:
Alejandro Pedraza 2019-02-13 15:33:22 -05:00 committed by GitHub
parent a0ae089e89
commit 0c50749990
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 5 deletions

View File

@ -174,20 +174,54 @@ func TestValidate(t *testing.T) {
}
})
t.Run("Rejects invalid log level", func(t *testing.T) {
t.Run("Rejects invalid controller log level", func(t *testing.T) {
options := newInstallOptions()
options.controllerLogLevel = "super"
expected := "--controller-log-level must be one of: panic, fatal, error, warn, info, debug"
err := options.validate()
if err == nil {
t.Fatalf("Expected error, got nothing")
t.Fatal("Expected error, got nothing")
}
if err.Error() != expected {
t.Fatalf("Expected error string\"%s\", got \"%s\"", expected, err)
}
})
t.Run("Properly validates proxy log level", func(t *testing.T) {
testCases := []struct {
input string
valid bool
}{
{"", false},
{"info", true},
{"somemodule", true},
{"bad%name", false},
{"linkerd2_proxy=debug", true},
{"linkerd2%proxy=debug", false},
{"linkerd2_proxy=foobar", false},
{"linker2d_proxy,std::option", true},
{"warn,linkerd2_proxy=info", true},
{"warn,linkerd2_proxy=foobar", false},
}
options := newInstallOptions()
for _, tc := range testCases {
options.proxyLogLevel = tc.input
err := options.validate()
if tc.valid && err != nil {
t.Fatalf("Error not expected: %s", err)
}
if !tc.valid && err == nil {
t.Fatalf("Expected error string \"%s is not a valid proxy log level\", got nothing", tc.input)
}
expectedErr := "\"%s\" is not a valid proxy log level - for allowed syntax check https://docs.rs/env_logger/0.6.0/env_logger/#enabling-logging"
if !tc.valid && err.Error() != fmt.Sprintf(expectedErr, tc.input) {
t.Fatalf("Expected error string \""+expectedErr+"\"", tc.input, err)
}
}
})
t.Run("Rejects single namespace install with auto inject", func(t *testing.T) {
options := newInstallOptions()
options.proxyAutoInject = true

View File

@ -42,9 +42,21 @@ var (
// These regexs are not as strict as they could be, but are a quick and dirty
// sanity check against illegal characters.
alphaNumDash = regexp.MustCompile("^[a-zA-Z0-9-]+$")
alphaNumDashDot = regexp.MustCompile("^[\\.a-zA-Z0-9-]+$")
alphaNumDashDotSlashColon = regexp.MustCompile("^[\\./a-zA-Z0-9-:]+$")
alphaNumDash = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
alphaNumDashDot = regexp.MustCompile(`^[\.a-zA-Z0-9-]+$`)
alphaNumDashDotSlashColon = regexp.MustCompile(`^[\./a-zA-Z0-9-:]+$`)
// Full Rust log level syntax at
// https://docs.rs/env_logger/0.6.0/env_logger/#enabling-logging
r = strings.NewReplacer("\t", "", "\n", "")
validProxyLogLevel = regexp.MustCompile(r.Replace(`
^(
(
(trace|debug|warn|info|error)|
(\w|::)+|
((\w|::)+=(trace|debug|warn|info|error))
)(?:,|$)
)+$`))
)
// RootCmd represents the root Cobra command
@ -315,6 +327,11 @@ func (options *proxyConfigOptions) validate() error {
return fmt.Errorf("--tls must be blank or set to \"%s\"", optionalTLS)
}
if !validProxyLogLevel.MatchString(options.proxyLogLevel) {
return fmt.Errorf("\"%s\" is not a valid proxy log level - for allowed syntax check https://docs.rs/env_logger/0.6.0/env_logger/#enabling-logging",
options.proxyLogLevel)
}
return nil
}