forked from segmentio/aws-okta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
149 lines (123 loc) · 3.21 KB
/
login.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
146
147
148
149
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/99designs/keyring"
"github.com/segmentio/aws-okta/lib"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
)
// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login <profile>",
Short: "login will authenticate you through okta and allow you to access your AWS environment through a browser",
RunE: loginRun,
}
// Stdout is the bool for -stdout
var Stdout bool
func init() {
RootCmd.AddCommand(loginCmd)
loginCmd.Flags().BoolVarP(&Stdout, "stdout", "", false, "Print login URL to stdout instead of opening in default browser")
}
func loginRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return ErrTooFewArguments
}
if len(args) > 1 {
return ErrTooManyArguments
}
profile := args[0]
config, err := lib.NewConfigFromEnv()
if err != nil {
return err
}
profiles, err := config.Parse()
if err != nil {
return err
}
if _, ok := profiles[profile]; !ok {
return fmt.Errorf("Profile '%s' not found in your aws config", profile)
}
opts := lib.ProviderOptions{
Profiles: profiles,
SessionDuration: sessionTTL,
AssumeRoleDuration: assumeRoleTTL,
}
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",
})
p, err := lib.NewProvider(kr, profile, opts)
if err != nil {
return err
}
creds, err := p.Retrieve()
if err != nil {
return err
}
jsonBytes, err := json.Marshal(map[string]string{
"sessionId": creds.AccessKeyID,
"sessionKey": creds.SecretAccessKey,
"sessionToken": creds.SessionToken,
})
if err != nil {
return err
}
req, err := http.NewRequest("GET", "https://signin.aws.amazon.com/federation", nil)
if err != nil {
return err
}
q := req.URL.Query()
q.Add("Action", "getSigninToken")
q.Add("Session", string(jsonBytes))
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Call to getSigninToken failed with %v", resp.Status)
}
var respParsed map[string]string
if err = json.Unmarshal([]byte(body), &respParsed); err != nil {
return err
}
signinToken, ok := respParsed["SigninToken"]
if !ok {
return err
}
destination := "https://console.aws.amazon.com/"
prof := profiles[profile]
if region, ok := prof["region"]; ok {
destination = fmt.Sprintf(
"https://%s.console.aws.amazon.com/console/home?region=%s",
region, region,
)
}
loginURL := fmt.Sprintf(
"https://signin.aws.amazon.com/federation?Action=login&Issuer=aws-vault&Destination=%s&SigninToken=%s",
url.QueryEscape(destination),
url.QueryEscape(signinToken),
)
if Stdout {
fmt.Println(loginURL)
} else if err = open.Run(loginURL); err != nil {
return err
}
return nil
}