From 78dbcfbeeec535dfc284dfa3c5a13b46893c0a50 Mon Sep 17 00:00:00 2001 From: gbolo Date: Wed, 14 Jun 2023 12:23:51 -0400 Subject: [PATCH] add a test case --- agent/acl_endpoint.go | 2 +- agent/acl_endpoint_test.go | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/agent/acl_endpoint.go b/agent/acl_endpoint.go index 29c6871e0eb9..d6f230a8f64e 100644 --- a/agent/acl_endpoint.go +++ b/agent/acl_endpoint.go @@ -442,7 +442,7 @@ func (s *HTTPHandlers) aclTokenSetInternal(req *http.Request, tokenAccessorID st } if !create { - // NOTE: AccessorID in the request body is optional not creating a new token. + // NOTE: AccessorID in the request body is optional when not creating a new token. // If not present in the body and only in the URL then it will be filled in by Consul. if args.ACLToken.AccessorID == "" { args.ACLToken.AccessorID = tokenAccessorID diff --git a/agent/acl_endpoint_test.go b/agent/acl_endpoint_test.go index 20c982492a7c..71a410d8faf3 100644 --- a/agent/acl_endpoint_test.go +++ b/agent/acl_endpoint_test.go @@ -907,6 +907,48 @@ func TestACL_HTTP(t *testing.T) { tokenMap[token.AccessorID] = token }) + t.Run("Update without AccessorID in request body", func(t *testing.T) { + originalToken := tokenMap[idMap["token-cloned"]] + + // Secret will be filled in + tokenInput := &structs.ACLToken{ + Description: "Better description for this cloned token", + Policies: []structs.ACLTokenPolicyLink{ + { + ID: idMap["policy-read-all-nodes"], + Name: policyMap[idMap["policy-read-all-nodes"]].Name, + }, + }, + NodeIdentities: []*structs.ACLNodeIdentity{ + { + NodeName: "foo", + Datacenter: "bar", + }, + }, + } + + req, _ := http.NewRequest("PUT", "/v1/acl/token/"+originalToken.AccessorID, jsonBody(tokenInput)) + req.Header.Add("X-Consul-Token", "root") + resp := httptest.NewRecorder() + obj, err := a.srv.ACLTokenCRUD(resp, req) + require.NoError(t, err) + token, ok := obj.(*structs.ACLToken) + require.True(t, ok) + + require.Equal(t, originalToken.AccessorID, token.AccessorID) + require.Equal(t, originalToken.SecretID, token.SecretID) + require.Equal(t, tokenInput.Description, token.Description) + require.Equal(t, tokenInput.Policies, token.Policies) + require.Equal(t, tokenInput.NodeIdentities, token.NodeIdentities) + require.True(t, token.CreateIndex > 0) + require.True(t, token.CreateIndex < token.ModifyIndex) + require.NotNil(t, token.Hash) + require.NotEqual(t, token.Hash, []byte{}) + require.NotEqual(t, token.Hash, originalToken.Hash) + + tokenMap[token.AccessorID] = token + }) + t.Run("CRUD Missing Token Accessor ID", func(t *testing.T) { req, _ := http.NewRequest("GET", "/v1/acl/token/", nil) req.Header.Add("X-Consul-Token", "root")