-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sensitive outputs shouldn't be exposed when using terraform.OutputRequired #476
Comments
Oh, good point! Would you be up for a PR to fix this? |
Ok, so running It seems to me that there is no easy way to be aware if we deal with sensitive data or not when calling wdyt @brikis98 ? |
Try |
yeah sure, |
Yea, what I meant was that the |
Thank you, that makes sense to me! I will take a proper look in a bit. |
We've been seeing this issue as well. We apply some terraform, and then use terratest's output functions to fetch out a randomly generated password. We need the password to ensure that the applied resources have been set up, but by getting the output, it also prints out the password to our logs - not ideal. It feels like by default logging should be turned off for any sensitive output. Has there been any progress on this issue? |
Here is a workaround for this that we recently identified: func fetchSensitiveOutput(t *testing.T, options *terraform.Options, name string) string {
defer func() {
options.Logger = nil
}()
options.Logger = logger.Discard // import path is github.com/gruntwork-io/terratest/modules/logger
return terraform.Output(t, options, name)
} |
I started using terratest to test my Kubernetes Webhook (k8s-image-swapper). The IAM keys and docker credentials were exposed in the logs, which is not ideal for CI purposes. I came up with the following solution: Usage: func TestHelmDeployment(t *testing.T) {
workingDir, _ := filepath.Abs("..")
awsAccountID := os.Getenv("AWS_ACCOUNT_ID")
awsRegion := os.Getenv("AWS_DEFAULT_REGION")
awsAccessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
awsSecretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
ecrRegistry := awsAccountID + ".dkr.ecr." + awsRegion + ".amazonaws.com"
ecrRepository := "docker.io/library/nginx"
logger.Default = logger.New(newSensitiveLogger(
logger.Default,
[]*regexp.Regexp{
regexp.MustCompile(awsAccountID),
regexp.MustCompile(awsAccessKeyID),
regexp.MustCompile(awsSecretAccessKey),
regexp.MustCompile(`(--docker-password=)\S+`),
},
))
... Implementation:: type sensitiveLogger struct{
logger logger.TestLogger
patterns []*regexp.Regexp
}
func newSensitiveLogger(logger *logger.Logger, patterns []*regexp.Regexp) *sensitiveLogger {
return &sensitiveLogger{
logger: logger,
patterns: patterns,
}
}
func (l *sensitiveLogger) Logf(t terratesttesting.TestingT, format string, args ...interface{}) {
var redactedArgs []interface{}
obfuscateWith := "$1*******"
redactedArgs = args
for _, pattern := range l.patterns {
for i, arg := range redactedArgs {
switch arg := arg.(type) {
case string:
redactedArgs[i] = pattern.ReplaceAllString(arg, obfuscateWith)
case []string:
var result []string
for _, s := range arg {
result = append(result, pattern.ReplaceAllString(s, obfuscateWith))
}
redactedArgs[i] = result
default:
panic("type needs implementation")
}
}
}
l.logger.Logf(t, format, redactedArgs...)
} @brikis98 What are your thoughts on this? Do you think this is something we can add to terratest itself? |
After some exploration, I haven't found a straightforward solution. The most ideal approach would be to implement a custom logger that can detect sensitive values as logs are being written. This would involve creating a streaming JSON decoder that can tolerate invalid tokens, identify JSON objects representing Terraform output values, check if they are marked as sensitive based on the An alternative would be to create a logger that first stores all log values in memory, and only writes them after analyzing the parsed output to identify which values are sensitive. It would then filter every occurrence of those sensitive values before writing the logs. This approach increases memory usage since all logs need to be held in memory temporarily, and also risks censoring non-sensitive variables that coincidentally share the same value as a sensitive one. As a fallback, users of the library can simply set |
Hi,
We are working on a test-case that creates some IAM users and access keys that we use for authenticating with some AWS resources within that test. To be able to use the credentials we pull them from the outputs, e.G:
outputs.tf:
test.go:
Using the helper methods to retrieve outputs with terraform will dump the outputs to the logs. I believe we should align the behavior of terratest with terraform and only write
<sensitive>
instead of the actual values to the logs.The text was updated successfully, but these errors were encountered: