2016-10-26 23:52:31 +00:00
|
|
|
package keysutil
|
2016-04-26 15:39:19 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-12-14 17:51:50 +00:00
|
|
|
"encoding/base64"
|
2016-05-03 03:46:39 +00:00
|
|
|
"errors"
|
2016-04-26 15:39:19 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2017-12-14 17:51:50 +00:00
|
|
|
"time"
|
2016-04-26 15:39:19 +00:00
|
|
|
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2016-04-26 15:39:19 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
shared = false
|
|
|
|
exclusive = true
|
|
|
|
)
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
var (
|
|
|
|
errNeedExclusiveLock = errors.New("an exclusive lock is needed for this operation")
|
|
|
|
)
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
// PolicyRequest holds values used when requesting a policy. Most values are
|
2016-09-21 14:29:42 +00:00
|
|
|
// only used during an upsert.
|
2016-10-26 23:52:31 +00:00
|
|
|
type PolicyRequest struct {
|
2016-09-21 14:29:42 +00:00
|
|
|
// The storage to use
|
2016-10-26 23:52:31 +00:00
|
|
|
Storage logical.Storage
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
// The name of the policy
|
2016-10-26 23:52:31 +00:00
|
|
|
Name string
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
// The key type
|
2016-10-26 23:52:31 +00:00
|
|
|
KeyType KeyType
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
// Whether it should be derived
|
2016-10-26 23:52:31 +00:00
|
|
|
Derived bool
|
2016-09-21 14:29:42 +00:00
|
|
|
|
|
|
|
// Whether to enable convergent encryption
|
2016-10-26 23:52:31 +00:00
|
|
|
Convergent bool
|
2016-09-21 14:29:42 +00:00
|
|
|
|
2017-01-23 16:04:43 +00:00
|
|
|
// Whether to allow export
|
|
|
|
Exportable bool
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
// Whether to upsert
|
2016-10-26 23:52:31 +00:00
|
|
|
Upsert bool
|
2017-12-15 14:08:28 +00:00
|
|
|
|
|
|
|
// Whether to allow plaintext backup
|
|
|
|
AllowPlaintextBackup bool
|
2016-09-21 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
type LockManager struct {
|
2016-04-26 15:39:19 +00:00
|
|
|
// A lock for each named key
|
|
|
|
locks map[string]*sync.RWMutex
|
|
|
|
|
|
|
|
// A mutex for the map itself
|
2016-04-26 19:30:39 +00:00
|
|
|
locksMutex sync.RWMutex
|
2016-04-26 15:39:19 +00:00
|
|
|
|
|
|
|
// If caching is enabled, the map of name to in-memory policy cache
|
2016-10-26 23:52:31 +00:00
|
|
|
cache map[string]*Policy
|
2016-04-26 15:39:19 +00:00
|
|
|
|
|
|
|
// Used for global locking, and as the cache map mutex
|
2016-05-03 03:46:39 +00:00
|
|
|
cacheMutex sync.RWMutex
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func NewLockManager(cacheDisabled bool) *LockManager {
|
|
|
|
lm := &LockManager{
|
2016-04-26 15:39:19 +00:00
|
|
|
locks: map[string]*sync.RWMutex{},
|
|
|
|
}
|
|
|
|
if !cacheDisabled {
|
2016-10-26 23:52:31 +00:00
|
|
|
lm.cache = map[string]*Policy{}
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
return lm
|
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func (lm *LockManager) CacheActive() bool {
|
2016-04-26 15:39:19 +00:00
|
|
|
return lm.cache != nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 21:29:30 +00:00
|
|
|
func (lm *LockManager) InvalidatePolicy(name string) {
|
|
|
|
// Check if it's in our cache. If so, return right away.
|
|
|
|
if lm.CacheActive() {
|
|
|
|
lm.cacheMutex.Lock()
|
|
|
|
defer lm.cacheMutex.Unlock()
|
|
|
|
delete(lm.cache, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func (lm *LockManager) policyLock(name string, lockType bool) *sync.RWMutex {
|
2016-04-26 19:30:39 +00:00
|
|
|
lm.locksMutex.RLock()
|
2016-04-26 15:39:19 +00:00
|
|
|
lock := lm.locks[name]
|
|
|
|
if lock != nil {
|
|
|
|
// We want to give this up before locking the lock, but it's safe --
|
|
|
|
// the only time we ever write to a value in this map is the first time
|
|
|
|
// we access the value, so it won't be changing out from under us
|
2016-04-26 19:30:39 +00:00
|
|
|
lm.locksMutex.RUnlock()
|
2016-05-03 03:46:39 +00:00
|
|
|
if lockType == exclusive {
|
2016-04-26 15:39:19 +00:00
|
|
|
lock.Lock()
|
|
|
|
} else {
|
|
|
|
lock.RLock()
|
|
|
|
}
|
2016-05-03 03:46:39 +00:00
|
|
|
return lock
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 19:30:39 +00:00
|
|
|
lm.locksMutex.RUnlock()
|
|
|
|
lm.locksMutex.Lock()
|
2016-04-26 15:39:19 +00:00
|
|
|
|
|
|
|
// Don't defer the unlock call because if we get a valid lock below we want
|
|
|
|
// to release the lock mutex right away to avoid the possibility of
|
|
|
|
// deadlock by trying to grab the second lock
|
|
|
|
|
|
|
|
// Check to make sure it hasn't been created since
|
|
|
|
lock = lm.locks[name]
|
|
|
|
if lock != nil {
|
2016-04-26 19:30:39 +00:00
|
|
|
lm.locksMutex.Unlock()
|
2016-05-03 03:46:39 +00:00
|
|
|
if lockType == exclusive {
|
2016-04-26 15:39:19 +00:00
|
|
|
lock.Lock()
|
|
|
|
} else {
|
|
|
|
lock.RLock()
|
|
|
|
}
|
2016-05-03 03:46:39 +00:00
|
|
|
return lock
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lock = &sync.RWMutex{}
|
|
|
|
lm.locks[name] = lock
|
2016-04-26 19:30:39 +00:00
|
|
|
lm.locksMutex.Unlock()
|
2016-05-03 03:46:39 +00:00
|
|
|
if lockType == exclusive {
|
2016-04-26 15:39:19 +00:00
|
|
|
lock.Lock()
|
|
|
|
} else {
|
|
|
|
lock.RLock()
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
return lock
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
func (lm *LockManager) UnlockPolicy(lock *sync.RWMutex, lockType bool) {
|
2016-05-03 03:46:39 +00:00
|
|
|
if lockType == exclusive {
|
2016-04-26 15:39:19 +00:00
|
|
|
lock.Unlock()
|
|
|
|
} else {
|
|
|
|
lock.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:51:50 +00:00
|
|
|
func (lm *LockManager) UpdateCache(name string, policy *Policy) {
|
|
|
|
if lm.CacheActive() {
|
|
|
|
lm.cacheMutex.Lock()
|
|
|
|
defer lm.cacheMutex.Unlock()
|
|
|
|
lm.cache[name] = policy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
// Get the policy with a read lock. If we get an error saying an exclusive lock
|
|
|
|
// is needed (for instance, for an upgrade/migration), give up the read lock,
|
|
|
|
// call again with an exclusive lock, then swap back out for a read lock.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) GetPolicyShared(ctx context.Context, storage logical.Storage, name string) (*Policy, *sync.RWMutex, error) {
|
|
|
|
p, lock, _, err := lm.getPolicyCommon(ctx, PolicyRequest{
|
2016-10-26 23:52:31 +00:00
|
|
|
Storage: storage,
|
|
|
|
Name: name,
|
2016-09-21 14:29:42 +00:00
|
|
|
}, shared)
|
2016-05-03 03:46:39 +00:00
|
|
|
if err == nil ||
|
|
|
|
(err != nil && err != errNeedExclusiveLock) {
|
|
|
|
return p, lock, err
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Try again while asking for an exclusive lock
|
2018-01-19 06:44:44 +00:00
|
|
|
p, lock, _, err = lm.getPolicyCommon(ctx, PolicyRequest{
|
2016-10-26 23:52:31 +00:00
|
|
|
Storage: storage,
|
|
|
|
Name: name,
|
2016-09-21 14:29:42 +00:00
|
|
|
}, exclusive)
|
2016-05-03 03:46:39 +00:00
|
|
|
if err != nil || p == nil || lock == nil {
|
|
|
|
return p, lock, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.Unlock()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
p, lock, _, err = lm.getPolicyCommon(ctx, PolicyRequest{
|
2016-10-26 23:52:31 +00:00
|
|
|
Storage: storage,
|
|
|
|
Name: name,
|
2016-09-21 14:29:42 +00:00
|
|
|
}, shared)
|
2016-05-03 03:46:39 +00:00
|
|
|
return p, lock, err
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
// Get the policy with an exclusive lock
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) GetPolicyExclusive(ctx context.Context, storage logical.Storage, name string) (*Policy, *sync.RWMutex, error) {
|
|
|
|
p, lock, _, err := lm.getPolicyCommon(ctx, PolicyRequest{
|
2016-10-26 23:52:31 +00:00
|
|
|
Storage: storage,
|
|
|
|
Name: name,
|
2016-09-21 14:29:42 +00:00
|
|
|
}, exclusive)
|
2016-05-03 03:46:39 +00:00
|
|
|
return p, lock, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the policy with a read lock; if it returns that an exclusive lock is
|
|
|
|
// needed, retry. If successful, call one more time to get a read lock and
|
|
|
|
// return the value.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) GetPolicyUpsert(ctx context.Context, req PolicyRequest) (*Policy, *sync.RWMutex, bool, error) {
|
2016-10-26 23:52:31 +00:00
|
|
|
req.Upsert = true
|
2016-09-21 14:29:42 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
p, lock, _, err := lm.getPolicyCommon(ctx, req, shared)
|
2016-05-03 03:46:39 +00:00
|
|
|
if err == nil ||
|
|
|
|
(err != nil && err != errNeedExclusiveLock) {
|
2016-05-03 04:19:18 +00:00
|
|
|
return p, lock, false, err
|
2016-05-03 03:46:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Try again while asking for an exclusive lock
|
2018-01-19 06:44:44 +00:00
|
|
|
p, lock, upserted, err := lm.getPolicyCommon(ctx, req, exclusive)
|
2016-05-03 03:46:39 +00:00
|
|
|
if err != nil || p == nil || lock == nil {
|
|
|
|
return p, lock, upserted, err
|
|
|
|
}
|
|
|
|
lock.Unlock()
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
req.Upsert = false
|
2016-09-21 14:29:42 +00:00
|
|
|
// Now get a shared lock for the return, but preserve the value of upserted
|
2018-01-19 06:44:44 +00:00
|
|
|
p, lock, _, err = lm.getPolicyCommon(ctx, req, shared)
|
2016-05-03 04:19:18 +00:00
|
|
|
|
|
|
|
return p, lock, upserted, err
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:51:50 +00:00
|
|
|
// RestorePolicy acquires an exclusive lock on the policy name and restores the
|
|
|
|
// given policy along with the archive.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) RestorePolicy(ctx context.Context, storage logical.Storage, name, backup string) error {
|
2017-12-14 17:51:50 +00:00
|
|
|
var p *Policy
|
|
|
|
var err error
|
|
|
|
|
|
|
|
backupBytes, err := base64.StdEncoding.DecodeString(backup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyData KeyData
|
|
|
|
err = jsonutil.DecodeJSON(backupBytes, &keyData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a different name if desired
|
|
|
|
if name != "" {
|
|
|
|
keyData.Policy.Name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
name = keyData.Policy.Name
|
|
|
|
|
2018-03-09 01:58:50 +00:00
|
|
|
// set the policy version cache
|
|
|
|
keyData.Policy.versionPrefixCache = &sync.Map{}
|
|
|
|
|
2017-12-14 17:51:50 +00:00
|
|
|
lockType := exclusive
|
|
|
|
lock := lm.policyLock(name, lockType)
|
|
|
|
defer lm.UnlockPolicy(lock, lockType)
|
|
|
|
|
|
|
|
// If the policy is in cache, error out
|
|
|
|
if lm.CacheActive() {
|
|
|
|
lm.cacheMutex.RLock()
|
|
|
|
p = lm.cache[name]
|
|
|
|
if p != nil {
|
|
|
|
lm.cacheMutex.RUnlock()
|
|
|
|
return fmt.Errorf(fmt.Sprintf("policy %q already exists", name))
|
|
|
|
}
|
|
|
|
lm.cacheMutex.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the policy exists in storage, error out
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err = lm.getStoredPolicy(ctx, storage, name)
|
2017-12-14 17:51:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p != nil {
|
|
|
|
return fmt.Errorf(fmt.Sprintf("policy %q already exists", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the archived keys
|
|
|
|
if keyData.ArchivedKeys != nil {
|
2018-01-19 06:44:44 +00:00
|
|
|
err = keyData.Policy.storeArchive(ctx, storage, keyData.ArchivedKeys)
|
2017-12-14 17:51:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to restore archived keys for policy %q: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that policy as a restored key
|
|
|
|
keyData.Policy.RestoreInfo = &RestoreInfo{
|
|
|
|
Time: time.Now(),
|
|
|
|
Version: keyData.Policy.LatestVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the policy. This will also attempt to adjust the archive.
|
2018-01-19 06:44:44 +00:00
|
|
|
err = keyData.Policy.Persist(ctx, storage)
|
2017-12-14 17:51:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to restore the policy %q: %v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cache to contain the restored policy
|
|
|
|
lm.UpdateCache(name, keyData.Policy)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) BackupPolicy(ctx context.Context, storage logical.Storage, name string) (string, error) {
|
|
|
|
p, lock, err := lm.GetPolicyExclusive(ctx, storage, name)
|
2017-12-14 17:51:50 +00:00
|
|
|
if lock != nil {
|
|
|
|
defer lock.Unlock()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return "", fmt.Errorf("invalid key %q", name)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
backup, err := p.Backup(ctx, storage)
|
2017-12-14 17:51:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cache since the policy would now have the backup information
|
|
|
|
lm.UpdateCache(name, p)
|
|
|
|
|
|
|
|
return backup, nil
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:39:19 +00:00
|
|
|
// When the function returns, a lock will be held on the policy if err == nil.
|
2016-05-03 03:46:39 +00:00
|
|
|
// It is the caller's responsibility to unlock.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) getPolicyCommon(ctx context.Context, req PolicyRequest, lockType bool) (*Policy, *sync.RWMutex, bool, error) {
|
2016-10-26 23:52:31 +00:00
|
|
|
lock := lm.policyLock(req.Name, lockType)
|
2016-05-03 03:46:39 +00:00
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
var p *Policy
|
2016-05-03 03:46:39 +00:00
|
|
|
var err error
|
2016-04-26 15:39:19 +00:00
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
// Check if it's in our cache. If so, return right away.
|
2016-04-26 15:39:19 +00:00
|
|
|
if lm.CacheActive() {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.cacheMutex.RLock()
|
2016-10-26 23:52:31 +00:00
|
|
|
p = lm.cache[req.Name]
|
2016-04-26 15:39:19 +00:00
|
|
|
if p != nil {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.cacheMutex.RUnlock()
|
|
|
|
return p, lock, false, nil
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.cacheMutex.RUnlock()
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
// Load it from storage
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err = lm.getStoredPolicy(ctx, req.Storage, req.Name)
|
2016-04-26 15:39:19 +00:00
|
|
|
if err != nil {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, err
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p == nil {
|
2016-05-03 03:46:39 +00:00
|
|
|
// This is the only place we upsert a new policy, so if upsert is not
|
2016-08-30 20:29:09 +00:00
|
|
|
// specified, or the lock type is wrong, unlock before returning
|
2016-10-26 23:52:31 +00:00
|
|
|
if !req.Upsert {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, nil
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
if lockType != exclusive {
|
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, errNeedExclusiveLock
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
switch req.KeyType {
|
2018-02-14 16:59:46 +00:00
|
|
|
case KeyType_AES256_GCM96, KeyType_ChaCha20_Poly1305:
|
2016-10-26 23:52:31 +00:00
|
|
|
if req.Convergent && !req.Derived {
|
2017-07-05 15:47:15 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
2016-09-21 14:29:42 +00:00
|
|
|
return nil, nil, false, fmt.Errorf("convergent encryption requires derivation to be enabled")
|
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
case KeyType_ECDSA_P256:
|
|
|
|
if req.Derived || req.Convergent {
|
2017-07-05 15:47:15 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, fmt.Errorf("key derivation and convergent encryption not supported for keys of type %v", req.KeyType)
|
2016-09-21 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 19:00:39 +00:00
|
|
|
case KeyType_ED25519:
|
|
|
|
if req.Convergent {
|
2017-07-05 15:47:15 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
2017-11-03 14:45:53 +00:00
|
|
|
return nil, nil, false, fmt.Errorf("convergent encryption not supported for keys of type %v", req.KeyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
case KeyType_RSA2048, KeyType_RSA4096:
|
|
|
|
if req.Derived || req.Convergent {
|
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, fmt.Errorf("key derivation and convergent encryption not supported for keys of type %v", req.KeyType)
|
2017-06-05 19:00:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
default:
|
2017-07-05 15:47:15 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
2016-10-26 23:52:31 +00:00
|
|
|
return nil, nil, false, fmt.Errorf("unsupported key type %v", req.KeyType)
|
2016-06-20 17:17:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
p = &Policy{
|
2017-12-15 14:08:28 +00:00
|
|
|
Name: req.Name,
|
|
|
|
Type: req.KeyType,
|
|
|
|
Derived: req.Derived,
|
|
|
|
Exportable: req.Exportable,
|
|
|
|
AllowPlaintextBackup: req.AllowPlaintextBackup,
|
2018-03-09 01:58:50 +00:00
|
|
|
versionPrefixCache: &sync.Map{},
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
2016-10-26 23:52:31 +00:00
|
|
|
if req.Derived {
|
|
|
|
p.KDF = Kdf_hkdf_sha256
|
|
|
|
p.ConvergentEncryption = req.Convergent
|
2016-08-26 18:11:03 +00:00
|
|
|
p.ConvergentVersion = 2
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = p.Rotate(ctx, req.Storage)
|
2016-04-26 15:39:19 +00:00
|
|
|
if err != nil {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if lm.CacheActive() {
|
|
|
|
// Since we didn't have the policy in the cache, if there was no
|
|
|
|
// error, write the value in.
|
|
|
|
lm.cacheMutex.Lock()
|
|
|
|
defer lm.cacheMutex.Unlock()
|
|
|
|
// Make sure a policy didn't appear. If so, it will only be set if
|
|
|
|
// there was no error, so assume it's good and return that
|
2016-10-26 23:52:31 +00:00
|
|
|
exp := lm.cache[req.Name]
|
2016-05-03 03:46:39 +00:00
|
|
|
if exp != nil {
|
|
|
|
return exp, lock, false, nil
|
|
|
|
}
|
|
|
|
if err == nil {
|
2016-10-26 23:52:31 +00:00
|
|
|
lm.cache[req.Name] = p
|
2016-05-03 03:46:39 +00:00
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to worry about upgrading since it will be a new policy
|
2016-05-03 03:46:39 +00:00
|
|
|
return p, lock, true, nil
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
if p.NeedsUpgrade() {
|
2016-05-03 03:46:39 +00:00
|
|
|
if lockType == shared {
|
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, errNeedExclusiveLock
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = p.Upgrade(ctx, req.Storage)
|
2016-04-26 15:39:19 +00:00
|
|
|
if err != nil {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.UnlockPolicy(lock, lockType)
|
|
|
|
return nil, nil, false, err
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
2016-05-03 03:46:39 +00:00
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
if lm.CacheActive() {
|
|
|
|
// Since we didn't have the policy in the cache, if there was no
|
|
|
|
// error, write the value in.
|
|
|
|
lm.cacheMutex.Lock()
|
|
|
|
defer lm.cacheMutex.Unlock()
|
|
|
|
// Make sure a policy didn't appear. If so, it will only be set if
|
|
|
|
// there was no error, so assume it's good and return that
|
2016-10-26 23:52:31 +00:00
|
|
|
exp := lm.cache[req.Name]
|
2016-05-03 03:46:39 +00:00
|
|
|
if exp != nil {
|
|
|
|
return exp, lock, false, nil
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
2016-05-03 03:46:39 +00:00
|
|
|
if err == nil {
|
2016-10-26 23:52:31 +00:00
|
|
|
lm.cache[req.Name] = p
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:46:39 +00:00
|
|
|
return p, lock, false, nil
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) DeletePolicy(ctx context.Context, storage logical.Storage, name string) error {
|
2016-05-03 03:46:39 +00:00
|
|
|
lm.cacheMutex.Lock()
|
|
|
|
lock := lm.policyLock(name, exclusive)
|
|
|
|
defer lock.Unlock()
|
|
|
|
defer lm.cacheMutex.Unlock()
|
2016-04-26 15:39:19 +00:00
|
|
|
|
2016-10-26 23:52:31 +00:00
|
|
|
var p *Policy
|
2016-04-26 15:39:19 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if lm.CacheActive() {
|
|
|
|
p = lm.cache[name]
|
2016-05-03 03:46:39 +00:00
|
|
|
}
|
|
|
|
if p == nil {
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err = lm.getStoredPolicy(ctx, storage, name)
|
2016-04-26 15:39:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("could not delete policy; not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.DeletionAllowed {
|
|
|
|
return fmt.Errorf("deletion is not allowed for this policy")
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = storage.Delete(ctx, "policy/"+name)
|
2016-04-26 15:39:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting policy %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = storage.Delete(ctx, "archive/"+name)
|
2016-04-26 15:39:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting archive %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lm.CacheActive() {
|
|
|
|
delete(lm.cache, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (lm *LockManager) getStoredPolicy(ctx context.Context, storage logical.Storage, name string) (*Policy, error) {
|
2018-03-09 19:01:24 +00:00
|
|
|
return LoadPolicy(ctx, storage, "policy/"+name)
|
2016-04-26 15:39:19 +00:00
|
|
|
}
|