Skip to content
Open
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
2 changes: 1 addition & 1 deletion .semgrep/rpc_endpoint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ rules:
return structs.ErrPermissionDenied
}
...
if err := $A.$B.AuthorizeClientAllocation(aclObj, ..., ...); err != nil {
if err := $A.$B.AuthorizeClientAllocation(..., aclObj, ..., ...); err != nil {
return err
}
...
Expand Down
3 changes: 2 additions & 1 deletion nomad/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ func (s *Server) ResolveAuthorizedClientNodePoolByNodeID(aclObj *acl.ACL, nodeID
}

func (s *Server) AuthorizeClientAllocation(
identity *structs.AuthenticatedIdentity,
aclObj *acl.ACL,
alloc *structs.Allocation,
allowNsOp func(*acl.ACL, string) bool,
) error {
return s.auth.AuthorizeClientAllocation(aclObj, alloc, allowNsOp)
return s.auth.AuthorizeClientAllocation(identity, aclObj, alloc, allowNsOp)
}

func (s *Server) ResolveAuthorizedClientNodePoolByServiceRegistrationID(
Expand Down
6 changes: 3 additions & 3 deletions nomad/alloc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (a *Alloc) GetAlloc(args *structs.AllocSpecificRequest,
reply.Alloc = out

// Re-check namespace in case it differs from request.
if err := a.srv.AuthorizeClientAllocation(aclObj, out, allowNsOp); err != nil {
if err := a.srv.AuthorizeClientAllocation(args.GetIdentity(), aclObj, out, allowNsOp); err != nil {
return structs.NewErrUnknownAllocation(args.AllocID)
}

Expand Down Expand Up @@ -231,7 +231,7 @@ func (a *Alloc) GetAllocs(args *structs.AllocsGetRequest,
continue
}

if err := a.srv.AuthorizeClientAllocation(aclObj, out, nil); err != nil {
if err := a.srv.AuthorizeClientAllocation(args.GetIdentity(), aclObj, out, nil); err != nil {
return err
}

Expand Down Expand Up @@ -492,7 +492,7 @@ func (a *Alloc) SignIdentities(args *structs.AllocIdentitiesRequest, reply *stru
continue
}

if err := a.srv.AuthorizeClientAllocation(aclObj, out, nil); err != nil {
if err := a.srv.AuthorizeClientAllocation(args.GetIdentity(), aclObj, out, nil); err != nil {
return err
}

Expand Down
5 changes: 5 additions & 0 deletions nomad/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ func (s *Authenticator) ResolveAuthorizedClientNodePoolByNodeID(aclObj *acl.ACL,
// fall back to namespace-based authorization when client-scoped authorization
// does not apply.
func (s *Authenticator) AuthorizeClientAllocation(
identity *structs.AuthenticatedIdentity,
aclObj *acl.ACL,
alloc *structs.Allocation,
allowNsOp func(*acl.ACL, string) bool,
Expand All @@ -673,6 +674,10 @@ func (s *Authenticator) AuthorizeClientAllocation(
return structs.ErrPermissionDenied
}

if identity != nil && AuthorizeSameNode(identity, alloc.NodeID) == nil {
return nil
}

if aclObj.AllowClientOp(alloc.Job.NodePool) {
return nil
}
Expand Down
27 changes: 25 additions & 2 deletions nomad/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1713,14 +1713,37 @@ func TestResolveAuthorizedClientNodePoolHelpers(t *testing.T) {
t.Run("authorize client allocation", func(t *testing.T) {
alloc := mock.Alloc()
alloc.Job.NodePool = node.NodePool
alloc.NodeID = node.ID

must.NoError(t, auth.AuthorizeClientAllocation(aclObj, alloc, nil))
must.ErrorIs(t, auth.AuthorizeClientAllocation(acl.NewClientACL("other-pool"), alloc, nil), structs.ErrPermissionDenied)
must.NoError(t, auth.AuthorizeClientAllocation(nil, aclObj, alloc, nil))
must.ErrorIs(t, auth.AuthorizeClientAllocation(nil, acl.NewClientACL("other-pool"), alloc, nil), structs.ErrPermissionDenied)
must.NoError(t, auth.AuthorizeClientAllocation(
nil,
acl.NewClientACL("other-pool"),
alloc,
func(_ *acl.ACL, ns string) bool { return ns == alloc.Namespace },
))

// Test identity-based authorization when node pool mismatches
matchingIdentity := &structs.AuthenticatedIdentity{
ClientID: node.ID,
}
must.NoError(t, auth.AuthorizeClientAllocation(
matchingIdentity,
acl.NewClientACL("other-pool"),
alloc,
nil,
))

mismatchIdentity := &structs.AuthenticatedIdentity{
ClientID: "other-node",
}
must.ErrorIs(t, auth.AuthorizeClientAllocation(
mismatchIdentity,
acl.NewClientACL("other-pool"),
alloc,
nil,
), structs.ErrPermissionDenied)
})

t.Run("resolve by service registration id", func(t *testing.T) {
Expand Down
Loading