Skip to content

Commit

Permalink
Merge branch 'main' into exit-codes
Browse files Browse the repository at this point in the history
  • Loading branch information
punmechanic authored Jan 24, 2024
2 parents af985cc + f186716 commit 055b0a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
9 changes: 5 additions & 4 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ func (a accountSet) WriteTable(w io.Writer, withHeaders bool) {

// Config stores all information related to the user
type Config struct {
Accounts *accountSet `json:"accounts"`
TTL uint `json:"ttl"`
TimeRemaining uint `json:"time_remaining"`
Tokens *TokenSet `json:"tokens"`
Accounts *accountSet `json:"accounts"`
TTL uint `json:"ttl"`
TimeRemaining uint `json:"time_remaining"`
Tokens *TokenSet `json:"tokens"`
LastUsedAccount *string `json:"last_used_account"`
}

func (c Config) GetOAuthToken() (*TokenSet, bool) {
Expand Down
2 changes: 1 addition & 1 deletion cli/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var (

const (
// DefaultTTL for requested credentials in hours
DefaultTTL uint = 1
DefaultTTL uint = 8
// DefaultTimeRemaining for new key requests in minutes
DefaultTimeRemaining uint = 5
LinuxAmd64BinaryName string = "keyconjurer-linux-amd64"
Expand Down
18 changes: 14 additions & 4 deletions cli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ var getCmd = &cobra.Command{
Long: `Retrieves temporary cloud API credentials for the specified account. It sends a push request to the first Duo device it finds associated with your account.
A role must be specified when using this command through the --role flag. You may list the roles you can assume through the roles command.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
config := ConfigFromCommand(cmd)
ctx := cmd.Context()
Expand Down Expand Up @@ -99,8 +98,18 @@ A role must be specified when using this command through the --role flag. You ma
ttl = 8
}

var accountID string
if len(args) > 0 {
accountID = args[0]
} else if config.LastUsedAccount != nil {
// No account specified. Can we use the most recent one?
accountID = *config.LastUsedAccount
} else {
return cmd.Usage()
}

bypassCache, _ := cmd.Flags().GetBool(FlagBypassCache)
account, ok := resolveApplicationInfo(config, bypassCache, args[0])
account, ok := resolveApplicationInfo(config, bypassCache, accountID)
if !ok {
return UnknownAccountError(args[0], FlagBypassCache)
}
Expand All @@ -125,7 +134,7 @@ A role must be specified when using this command through the --role flag. You ma
}

if credentials.ValidUntil(account, time.Duration(timeRemaining)*time.Minute) {
return echoCredentials(args[0], args[0], credentials, outputType, shellType, awsCliPath, tencentCliPath)
return echoCredentials(accountID, accountID, credentials, outputType, shellType, awsCliPath, tencentCliPath)
}

samlResponse, assertionStr, err := DiscoverConfigAndExchangeTokenForAssertion(cmd.Context(), NewHTTPClient(), config.Tokens, oidcDomain, clientID, account.ID)
Expand Down Expand Up @@ -172,8 +181,9 @@ A role must be specified when using this command through the --role flag. You ma
if account != nil {
account.MostRecentRole = roleName
}
config.LastUsedAccount = &accountID

return echoCredentials(args[0], args[0], credentials, outputType, shellType, awsCliPath, tencentCliPath)
return echoCredentials(accountID, accountID, credentials, outputType, shellType, awsCliPath, tencentCliPath)
}}

func echoCredentials(id, name string, credentials CloudCredentials, outputType, shellType, awsCliPath, tencentCliPath string) error {
Expand Down

0 comments on commit 055b0a3

Please sign in to comment.