Skip to content

Commit

Permalink
acl: return 400 not 404 code when creating an invalid policy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Feb 1, 2023
1 parent d375f60 commit 0cfbac0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
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

0 comments on commit 0cfbac0

Please sign in to comment.