-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-env.go
163 lines (133 loc) · 3.58 KB
/
aws-env.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/codegangsta/cli"
"gopkg.in/ini.v1"
)
var flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "display more output",
},
cli.StringFlag{
Name: "f, file",
Value: "credentials",
Usage: "aws credentials file",
},
cli.StringFlag{
Name: "p, profile",
Value: "default",
Usage: "profile to extract from aws credentials file",
},
cli.StringFlag{
Name: "aws-home",
Usage: "location of aws home",
EnvVar: "AWS_HOME",
},
}
var subCommands = []cli.Command{
{
Name: "access-key",
Usage: "retrieve and output the AWS access key id for the provided profile",
Action: CmdGetAccessKey},
{
Name: "secret-key",
Usage: "retrieve and output the AWS secret key for the provided profile",
Action: CmdGetSecretKey},
}
// Params are the options that will used to pull data out
// out of the credentials file
type Params struct {
awsHome string
fileName string
profile string
}
func entering(n string) string {
log.Printf("Entering [%s]", n)
return n
}
func exiting(n string) {
log.Printf("Exiting [%s]", n)
}
func setup(ctx *cli.Context) error {
if !ctx.Bool("verbose") {
log.SetOutput(ioutil.Discard)
}
return nil
}
func buildCredentialsPath(awsHome, fileName string) string {
if "" == awsHome {
fmt.Fprintln(os.Stderr, "Environment variable [$AWS_HOME] is not set. Is the aws-cli installed? If not, install it or use --aws-home option")
os.Exit(1)
}
return path.Join(awsHome, fileName)
}
func processGlobalContext(ctx *cli.Context, handler func(map[string]string)) {
params := Params{ctx.GlobalString("aws-home"), ctx.GlobalString("file"), ctx.GlobalString("profile")}
m, err := process(params)
if nil != err {
log.Fatalln(err)
}
handler(m)
}
func process(params Params) (map[string]string, error) {
credentialPath := buildCredentialsPath(params.awsHome, params.fileName)
log.Printf("Inspecting file: %s\n", credentialPath)
log.Printf("Extracting profile [%s]\n", params.profile)
cfg, err := ini.Load(credentialPath)
if nil != err {
fmt.Println("An error occurred loading the file")
return nil, err
}
section, err := cfg.GetSection(params.profile)
if nil != err {
fmt.Println("An error occurred extracting the profile")
return nil, err
}
return section.KeysHash(), nil
}
// CmdProcess extracts the AWS Keys from the profile of the of the specified
// credentials file. These values will then be set as the environment vaiables
func CmdProcess(ctx *cli.Context) {
defer exiting(entering("CmdProcess"))
params := Params{ctx.String("aws-home"), ctx.String("file"), ctx.String("profile")}
m, err := process(params)
if nil != err {
log.Fatalln(err)
}
for k, v := range m {
k := strings.ToUpper(k)
fmt.Printf("export %s=\"%v\"\n", k, v)
}
}
// CmdGetAccessKey will output the AWS access key from the profile of the specified
// credentials file.
func CmdGetAccessKey(ctx *cli.Context) {
defer exiting(entering("CmdGetAccess"))
processGlobalContext(ctx, func(m map[string]string) {
fmt.Println(m["aws_access_key_id"])
})
}
// CmdGetSecretKey will output the AWS secret access key from the profile of the specified
// credentials file
func CmdGetSecretKey(ctx *cli.Context) {
defer exiting(entering("CmdGetSecretKey"))
processGlobalContext(ctx, func(m map[string]string) {
fmt.Println(m["aws_secret_access_key"])
})
}
func main() {
app := cli.NewApp()
app.Name = "aws-env"
app.Usage = "extract AWS Access Key Id and Secret Access Key"
app.Flags = flags
app.Before = setup
app.Action = CmdProcess
app.Commands = subCommands
app.Run(os.Args)
}