-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.go
96 lines (81 loc) · 2.77 KB
/
configure.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
package commands
import (
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/versent/saml2aws"
"github.com/versent/saml2aws/helper/credentials"
"github.com/versent/saml2aws/pkg/cfg"
"github.com/versent/saml2aws/pkg/flags"
"github.com/versent/saml2aws/pkg/prompter"
"github.com/versent/saml2aws/pkg/provider/onelogin"
)
// OneLoginOAuthPath is the path used to generate OAuth token in order to access OneLogin's API.
const OneLoginOAuthPath = "/auth/oauth2/v2/token"
// Configure configure account profiles
func Configure(configFlags *flags.CommonFlags) error {
idpAccountName := configFlags.IdpAccount
cfgm, err := cfg.NewConfigManager(cfg.DefaultConfigPath)
if err != nil {
return errors.Wrap(err, "failed to load configuration")
}
account, err := cfgm.LoadIDPAccount(idpAccountName)
if err != nil {
return errors.Wrap(err, "failed to load idp account")
}
// update username and hostname if supplied
flags.ApplyFlagOverrides(configFlags, account)
// do we need to prompt for values now?
if !configFlags.SkipPrompt {
err = saml2aws.PromptForConfigurationDetails(account)
if err != nil {
return errors.Wrap(err, "failed to input configuration")
}
if credentials.SupportsStorage() {
if err := storeCredentials(configFlags, account); err != nil {
return err
}
}
}
err = cfgm.SaveIDPAccount(idpAccountName, account)
if err != nil {
return errors.Wrap(err, "failed to save configuration")
}
fmt.Println("")
fmt.Println(account)
fmt.Println("")
fmt.Printf("Configuration saved for IDP account: %s\n", idpAccountName)
return nil
}
func storeCredentials(configFlags *flags.CommonFlags, account *cfg.IDPAccount) error {
if configFlags.Password != "" {
if err := credentials.SaveCredentials(account.URL, account.Username, configFlags.Password); err != nil {
return errors.Wrap(err, "error storing password in keychain")
}
} else {
password := prompter.Password("Password")
if password != "" {
if confirmPassword := prompter.Password("Confirm"); confirmPassword == password {
if err := credentials.SaveCredentials(account.URL, account.Username, password); err != nil {
return errors.Wrap(err, "error storing password in keychain")
}
} else {
fmt.Println("Passwords did not match")
os.Exit(1)
}
} else {
fmt.Println("No password supplied")
}
}
if account.Provider == onelogin.ProviderName {
if configFlags.ClientID == "" || configFlags.ClientSecret == "" {
fmt.Println("OneLogin provider requires --client_id and --client_secret flags to be set.")
os.Exit(1)
}
if err := credentials.SaveCredentials(path.Join(account.URL, OneLoginOAuthPath), configFlags.ClientID, configFlags.ClientSecret); err != nil {
return errors.Wrap(err, "error storing client_id and client_secret in keychain")
}
}
return nil
}