Redact sensitive values before logging them (#33829)
* use a common way to redact sensitive values before logging them * fix panic on missing testCase.err, simplify require checks * fix a silly typo * combine readConfig and buildConnectionString methods, as they are closely related
This commit is contained in:
+32
-35
@@ -36,7 +36,7 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
redactedPassword = "*********"
|
||||
RedactedPassword = "*********"
|
||||
DefaultHTTPAddr = "0.0.0.0"
|
||||
Dev = "development"
|
||||
Prod = "production"
|
||||
@@ -431,14 +431,33 @@ func ToAbsUrl(relativeUrl string) string {
|
||||
return AppUrl + relativeUrl
|
||||
}
|
||||
|
||||
func shouldRedactKey(s string) bool {
|
||||
uppercased := strings.ToUpper(s)
|
||||
return strings.Contains(uppercased, "PASSWORD") || strings.Contains(uppercased, "SECRET") || strings.Contains(uppercased, "PROVIDER_CONFIG")
|
||||
}
|
||||
|
||||
func shouldRedactURLKey(s string) bool {
|
||||
uppercased := strings.ToUpper(s)
|
||||
return strings.Contains(uppercased, "DATABASE_URL")
|
||||
func RedactedValue(key, value string) string {
|
||||
uppercased := strings.ToUpper(key)
|
||||
// Sensitive information: password, secrets etc
|
||||
for _, pattern := range []string{
|
||||
"PASSWORD",
|
||||
"SECRET",
|
||||
"PROVIDER_CONFIG",
|
||||
"PRIVATE_KEY",
|
||||
"SECRET_KEY",
|
||||
"CERTIFICATE",
|
||||
} {
|
||||
if strings.Contains(uppercased, pattern) {
|
||||
return RedactedPassword
|
||||
}
|
||||
}
|
||||
// Sensitive URLs that might contain username and password
|
||||
for _, pattern := range []string{
|
||||
"DATABASE_URL",
|
||||
} {
|
||||
if strings.Contains(uppercased, pattern) {
|
||||
if u, err := url.Parse(value); err == nil {
|
||||
return u.Redacted()
|
||||
}
|
||||
}
|
||||
}
|
||||
// Otherwise return unmodified value
|
||||
return value
|
||||
}
|
||||
|
||||
func applyEnvVariableOverrides(file *ini.File) error {
|
||||
@@ -450,24 +469,7 @@ func applyEnvVariableOverrides(file *ini.File) error {
|
||||
|
||||
if len(envValue) > 0 {
|
||||
key.SetValue(envValue)
|
||||
if shouldRedactKey(envKey) {
|
||||
envValue = redactedPassword
|
||||
}
|
||||
if shouldRedactURLKey(envKey) {
|
||||
u, err := url.Parse(envValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse environment variable. key: %s, value: %s. error: %v", envKey, envValue, err)
|
||||
}
|
||||
ui := u.User
|
||||
if ui != nil {
|
||||
_, exists := ui.Password()
|
||||
if exists {
|
||||
u.User = url.UserPassword(ui.Username(), "-redacted-")
|
||||
envValue = u.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
appliedEnvOverrides = append(appliedEnvOverrides, fmt.Sprintf("%s=%s", envKey, envValue))
|
||||
appliedEnvOverrides = append(appliedEnvOverrides, fmt.Sprintf("%s=%s", envKey, RedactedValue(envKey, envValue)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -549,10 +551,8 @@ func applyCommandLineDefaultProperties(props map[string]string, file *ini.File)
|
||||
value, exists := props[keyString]
|
||||
if exists {
|
||||
key.SetValue(value)
|
||||
if shouldRedactKey(keyString) {
|
||||
value = redactedPassword
|
||||
}
|
||||
appliedCommandLineProperties = append(appliedCommandLineProperties, fmt.Sprintf("%s=%s", keyString, value))
|
||||
appliedCommandLineProperties = append(appliedCommandLineProperties,
|
||||
fmt.Sprintf("%s=%s", keyString, RedactedValue(keyString, value)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1059,10 +1059,7 @@ func (s *DynamicSection) Key(k string) *ini.Key {
|
||||
}
|
||||
|
||||
key.SetValue(envValue)
|
||||
if shouldRedactKey(envKey) {
|
||||
envValue = redactedPassword
|
||||
}
|
||||
s.Logger.Info("Config overridden from Environment variable", "var", fmt.Sprintf("%s=%s", envKey, envValue))
|
||||
s.Logger.Info("Config overridden from Environment variable", "var", fmt.Sprintf("%s=%s", envKey, RedactedValue(envKey, envValue)))
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
@@ -78,16 +78,6 @@ func TestLoadingSettings(t *testing.T) {
|
||||
So(appliedEnvOverrides, ShouldContain, "GF_SECURITY_ADMIN_PASSWORD=*********")
|
||||
})
|
||||
|
||||
Convey("Should return an error when url is invalid", func() {
|
||||
err := os.Setenv("GF_DATABASE_URL", "postgres.%31://grafana:secret@postgres:5432/grafana")
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := NewCfg()
|
||||
err = cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("Should replace password in URL when url environment is defined", func() {
|
||||
err := os.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database")
|
||||
require.NoError(t, err)
|
||||
@@ -96,7 +86,7 @@ func TestLoadingSettings(t *testing.T) {
|
||||
err = cfg.Load(&CommandLineArgs{HomePath: "../../"})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:-redacted-@localhost:3306/database")
|
||||
So(appliedEnvOverrides, ShouldContain, "GF_DATABASE_URL=mysql://user:xxxxx@localhost:3306/database")
|
||||
})
|
||||
|
||||
Convey("Should get property map from command line args array", func() {
|
||||
|
||||
Reference in New Issue
Block a user