This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·404 lines (352 loc) · 10 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
import (
"database/sql"
"fmt"
"gosandbox/acloud"
"gosandbox/cli"
"gosandbox/core"
"gosandbox/gh"
"gosandbox/proxy"
"os"
"strings"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/manifoldco/promptui"
"github.com/rs/zerolog"
)
func main() {
ZeroLog()
var p acloud.ACloudProvider
p, err := Bootstrap(p)
cli.Success("Provider after Bootstrap: ", p)
cli.PrintIfErr(err)
exit := false
for !exit {
Execute(p)
}
}
func ZeroLog() {
//look through all os.Args and see if one is "prod"
for _, arg := range os.Args {
if arg == "prod" {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
break
}
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
// print level of global logger
fmt.Println("global logger level : ")
cli.Success(zerolog.GlobalLevel())
}
func Bootstrap(p acloud.ACloudProvider) (acloud.ACloudProvider, error) {
if zerolog.GlobalLevel() != zerolog.DebugLevel {
cli.Welcome()
}
cli.Success("len(os.Args) : ", len(os.Args))
cli.Success("os.Args : ", os.Args)
p, err := ConnectBrowser(p)
cli.PrintIfErr(err)
cli.Success("environment : ", p.ACloudEnv)
p, err = Sandbox(&p)
cli.PrintIfErr(err)
cli.Success("p : ", p)
return p, err
}
func Sandbox(p *acloud.ACloudProvider) (acloud.ACloudProvider, error) {
//scrape credentials
elems, err := acloud.Sandbox(p.Connection, p.ACloudEnv.Download_key)
cli.PrintIfErr(err)
cli.Success("rod html elements : ", elems)
// copy credentials to clipboard
p.SandboxCredential, err = acloud.CopySvg(elems)
cli.PrintIfErr(err)
cli.Success("credentials : ", p.SandboxCredential)
// //DISPLAY WITH COLORS PROMINENTLY TO THE USER
// acloud.DisplayCreds(creds)
return *p, err
}
func ConnectBrowser(p acloud.ACloudProvider) (acloud.ACloudProvider, error) {
u := launcher.MustResolveURL("")
browser := rod.New().ControlURL(u).MustConnect()
ACloudEnv, err := cli.LoadEnv()
cli.PrintIfErr(err)
p.ACloudEnv = ACloudEnv
Connection := core.Connect(browser, p.ACloudEnv.Url)
cli.Success("Connection after: ", Connection)
p.Connection = Connection
return p, nil
}
// func bootstrap(p acloud.ACloudProvider) acloud.ACloudProvider {
// cli.Success("getting sandbox credentials...")
// //get sandbox creds
// p, err := GetSandboxCreds(p.ACloudEnv, &p)
// cli.PrintIfErr(err)
// //create sqlite table
// // p.SQLiteRepository, err = ConnectSQLiteTable()
// // cli.PrintIfErr(err)
// return p
// }
func GetTemplates() *promptui.SelectTemplates {
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F336 {{ .Label | yellow }} ",
Inactive: " {{ .Label | cyan }} ",
Selected: "\U0001FAD1 {{ .Label | green | cyan }}",
}
return templates
}
func GetSearcher(options []cli.PromptOptions) func(input string, index int) bool {
searcher := func(input string, index int) bool {
option := options[index]
name := strings.Replace(strings.ToLower(option.Label), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
return searcher
}
func Select(promptTitle string, options []cli.PromptOptions) *promptui.Select {
prompt := promptui.Select{
Label: promptTitle,
Items: options,
Templates: GetTemplates(),
Size: 4,
Searcher: GetSearcher(options),
}
return &prompt
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(p acloud.ACloudProvider) {
options := []cli.PromptOptions{
{
Label: "Exit CLI",
Key: 0,
}, {
Label: "Scrape Sandbox Credentials",
Key: 1,
},
{
Label: "Download Text File of Sandbox Credentials",
Key: 2,
},
{
Label: "Append Sandbox Credentials to AWS Config",
Key: 3,
},
{
Label: "Display Sandbox Credentials",
Key: 4,
},
{
Label: "Set Credentials in GitHub Secret",
Key: 5,
},
{
Label: "Open AWS Console for Sandbox",
Key: 6,
},
{
Label: "Write Credentials to SQLite table",
Key: 7,
},
{
Label: "Read Last Credentials in SQLite table",
Key: 8,
},
}
prompt := Select("Welcome to GOSANDBOX - Please select an option: ", options)
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return //os.Exit(1)
}
fmt.Printf("Option %d: %s\n", i+1, options[i].Label)
switch options[i].Key {
case 0:
os.Exit(0)
case 1:
// p.ACloudEnv, err = EnvLocation()
// cli.PrintIfErr(err)
// cli.Success("environment : ", p.ACloudEnv)
// p, err = Sandbox(p.ACloudEnv, &p)
// cli.PrintIfErr(err)
// cli.Success("credentials : ", p.SandboxCredential)
case 2:
// download text file of policies
DownloadTextFile(p.SandboxCredential)
case 3:
// append aws creds to .aws/credentials file
AppendCreds(p.SandboxCredential)
case 4:
//DISPLAY WITH COLORS PROMINENTLY TO THE USER
acloud.DisplayCreds(p.SandboxCredential)
case 5:
//set sandbox creds in github secret
SandboxToGithub(p.SandboxCredential)
case 6:
//open aws console for sandbox
OpenAWSConsole(p.SandboxCredential)
case 7:
//write to sqlite table
WriteCredsToSQLiteTable(p)
case 8:
//read from sqlite table
p.SandboxCredential = *GetLastWrittenCredsFromSQLiteTable(p)
acloud.DisplayCreds(p.SandboxCredential)
}
Execute(p)
}
func ConnectSQLiteTable() (*acloud.SQLiteRepository, error) {
fileName := "./data/sqlite.db"
db, err := sql.Open("sqlite3", fileName)
cli.Success("db : ", db)
cli.PrintIfErr(err)
sandboxRepository := acloud.NewSQLiteRepository(db)
sandboxRepository.Migrate()
cli.Success("sandboxRepository : ", sandboxRepository)
return sandboxRepository, err
}
func WriteCredsToSQLiteTable(p acloud.ACloudProvider) {
//write to sqlite table
created, err := p.SQLiteRepository.Create(p.SandboxCredential)
cli.Success("created : ", created)
cli.PrintIfErr(err)
}
func GetLastWrittenCredsFromSQLiteTable(p acloud.ACloudProvider) *acloud.SandboxCredential {
//read from sqlite table
creds, err := p.SQLiteRepository.Last()
cli.Success("creds : ", creds)
cli.PrintIfErr(err)
return creds
}
func OpenAWSConsole(creds acloud.SandboxCredential) {
//if credentials are empty, return error
if len(creds.AccessKey) == 0 || len(creds.KeyID) == 0 || len(creds.User) == 0 {
cli.Error("Warning: credentials are empty")
return
}
//login to console with credentials url, username, and password
browser, err := core.Login(core.WebsiteLogin{creds.URL, creds.User, creds.Password})
cli.PrintIfErr(err)
cli.Success("browser : ", browser)
}
func SandboxToGithub(creds acloud.SandboxCredential) {
//github PAT
token, err := gh.GetToken()
cli.PrintIfErr(err)
//if token is empty, return error
if len(token) == 0 {
cli.Error("Github token is empty")
return
}
//if credentials are empty, return error
if len(creds.AccessKey) == 0 || len(creds.KeyID) == 0 || len(creds.User) == 0 {
cli.Error("credentials are empty")
return
}
// authorize using env TOKEN
ctx, client, err := gh.GithubAuth(token)
cli.PrintIfErr(err)
// get repo owner
owner, err := cli.PromptRepoOwner()
cli.PrintIfErr(err)
// get repo name
repo, err := cli.PromptRepoName()
cli.PrintIfErr(err)
//create string arrays of credentials
keys, vals := acloud.KeyVals(creds)
cli.Success("writing credentials to github secrets....")
//loop over keys and vals
for i, key := range keys {
//create secret in github
// err := gh.CreateSecret(key, vals[i])
if err := gh.AddRepoSecret(ctx, client, owner, repo, key, vals[i]); err != nil {
cli.PrintIfErr(err)
}
cli.Success("secret : " + key)
fmt.Println("value : " + vals[i])
}
cli.Success("credentials written to " + owner + "/" + repo)
}
func DownloadTextFile(creds acloud.SandboxCredential) {
//if credentials are empty, return error
if len(creds.AccessKey) == 0 || len(creds.KeyID) == 0 || len(creds.User) == 0 {
cli.Error("Warning: credentials are empty")
return
}
//create string arrays of credentials
keys, vals := acloud.KeyVals(creds)
//create policies with map
policies, err := proxy.Policies(keys, vals)
cli.PrintIfErr(err)
cli.Success("policies : ", policies)
// ask if they want to download a text file with the credentials
if cli.PromptDownload() == true {
// download text file of policies
filename := cli.PromptFileName()
err = core.DocumentDownload(filename, policies)
cli.PrintIfErr(err)
}
}
func AppendCreds(creds acloud.SandboxCredential) {
//ask if they want the credentials to be added to their aws config
path := cli.PromptGetInput(cli.PromptContent{
Label: "Where would you like your sandbox credentials appended?",
})
//if credentials are empty, return error
if len(creds.AccessKey) == 0 || len(creds.KeyID) == 0 || len(creds.User) == 0 {
cli.Error("Warning: credentials are empty")
return
}
if cli.PromptConfig() == true {
//ask for path to aws config
// path := core.PromptFilePath()
// append aws creds to .aws/credentials file
err := core.AppendAwsCredentials(core.LocalCreds{
Path: path,
User: creds.User,
KeyID: creds.KeyID,
AccessKey: creds.AccessKey,
})
// if error, ask for path to aws config again
if err != nil {
cli.PrintIfErr(err)
AppendCreds(creds)
}
cli.Success("aws credentials appended @ :", path)
}
}
func EnvLocation() (cliEnv cli.ACloudEnv, err error) {
options := []cli.PromptOptions{
{
Label: "Get sandbox credentials with .env file located in your current directory",
Key: 1,
}, {
Label: "Get sandbox credentials from .env file in a custom location",
Key: 2,
}, {
Label: "Get sandbox credentials manually with env information entered via cli prompt",
Key: 3,
},
}
prompt := Select("How would you like to input .env details: ", options)
i, _, err := prompt.Run()
if err != nil {
cli.PrintIfErr(err)
return cli.ACloudEnv{}, err
}
cli.Success("You choose number %d: %s\n", i+1, options[i].Label)
switch options[i].Key {
case 1:
cliEnv, err = cli.GetEnv(".env")
case 2:
cliEnv, err = cli.PromptEnvFile()
case 3:
cliEnv, err = cli.PromptManual()
}
cli.PrintIfErr(err)
if err != nil {
return cli.ACloudEnv{}, err
}
return cliEnv, nil
}