2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-14 20:28:46 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/keysutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-09-14 20:28:46 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 21:24:11 +00:00
|
|
|
func (b *backend) pathRotate() *framework.Path {
|
2015-09-14 20:28:46 +00:00
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "keys/" + framework.GenericNameRegex("name") + "/rotate",
|
2023-04-10 18:20:53 +00:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: operationPrefixTransit,
|
|
|
|
OperationVerb: "rotate",
|
|
|
|
OperationSuffix: "key",
|
|
|
|
},
|
|
|
|
|
2015-09-14 20:28:46 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"name": {
|
2015-09-14 20:28:46 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the key",
|
|
|
|
},
|
2023-01-27 19:39:58 +00:00
|
|
|
"managed_key_name": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "The name of the managed key to use for the new version of this transit key",
|
|
|
|
},
|
|
|
|
"managed_key_id": {
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "The UUID of the managed key to use for the new version of this transit key",
|
|
|
|
},
|
2015-09-14 20:28:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-27 21:24:11 +00:00
|
|
|
logical.UpdateOperation: b.pathRotateWrite,
|
2015-09-14 20:28:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRotateHelpSyn,
|
|
|
|
HelpDescription: pathRotateHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRotateWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-09-14 20:28:46 +00:00
|
|
|
name := d.Get("name").(string)
|
2023-01-27 19:39:58 +00:00
|
|
|
managedKeyName := d.Get("managed_key_name").(string)
|
|
|
|
managedKeyId := d.Get("managed_key_id").(string)
|
2015-09-14 20:28:46 +00:00
|
|
|
|
2016-01-27 21:24:11 +00:00
|
|
|
// Get the policy
|
2021-09-13 21:44:56 +00:00
|
|
|
p, _, err := b.GetPolicy(ctx, keysutil.PolicyRequest{
|
2018-06-12 16:24:12 +00:00
|
|
|
Storage: req.Storage,
|
|
|
|
Name: name,
|
2019-10-17 17:33:00 +00:00
|
|
|
}, b.GetRandomReader())
|
2016-04-21 20:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-26 15:39:19 +00:00
|
|
|
if p == nil {
|
2016-05-03 03:46:39 +00:00
|
|
|
return logical.ErrorResponse("key not found"), logical.ErrInvalidRequest
|
2015-09-14 20:28:46 +00:00
|
|
|
}
|
2018-06-12 16:24:12 +00:00
|
|
|
if !b.System().CachingDisabled() {
|
|
|
|
p.Lock(true)
|
|
|
|
}
|
2015-09-14 20:28:46 +00:00
|
|
|
|
2023-01-27 19:39:58 +00:00
|
|
|
if p.Type == keysutil.KeyType_MANAGED_KEY {
|
|
|
|
var keyId string
|
|
|
|
keyId, err = GetManagedKeyUUID(ctx, b, managedKeyName, managedKeyId)
|
|
|
|
if err != nil {
|
|
|
|
p.Unlock()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = p.RotateManagedKey(ctx, req.Storage, keyId)
|
|
|
|
} else {
|
|
|
|
// Rotate the policy
|
|
|
|
err = p.Rotate(ctx, req.Storage, b.GetRandomReader())
|
|
|
|
}
|
2015-09-14 20:28:46 +00:00
|
|
|
|
2018-06-12 16:24:12 +00:00
|
|
|
p.Unlock()
|
2015-09-14 20:28:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathRotateHelpSyn = `Rotate named encryption key`
|
|
|
|
|
|
|
|
const pathRotateHelpDesc = `
|
|
|
|
This path is used to rotate the named key. After rotation,
|
|
|
|
new encryption requests using this name will use the new key,
|
|
|
|
but decryption will still be supported for older versions.
|
|
|
|
`
|