-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
210 lines (171 loc) · 4.77 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"bufio"
"context"
b64 "encoding/base64"
"encoding/json"
"errors"
"os"
"os/user"
"path/filepath"
"regexp"
"time"
"github.com/fatih/color"
"github.com/gen2brain/beeep"
"github.com/git-lfs/go-netrc/netrc"
"github.com/theckman/yacspin"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
)
// Time before MFA step times out
const MFA_TIMEOUT = 30
var cfg = yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[59],
Suffix: "AWS SSO Signing in: ",
SuffixAutoColon: false,
Message: "",
StopCharacter: "✓",
StopFailCharacter: "✗",
StopMessage: "Logged in successfully",
StopFailMessage: "Log in failed",
StopColors: []string{"fgGreen"},
}
var spinner, _ = yacspin.New(cfg)
func main() {
spinner.Start()
// get sso url from stdin
url := getURL()
// start aws sso login
ssoLogin(url)
spinner.Stop()
time.Sleep(1 * time.Second)
}
// returns sso url from stdin.
func getURL() string {
spinner.Message("reading url from stdin")
scanner := bufio.NewScanner(os.Stdin)
url := ""
for url == "" {
scanner.Scan()
t := scanner.Text()
r, _ := regexp.Compile("^https.*user_code=([A-Z]{4}-?){2}")
if r.MatchString(t) {
url = t
}
}
return url
}
// get aws credentials from netrc file
func getCredentials() (string, string) {
spinner.Message("fetching credentials from .netrc")
usr, _ := user.Current()
f, err := netrc.ParseFile(filepath.Join(usr.HomeDir, ".netrc"))
if err != nil {
panic(".netrc file not found in HOME directory")
}
username := f.FindMachine("headless-sso", "").Login
passphrase := f.FindMachine("headless-sso", "").Password
return username, passphrase
}
// login with hardware MFA
func ssoLogin(url string) {
username, passphrase := getCredentials()
spinner.Message(color.MagentaString("init headless-browser \n"))
spinner.Pause()
browser := rod.New().MustConnect().Trace(false)
loadCookies(*browser)
defer browser.MustClose()
err := rod.Try(func() {
page := browser.MustPage(url)
// authorize
spinner.Unpause()
spinner.Message("logging in")
page.MustElementR("button", "Next").MustWaitEnabled().MustPress()
// sign-in
page.Race().ElementR("button", "Allow").MustHandle(func(e *rod.Element) {
}).Element("#awsui-input-0").MustHandle(func(e *rod.Element) {
signIn(*page, username, passphrase)
// mfa required step
mfa(*page)
}).MustDo()
// allow request
unauthorized := true
for unauthorized {
txt := page.Timeout(MFA_TIMEOUT * time.Second).MustElement(".awsui-util-mb-s").MustWaitLoad().MustText()
if txt == "Request approved" {
unauthorized = false
} else {
exists, _, _ := page.HasR("button", "Allow")
if exists {
page.MustWaitLoad().MustElementR("button", "Allow").MustClick()
}
time.Sleep(500 * time.Millisecond)
}
}
saveCookies(*browser)
})
if errors.Is(err, context.DeadlineExceeded) {
panic("Timed out waiting for MFA")
} else if err != nil {
panic(err.Error())
}
}
// executes aws sso signin step
func signIn(page rod.Page, username, passphrase string) {
page.MustElement("#awsui-input-0").MustInput(username).MustPress(input.Enter)
page.MustElement("#awsui-input-1").MustInput(passphrase).MustPress(input.Enter)
}
// TODO: allow user to enter MFA Code
func mfa(page rod.Page) {
_ = beeep.Notify("headless-sso", "Touch U2F device to proceed with authenticating AWS SSO", "")
_ = beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
spinner.Message(color.YellowString("Touch U2F"))
}
// load cookies
func loadCookies(browser rod.Browser) {
spinner.Message("loading cookies")
dirname, err := os.UserHomeDir()
if err != nil {
error(err.Error())
}
data, _ := os.ReadFile(dirname + "/.headless-sso")
sEnc, _ := b64.StdEncoding.DecodeString(string(data))
var cookie *proto.NetworkCookie
json.Unmarshal(sEnc, &cookie)
if cookie != nil {
browser.MustSetCookies(cookie)
}
}
// save authn cookie
func saveCookies(browser rod.Browser) {
dirname, err := os.UserHomeDir()
if err != nil {
error(err.Error())
}
cookies := (browser.MustGetCookies())
for _, cookie := range cookies {
if cookie.Name == "x-amz-sso_authn" {
data, _ := json.Marshal(cookie)
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
err = os.WriteFile(dirname+"/.headless-sso", []byte(sEnc), 0644)
if err != nil {
error("Failed to save x-amz-sso_authn cookie")
}
break
}
}
}
// print error message and exit
func panic(errorMsg string) {
red := color.New(color.FgRed).SprintFunc()
spinner.StopFailMessage(red("Login failed error - " + errorMsg))
spinner.StopFail()
os.Exit(1)
}
// print error message
func error(errorMsg string) {
yellow := color.New(color.FgYellow).SprintFunc()
spinner.Message("Warn: " + yellow(errorMsg))
}