forked from segmentio/aws-okta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
83 lines (68 loc) · 1.6 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
package cmd
import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/99designs/keyring"
"github.com/segmentio/aws-okta/lib"
"github.com/spf13/cobra"
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "add your okta credentials",
RunE: add,
}
func init() {
RootCmd.AddCommand(addCmd)
}
func add(cmd *cobra.Command, args []string) error {
var allowedBackends []keyring.BackendType
if backend != "" {
allowedBackends = append(allowedBackends, keyring.BackendType(backend))
}
kr, err := keyring.Open(keyring.Config{
AllowedBackends: allowedBackends,
KeychainTrustApplication: true,
// this keychain name is for backwards compatibility
ServiceName: "aws-okta-login",
LibSecretCollectionName: "awsvault",
})
if err != nil {
log.Fatal(err)
}
// Ask username password from prompt
organization, err := lib.Prompt("Okta organization", false)
if err != nil {
return err
}
username, err := lib.Prompt("Okta username", false)
if err != nil {
return err
}
password, err := lib.Prompt("Okta password", true)
if err != nil {
return err
}
fmt.Println()
creds := lib.OktaCreds{
Organization: organization,
Username: username,
Password: password,
}
encoded, err := json.Marshal(creds)
if err != nil {
return err
}
item := keyring.Item{
Key: "okta-creds",
Data: encoded,
Label: "okta credentials",
KeychainNotTrustApplication: false,
}
if err := kr.Set(item); err != nil {
return ErrFailedToSetCredentials
}
log.Infof("Added credentials for user %s", username)
return nil
}