-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (151 loc) · 4.21 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/go-ini/ini"
)
var (
credentialsFile string
configFile string
verbose bool
help bool
)
func init() {
flag.StringVar(&credentialsFile, "f", "~/.aws/credentials", "Path to AWS credentials file")
flag.StringVar(&configFile, "c", "~/.aws/config", "Path to AWS credentials file")
flag.BoolVar(&verbose, "v", false, "Verbose output for debugging")
flag.BoolVar(&help, "h", false, "Print command usage")
flag.Parse()
if help {
usage := `aws-profiles is a tool to manage multiple AWS profiles using the credentials file
Usage:
aws-profiles [-f filepath] [-v] [-h] profile-name
-f Override the default credentials file location (~/.aws/credentials)
-v Turn on verbose logging for debugging
-h Print this message
`
fmt.Println(usage)
os.Exit(0)
}
// disable logging if verbose is false
if !verbose {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
}
}
func main() {
requestedProfile := os.Args[1]
//log.Printf("Args1: %v", requestedProfile)
//log.Printf("Credentialsfile: %s", expandTildeToUserHome(credentialsFile))
cfgCredentials, err := ini.Load(expandTildeToUserHome(credentialsFile))
if err != nil {
fmt.Printf("Unable to read file: %v\n", err)
}
cfgConfig, err := ini.Load(expandTildeToUserHome(configFile))
if err != nil {
fmt.Printf("Unable to read file: %v\n", err)
}
for _, section := range cfgCredentials.Sections() {
if section.Name() == requestedProfile {
keyHash := section.KeysHash()
for k, v := range keyHash {
if k == "__name__" {
continue
}
canonicalForm, err := convertCredentialsEntry(k)
if err != nil {
continue
}
fmt.Printf("export %s=%s\n", canonicalForm, v)
}
}
}
for _, section := range cfgConfig.Sections() {
profile := "profile "+requestedProfile
sectionName :=strings.TrimSpace(section.Name())
if strings.Compare(profile,sectionName) == 0 {
keyHash := section.KeysHash()
for k, v := range keyHash {
canonicalForm, err := convertConfigEntry(k)
if err != nil {
continue
}
// Only Change dir if not in subdir
if canonicalForm == "CHDIR" {
currentWorkingDirectory, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if strings.HasPrefix(currentWorkingDirectory, v){
}else{
fmt.Printf("export %s=%s\n", canonicalForm, v)
}
}else{
fmt.Printf("export %s=%s\n", canonicalForm, v)
}
}
}
}
fmt.Printf("export %s=%s\n", "AWSUME_PROFILE", requestedProfile)
fmt.Printf("export %s=%s\n", "AWS_DEFAULT_PROFILE", requestedProfile)
// Treat mfa/awsumerole
// if source_profile is there
// get key/secret from the source profile
// get arn from the source profile
// ask user (in bash?)
// aws sts get-session-token --serial-number arn-of-the-mfa-device --token-code code-from-token
// export stuff
// "SecretAccessKey": "secret-access-key",
// "SessionToken": "temporary-session-token",
// "Expiration": "expiration-date-time",
// "AccessKeyId": "access-key-id"
}
func expandTildeToUserHome(filePath string) string {
if strings.HasPrefix(filePath, "~/") {
return filepath.Join(os.Getenv("HOME"), filePath[2:])
}
return filePath
}
func convertCredentialsEntry(credFileVar string) (string, error) {
switch credFileVar {
case "region":
return "AWS_DEFAULT_REGION", nil
case "output":
return "AWS_DEFAULT_OUTPUT", nil
case "aws_access_key_id":
return "AWS_ACCESS_KEY_ID", nil
case "aws_secret_access_key":
return "AWS_SECRET_ACCESS_KEY", nil
case "aws_sts_token":
return "AWS_STS_TOKEN", nil
case "aws_session_token":
return "AWS_SESSION_TOKEN", nil
default:
return "", fmt.Errorf("Unknown credentials file variable: %s", credFileVar)
}
}
func convertConfigEntry(credFileVar string) (string, error) {
switch credFileVar {
case "region":
return "AWS_DEFAULT_REGION", nil
case "workdir":
return "CHDIR", nil
case "itermbadge":
return "ITERMBADGE", nil
case "taskwarrior":
return "TASKWARRIOR", nil
case "source_profile":
return "source_profile", nil
case "url":
return "AWS_URL", nil
case "output":
return "AWS_DEFAULT_OUTPUT", nil
default:
return "", fmt.Errorf("Unknown credentials file variable: %s", credFileVar)
}
}