Commit 5315b198 authored by Matthew Hooker's avatar Matthew Hooker

common/config: config filter function [GH-521]

Fixes #521
parent 94487871
...@@ -143,7 +143,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -143,7 +143,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs return errs
} }
log.Printf("Config: %+v", b.config) log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil return nil
} }
......
...@@ -56,7 +56,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -56,7 +56,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs return errs
} }
log.Printf("Config: %+v", b.config) log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil return nil
} }
......
...@@ -159,7 +159,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -159,7 +159,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs return errs
} }
log.Printf("Config: %+v", b.config) log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil return nil
} }
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
"os" "os"
"strings"
"time" "time"
) )
...@@ -165,10 +164,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -165,10 +164,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs return errs
} }
configRepr := fmt.Sprintf("Config: %+v", b.config) common.ScrubConfig(b.config, b.config.ClientID, b.config.APIKey)
scrubbedConfig := strings.Replace(configRepr, b.config.ClientID, "CLIENT_ID", -1)
scrubbedConfig = strings.Replace(scrubbedConfig, b.config.APIKey, "API_KEY", -1)
log.Println(scrubbedConfig)
return nil return nil
} }
......
...@@ -51,7 +51,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { ...@@ -51,7 +51,7 @@ func (b *Builder) Prepare(raws ...interface{}) error {
return errs return errs
} }
log.Printf("Config: %+v", b.config) log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey)
return nil return nil
} }
......
...@@ -12,6 +12,16 @@ import ( ...@@ -12,6 +12,16 @@ import (
"strings" "strings"
) )
// ScrubConfig is a helper that returns a string representation of
// any struct with the given values stripped out.
func ScrubConfig(target interface{}, values ...string) string {
conf := fmt.Sprintf("Config: %+v", target)
for _, value := range values {
conf = strings.Replace(conf, value, "<Filtered>", -1)
}
return conf
}
// CheckUnusedConfig is a helper that makes sure that the there are no // CheckUnusedConfig is a helper that makes sure that the there are no
// unused configuration keys, properly ignoring keys that don't matter. // unused configuration keys, properly ignoring keys that don't matter.
func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError { func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError {
......
...@@ -159,3 +159,20 @@ func TestDownloadableURL_FilePaths(t *testing.T) { ...@@ -159,3 +159,20 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
} }
} }
} }
func TestScrubConfig(t *testing.T) {
type Inner struct {
Baz string
}
type Local struct {
Foo string
Bar string
Inner
}
c := Local{"foo", "bar", Inner{"bar"}}
expect := "Config: {Foo:foo Bar:<Filtered> Inner:{Baz:<Filtered>}}"
conf := ScrubConfig(c, c.Bar)
if conf != expect {
t.Fatalf("got %s, expected %s", conf, expect)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment