Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acl: return 400 not 404 code when creating an invalid policy. #16000

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/16000.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
acl: Fixed a bug where creating/updating a policy which was invalid would return a 404 status code, not a 400
```
39 changes: 27 additions & 12 deletions command/agent/acl_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,46 @@ func TestHTTP_ACLPolicyCreate(t *testing.T) {
p1 := mock.ACLPolicy()
buf := encodeReq(p1)
req, err := http.NewRequest("PUT", "/v1/acl/policy/"+p1.Name, buf)
if err != nil {
t.Fatalf("err: %v", err)
}
must.NoError(t, err)

respW := httptest.NewRecorder()
setToken(req, s.RootToken)

// Make the request
obj, err := s.Server.ACLPolicySpecificRequest(respW, req)
assert.Nil(t, err)
assert.Nil(t, obj)
must.NoError(t, err)
must.Nil(t, obj)

// Check for the index
if respW.Result().Header.Get("X-Nomad-Index") == "" {
t.Fatalf("missing index")
}
must.StrNotEqFold(t, "", respW.Result().Header.Get("X-Nomad-Index"))

// Check policy was created
state := s.Agent.server.State()
out, err := state.ACLPolicyByName(nil, p1.Name)
assert.Nil(t, err)
assert.NotNil(t, out)
must.NoError(t, err)
must.NotNil(t, out)

p1.CreateIndex, p1.ModifyIndex = out.CreateIndex, out.ModifyIndex
assert.Equal(t, p1.Name, out.Name)
assert.Equal(t, p1, out)
must.Eq(t, p1.Name, out.Name)
must.Eq(t, p1, out)

// Create a policy that is invalid. This ensures we call the validation
// func in the RPC handler, also that the correct code and error is
// returned.
aclPolicy2 := mock.ACLPolicy()
aclPolicy2.Rules = "invalid"

aclPolicy2Req, err := http.NewRequest(http.MethodPut, "/v1/acl/policy/"+aclPolicy2.Name, encodeReq(aclPolicy2))
must.NoError(t, err)

respW = httptest.NewRecorder()
setToken(aclPolicy2Req, s.RootToken)

// Make the request
aclPolicy2Obj, err := s.Server.ACLPolicySpecificRequest(respW, aclPolicy2Req)
must.ErrorContains(t, err, "400")
must.ErrorContains(t, err, "failed to parse rules")
must.Nil(t, aclPolicy2Obj)
})
}

Expand Down
2 changes: 1 addition & 1 deletion nomad/acl_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (a *ACL) UpsertPolicies(args *structs.ACLPolicyUpsertRequest, reply *struct
// Validate each policy, compute hash
for idx, policy := range args.Policies {
if err := policy.Validate(); err != nil {
return structs.NewErrRPCCodedf(404, "policy %d invalid: %v", idx, err)
return structs.NewErrRPCCodedf(http.StatusBadRequest, "policy %d invalid: %v", idx, err)
}
policy.SetHash()
}
Expand Down
7 changes: 2 additions & 5 deletions nomad/acl_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"net/url"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -722,10 +721,8 @@ func TestACLEndpoint_UpsertPolicies_Invalid(t *testing.T) {
}
var resp structs.GenericResponse
err := msgpackrpc.CallWithCodec(codec, "ACL.UpsertPolicies", req, &resp)
assert.NotNil(t, err)
if !strings.Contains(err.Error(), "failed to parse") {
t.Fatalf("bad: %s", err)
}
must.ErrorContains(t, err, "400")
must.ErrorContains(t, err, "failed to parse")
}

func TestACLEndpoint_GetToken(t *testing.T) {
Expand Down