f3911cce66
* Rename path_config -> path_keys_config Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com> * Add config/keys to disable upserting Transit would allow anyone with Create permissions on the encryption endpoint to automatically create new encryption keys. This becomes hard to reason about for operators, especially if typos are subtly introduced (e.g., my-key vs my_key) -- there is no way to merge these two keys afterwards. Add the ability to globally disable upserting, so that if the applications using Transit do not need the capability, it can be globally disallowed even under permissive policies. Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com> * Add documentation on disabling upsert Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com> * Add changelog entry Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com> * Update website/content/api-docs/secret/transit.mdx Co-authored-by: tjperry07 <tjperry07@users.noreply.github.com> * Update website/content/api-docs/secret/transit.mdx Co-authored-by: tjperry07 <tjperry07@users.noreply.github.com> Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com> Co-authored-by: tjperry07 <tjperry07@users.noreply.github.com>
118 lines
2.8 KiB
Go
118 lines
2.8 KiB
Go
package transit
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
)
|
|
|
|
const keysConfigPath = "config/keys"
|
|
|
|
type keysConfig struct {
|
|
DisableUpsert bool `json:"disable_upsert"`
|
|
}
|
|
|
|
var defaultKeysConfig = keysConfig{
|
|
DisableUpsert: false,
|
|
}
|
|
|
|
func (b *backend) pathConfigKeys() *framework.Path {
|
|
return &framework.Path{
|
|
Pattern: "config/keys",
|
|
Fields: map[string]*framework.FieldSchema{
|
|
"disable_upsert": {
|
|
Type: framework.TypeBool,
|
|
Description: `Whether to allow automatic upserting (creation) of
|
|
keys on the encrypt endpoint.`,
|
|
},
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
logical.UpdateOperation: b.pathConfigKeysWrite,
|
|
logical.ReadOperation: b.pathConfigKeysRead,
|
|
},
|
|
|
|
HelpSynopsis: pathConfigKeysHelpSyn,
|
|
HelpDescription: pathConfigKeysHelpDesc,
|
|
}
|
|
}
|
|
|
|
func (b *backend) readConfigKeys(ctx context.Context, req *logical.Request) (*keysConfig, error) {
|
|
entry, err := req.Storage.Get(ctx, keysConfigPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to fetch keys configuration: %w", err)
|
|
}
|
|
|
|
var cfg keysConfig
|
|
if entry == nil {
|
|
cfg = defaultKeysConfig
|
|
return &cfg, nil
|
|
}
|
|
|
|
if err := entry.DecodeJSON(&cfg); err != nil {
|
|
return nil, fmt.Errorf("failed to decode keys configuration: %w", err)
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
func (b *backend) writeConfigKeys(ctx context.Context, req *logical.Request, cfg *keysConfig) error {
|
|
entry, err := logical.StorageEntryJSON(keysConfigPath, cfg)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal keys configuration: %w", err)
|
|
}
|
|
|
|
return req.Storage.Put(ctx, entry)
|
|
}
|
|
|
|
func respondConfigKeys(cfg *keysConfig) *logical.Response {
|
|
return &logical.Response{
|
|
Data: map[string]interface{}{
|
|
"disable_upsert": cfg.DisableUpsert,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (b *backend) pathConfigKeysWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
upsert := d.Get("disable_upsert").(bool)
|
|
|
|
cfg, err := b.readConfigKeys(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
modified := false
|
|
|
|
if cfg.DisableUpsert != upsert {
|
|
cfg.DisableUpsert = upsert
|
|
modified = true
|
|
}
|
|
|
|
if modified {
|
|
if err := b.writeConfigKeys(ctx, req, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return respondConfigKeys(cfg), nil
|
|
}
|
|
|
|
func (b *backend) pathConfigKeysRead(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
|
|
cfg, err := b.readConfigKeys(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return respondConfigKeys(cfg), nil
|
|
}
|
|
|
|
const pathConfigKeysHelpSyn = `Configuration common across all keys`
|
|
|
|
const pathConfigKeysHelpDesc = `
|
|
This path is used to configure common functionality across all keys. Currently,
|
|
this supports limiting the ability to automatically create new keys when an
|
|
unknown key is used for encryption (upsert).
|
|
`
|