-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
470 lines (421 loc) · 13.6 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v2"
)
const defaultVersionNumber = "No version provided"
const userAgentAppName = "cloud_gate_cli"
var (
// Must be a global variable in the data segment so that the build
// process can inject the version number on the fly when building the
// binary. Use only from the Usage() function.
Version = defaultVersionNumber
DefaultBaseURL = ""
userAgentString = userAgentAppName
)
const defaultOutputProfilePrefix = ""
var (
certFilename = flag.String("cert", filepath.Join(getUserHomeDir(), ".ssl", "keymaster.cert"), "A PEM eoncoded certificate file.")
keyFilename = flag.String("key", filepath.Join(getUserHomeDir(), ".ssl", "keymaster.key"), "A PEM encoded private key file.")
baseURL = flag.String("baseURL", DefaultBaseURL,
"location of the cloud-broker")
crededentialFilename = flag.String("credentialFile", filepath.Join(getUserHomeDir(), ".aws", "credentials"), "An Ini file with credentials")
askAdminRoles = flag.Bool("askAdminRoles", false, "ask also for admin roles")
outputProfilePrefix = flag.String("outputProfilePrefix", defaultOutputProfilePrefix, "prefix to put to profile names $PREFIX$accountName-$roleName")
lowerCaseProfileName = flag.Bool("lowerCaseProfileName", false, "set profile names to lowercase")
configFilename = flag.String("configFile", filepath.Join(getUserHomeDir(), ".config", "cloud-gate", "config.yml"), "An Ini file with credentials")
oldBotoCompat = flag.Bool("oldBotoCompat", false, "add aws_security_token for OLD boto installations (not recommended)")
includeRoleREFilter = flag.String("includeRoleREFilter", "", "Positive RE filter that role/account MUST match")
excludeRoleREFilter = flag.String("excludeRoleREFilter", "", "Negative RE filter. Acount/Role values matching will not be generated")
logLevel = flag.Uint("logLevel", 1, "Verbosity of logging")
)
type AppConfigFile struct {
BaseURL string `yaml:"base_url"`
OutputProfilePrefix string `yaml:"output_profile_prefix"`
LowerCaseProfileName bool `yaml:"lower_case_profile_name"`
IncludeRoleREFilter string `yaml:"include_role_re_filter"`
ExcludeRoleREFilter string `yaml:"exclude_role_re_filter"`
CertFilename string `yaml:"cert_filename"`
KeyFilename string `yaml:"key_filename"`
}
type cloudAccountInfo struct {
Name string
AvailableRoles []string
}
type getAccountInfo struct {
AuthUsername string
CloudAccounts map[string]cloudAccountInfo
}
type AWSCredentialsJSON struct {
SessionId string `json:"sessionId"`
SessionKey string `json:"sessionKey"`
SessionToken string `json:"sessionToken"`
Region string `json:"region,omitempty"`
Expiration time.Time `json:"cloudgate_comment_expiration,omitempty"`
}
func loggerPrintf(level uint, format string, v ...interface{}) {
if level <= *logLevel {
log.Printf(format, v...)
}
}
func loadVerifyConfigFile(filename string) (AppConfigFile, error) {
var config AppConfigFile
if _, err := os.Stat(filename); os.IsNotExist(err) {
err = saveDefaultConfig(filename)
if err != nil {
return config, err
}
}
source, err := ioutil.ReadFile(filename)
if err != nil {
err = errors.New("cannot read config file")
return config, err
}
err = yaml.Unmarshal(source, &config)
if err != nil {
err = errors.New("Cannot parse config file")
return config, err
}
if len(config.BaseURL) < 1 {
err = errors.New("Invalid Config file... no place get the credentials")
return config, err
}
// TODO: ensure all enpoints are https urls
return config, nil
}
func saveDefaultConfig(configFilename string) error {
os.MkdirAll(filepath.Dir(configFilename), 0755)
config := AppConfigFile{
BaseURL: *baseURL,
OutputProfilePrefix: defaultOutputProfilePrefix,
LowerCaseProfileName: false,
}
configBytes, err := yaml.Marshal(config)
if err != nil {
return err
}
return ioutil.WriteFile(configFilename, configBytes, 0644)
}
const badReturnErrText = "bad return code"
const sleepDuration = 1800 * time.Second
const failureSleepDuration = 60 * time.Second
func getAndUpdateCreds(client *http.Client, baseUrl, accountName, roleName string,
cfg *ini.File, outputProfilePrefix string,
lowerCaseProfileName bool) error {
loggerPrintf(1, "Getting creds for account=%s, role=%s", accountName, roleName)
values := url.Values{"accountName": {accountName}, "roleName": {roleName}}
req, err := http.NewRequest("POST", baseUrl+"/generatetoken", strings.NewReader(values.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", userAgentString)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Dump response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 300 {
return errors.New(badReturnErrText)
}
//log.Println(string(data))
var awsCreds AWSCredentialsJSON
err = json.Unmarshal(data, &awsCreds)
if err != nil {
log.Fatal(err)
}
//log.Printf("%+v", awsCreds)
fileProfile := outputProfilePrefix + accountName + "-" + roleName
if lowerCaseProfileName {
fileProfile = strings.ToLower(fileProfile)
}
cfg.Section(fileProfile).Key("aws_access_key_id").SetValue(awsCreds.SessionId)
cfg.Section(fileProfile).Key("aws_secret_access_key").SetValue(awsCreds.SessionKey)
cfg.Section(fileProfile).Key("aws_session_token").SetValue(awsCreds.SessionToken)
if *oldBotoCompat {
cfg.Section(fileProfile).Key("aws_security_token").SetValue(awsCreds.SessionToken)
} else {
cfg.Section(fileProfile).DeleteKey("aws_security_token")
}
if !awsCreds.Expiration.IsZero() {
cfg.Section(fileProfile).Key("token_expiration").SetValue(awsCreds.Expiration.UTC().Format(time.RFC3339))
} else {
cfg.Section(fileProfile).DeleteKey("token_expiration")
}
return nil
}
func getParseURLEnvVariable(name string) (*url.URL, error) {
envVariable := os.Getenv(name)
if len(envVariable) < 1 {
return nil, nil
}
envURL, err := url.Parse(envVariable)
if err != nil {
return nil, err
}
return envURL, nil
}
func setupCredentialFile(credentialFilename string) (*ini.File, error) {
// Create file if it does not exist
if _, err := os.Stat(credentialFilename); os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(credentialFilename), 0770)
file, err := os.OpenFile(credentialFilename, os.O_RDONLY|os.O_CREATE, 0660)
if err != nil {
return nil, err
}
file.Close()
}
return ini.Load(credentialFilename)
}
func setupHttpClient(cert tls.Certificate) (*http.Client, error) {
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
// proxy env variables in ascending order of preference, lower case 'http_proxy' dominates
// just like curl
proxyEnvVariables := []string{"HTTP_PROXY", "HTTPS_PROXY", "http_proxy"}
for _, proxyVar := range proxyEnvVariables {
httpProxy, err := getParseURLEnvVariable(proxyVar)
if err == nil && httpProxy != nil {
transport.Proxy = http.ProxyURL(httpProxy)
}
}
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
return client, nil
}
func getAccountsList(client *http.Client, baseUrl string) (*getAccountInfo, error) {
loggerPrintf(4, "Top of getAcountsList")
// Do GET something
req, err := http.NewRequest("GET", baseUrl, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgentString)
resp, err := client.Do(req)
if err != nil {
log.Printf("failed to connect err=%s transport=%+v ", err, client.Transport)
if resp != nil {
log.Printf("resp=+%v", resp)
}
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusUnauthorized {
log.Printf("getAccountsList, Failed Unauthorized, Please check your certificate configuration.")
}
return nil, fmt.Errorf("getAccountsList: Failed to Get accounts Status=%d", resp.StatusCode)
}
// Dump response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var accountList getAccountInfo
err = json.Unmarshal(data, &accountList)
if err != nil {
log.Printf("Error decoding account Data, data=%s", data)
log.Fatal(err)
}
loggerPrintf(2, "accountList=%v", accountList)
return &accountList, nil
}
func getCerts(cert tls.Certificate, baseUrl string,
credentialFilename string, askAdminRoles bool,
outputProfilePrefix string, lowerCaseProfileName bool,
includeRoleRE *regexp.Regexp, excludeRoleRE *regexp.Regexp) (int, error) {
loggerPrintf(4, "Top of getCerts")
credFile, err := setupCredentialFile(credentialFilename)
if err != nil {
return 0, fmt.Errorf("getCerts error from CredentialFile: %s", err)
}
client, err := setupHttpClient(cert)
if err != nil {
return 0, err
}
accountList, err := getAccountsList(client, baseUrl)
if err != nil {
return 0, err
}
credentialsGenerated := 0
for _, account := range accountList.CloudAccounts {
for _, roleName := range account.AvailableRoles {
adminRole, err := regexp.Match("(?i)admin", []byte(roleName))
if adminRole && !askAdminRoles {
continue
}
if err != nil {
log.Fatalf("error on regexp=%s", err)
}
computedName := account.Name + "-" + roleName
if includeRoleRE != nil {
if !includeRoleRE.Match([]byte(computedName)) {
continue
}
}
if excludeRoleRE != nil {
if excludeRoleRE.Match([]byte(computedName)) {
continue
}
}
err = getAndUpdateCreds(client, baseUrl,
account.Name, roleName, credFile,
outputProfilePrefix, lowerCaseProfileName)
if err != nil {
if err.Error() == badReturnErrText {
log.Printf("skipping role")
continue
}
log.Fatalf("error on getAnd UpdateCreds=%s", err)
}
credentialsGenerated += 1
}
}
err = credFile.SaveTo(credentialFilename)
if err != nil {
log.Fatal(err)
}
return credentialsGenerated, nil
}
//Assumes cert is pem ecoded
func getCertExpirationTime(certFilename string) (time.Time, error) {
dat, err := ioutil.ReadFile(certFilename)
if err != nil {
return time.Now(), err
}
block, _ := pem.Decode(dat)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return time.Now(), err
}
return cert.NotAfter, nil
}
func getUserHomeDir() (homeDir string) {
homeDir = os.Getenv("HOME")
if homeDir != "" {
return homeDir
}
usr, err := user.Current()
if err != nil {
return homeDir
}
// TODO: verify on Windows... see: http://stackoverflow.com/questions/7922270/obtain-users-home-directory
homeDir = usr.HomeDir
return
}
func computeUserAgent() {
uaVersion := Version
if Version == defaultVersionNumber {
uaVersion = "0.0"
}
userAgentString = fmt.Sprintf("%s/%s (%s %s)", userAgentAppName, uaVersion, runtime.GOOS, runtime.GOARCH)
}
func usage() {
fmt.Fprintf(
os.Stderr, "Usage of %s (version %s):\n", os.Args[0], Version)
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
flag.Parse()
computeUserAgent()
config, err := loadVerifyConfigFile(*configFilename)
if err != nil {
log.Fatal(err)
}
if *outputProfilePrefix != defaultOutputProfilePrefix {
config.OutputProfilePrefix = *outputProfilePrefix
}
// Because we want to display a sensible error message when using flags
// and we cannot tell using the flags package if a value has been modified
// we compare the processed value with the default so that any non-default
// value entered on the cli overrides the values in the config.
if *certFilename == filepath.Join(getUserHomeDir(), ".ssl", "keymaster.cert") {
if config.CertFilename != "" {
*certFilename = config.CertFilename
}
}
if *keyFilename == filepath.Join(getUserHomeDir(), ".ssl", "keymaster.key") {
if config.KeyFilename != "" {
*keyFilename = config.KeyFilename
}
}
var includeRoleRE *regexp.Regexp
if *includeRoleREFilter != "" {
config.IncludeRoleREFilter = *includeRoleREFilter
}
if config.IncludeRoleREFilter != "" {
includeRoleRE, err = regexp.Compile(config.IncludeRoleREFilter)
if err != nil {
log.Fatal(err)
}
}
var excludeRoleRE *regexp.Regexp
if *excludeRoleREFilter != "" {
config.ExcludeRoleREFilter = *excludeRoleREFilter
}
if config.ExcludeRoleREFilter != "" {
excludeRoleRE, err = regexp.Compile(config.ExcludeRoleREFilter)
if err != nil {
log.Fatal(err)
}
}
loggerPrintf(1, "Configuration Loaded")
loggerPrintf(2, "config=%+v", config)
loggerPrintf(2, "Using Cert=%s, key=%s", *certFilename, *keyFilename)
certNotAfter, err := getCertExpirationTime(*certFilename)
if err != nil {
log.Fatalf("Error on getCertExpirationTime: %s", err)
}
if certNotAfter.Before(time.Now()) {
log.Fatalf("keymaster certificate is expired, please run keymaster binary. Certificate expired at %s", certNotAfter)
}
for certNotAfter.After(time.Now()) {
cert, err := tls.LoadX509KeyPair(*certFilename, *keyFilename)
if err != nil {
log.Fatalf("Error Loading X509KeyPair: %s", err)
}
credentialCount, err := getCerts(cert, config.BaseURL, *crededentialFilename,
*askAdminRoles, config.OutputProfilePrefix, *lowerCaseProfileName,
includeRoleRE, excludeRoleRE)
if err != nil {
log.Printf("err=%s", err)
log.Printf("Failure getting certs, retrying in (%s)", failureSleepDuration)
time.Sleep(failureSleepDuration)
} else {
log.Printf("%d credentials successfully generated. Sleeping for (%s)", credentialCount, sleepDuration)
time.Sleep(sleepDuration)
}
certNotAfter, err = getCertExpirationTime(*certFilename)
if err != nil {
log.Fatal(err)
}
}
log.Printf("done")
}