2026-04-06 15:48:37 +05:30
// Copyright IBM Corp. 2020, 2026
2022-08-05 16:08:48 -04:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-07-14 14:32:47 -07:00
package authenticate
import (
2021-02-25 12:43:17 -05:00
"errors"
2020-07-14 14:32:47 -07:00
"fmt"
"os"
"strings"
2020-09-24 14:34:58 -07:00
"github.com/hashicorp/boundary/api"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/api/authmethods"
2023-03-10 15:06:39 -05:00
"github.com/hashicorp/boundary/globals"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/cmd/base"
2022-08-05 13:21:46 -04:00
"github.com/hashicorp/go-secure-stdlib/parseutil"
2021-07-14 17:38:52 -07:00
"github.com/hashicorp/go-secure-stdlib/password"
2020-07-14 14:32:47 -07:00
"github.com/mitchellh/cli"
"github.com/mitchellh/go-wordwrap"
"github.com/posener/complete"
)
2021-01-07 10:02:32 -05:00
var (
_ cli . Command = ( * PasswordCommand )( nil )
_ cli . CommandAutocomplete = ( * PasswordCommand )( nil )
)
2020-07-14 14:32:47 -07:00
2021-01-07 10:02:32 -05:00
var (
2026-10-12 10:20:06 -07:00
envPassword = "THRESHOLD_AUTHENTICATE_PASSWORD_PASSWORD"
envLoginName = "THRESHOLD_AUTHENTICATE_PASSWORD_LOGIN_NAME"
2021-01-07 10:02:32 -05:00
)
2020-07-14 14:32:47 -07:00
type PasswordCommand struct {
* base . Command
2020-10-02 19:20:30 -04:00
flagLoginName string
flagPassword string
2023-01-31 10:07:10 -08:00
2023-11-01 12:45:35 -07:00
parsedOpts base . Options
2020-07-14 14:32:47 -07:00
}
func ( c * PasswordCommand ) Synopsis () string {
2026-10-12 10:20:06 -07:00
return wordwrap . WrapString ( "Invoke the password auth method to authenticate with Threshold" , base . TermWidth )
2020-07-14 14:32:47 -07:00
}
func ( c * PasswordCommand ) Help () string {
return base . WrapForHelpText ([] string {
2026-10-12 10:20:06 -07:00
"Usage: threshold authenticate password [options] [args]" ,
2020-07-14 14:32:47 -07:00
"" ,
2026-10-12 10:20:06 -07:00
" Invoke the password auth method to authenticate the Threshold CLI. Example:" ,
2020-07-14 14:32:47 -07:00
"" ,
2026-10-12 10:20:06 -07:00
` $ threshold authenticate password -auth-method-id ampw_1234567890 -login-name foo` ,
2020-10-09 20:26:48 -04:00
"" ,
"" ,
2020-07-14 14:32:47 -07:00
}) + c . Flags (). Help ()
}
func ( c * PasswordCommand ) Flags () * base . FlagSets {
set := c . FlagSet ( base . FlagSetHTTP | base . FlagSetClient | base . FlagSetOutputFormat )
f := set . NewFlagSet ( "Command Options" )
f . StringVar ( & base . StringVar {
2020-08-13 18:32:58 -04:00
Name : "login-name" ,
Target : & c . flagLoginName ,
EnvVar : envLoginName ,
2022-08-05 13:21:46 -04:00
Usage : "The login name corresponding to an account within the given auth method." ,
2020-07-14 14:32:47 -07:00
})
f . StringVar ( & base . StringVar {
Name : "password" ,
Target : & c . flagPassword ,
EnvVar : envPassword ,
2022-08-05 16:08:48 -04:00
Usage : "The password associated with the login name. If blank, the command will prompt for the password to be entered interactively in a non-echoing way. Otherwise, this can refer to a file on disk (file://) from which a password will be read or an env var (env://) from which the password will be read." ,
2020-07-14 14:32:47 -07:00
})
f . StringVar ( & base . StringVar {
2020-08-13 18:32:58 -04:00
Name : "auth-method-id" ,
2026-10-12 10:20:06 -07:00
EnvVar : "THRESHOLD_AUTH_METHOD_ID" ,
2020-10-02 19:20:30 -04:00
Target : & c . FlagAuthMethodId ,
2022-08-05 13:21:46 -04:00
Usage : "The auth-method resource to use for the operation." ,
2020-07-14 14:32:47 -07:00
})
2023-11-01 12:45:35 -07:00
if ! c . parsedOpts . WithSkipScopeIdFlag {
2023-01-31 10:07:10 -08:00
f . StringVar ( & base . StringVar {
Name : "scope-id" ,
2026-10-12 10:20:06 -07:00
EnvVar : "THRESHOLD_SCOPE_ID" ,
2023-01-31 10:07:10 -08:00
Target : & c . FlagScopeId ,
Usage : "The scope ID to use for the operation." ,
})
}
2020-07-14 14:32:47 -07:00
return set
}
func ( c * PasswordCommand ) AutocompleteArgs () complete . Predictor {
return complete . PredictAnything
}
func ( c * PasswordCommand ) AutocompleteFlags () complete . Flags {
return c . Flags (). Completions ()
}
func ( c * PasswordCommand ) Run ( args [] string ) int {
2023-11-01 12:45:35 -07:00
c . parsedOpts = base . GetOpts ( c . Opts ... )
2023-01-31 10:07:10 -08:00
2020-07-14 14:32:47 -07:00
f := c . Flags ()
if err := f . Parse ( args ); err != nil {
2021-02-25 12:43:17 -05:00
c . PrintCliError ( err )
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-07-14 14:32:47 -07:00
}
2023-01-31 10:07:10 -08:00
client , err := c . Client ( base . WithNoTokenScope (), base . WithNoTokenValue ())
if c . WrapperCleanupFunc != nil {
defer func () {
if err := c . WrapperCleanupFunc (); err != nil {
c . PrintCliError ( fmt . Errorf ( "Error cleaning kms wrapper: %w" , err ))
}
}()
}
if err != nil {
c . PrintCliError ( fmt . Errorf ( "Error creating API client: %w" , err ))
return base . CommandCliError
}
aClient := authmethods . NewClient ( client )
// if auth method ID isn't passed on the CLI, try looking up the primary auth method ID
if c . FlagAuthMethodId == "" {
// if flag for scope is empty try looking up global
if c . FlagScopeId == "" {
c . FlagScopeId = "global"
}
2023-03-10 15:06:39 -05:00
pri , err := getPrimaryAuthMethodId ( c . Context , aClient , c . FlagScopeId , globals . PasswordAuthMethodPrefix )
2023-01-31 10:07:10 -08:00
if err != nil {
c . PrintCliError ( err )
return base . CommandUserError
}
c . FlagAuthMethodId = pri
}
switch c . flagLoginName {
case "" :
fmt . Print ( "Please enter the login name (it will be hidden): " )
value , err := password . Read ( os . Stdin )
fmt . Print ( "\n" )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "An error occurred attempting to read the login name. The raw error message is shown below but usually this is because you attempted to pipe a value into the command or you are executing outside of a terminal (TTY). The raw error was:\n\n%s" , err . Error ()))
return base . CommandUserError
}
c . flagLoginName = strings . TrimSpace ( value )
2020-07-14 14:32:47 -07:00
}
2022-08-05 13:21:46 -04:00
switch c . flagPassword {
case "" :
2022-08-05 19:27:14 -04:00
fmt . Print ( "Please enter the password (it will be hidden): " )
2020-07-14 14:32:47 -07:00
value , err := password . Read ( os . Stdin )
fmt . Print ( "\n" )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "An error occurred attempting to read the password. The raw error message is shown below but usually this is because you attempted to pipe a value into the command or you are executing outside of a terminal (TTY). The raw error was:\n\n%s" , err . Error ()))
2021-03-04 20:05:17 -05:00
return base . CommandUserError
2020-07-14 14:32:47 -07:00
}
c . flagPassword = strings . TrimSpace ( value )
2022-08-05 13:21:46 -04:00
default :
2022-08-05 16:08:48 -04:00
password , err := parseutil . MustParsePath ( c . flagPassword )
switch {
case err == nil :
case errors . Is ( err , parseutil . ErrNotParsed ):
c . UI . Error ( "Password flag must be used with env:// or file:// syntax or left empty for an interactive prompt" )
return base . CommandUserError
default :
2022-08-05 13:21:46 -04:00
c . UI . Error ( fmt . Sprintf ( "Error parsing password flag: %v" , err ))
return base . CommandUserError
}
c . flagPassword = password
2020-07-14 14:32:47 -07:00
}
2022-03-16 17:04:07 -04:00
result , err := aClient . Authenticate ( c . Context , c . FlagAuthMethodId , "login" ,
2022-10-25 18:29:38 -05:00
map [ string ] any {
2020-09-08 13:27:51 -07:00
"login_name" : c . flagLoginName ,
"password" : c . flagPassword ,
})
2020-07-14 14:32:47 -07:00
if err != nil {
2020-10-06 14:08:49 -07:00
if apiErr := api . AsServerError ( err ); apiErr != nil {
2021-02-25 12:43:17 -05:00
c . PrintApiError ( apiErr , "Error from controller when performing authentication" )
2021-03-04 20:05:17 -05:00
return base . CommandApiError
2020-09-24 14:34:58 -07:00
}
2021-02-25 12:43:17 -05:00
c . PrintCliError ( fmt . Errorf ( "Error trying to perform authentication: %w" , err ))
2021-03-04 20:05:17 -05:00
return base . CommandCliError
2020-07-14 14:32:47 -07:00
}
2023-11-01 12:45:35 -07:00
return saveAndOrPrintToken ( c . Command , result , c . Opts ... )
2020-07-14 14:32:47 -07:00
}