Skip to content

Commit

Permalink
[v15] Replace NewCLIPromptV2 (#48189)
Browse files Browse the repository at this point in the history
* Replace NewCLIPromptV2. (#47927)

* Update e ref to branch/v15; Add missing changes.
  • Loading branch information
Joerger authored Oct 31, 2024
1 parent c0120e7 commit 5bef5fb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion e
Submodule e updated from 456398 to af339e
2 changes: 1 addition & 1 deletion lib/client/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type WebauthnLoginFunc = libmfa.WebauthnLoginFunc
func (tc *TeleportClient) NewMFAPrompt(opts ...mfa.PromptOpt) mfa.Prompt {
cfg := tc.newPromptConfig(opts...)

var prompt mfa.Prompt = libmfa.NewCLIPromptV2(&libmfa.CLIPromptConfig{
var prompt mfa.Prompt = libmfa.NewCLIPrompt(&libmfa.CLIPromptConfig{
PromptConfig: *cfg,
Writer: tc.Stderr,
PreferOTP: tc.PreferOTP,
Expand Down
25 changes: 8 additions & 17 deletions lib/client/mfa/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,8 @@ type CLIPrompt struct {
cfg CLIPromptConfig
}

// NewCLIPrompt returns a new CLI mfa prompt with the config and writer.
// TODO(Joerger): Delete once /e is no longer dependent on it.
func NewCLIPrompt(cfg *PromptConfig, writer io.Writer) *CLIPrompt {
// If no config is provided, use defaults (zero value).
if cfg == nil {
cfg = new(PromptConfig)
}
return NewCLIPromptV2(&CLIPromptConfig{
PromptConfig: *cfg,
Writer: writer,
})
}

// NewCLIPromptV2 returns a new CLI mfa prompt with the given config.
// TODO(Joerger): this is V2 because /e depends on a different function
// signature for NewCLIPrompt, so this requires a couple follow up PRs to fix.
func NewCLIPromptV2(cfg *CLIPromptConfig) *CLIPrompt {
// NewCLIPrompt returns a new CLI mfa prompt with the given config.
func NewCLIPrompt(cfg *CLIPromptConfig) *CLIPrompt {
// If no config is provided, use defaults (zero value).
if cfg == nil {
cfg = new(CLIPromptConfig)
Expand All @@ -87,6 +72,12 @@ func NewCLIPromptV2(cfg *CLIPromptConfig) *CLIPrompt {
}
}

// NewCLIPromptV2 returns a new CLI mfa prompt with the given config.
// TODO(Joerger): remove once /e is no longer dependent on this.
func NewCLIPromptV2(cfg *CLIPromptConfig) *CLIPrompt {
return NewCLIPrompt(cfg)
}

func (c *CLIPrompt) stdin() prompt.StdinReader {
if c.cfg.StdinFunc == nil {
return prompt.Stdin()
Expand Down
23 changes: 15 additions & 8 deletions lib/client/mfa/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func TestCLIPrompt(t *testing.T) {
expectStdOut: "",
challenge: &proto.MFAAuthenticateChallenge{},
expectResp: &proto.MFAAuthenticateResponse{},
}, {
},
{
name: "OK webauthn",
expectStdOut: "Tap any security key\n",
challenge: &proto.MFAAuthenticateChallenge{
Expand All @@ -64,7 +65,8 @@ func TestCLIPrompt(t *testing.T) {
Webauthn: &webauthnpb.CredentialAssertionResponse{},
},
},
}, {
},
{
name: "OK totp",
expectStdOut: "Enter an OTP code from a device:\n",
stdin: "123456",
Expand All @@ -78,7 +80,8 @@ func TestCLIPrompt(t *testing.T) {
},
},
},
}, {
},
{
name: "OK webauthn or totp choose webauthn",
expectStdOut: "Tap any security key or enter a code from a OTP device\n",
challenge: &proto.MFAAuthenticateChallenge{
Expand All @@ -90,7 +93,8 @@ func TestCLIPrompt(t *testing.T) {
Webauthn: &webauthnpb.CredentialAssertionResponse{},
},
},
}, {
},
{
name: "OK webauthn or totp choose totp",
expectStdOut: "Tap any security key or enter a code from a OTP device\n",
stdin: "123456",
Expand All @@ -105,21 +109,24 @@ func TestCLIPrompt(t *testing.T) {
},
},
},
}, {
},
{
name: "NOK no webauthn response",
expectStdOut: "Tap any security key\n",
challenge: &proto.MFAAuthenticateChallenge{
WebauthnChallenge: &webauthnpb.CredentialAssertion{},
},
expectErr: context.DeadlineExceeded,
}, {
},
{
name: "NOK no totp response",
expectStdOut: "Enter an OTP code from a device:\n",
challenge: &proto.MFAAuthenticateChallenge{
TOTP: &proto.TOTPChallenge{},
},
expectErr: context.DeadlineExceeded,
}, {
},
{
name: "NOK no webauthn or totp response",
expectStdOut: "Tap any security key or enter a code from a OTP device\n",
challenge: &proto.MFAAuthenticateChallenge{
Expand Down Expand Up @@ -260,7 +267,7 @@ Enter your security key PIN:
buffer := make([]byte, 0, 100)
out := bytes.NewBuffer(buffer)

prompt := mfa.NewCLIPromptV2(&mfa.CLIPromptConfig{
prompt := mfa.NewCLIPrompt(&mfa.CLIPromptConfig{
PromptConfig: *cfg,
Writer: out,
AllowStdinHijack: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/client/mfa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestPromptMFAChallenge_usingNonRegisteredDevice(t *testing.T) {
test.customizePrompt(cliConfig)
}

_, err := mfa.NewCLIPromptV2(cliConfig).Run(ctx, test.challenge)
_, err := mfa.NewCLIPrompt(cliConfig).Run(ctx, test.challenge)
if !errors.Is(err, wancli.ErrUsingNonRegisteredDevice) {
t.Errorf("PromptMFAChallenge returned err=%q, want %q", err, wancli.ErrUsingNonRegisteredDevice)
}
Expand Down
6 changes: 4 additions & 2 deletions lib/client/weblogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ func SSHAgentMFALogin(ctx context.Context, login SSHLoginMFA) (*authclient.SSHLo

promptMFA := login.PromptMFA
if promptMFA == nil {
promptMFA = libmfa.NewCLIPrompt(libmfa.NewPromptConfig(login.ProxyAddr), os.Stderr)
cfg := libmfa.NewPromptConfig(login.ProxyAddr)
promptMFA = libmfa.NewCLIPrompt(&libmfa.CLIPromptConfig{PromptConfig: *cfg})
}

respPB, err := promptMFA.Run(ctx, chal)
Expand Down Expand Up @@ -856,7 +857,8 @@ func SSHAgentMFAWebSessionLogin(ctx context.Context, login SSHLoginMFA) (*WebCli

promptMFA := login.PromptMFA
if promptMFA == nil {
promptMFA = libmfa.NewCLIPrompt(libmfa.NewPromptConfig(login.ProxyAddr), os.Stderr)
cfg := libmfa.NewPromptConfig(login.ProxyAddr)
promptMFA = libmfa.NewCLIPrompt(&libmfa.CLIPromptConfig{PromptConfig: *cfg})
}

respPB, err := promptMFA.Run(ctx, chal)
Expand Down
2 changes: 1 addition & 1 deletion tool/tctl/common/admin_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ func newAdminActionTestSuite(t *testing.T) *adminActionTestSuite {
promptCfg := libmfa.NewPromptConfig(proxyPublicAddr.String(), opts...)
promptCfg.WebauthnLoginFunc = mockWebauthnLogin
promptCfg.WebauthnSupported = true
return libmfa.NewCLIPromptV2(&libmfa.CLIPromptConfig{
return libmfa.NewCLIPrompt(&libmfa.CLIPromptConfig{
PromptConfig: *promptCfg,
})
}
Expand Down
2 changes: 1 addition & 1 deletion tool/tctl/common/tctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TryRun(commands []CLICommand, args []string) error {
proxyAddr := resp.ProxyPublicAddr
client.SetMFAPromptConstructor(func(opts ...mfa.PromptOpt) mfa.Prompt {
promptCfg := libmfa.NewPromptConfig(proxyAddr, opts...)
return libmfa.NewCLIPromptV2(&libmfa.CLIPromptConfig{
return libmfa.NewCLIPrompt(&libmfa.CLIPromptConfig{
PromptConfig: *promptCfg,
})
})
Expand Down

0 comments on commit 5bef5fb

Please sign in to comment.