-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package config | ||
|
||
import ( | ||
"certalert/internal/utils" | ||
"strings" | ||
) | ||
|
||
// redactVariable redacts sensitive data from the config if it is not prefixed with env: or file: | ||
func redactVariable(s string) string { | ||
if strings.HasPrefix(s, "env:") || strings.HasPrefix(s, "file:") { | ||
return s | ||
} | ||
return "<REDACTED>" | ||
} | ||
|
||
// RedactConfig redacts sensitive data from the config | ||
func RedactConfig(config *Config) error { | ||
|
||
if utils.HasKey(config.Pushgateway, "Address") { | ||
config.Pushgateway.Address = redactVariable(config.Pushgateway.Address) | ||
} | ||
|
||
if utils.HasKey(config.Pushgateway, "Basic.Username") { | ||
config.Pushgateway.Auth.Basic.Username = redactVariable(config.Pushgateway.Auth.Basic.Username) | ||
} | ||
|
||
if utils.HasKey(config.Pushgateway, "Basic.Password") { | ||
config.Pushgateway.Auth.Basic.Password = redactVariable(config.Pushgateway.Auth.Basic.Password) | ||
} | ||
|
||
if utils.HasKey(config.Pushgateway, "Bearer.Token") { | ||
config.Pushgateway.Auth.Bearer.Token = redactVariable(config.Pushgateway.Auth.Bearer.Token) | ||
} | ||
|
||
for idx, cert := range config.Certs { | ||
config.Certs[idx].Password = redactVariable(cert.Password) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package config | ||
|
||
import "testing" | ||
|
||
func TestRedactVariable(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"env:SECRET_ENV", "env:SECRET_ENV"}, // Should not redact env: prefixed strings | ||
{"file:/path/to/secret", "file:/path/to/secret"}, // Should not redact file: prefixed strings | ||
{"mysecret", "<REDACTED>"}, // Should redact non-prefixed strings | ||
{"", "<REDACTED>"}, // Should redact empty strings | ||
{"filemysecret", "<REDACTED>"}, // Should redact strings that contains the word 'file' but not prefixed with 'file:' | ||
{"envmysecret", "<REDACTED>"}, // Should redact strings that contains the word 'env' but not prefixed with 'env:' | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.input, func(t *testing.T) { | ||
actual := redactVariable(tt.input) | ||
if actual != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters