mirror of https://github.com/hashicorp/packer
Merge pull request #13582 from hashicorp/multi-line-secrets
BUG: Scrub multiline sensitive values from build outputpull/13588/head
commit
a664835382
@ -0,0 +1,46 @@
|
||||
// Copyright IBM Corp. 2013, 2025
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package command
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildScrubsSensitiveMultilineShellLocalOutput(t *testing.T) {
|
||||
templatePath := filepath.Join(testFixture("repro-sensitive-multiline"), testBuildSensitiveMultilineShellLocalFixture(runtime.GOOS))
|
||||
|
||||
c := &BuildCommand{
|
||||
Meta: TestMetaFile(t),
|
||||
}
|
||||
|
||||
if exitCode := c.Run([]string{templatePath}); exitCode != 0 {
|
||||
out, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta)
|
||||
t.Fatalf("build failed with exit code %d\nstdout: %q\nstderr: %q", exitCode, out, stderr)
|
||||
}
|
||||
|
||||
out, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta)
|
||||
output := out + "\n" + stderr
|
||||
secret := "line-one-secret\nline-two-secret\nline-three-secret"
|
||||
|
||||
if strings.Contains(output, secret) {
|
||||
t.Fatalf("multiline sensitive value leaked to build output: %q", output)
|
||||
}
|
||||
if strings.Contains(output, "line-one-secret") {
|
||||
t.Fatalf("sensitive line leaked to build output: %q", output)
|
||||
}
|
||||
if !strings.Contains(output, "<sensitive>") {
|
||||
t.Fatalf("expected scrubbed output, got: %q", output)
|
||||
}
|
||||
}
|
||||
|
||||
func testBuildSensitiveMultilineShellLocalFixture(goos string) string {
|
||||
if goos == "windows" {
|
||||
return "multi-pwd.windows.pkr.hcl"
|
||||
}
|
||||
|
||||
return "multi-pwd.unix.pkr.hcl"
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
variable "secret_multiline" {
|
||||
type = string
|
||||
sensitive = true
|
||||
default = "line-one-secret\nline-two-secret\nline-three-secret"
|
||||
}
|
||||
|
||||
source "null" "example" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["sources.null.example"]
|
||||
|
||||
provisioner "shell-local" {
|
||||
inline = [
|
||||
"printf 'BEGIN\n%s\nEND\n' '${var.secret_multiline}'"
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
variable "secret_multiline" {
|
||||
type = string
|
||||
sensitive = true
|
||||
default = "line-one-secret\nline-two-secret\nline-three-secret"
|
||||
}
|
||||
|
||||
source "null" "example" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["sources.null.example"]
|
||||
|
||||
provisioner "shell-local" {
|
||||
tempfile_extension = ".ps1"
|
||||
environment_vars = ["SECRET_MULTILINE=${var.secret_multiline}"]
|
||||
execute_command = ["powershell.exe", "{{.Vars}} {{.Script}}"]
|
||||
env_var_format = "$env:%s=\"%s\"; "
|
||||
inline = [
|
||||
"Write-Output 'BEGIN'",
|
||||
"Write-Output $env:SECRET_MULTILINE",
|
||||
"Write-Output 'END'"
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
// Copyright IBM Corp. 2013, 2025
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package packer
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
||||
)
|
||||
|
||||
func RegisterSecret(secret string) {
|
||||
if secret == "" {
|
||||
return
|
||||
}
|
||||
|
||||
secrets := map[string]struct{}{
|
||||
secret: {},
|
||||
}
|
||||
|
||||
normalized := strings.ReplaceAll(secret, "\r\n", "\n")
|
||||
secrets[normalized] = struct{}{}
|
||||
|
||||
for _, line := range strings.Split(normalized, "\n") {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
secrets[line] = struct{}{}
|
||||
}
|
||||
|
||||
for value := range secrets {
|
||||
packersdk.LogSecretFilter.Set(value)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue