Merge pull request #3278 from hashicorp/f-acl-job-getjob
Add ACL for GetJob
This commit is contained in:
commit
97e34725e1
|
@ -654,6 +654,13 @@ func (j *Job) GetJob(args *structs.JobSpecificRequest,
|
|||
}
|
||||
defer metrics.MeasureSince([]string{"nomad", "job", "get_job"}, 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,
|
||||
|
|
|
@ -1939,6 +1939,64 @@ func TestJobEndpoint_GetJob(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestJobEndpoint_GetJob_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 the job
|
||||
job := mock.Job()
|
||||
err := state.UpsertJob(1000, job)
|
||||
assert.Nil(err)
|
||||
|
||||
// Lookup the job
|
||||
get := &structs.JobSpecificRequest{
|
||||
JobID: job.ID,
|
||||
QueryOptions: structs.QueryOptions{
|
||||
Region: "global",
|
||||
Namespace: job.Namespace,
|
||||
},
|
||||
}
|
||||
|
||||
// Looking up the job without a token should fail
|
||||
var resp structs.SingleJobResponse
|
||||
err = msgpackrpc.CallWithCodec(codec, "Job.GetJob", get, &resp)
|
||||
assert.NotNil(err)
|
||||
assert.Contains(err.Error(), "Permission denied")
|
||||
|
||||
// Expect failure for request with an invalid token
|
||||
invalidToken := CreatePolicyAndToken(t, state, 1003, "test-invalid",
|
||||
NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityListJobs}))
|
||||
|
||||
get.SecretID = invalidToken.SecretID
|
||||
var invalidResp structs.SingleJobResponse
|
||||
err = msgpackrpc.CallWithCodec(codec, "Job.GetJob", get, &invalidResp)
|
||||
assert.NotNil(err)
|
||||
assert.Contains(err.Error(), "Permission denied")
|
||||
|
||||
// Looking up the job with a management token should succeed
|
||||
get.SecretID = root.SecretID
|
||||
var validResp structs.SingleJobResponse
|
||||
err = msgpackrpc.CallWithCodec(codec, "Job.GetJob", get, &validResp)
|
||||
assert.Nil(err)
|
||||
assert.Equal(job.ID, validResp.Job.ID)
|
||||
|
||||
// Looking up the job with a valid token should succeed
|
||||
validToken := CreatePolicyAndToken(t, state, 1005, "test-valid",
|
||||
NamespacePolicy(structs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}))
|
||||
|
||||
get.SecretID = validToken.SecretID
|
||||
var validResp2 structs.SingleJobResponse
|
||||
err = msgpackrpc.CallWithCodec(codec, "Job.GetJob", get, &validResp2)
|
||||
assert.Nil(err)
|
||||
assert.Equal(job.ID, validResp2.Job.ID)
|
||||
}
|
||||
|
||||
func TestJobEndpoint_GetJob_Blocking(t *testing.T) {
|
||||
t.Parallel()
|
||||
s1 := testServer(t, nil)
|
||||
|
|
|
@ -236,9 +236,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
|
||||
|
||||
|
|
Loading…
Reference in New Issue