helper/salt: track if salt was generated

This commit is contained in:
Armon Dadgar 2015-06-30 16:47:49 -07:00
parent e892d728a2
commit 3084f64e5c
2 changed files with 18 additions and 2 deletions

View File

@ -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 {

View File

@ -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)