From 5315b19822b4865d86f3892d3f6d9f11b8acc47e Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Fri, 11 Oct 2013 03:50:08 -0700 Subject: [PATCH] common/config: config filter function [GH-521] Fixes #521 --- builder/amazon/chroot/builder.go | 2 +- builder/amazon/ebs/builder.go | 2 +- builder/amazon/instance/builder.go | 2 +- builder/digitalocean/builder.go | 6 +----- builder/openstack/builder.go | 2 +- common/config.go | 10 ++++++++++ common/config_test.go | 17 +++++++++++++++++ 7 files changed, 32 insertions(+), 9 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 00119b487..f373b4ba2 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -143,7 +143,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { return errs } - log.Printf("Config: %+v", b.config) + log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey) return nil } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 194e10bc9..376a90e2b 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -56,7 +56,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { return errs } - log.Printf("Config: %+v", b.config) + log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey) return nil } diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 44691ecee..8b3abc75b 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -159,7 +159,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { return errs } - log.Printf("Config: %+v", b.config) + log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey) return nil } diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index f8a25b4b3..acf92c240 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -11,7 +11,6 @@ import ( "github.com/mitchellh/packer/packer" "log" "os" - "strings" "time" ) @@ -165,10 +164,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { return errs } - configRepr := fmt.Sprintf("Config: %+v", b.config) - scrubbedConfig := strings.Replace(configRepr, b.config.ClientID, "CLIENT_ID", -1) - scrubbedConfig = strings.Replace(scrubbedConfig, b.config.APIKey, "API_KEY", -1) - log.Println(scrubbedConfig) + common.ScrubConfig(b.config, b.config.ClientID, b.config.APIKey) return nil } diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index 95204dab6..6e9c961b4 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -51,7 +51,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { return errs } - log.Printf("Config: %+v", b.config) + log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey) return nil } diff --git a/common/config.go b/common/config.go index b579562d5..460eeabc7 100644 --- a/common/config.go +++ b/common/config.go @@ -12,6 +12,16 @@ import ( "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, "", -1) + } + return conf +} + // CheckUnusedConfig is a helper that makes sure that the there are no // unused configuration keys, properly ignoring keys that don't matter. func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError { diff --git a/common/config_test.go b/common/config_test.go index e7f469bcc..ea994de51 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -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: Inner:{Baz:}}" + conf := ScrubConfig(c, c.Bar) + if conf != expect { + t.Fatalf("got %s, expected %s", conf, expect) + } +}