forked from segmentio/aws-okta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.go
145 lines (122 loc) · 3.3 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cmd
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/99designs/keyring"
analytics "github.com/segmentio/analytics-go"
"github.com/segmentio/aws-okta/lib"
"github.com/spf13/cobra"
)
var (
organization string
oktaDomain string
oktaRegion string
oktaAccountName string
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "add your okta credentials",
RunE: add,
}
func init() {
RootCmd.AddCommand(addCmd)
addCmd.Flags().StringVarP(&oktaDomain, "domain", "", "", "Okta domain (e.g. <orgname>.okta.com)")
addCmd.Flags().StringVarP(&username, "username", "", "", "Okta username")
addCmd.Flags().StringVarP(&oktaAccountName, "account", "", "", "Okta account name")
}
func add(cmd *cobra.Command, args []string) error {
var allowedBackends []keyring.BackendType
if backend != "" {
allowedBackends = append(allowedBackends, keyring.BackendType(backend))
}
kr, err := lib.OpenKeyring(allowedBackends)
if err != nil {
log.Fatal(err)
}
if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("backend", backend).
Set("aws-okta-version", version).
Set("command", "add"),
})
}
// 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
}
}
if username == "" {
username, err = lib.Prompt("Okta username", false)
if err != nil {
return err
}
}
if oktaAccountName == "" {
oktaAccountName = "okta-creds"
} else {
oktaAccountName = "okta-creds-" + oktaAccountName
}
log.Debugf("Keyring key: %s", oktaAccountName)
// Ask for password from prompt
password, err := lib.Prompt("Okta password", true)
if err != nil {
return err
}
fmt.Println()
creds := lib.OktaCreds{
Organization: organization,
Username: username,
Password: password,
Domain: oktaDomain,
}
// Profiles aren't parsed during `add`, but still want
// to centralize the MFA config logic
var dummyProfiles lib.Profiles
updateMfaConfig(cmd, dummyProfiles, "", &mfaConfig)
if err := creds.Validate(mfaConfig); err != nil {
log.Debugf("Failed to validate credentials: %s", err)
return ErrFailedToValidateCredentials
}
encoded, err := json.Marshal(creds)
if err != nil {
return err
}
item := keyring.Item{
Key: oktaAccountName,
Data: encoded,
Label: "okta credentials",
KeychainNotTrustApplication: false,
}
if err := kr.Set(item); err != nil {
log.Debugf("Failed to add user to keyring: %s", err)
return ErrFailedToSetCredentials
}
log.Infof("Added credentials for user %s", username)
return nil
}