diff --git a/helper/salt/salt.go b/helper/salt/salt.go index b6057e567..9028e9abd 100644 --- a/helper/salt/salt.go +++ b/helper/salt/salt.go @@ -21,8 +21,9 @@ const ( // using the global salt. Primarily, this allows paths in the storage // backend to be obfuscated if they may contain sensitive information. type Salt struct { - config *Config - salt string + config *Config + salt string + generated bool } type HashFunc func([]byte) []byte @@ -70,6 +71,7 @@ func NewSalt(view logical.Storage, config *Config) (*Salt, error) { // Generate a new salt if necessary if s.salt == "" { s.salt = uuid.GenerateUUID() + s.generated = true raw = &logical.StorageEntry{ Key: config.Location, Value: []byte(s.salt), @@ -87,6 +89,12 @@ func (s *Salt) SaltID(id string) string { return SaltID(s.salt, id, s.config.HashFunc) } +// DidGenerate returns if the underlying salt value was generated +// on initialization or if an existing salt value was loaded +func (s *Salt) DidGenerate() bool { + return s.generated +} + // SaltID is used to apply a salt and hash functio to an ID to make sure // it is not reversable func SaltID(salt, id string, hash HashFunc) string { diff --git a/helper/salt/salt_test.go b/helper/salt/salt_test.go index 77f883c90..8d07c713f 100644 --- a/helper/salt/salt_test.go +++ b/helper/salt/salt_test.go @@ -18,6 +18,10 @@ func TestSalt(t *testing.T) { t.Fatalf("err: %v", err) } + if !salt.DidGenerate() { + t.Fatalf("expected generation") + } + // Verify the salt exists out, err := inm.Get(DefaultLocation) if err != nil { @@ -33,6 +37,10 @@ func TestSalt(t *testing.T) { t.Fatalf("err: %v", err) } + if salt2.DidGenerate() { + t.Fatalf("unexpected generation") + } + // Check for a match if salt.salt != salt2.salt { t.Fatalf("salt mismatch: %s %s", salt.salt, salt2.salt)