From f912619157ac0a0782a9103ef585cebe28457a61 Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Tue, 26 Sep 2017 17:38:03 +0000 Subject: [PATCH] add ACL for GetJob endpoint --- nomad/job_endpoint.go | 7 +++++ nomad/job_endpoint_test.go | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/nomad/job_endpoint.go b/nomad/job_endpoint.go index a788f57aa..9e629f550 100644 --- a/nomad/job_endpoint.go +++ b/nomad/job_endpoint.go @@ -632,6 +632,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, diff --git a/nomad/job_endpoint_test.go b/nomad/job_endpoint_test.go index 5cac4b670..1b03e57d0 100644 --- a/nomad/job_endpoint_test.go +++ b/nomad/job_endpoint_test.go @@ -1718,6 +1718,63 @@ 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) + + // 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) + + // 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)