Skip to content

Commit

Permalink
backport of commit 4173763
Browse files Browse the repository at this point in the history
  • Loading branch information
victorr committed May 24, 2024
1 parent 91fce38 commit cc6cb29
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
14 changes: 13 additions & 1 deletion builtin/logical/transit/path_hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,19 @@ func (b *backend) pathHMACVerify(ctx context.Context, req *logical.Request, d *f
name := d.Get("name").(string)
algorithm := d.Get("urlalgorithm").(string)
if algorithm == "" {
algorithm = d.Get("algorithm").(string)
hashAlgorithmRaw, hasHashAlgorithm := d.GetOk("hash_algorithm")
algorithmRaw, hasAlgorithm := d.GetOk("algorithm")

// As `algorithm` is deprecated, make sure we only read it if
// `hash_algorithm` is not present.
switch {
case hasHashAlgorithm:
algorithm = hashAlgorithmRaw.(string)
case hasAlgorithm:
algorithm = algorithmRaw.(string)
default:
algorithm = d.Get("hash_algorithm").(string)
}
}

// Get the policy
Expand Down
41 changes: 32 additions & 9 deletions builtin/logical/transit/path_hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,40 @@ func TestTransit_HMAC(t *testing.T) {
}

// Now verify
verify := func() {
t.Helper()

resp, err = b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("%v: %v", err, resp)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
if errStr, ok := resp.Data["error"]; ok {
t.Fatalf("error validating hmac: %s", errStr)
}
if resp.Data["valid"].(bool) == false {
t.Fatalf(fmt.Sprintf("error validating hmac;\nreq:\n%#v\nresp:\n%#v", *req, *resp))
}
}
req.Path = strings.ReplaceAll(req.Path, "hmac", "verify")
req.Data["hmac"] = value.(string)
resp, err = b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatalf("%v: %v", err, resp)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
if resp.Data["valid"].(bool) == false {
panic(fmt.Sprintf("error validating hmac;\nreq:\n%#v\nresp:\n%#v", *req, *resp))
verify()

// If `algorithm` parameter is used, try with `hash_algorithm` as well
if algorithm, ok := req.Data["algorithm"]; ok {
// Note that `hash_algorithm` takes precedence over `algorithm`, since the
// latter is deprecated.
req.Data["hash_algorithm"] = algorithm
req.Data["algorithm"] = "xxx"
defer func() {
// Restore the req fields, since it is re-used by the tests below
delete(req.Data, "hash_algorithm")
req.Data["algorithm"] = algorithm
}()

verify()
}
}

Expand Down
3 changes: 3 additions & 0 deletions changelog/27211.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
secrets/transit: Use 'hash_algorithm' parameter if present in HMAC verify requests. Otherwise fall back to deprecated 'algorithm' parameter.
```

0 comments on commit cc6cb29

Please sign in to comment.