Skip to content

Commit

Permalink
backport of commit 6814f8a (#14594)
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Rodriguez <[email protected]>
  • Loading branch information
1 parent 88c8e85 commit a69d9a5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 24 deletions.
11 changes: 5 additions & 6 deletions builtin/logical/transit/path_encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,17 @@ func (b *backend) pathEncryptWrite(ctx context.Context, req *logical.Request, d
return logical.ErrorResponse("missing batch input to process"), logical.ErrInvalidRequest
}
} else {
valueRaw, ok := d.Raw["plaintext"]
if !ok {
return logical.ErrorResponse("missing plaintext to encrypt"), logical.ErrInvalidRequest
valueRaw, ok, err := d.GetOkErr("plaintext")
if err != nil {
return nil, err
}
plaintext, ok := valueRaw.(string)
if !ok {
return logical.ErrorResponse("expected plaintext of type 'string', got unconvertible type '%T'", valueRaw), logical.ErrInvalidRequest
return logical.ErrorResponse("missing plaintext to encrypt"), logical.ErrInvalidRequest
}

batchInputItems = make([]BatchRequestItem, 1)
batchInputItems[0] = BatchRequestItem{
Plaintext: plaintext,
Plaintext: valueRaw.(string),
Context: d.Get("context").(string),
Nonce: d.Get("nonce").(string),
KeyVersion: d.Get("key_version").(int),
Expand Down
6 changes: 1 addition & 5 deletions builtin/logical/transit/path_encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ func TestTransit_MissingPlaintext(t *testing.T) {
t.Fatalf("err:%v resp:%#v", err, resp)
}

encData := map[string]interface{}{
"plaintext": nil,
}

encReq := &logical.Request{
Operation: logical.UpdateOperation,
Path: "encrypt/existing_key",
Storage: s,
Data: encData,
Data: map[string]interface{}{},
}
resp, err = b.HandleRequest(context.Background(), encReq)
if resp == nil || !resp.IsError() {
Expand Down
11 changes: 5 additions & 6 deletions builtin/logical/transit/path_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ Defaults to "sha2-256".`,
}

func (b *backend) pathHashWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
rawInput, ok := d.Raw["input"]
if !ok {
return logical.ErrorResponse("input missing"), logical.ErrInvalidRequest
rawInput, ok, err := d.GetOkErr("input")
if err != nil {
return nil, err
}

inputB64, ok := rawInput.(string)
if !ok {
return logical.ErrorResponse("expected input of type 'string', got unconvertible type '%T'", rawInput), logical.ErrInvalidRequest
return logical.ErrorResponse("input missing"), logical.ErrInvalidRequest
}

inputB64 := rawInput.(string)
format := d.Get("format").(string)
algorithm := d.Get("urlalgorithm").(string)
if algorithm == "" {
Expand Down
2 changes: 1 addition & 1 deletion builtin/logical/transit/path_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestTransit_Hash(t *testing.T) {
doRequest(req, false, "98rFrYMEIqVAizamCmBiBoe+GAdlo+KJW8O9vYV8nggkbIMGTU42EvDLkn8+rSCEE6uYYkv3sGF68PA/YggJdg==")

// Test bad input/format/algorithm
req.Data["input"] = nil
delete(req.Data, "input")
doRequest(req, true, "")

req.Data["input"] = "dGhlIHF1aWNrIGJyb3duIGZveA=="
Expand Down
10 changes: 5 additions & 5 deletions builtin/logical/transit/path_trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func (b *backend) pathTrimUpdate() framework.OperationFunc {
}
defer p.Unlock()

minAvailableVersionRaw, ok := d.Raw["min_available_version"]
if !ok {
return logical.ErrorResponse("missing min_available_version"), nil
minAvailableVersionRaw, ok, err := d.GetOkErr("min_available_version")
if err != nil {
return nil, err
}
minAvailableVersion, ok := minAvailableVersionRaw.(int)
if !ok {
return logical.ErrorResponse("expected min_available_version of type 'int', got unconvertible type '%T'", minAvailableVersionRaw), logical.ErrInvalidRequest
return logical.ErrorResponse("missing min_available_version"), nil
}
minAvailableVersion := minAvailableVersionRaw.(int)

originalMinAvailableVersion := p.MinAvailableVersion

Expand Down
2 changes: 1 addition & 1 deletion changelog/14074.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:bug
secrets/transit: Return an error if any required parameter is missing or nil. Do not encrypt nil plaintext as if it was an empty string.
secrets/transit: Return an error if any required parameter is missing.
```

0 comments on commit a69d9a5

Please sign in to comment.