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

job statuses: fix filtering for namespace parameter #23456

Merged
merged 1 commit into from
Jun 27, 2024
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/23456.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Fixed support for namespace parameter on job statuses API
```
10 changes: 4 additions & 6 deletions nomad/job_endpoint_statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ func (j *Job) Statuses(
} else if err != nil {
return err
}
// since the state index we're using doesn't include namespace,
// explicitly add the user-provided ns to our filter if needed.
// (allowableNamespaces will be nil if the caller sent a mgmt token)
if allowableNamespaces == nil &&
namespace != "" &&
namespace != structs.AllNamespacesSentinel {
// since the state index we're using doesn't include namespace, explicitly
// set the user-provided ns to our filter if needed. we've already verified
// that the user has access to the specific namespace above
if namespace != "" && namespace != structs.AllNamespacesSentinel {
allowableNamespaces = map[string]bool{
namespace: true,
}
Expand Down
22 changes: 17 additions & 5 deletions nomad/job_endpoint_statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ func TestJob_Statuses_ACL(t *testing.T) {
t.Cleanup(cleanup)
testutil.WaitForLeader(t, s.RPC)

job1 := mock.MinJob()
job2 := mock.MinJob()
job2.Namespace = "infra"
must.NoError(t, s.State().UpsertNamespaces(100, []*structs.Namespace{{Name: "infra"}}))
must.NoError(t, s.State().UpsertJob(structs.MsgTypeTestSetup, 101, nil, job1))
must.NoError(t, s.State().UpsertJob(structs.MsgTypeTestSetup, 102, nil, job2))

insufficientToken := mock.CreatePolicyAndToken(t, s.State(), 1, "job-lister",
mock.NamespacePolicy("default", "", []string{"list-jobs"}))
happyToken := mock.CreatePolicyAndToken(t, s.State(), 2, "job-reader",
mock.NamespacePolicy("default", "", []string{"read-job"}))
mock.NamespacePolicy("*", "", []string{"read-job"}))

for _, tc := range []struct {
name, token, err string
name, token, err, ns string
expectJobs int
}{
{"no token", "", "Permission denied"},
{"insufficient perms", insufficientToken.SecretID, "Permission denied"},
{"happy token", happyToken.SecretID, ""},
{"no token", "", "Permission denied", "", 0},
{"insufficient perms", insufficientToken.SecretID, "Permission denied", "", 0},
{"happy token specific ns", happyToken.SecretID, "", "infra", 1},
{"happy token wildcard ns", happyToken.SecretID, "", "*", 2},
} {
t.Run(tc.name, func(t *testing.T) {
req := &structs.JobStatusesRequest{}
req.QueryOptions.Region = "global"
req.QueryOptions.AuthToken = tc.token
req.QueryOptions.Namespace = tc.ns

var resp structs.JobStatusesResponse
err := s.RPC("Job.Statuses", &req, &resp)
Expand All @@ -45,6 +55,8 @@ func TestJob_Statuses_ACL(t *testing.T) {
must.ErrorContains(t, err, tc.err)
} else {
must.NoError(t, err)
must.Len(t, tc.expectJobs, resp.Jobs,
must.Sprint("expected jobs to be filtered by namespace"))
}
})
}
Expand Down
Loading