Skip to content

Commit

Permalink
backport of commit 3481a62
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Oct 19, 2022
1 parent 0aa3ca6 commit 363d20d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
2 changes: 1 addition & 1 deletion nomad/acl_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ func (a *ACL) GetRolesByID(args *structs.ACLRolesByIDRequest, reply *structs.ACL
if token == nil {
return structs.ErrTokenNotFound
}
if token.Type != structs.ACLManagementToken && !token.RoleSubset(args.ACLRoleIDs) {
if token.Type != structs.ACLManagementToken && !token.HasRoles(args.ACLRoleIDs) {
return structs.ErrPermissionDenied
}

Expand Down
11 changes: 4 additions & 7 deletions nomad/structs/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,10 @@ func (a *ACLToken) IsExpired(t time.Time) bool {
return a.ExpirationTime.Before(t) || t.IsZero()
}

// RoleSubset checks if a given set of role IDs are assigned to the ACL token.
func (a *ACLToken) RoleSubset(roleIDs []string) bool {

// Hot-path: management tokens allows access to all roles.
if a.Type == ACLManagementToken {
return true
}
// HasRoles checks if a given set of role IDs are assigned to the ACL token. It
// does not account for management tokens, therefore it is the responsibility
// of the caller to perform this check, if required.
func (a *ACLToken) HasRoles(roleIDs []string) bool {

// Generate a set of role IDs that the token is assigned.
roleSet := set.FromFunc(a.Roles, func(roleLink *ACLTokenRoleLink) string { return roleLink.ID })
Expand Down
12 changes: 2 additions & 10 deletions nomad/structs/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,13 @@ func TestACLToken_IsExpired(t *testing.T) {
}
}

func TestACLToken_RoleSubset(t *testing.T) {
func TestACLToken_HasRoles(t *testing.T) {
testCases := []struct {
name string
inputToken *ACLToken
inputRoleIDs []string
expectedOutput bool
}{
{
name: "management token",
inputToken: &ACLToken{
Type: ACLManagementToken,
},
inputRoleIDs: []string{"foo", "bar", "baz"},
expectedOutput: true,
},
{
name: "client token request all subset",
inputToken: &ACLToken{
Expand Down Expand Up @@ -368,7 +360,7 @@ func TestACLToken_RoleSubset(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualOutput := tc.inputToken.RoleSubset(tc.inputRoleIDs)
actualOutput := tc.inputToken.HasRoles(tc.inputRoleIDs)
require.Equal(t, tc.expectedOutput, actualOutput)
})
}
Expand Down

0 comments on commit 363d20d

Please sign in to comment.