Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Added CLI arguments for aws-okta add #137

Merged
merged 3 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
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
68 changes: 45 additions & 23 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/spf13/cobra"
)

var (
organization string
oktaDomain string
oktaRegion string
)

// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Expand All @@ -21,6 +27,8 @@ var addCmd = &cobra.Command{

func init() {
RootCmd.AddCommand(addCmd)
addCmd.Flags().StringVarP(&oktaDomain, "domain", "", "", "Okta domain (e.g. <orgname>.okta.com)")
addCmd.Flags().StringVarP(&username, "username", "", "", "Okta username")
}

func add(cmd *cobra.Command, args []string) error {
Expand All @@ -45,30 +53,44 @@ func add(cmd *cobra.Command, args []string) error {
})
}

// Ask username password from prompt
organization, err := lib.Prompt("Okta organization", false)
if err != nil {
return err
// Ask Okta organization details if not given in command line argument
if oktaDomain == "" {
organization, err = lib.Prompt("Okta organization", false)
if err != nil {
return err
}

oktaRegion, err = lib.Prompt("Okta region ([us], emea, preview)", false)
if err != nil {
return err
}
if oktaRegion == "" {
oktaRegion = "us"
}

tld, err := lib.GetOktaDomain(oktaRegion)
if err != nil {
return err
}
defaultOktaDomain := fmt.Sprintf("%s.%s", organization, tld)

oktaDomain, err = lib.Prompt("Okta domain ["+defaultOktaDomain+"]", false)
if err != nil {
return err
}
if oktaDomain == "" {
oktaDomain = defaultOktaDomain
}
}

oktaRegion, err := lib.Prompt("Okta region ([us], emea, preview)", false)
if err != nil {
return err
}
if oktaRegion == "" {
oktaRegion = "us"
}

oktaDomain, err := lib.Prompt("Okta domain ["+oktaRegion+".okta.com]", false)
if err != nil {
return err
}

username, err := lib.Prompt("Okta username", false)
if err != nil {
return err
if username == "" {
username, err = lib.Prompt("Okta username", false)
if err != nil {
return err
}
}

// Ask for password from prompt
password, err := lib.Prompt("Okta password", true)
if err != nil {
return err
Expand Down Expand Up @@ -98,9 +120,9 @@ func add(cmd *cobra.Command, args []string) error {
}

item := keyring.Item{
Key: "okta-creds",
Data: encoded,
Label: "okta credentials",
Key: "okta-creds",
Data: encoded,
Label: "okta credentials",
KeychainNotTrustApplication: false,
}

Expand Down
9 changes: 5 additions & 4 deletions lib/okta.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *OktaCreds) Validate(mfaConfig MFAConfig) error {
return nil
}

func getOktaDomain(region string) (string, error) {
func GetOktaDomain(region string) (string, error) {
switch region {
case "us":
return OktaServerUs, nil
Expand Down Expand Up @@ -131,6 +131,7 @@ func NewOktaClient(creds OktaCreds, oktaAwsSAMLUrl string, sessionCookie string,
},
})
}
log.Debug("domain: " + domain)

return &OktaClient{
// Setting Organization for backwards compatibility
Expand Down Expand Up @@ -561,9 +562,9 @@ func (p *OktaProvider) Retrieve() (sts.Credentials, string, error) {
}

newCookieItem := keyring.Item{
Key: p.OktaSessionCookieKey,
Data: []byte(newSessionCookie),
Label: "okta session cookie",
Key: p.OktaSessionCookieKey,
Data: []byte(newSessionCookie),
Label: "okta session cookie",
KeychainNotTrustApplication: false,
}

Expand Down