From 0216f99727c1e7b74c203bf45e2963d8f4c75400 Mon Sep 17 00:00:00 2001 From: Brian Kassouf Date: Fri, 9 Mar 2018 11:01:24 -0800 Subject: [PATCH] helper/keysutil: Add a LoadPolicy function (#4116) * helper/keysutil: Add a LoadPolicy function * Use the load policy function in the lock manager --- helper/keysutil/lock_manager.go | 20 +------------------- helper/keysutil/policy.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/helper/keysutil/lock_manager.go b/helper/keysutil/lock_manager.go index 24b25f380c..778c28cbcb 100644 --- a/helper/keysutil/lock_manager.go +++ b/helper/keysutil/lock_manager.go @@ -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) } diff --git a/helper/keysutil/policy.go b/helper/keysutil/policy.go index b13ebd5938..50bc3b868c 100644 --- a/helper/keysutil/policy.go +++ b/helper/keysutil/policy.go @@ -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"`