Skip to content

Commit

Permalink
Revert "Transit UX improvements: show key policy, configs on write (h…
Browse files Browse the repository at this point in the history
…ashicorp#20652)"

This reverts commit d52d307.
  • Loading branch information
tsaarni committed Mar 8, 2024
1 parent 6f67d28 commit 3942219
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 58 deletions.
16 changes: 12 additions & 4 deletions builtin/logical/transit/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,9 @@ func testConvergentEncryptionCommon(t *testing.T, ver int, keyType keysutil.KeyT
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp, "expected populated request")
if resp != nil {
t.Fatal("expected nil response")
}

p, err := keysutil.LoadPolicy(context.Background(), storage, path.Join("policy", "testkey"))
if err != nil {
Expand Down Expand Up @@ -1565,7 +1567,9 @@ func TestBadInput(t *testing.T) {
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp, "expected populated request")
if resp != nil {
t.Fatal("expected nil response")
}

req.Path = "decrypt/test"
req.Data = map[string]interface{}{
Expand Down Expand Up @@ -1654,7 +1658,9 @@ func TestTransit_AutoRotateKeys(t *testing.T) {
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp, "expected populated request")
if resp != nil {
t.Fatal("expected nil response")
}

// Write a key with an auto rotate value one day in the future
req = &logical.Request{
Expand All @@ -1669,7 +1675,9 @@ func TestTransit_AutoRotateKeys(t *testing.T) {
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resp, "expected populated request")
if resp != nil {
t.Fatal("expected nil response")
}

// Run the rotation check and ensure none of the keys have rotated
b.checkAutoRotateAfter = time.Now()
Expand Down
6 changes: 1 addition & 5 deletions builtin/logical/transit/path_cache_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ func (b *backend) pathCacheConfigWrite(ctx context.Context, req *logical.Request
return nil, err
}

return &logical.Response{
Data: map[string]interface{}{
"size": cacheSize,
},
}, nil
return nil, nil
}

type configCache struct {
Expand Down
30 changes: 12 additions & 18 deletions builtin/logical/transit/path_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,12 @@ func (b *backend) pathPolicyWrite(ctx context.Context, req *logical.Request, d *
p.Unlock()
}

resp, err := b.formatKeyPolicy(p, nil)
if err != nil {
return nil, err
}
resp := &logical.Response{}
if !upserted {
resp.AddWarning(fmt.Sprintf("key %s already existed", name))
}
return resp, nil

return nil, nil
}

// Built-in helper type for returning asymmetric keys
Expand Down Expand Up @@ -289,19 +287,6 @@ func (b *backend) pathPolicyRead(ctx context.Context, req *logical.Request, d *f
}
defer p.Unlock()

contextRaw := d.Get("context").(string)
var context []byte
if len(contextRaw) != 0 {
context, err = base64.StdEncoding.DecodeString(contextRaw)
if err != nil {
return logical.ErrorResponse("failed to base64-decode context"), logical.ErrInvalidRequest
}
}

return b.formatKeyPolicy(p, context)
}

func (b *backend) formatKeyPolicy(p *keysutil.Policy, context []byte) (*logical.Response, error) {
// Return the response
resp := &logical.Response{
Data: map[string]interface{}{
Expand Down Expand Up @@ -358,6 +343,15 @@ func (b *backend) formatKeyPolicy(p *keysutil.Policy, context []byte) (*logical.
}
}

contextRaw := d.Get("context").(string)
var context []byte
if len(contextRaw) != 0 {
context, err = base64.StdEncoding.DecodeString(contextRaw)
if err != nil {
return logical.ErrorResponse("failed to base64-decode context"), logical.ErrInvalidRequest
}
}

switch p.Type {
case keysutil.KeyType_AES128_GCM96, keysutil.KeyType_AES256_GCM96, keysutil.KeyType_ChaCha20_Poly1305:
retKeys := map[string]int64{}
Expand Down
28 changes: 7 additions & 21 deletions builtin/logical/transit/path_keys_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ func (b *backend) pathKeysConfigWrite(ctx context.Context, req *logical.Request,
}
defer p.Unlock()

var warning string

originalMinDecryptionVersion := p.MinDecryptionVersion
originalMinEncryptionVersion := p.MinEncryptionVersion
originalDeletionAllowed := p.DeletionAllowed
Expand All @@ -115,6 +113,8 @@ func (b *backend) pathKeysConfigWrite(ctx context.Context, req *logical.Request,
}
}()

resp = &logical.Response{}

persistNeeded := false

minDecryptionVersionRaw, ok := d.GetOk("min_decryption_version")
Expand All @@ -127,7 +127,7 @@ func (b *backend) pathKeysConfigWrite(ctx context.Context, req *logical.Request,

if minDecryptionVersion == 0 {
minDecryptionVersion = 1
warning = "since Vault 0.3, transit key numbering starts at 1; forcing minimum to 1"
resp.AddWarning("since Vault 0.3, transit key numbering starts at 1; forcing minimum to 1")
}

if minDecryptionVersion != p.MinDecryptionVersion {
Expand Down Expand Up @@ -225,14 +225,7 @@ func (b *backend) pathKeysConfigWrite(ctx context.Context, req *logical.Request,
}

if !persistNeeded {
resp, err := b.formatKeyPolicy(p, nil)
if err != nil {
return nil, err
}
if warning != "" {
resp.AddWarning(warning)
}
return resp, nil
return nil, nil
}

switch {
Expand All @@ -242,18 +235,11 @@ func (b *backend) pathKeysConfigWrite(ctx context.Context, req *logical.Request,
return logical.ErrorResponse("min decryption version should not be less then min available version"), nil
}

if err := p.Persist(ctx, req.Storage); err != nil {
return nil, err
if len(resp.Warnings) == 0 {
return nil, p.Persist(ctx, req.Storage)
}

resp, err = b.formatKeyPolicy(p, nil)
if err != nil {
return nil, err
}
if warning != "" {
resp.AddWarning(warning)
}
return resp, nil
return resp, p.Persist(ctx, req.Storage)
}

const pathKeysConfigHelpSyn = `Configure a named encryption key`
Expand Down
8 changes: 2 additions & 6 deletions builtin/logical/transit/path_rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (b *backend) pathRotateWrite(ctx context.Context, req *logical.Request, d *
if !b.System().CachingDisabled() {
p.Lock(true)
}
defer p.Unlock()

if p.Type == keysutil.KeyType_MANAGED_KEY {
var keyId string
Expand All @@ -79,11 +78,8 @@ func (b *backend) pathRotateWrite(ctx context.Context, req *logical.Request, d *
err = p.Rotate(ctx, req.Storage, b.GetRandomReader())
}

if err != nil {
return nil, err
}

return b.formatKeyPolicy(p, nil)
p.Unlock()
return nil, err
}

const pathRotateHelpSyn = `Rotate named encryption key`
Expand Down
2 changes: 1 addition & 1 deletion builtin/logical/transit/path_trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (b *backend) pathTrimUpdate() framework.OperationFunc {
return nil, err
}

return b.formatKeyPolicy(p, nil)
return nil, nil
}
}

Expand Down
3 changes: 0 additions & 3 deletions changelog/20652.txt

This file was deleted.

0 comments on commit 3942219

Please sign in to comment.