TUN-8591 login command without extra text

Also unifies `access token` and `access login` interface
This commit is contained in:
Kornel 2024-08-15 23:04:08 +01:00
parent 30c435fee6
commit a9365296ae
1 changed files with 37 additions and 11 deletions

View File

@ -26,6 +26,7 @@ import (
) )
const ( const (
appURLFlag = "app"
loginQuietFlag = "quiet" loginQuietFlag = "quiet"
sshHostnameFlag = "hostname" sshHostnameFlag = "hostname"
sshDestinationFlag = "destination" sshDestinationFlag = "destination"
@ -86,6 +87,7 @@ func Commands() []*cli.Command {
Name: "login", Name: "login",
Action: cliutil.Action(login), Action: cliutil.Action(login),
Usage: "login <url of access application>", Usage: "login <url of access application>",
ArgsUsage: "url of Access application",
Description: `The login subcommand initiates an authentication flow with your identity provider. Description: `The login subcommand initiates an authentication flow with your identity provider.
The subcommand will launch a browser. For headless systems, a url is provided. The subcommand will launch a browser. For headless systems, a url is provided.
Once authenticated with your identity provider, the login command will generate a JSON Web Token (JWT) Once authenticated with your identity provider, the login command will generate a JSON Web Token (JWT)
@ -97,6 +99,13 @@ func Commands() []*cli.Command {
Aliases: []string{"q"}, Aliases: []string{"q"},
Usage: "do not print the jwt to the command line", Usage: "do not print the jwt to the command line",
}, },
&cli.BoolFlag{
Name: "no-verbose",
Usage: "print only the jwt to stdout",
},
&cli.StringFlag{
Name: appURLFlag,
},
}, },
}, },
{ {
@ -111,12 +120,12 @@ func Commands() []*cli.Command {
{ {
Name: "token", Name: "token",
Action: cliutil.Action(generateToken), Action: cliutil.Action(generateToken),
Usage: "token -app=<url of access application>", Usage: "token <url of access application>",
ArgsUsage: "url of Access application", ArgsUsage: "url of Access application",
Description: `The token subcommand produces a JWT which can be used to authenticate requests.`, Description: `The token subcommand produces a JWT which can be used to authenticate requests.`,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "app", Name: appURLFlag,
}, },
}, },
}, },
@ -232,9 +241,8 @@ func login(c *cli.Context) error {
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog) log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
args := c.Args() appURL, err := getAppURLFromArgs(c)
appURL, err := parseURL(args.First()) if err != nil {
if args.Len() < 1 || err != nil {
log.Error().Msg("Please provide the url of the Access application") log.Error().Msg("Please provide the url of the Access application")
return err return err
} }
@ -261,7 +269,14 @@ func login(c *cli.Context) error {
if c.Bool(loginQuietFlag) { if c.Bool(loginQuietFlag) {
return nil return nil
} }
// Chatty by default for backward compat. The new --app flag
// is an implicit opt-out of the backwards-compatible chatty output.
if c.Bool("no-verbose") || c.IsSet(appURLFlag) {
fmt.Fprint(os.Stdout, cfdToken)
} else {
fmt.Fprintf(os.Stdout, "Successfully fetched your token:\n\n%s\n\n", cfdToken) fmt.Fprintf(os.Stdout, "Successfully fetched your token:\n\n%s\n\n", cfdToken)
}
return nil return nil
} }
@ -340,6 +355,17 @@ func run(cmd string, args ...string) error {
return c.Run() return c.Run()
} }
func getAppURLFromArgs(c *cli.Context) (*url.URL, error) {
var appURLStr string
args := c.Args()
if args.Len() < 1 {
appURLStr = c.String(appURLFlag)
} else {
appURLStr = args.First()
}
return parseURL(appURLStr)
}
// token dumps provided token to stdout // token dumps provided token to stdout
func generateToken(c *cli.Context) error { func generateToken(c *cli.Context) error {
err := sentry.Init(sentry.ClientOptions{ err := sentry.Init(sentry.ClientOptions{
@ -349,8 +375,8 @@ func generateToken(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
appURL, err := parseURL(c.String("app")) appURL, err := getAppURLFromArgs(c)
if err != nil || c.NumFlags() < 1 { if err != nil {
fmt.Fprintln(os.Stderr, "Please provide a url.") fmt.Fprintln(os.Stderr, "Please provide a url.")
return err return err
} }