Skip to content

Commit

Permalink
Require admin MFA for get/list tokens. (#38645)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerger authored Feb 26, 2024
1 parent 7008bef commit e39808c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/auth/auth_with_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,11 @@ func (a *ServerWithRoles) GetTokens(ctx context.Context) ([]types.ProvisionToken
if err := a.action(apidefaults.Namespace, types.KindToken, types.VerbList, types.VerbRead); err != nil {
return nil, trace.Wrap(err)
}

if err := a.context.AuthorizeAdminAction(); err != nil {
return nil, trace.Wrap(err)
}

return a.authServer.GetTokens(ctx)
}

Expand All @@ -2035,6 +2040,11 @@ func (a *ServerWithRoles) GetToken(ctx context.Context, token string) (types.Pro
return nil, trace.Wrap(err)
}
}

if err := a.context.AuthorizeAdminAction(); err != nil {
return nil, trace.Wrap(err)
}

return a.authServer.GetToken(ctx, token)
}

Expand Down
30 changes: 30 additions & 0 deletions tool/tctl/common/admin_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ func (s *adminActionTestSuite) testTokens(t *testing.T) {
cliCommand: &tctl.TokensCommand{},
setup: createToken,
cleanup: deleteToken,
}, {
command: "tokens ls",
cliCommand: &tctl.TokensCommand{},
setup: createToken,
cleanup: deleteToken,
},
} {
t.Run(tc.command, func(t *testing.T) {
Expand All @@ -385,6 +390,7 @@ func (s *adminActionTestSuite) testTokens(t *testing.T) {
resource: token,
resourceCreate: createToken,
resourceCleanup: deleteToken,
testGetList: true,
})
})

Expand Down Expand Up @@ -820,6 +826,10 @@ type resourceCommandTestCase struct {
resource types.Resource
resourceCreate func() error
resourceCleanup func() error

// Tests get/list resource, for privileged resources
// like tokens that should require MFA to be seen.
testGetList bool
}

func (s *adminActionTestSuite) testResourceCommand(t *testing.T, ctx context.Context, tc resourceCommandTestCase) {
Expand Down Expand Up @@ -854,6 +864,26 @@ func (s *adminActionTestSuite) testResourceCommand(t *testing.T, ctx context.Con
cleanup: tc.resourceCleanup,
})
})

if tc.testGetList {
t.Run("tctl get", func(t *testing.T) {
s.testCommand(t, ctx, adminActionTestCase{
command: fmt.Sprintf("get %v", getResourceRef(tc.resource)),
cliCommand: &tctl.ResourceCommand{},
setup: tc.resourceCreate,
cleanup: tc.resourceCleanup,
})
})

t.Run("tctl list", func(t *testing.T) {
s.testCommand(t, ctx, adminActionTestCase{
command: fmt.Sprintf("get %v", tc.resource.GetKind()),
cliCommand: &tctl.ResourceCommand{},
setup: tc.resourceCreate,
cleanup: tc.resourceCleanup,
})
})
}
}

type editCommandTestCase struct {
Expand Down

0 comments on commit e39808c

Please sign in to comment.