mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user