helper/keysutil: Add a LoadPolicy function (#4116)

* helper/keysutil: Add a LoadPolicy function

* Use the load policy function in the lock manager
This commit is contained in:
Brian Kassouf
2018-03-09 11:01:24 -08:00
committed by GitHub
parent dcd032d381
commit 0216f99727
2 changed files with 23 additions and 19 deletions

View File

@@ -500,23 +500,5 @@ func (lm *LockManager) DeletePolicy(ctx context.Context, storage logical.Storage
}
func (lm *LockManager) getStoredPolicy(ctx context.Context, storage logical.Storage, name string) (*Policy, error) {
// Check if the policy already exists
raw, err := storage.Get(ctx, "policy/"+name)
if err != nil {
return nil, err
}
if raw == nil {
return nil, nil
}
// Decode the policy
var policy Policy
err = jsonutil.DecodeJSON(raw.Value, &policy)
if err != nil {
return nil, err
}
policy.versionPrefixCache = &sync.Map{}
return &policy, nil
return LoadPolicy(ctx, storage, "policy/"+name)
}

View File

@@ -262,6 +262,28 @@ func NewPolicy(config PolicyConfig) *Policy {
}
}
// LoadPolicy will load a policy from the provided storage path and set the
// necessary un-exported variables. It is particularly useful when accessing a
// policy without the lock manager.
func LoadPolicy(ctx context.Context, s logical.Storage, path string) (*Policy, error) {
raw, err := s.Get(ctx, path)
if err != nil {
return nil, err
}
if raw == nil {
return nil, nil
}
var policy Policy
err = jsonutil.DecodeJSON(raw.Value, &policy)
if err != nil {
return nil, err
}
policy.versionPrefixCache = &sync.Map{}
return &policy, nil
}
// Policy is the struct used to store metadata
type Policy struct {
Name string `json:"name"`