Skip to content

Commit

Permalink
Merge pull request #3281 from hashicorp/f-acl-job-evaluations
Browse files Browse the repository at this point in the history
Add ACL for Job Evaluations endpoint
  • Loading branch information
chelseakomlo authored Sep 27, 2017
2 parents 67b6f2d + c4e1c02 commit ecf5599
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
7 changes: 7 additions & 0 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,13 @@ func (j *Job) Evaluations(args *structs.JobSpecificRequest,
}
defer metrics.MeasureSince([]string{"nomad", "job", "evaluations"}, time.Now())

// Check for read-job permissions
if aclObj, err := j.srv.resolveToken(args.SecretID); err != nil {
return err
} else if aclObj != nil && !aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityReadJob) {
return structs.ErrPermissionDenied
}

// Setup the blocking query
opts := blockingOptions{
queryOpts: &args.QueryOptions,
Expand Down
61 changes: 61 additions & 0 deletions nomad/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,67 @@ func TestJobEndpoint_Evaluations(t *testing.T) {
}
}

func TestJobEndpoint_Evaluations_ACL(t *testing.T) {
t.Parallel()
assert := assert.New(t)

s1, root := testACLServer(t, nil)
defer s1.Shutdown()
codec := rpcClient(t, s1)
testutil.WaitForLeader(t, s1.RPC)
state := s1.fsm.State()

// Create evaluations for the same job
eval1 := mock.Eval()
eval2 := mock.Eval()
eval2.JobID = eval1.JobID
err := state.UpsertEvals(1000,
[]*structs.Evaluation{eval1, eval2})
assert.Nil(err)

// Lookup the jobs
get := &structs.JobSpecificRequest{
JobID: eval1.JobID,
QueryOptions: structs.QueryOptions{
Region: "global",
Namespace: eval1.Namespace,
},
}

// Attempt to fetch without providing a token
var resp structs.JobEvaluationsResponse
err = msgpackrpc.CallWithCodec(codec, "Job.Evaluations", get, &resp)
assert.NotNil(err)
assert.Contains(err.Error(), "Permission denied")

// Attempt to fetch the response with an invalid token
invalidToken := CreatePolicyAndToken(t, state, 1001, "test-invalid",
NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityListJobs}))

get.SecretID = invalidToken.SecretID
var invalidResp structs.JobEvaluationsResponse
err = msgpackrpc.CallWithCodec(codec, "Job.Evaluations", get, &invalidResp)
assert.NotNil(err)
assert.Contains(err.Error(), "Permission denied")

// Attempt to fetch with valid management token should succeed
get.SecretID = root.SecretID
var validResp structs.JobEvaluationsResponse
err = msgpackrpc.CallWithCodec(codec, "Job.Evaluations", get, &validResp)
assert.Nil(err)
assert.Equal(2, len(validResp.Evaluations))

// Attempt to fetch with valid token should succeed
validToken := CreatePolicyAndToken(t, state, 1003, "test-valid",
NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}))

get.SecretID = validToken.SecretID
var validResp2 structs.JobEvaluationsResponse
err = msgpackrpc.CallWithCodec(codec, "Job.Evaluations", get, &validResp2)
assert.Nil(err)
assert.Equal(2, len(validResp2.Evaluations))
}

func TestJobEndpoint_Evaluations_Blocking(t *testing.T) {
t.Parallel()
s1 := testServer(t, nil)
Expand Down
6 changes: 3 additions & 3 deletions website/source/api/jobs.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,9 @@ The table below shows this endpoint's support for
[blocking queries](/api/index.html#blocking-queries) and
[required ACLs](/api/index.html#acls).

| Blocking Queries | ACL Required |
| ---------------- | ------------ |
| `YES` | `none` |
| Blocking Queries | ACL Required |
| ---------------- | -------------------------- |
| `YES` | `namespace:read-job` |

### Parameters

Expand Down

0 comments on commit ecf5599

Please sign in to comment.