Skip to content
Merged
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
73 changes: 34 additions & 39 deletions tool/tctl/common/admin_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/client"
Expand All @@ -50,14 +51,11 @@ import (
)

func TestAdminActionMFA(t *testing.T) {
s := newAdminActionTestSuite(t)

t.Run("Users", s.testAdminActionMFA_Users)
t.Run("Bots", s.testAdminActionMFA_Bots)
t.Run("Roles", s.testAdminActionMFA_Roles)
suite.Run(t, &AdminActionTestSuite{})
}

func (s *adminActionTestSuite) testAdminActionMFA_Users(t *testing.T) {
func (s *AdminActionTestSuite) TestUser() {
t := s.T()
ctx := context.Background()

user, err := types.NewUser("teleuser")
Expand Down Expand Up @@ -98,18 +96,19 @@ func (s *adminActionTestSuite) testAdminActionMFA_Users(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
s.runTestCase(t, ctx, tc)
s.testCommand(t, ctx, tc)
})
}

s.testAdminActionMFA_ResourceCommand(t, ctx, resourceCommandTestCase{
s.testResourceCommand(t, ctx, resourceCommandTestCase{
resource: user,
resourceCreate: createUser,
resourceDelete: deleteUser,
})
}

func (s *adminActionTestSuite) testAdminActionMFA_Bots(t *testing.T) {
func (s *AdminActionTestSuite) TestBot() {
t := s.T()
ctx := context.Background()

botReq := &proto.CreateBotRequest{
Expand Down Expand Up @@ -141,13 +140,14 @@ func (s *adminActionTestSuite) testAdminActionMFA_Bots(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
s.runTestCase(t, ctx, tc)
s.testCommand(t, ctx, tc)
})
}
})
}

func (s *adminActionTestSuite) testAdminActionMFA_Roles(t *testing.T) {
func (s *AdminActionTestSuite) TestRole() {
t := s.T()
ctx := context.Background()

role, err := types.NewRole("telerole", types.RoleSpecV6{})
Expand All @@ -166,13 +166,13 @@ func (s *adminActionTestSuite) testAdminActionMFA_Roles(t *testing.T) {
return s.authServer.DeleteRole(ctx, role.GetName())
}

s.testAdminActionMFA_ResourceCommand(t, ctx, resourceCommandTestCase{
s.testResourceCommand(t, ctx, resourceCommandTestCase{
resource: role,
resourceCreate: createRole,
resourceDelete: deleteRole,
})

s.testAdminActionMFA_EditCommand(t, ctx, editCommandTestCase{
s.testEditCommand(t, ctx, editCommandTestCase{
resourceRef: getResourceRef(role),
resourceCreate: createRole,
resourceGet: getRole,
Expand All @@ -186,23 +186,23 @@ type resourceCommandTestCase struct {
resourceDelete func() error
}

func (s *adminActionTestSuite) testAdminActionMFA_ResourceCommand(t *testing.T, ctx context.Context, tc resourceCommandTestCase) {
func (s *AdminActionTestSuite) testResourceCommand(t *testing.T, ctx context.Context, tc resourceCommandTestCase) {
t.Helper()

f, err := os.CreateTemp(t.TempDir(), "resource-*.yaml")
require.NoError(t, err)
require.NoError(t, utils.WriteYAML(f, tc.resource))

t.Run("tctl create", func(t *testing.T) {
s.runTestCase(t, ctx, adminActionTestCase{
s.testCommand(t, ctx, adminActionTestCase{
command: fmt.Sprintf("create %v", f.Name()),
cliCommand: &tctl.ResourceCommand{},
cleanup: tc.resourceDelete,
})
})

t.Run("tctl create -f", func(t *testing.T) {
s.runTestCase(t, ctx, adminActionTestCase{
s.testCommand(t, ctx, adminActionTestCase{
command: fmt.Sprintf("create -f %v", f.Name()),
cliCommand: &tctl.ResourceCommand{},
setup: tc.resourceCreate,
Expand All @@ -211,7 +211,7 @@ func (s *adminActionTestSuite) testAdminActionMFA_ResourceCommand(t *testing.T,
})

t.Run("tctl rm", func(t *testing.T) {
s.runTestCase(t, ctx, adminActionTestCase{
s.testCommand(t, ctx, adminActionTestCase{
command: fmt.Sprintf("rm %v", getResourceRef(tc.resource)),
cliCommand: &tctl.ResourceCommand{},
setup: tc.resourceCreate,
Expand All @@ -227,9 +227,9 @@ type editCommandTestCase struct {
resourceDelete func() error
}

func (s *adminActionTestSuite) testAdminActionMFA_EditCommand(t *testing.T, ctx context.Context, tc editCommandTestCase) {
func (s *AdminActionTestSuite) testEditCommand(t *testing.T, ctx context.Context, tc editCommandTestCase) {
t.Run("tctl edit", func(t *testing.T) {
s.runTestCase(t, ctx, adminActionTestCase{
s.testCommand(t, ctx, adminActionTestCase{
command: fmt.Sprintf("edit %v", tc.resourceRef),
setup: tc.resourceCreate,
cliCommand: &tctl.EditCommand{
Expand All @@ -252,16 +252,17 @@ func (s *adminActionTestSuite) testAdminActionMFA_EditCommand(t *testing.T, ctx
})
}

type adminActionTestSuite struct {
type AdminActionTestSuite struct {
suite.Suite
authServer *auth.Server
// userClientWithMFA supports MFA prompt for admin actions.
userClientWithMFA auth.ClientI
// userClientWithMFA does not support MFA prompt for admin actions.
userClientNoMFA auth.ClientI
}

func newAdminActionTestSuite(t *testing.T) *adminActionTestSuite {
t.Helper()
func (s *AdminActionTestSuite) SetupSuite() {
t := s.T()
ctx := context.Background()

authPref, err := types.NewAuthPreference(types.AuthPreferenceSpecV2{
Expand All @@ -285,7 +286,7 @@ func newAdminActionTestSuite(t *testing.T) *adminActionTestSuite {
)
authAddr, err := process.AuthAddr()
require.NoError(t, err)
authServer := process.GetAuthServer()
s.authServer = process.GetAuthServer()

// create admin role and user.
username := "admin"
Expand All @@ -300,16 +301,16 @@ func newAdminActionTestSuite(t *testing.T) *adminActionTestSuite {
},
})
require.NoError(t, err)
adminRole, err = authServer.CreateRole(ctx, adminRole)
adminRole, err = s.authServer.CreateRole(ctx, adminRole)
require.NoError(t, err)

user, err := types.NewUser(username)
user.SetRoles([]string{adminRole.GetName()})
require.NoError(t, err)
_, err = authServer.CreateUser(ctx, user)
_, err = s.authServer.CreateUser(ctx, user)
require.NoError(t, err)

mockWebauthnLogin := setupWebAuthn(t, authServer, username)
mockWebauthnLogin := setupWebAuthn(t, s.authServer, username)
mockMFAPromptConstructor := func(opts ...mfa.PromptOpt) mfa.Prompt {
promptCfg := libmfa.NewPromptConfig(proxyPublicAddr.String(), opts...)
promptCfg.WebauthnLoginFunc = mockWebauthnLogin
Expand All @@ -335,28 +336,22 @@ func newAdminActionTestSuite(t *testing.T) *adminActionTestSuite {
)
require.NoError(t, err)

userClientNoMFA, err := auth.NewClient(client.Config{
s.userClientNoMFA, err = auth.NewClient(client.Config{
Addrs: []string{authAddr.String()},
Credentials: []client.Credentials{
client.LoadProfile(tshHome, ""),
},
})
require.NoError(t, err)

userClientWithMFA, err := auth.NewClient(client.Config{
s.userClientWithMFA, err = auth.NewClient(client.Config{
Addrs: []string{authAddr.String()},
Credentials: []client.Credentials{
client.LoadProfile(tshHome, ""),
},
MFAPromptConstructor: mockMFAPromptConstructor,
})
require.NoError(t, err)

return &adminActionTestSuite{
authServer: authServer,
userClientNoMFA: userClientNoMFA,
userClientWithMFA: userClientWithMFA,
}
}

type adminActionTestCase struct {
Expand All @@ -366,16 +361,16 @@ type adminActionTestCase struct {
cleanup func() error
}

func (s *adminActionTestSuite) runTestCase(t *testing.T, ctx context.Context, tc adminActionTestCase) {
func (s *AdminActionTestSuite) testCommand(t *testing.T, ctx context.Context, tc adminActionTestCase) {
t.Helper()

t.Run("OK with MFA", func(t *testing.T) {
err := runTestSubCase(t, ctx, s.userClientWithMFA, tc)
err := runTestCase(t, ctx, s.userClientWithMFA, tc)
require.NoError(t, err)
})

t.Run("NOK without MFA", func(t *testing.T) {
err := runTestSubCase(t, ctx, s.userClientNoMFA, tc)
err := runTestCase(t, ctx, s.userClientNoMFA, tc)
require.ErrorIs(t, err, &mfa.ErrAdminActionMFARequired)
})

Expand All @@ -391,12 +386,12 @@ func (s *adminActionTestSuite) runTestCase(t *testing.T, ctx context.Context, tc
require.NoError(t, s.authServer.SetAuthPreference(ctx, originalAuthPref))
})

err = runTestSubCase(t, ctx, s.userClientNoMFA, tc)
err = runTestCase(t, ctx, s.userClientNoMFA, tc)
require.NoError(t, err)
})
}

func runTestSubCase(t *testing.T, ctx context.Context, client auth.ClientI, tc adminActionTestCase) error {
func runTestCase(t *testing.T, ctx context.Context, client auth.ClientI, tc adminActionTestCase) error {
t.Helper()

if tc.setup != nil {
Expand Down