-
Notifications
You must be signed in to change notification settings - Fork 2
/
acfmgr_test.go
307 lines (283 loc) · 9.68 KB
/
acfmgr_test.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
package acfmgr
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"os/user"
"time"
"runtime"
"github.com/aws/aws-sdk-go-v2/aws"
)
const baseCredFile string = `
[testing]
foo
bar
[newentry]
bar
foo
`
const expectedResult string = `
[testing]
foo
bar
[newentry]
bar
foo
[acfmgrtest]
# DO NOT EDIT
# ACFMGR MANAGED SECTION
# (Will be overwritten regularly)
####################################################
# ASSUMED ROLE: NA
# ASSUMED FROM INSTANCE ROLE: NA
# GENERATED: 2020-01-13 18:38:19.389637 +0000 UTC
# EXPIRES@ 2020-01-08 14:03:02 +0000 UTC
aws_access_key_id = AHENVMSKIRUEQNFHGZTA
aws_secret_access_key = ZcqCQl34NF8PtXHSdbBk3mZze1plNNSWqnmsz523
aws_session_token = f8sNh8tocFpiabpbOGHfpqSYSgOQcNqvbzyNpAYW9gxWOlAcGpaPJMQoeDM/0AQjHnvA8qMA8Q2jdxFmPwLHA184JI9YXVXs3a6ig2GMKvtTYXYwe4HKbymJm4zWxcG7OWwPee8BlZbY+F/T+lmNguge42ePV3mA5uyK5oTgryTG9TNFBtmh518OCdRXBDwwPWwQbfLWM/95KaOnZRIr/TpkjdWk4iCFXmKTIs5RKwDrS9mmD66cj6KTNsAGDxw29wYLOXlcB3MXbuEZzgew6tn8vpzonBIRiFy74Oym6Ct1sFcNXVKrwmn2Ojnmec3KCAbFwynyTHPxE2PpHlVhQhvb2Azw2FeLGAw1btiItcvLDrS3cDI3TfQNaa8L2MX3Zfr2yBv9UUS4MfS2pZQ42Czze7PMRk6LrWh0HA+SdBUG6XeXDHcvXH3rH4GxJHuDhALCgNabFYwuXysXdGP=
`
const expectedResultDeletionEnd string = `
[testing]
foo
bar
`
const expectedResultDeletionMiddle string = `
[newentry]
bar
foo
`
func writeBaseFile(filename string) error {
var b bytes.Buffer
_, err := b.WriteString(baseCredFile)
err = ioutil.WriteFile(filename, b.Bytes(), 0644)
return err
}
func replaceLine(contents []byte, linecontains, replaceline string) (output []byte) {
lines := strings.Split(string(contents), "\n")
for i, line := range lines {
if strings.Contains(line, linecontains) {
lines[i] = replaceline
}
}
newcontents := strings.Join(lines, "\n")
output = []byte(newcontents)
return output
}
func TestModifyEntry(t *testing.T) {
filename := "./acfmgr_credfile_test.txt"
err := writeBaseFile(filename)
if err != nil {
t.Errorf("Error making basefile: %s", err)
}
sess, err := NewCredFileSession(filename)
if err != nil {
t.Errorf("Error making credfile session: %s", err)
}
pfi := ProfileEntryInput{
Credential: getFakeCreds(),
ProfileEntryName: "acfmgrtest",
}
err = sess.NewEntry(&pfi)
if err != nil {
t.Errorf("Error adding entry: %s", err)
}
err = sess.AssertEntries()
if err != nil {
t.Errorf("Error asserting entries: %s", err)
}
fullContents, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Error reading file: %s", err)
}
// fix generated date so contents match
replacement := "# GENERATED: 2020-01-13 18:38:19.389637 +0000 UTC"
fullContents = replaceLine(fullContents, "GENERATED", replacement)
got := string(fullContents)
if got != expectedResult {
t.Errorf("Result not expected. Got: %s", got)
}
defer os.Remove(filename)
}
func TestDeleteEntryAtEnd(t *testing.T) {
filename := "./acfmgr_credfile_test1.txt"
err := writeBaseFile(filename)
if err != nil {
t.Errorf("Error making basefile: %s", err)
}
sess, err := NewCredFileSession(filename)
if err != nil {
t.Errorf("Error making credfile session: %s", err)
}
pfi := ProfileEntryInput{
Credential: getFakeCreds(),
ProfileEntryName: "newentry",
}
err = sess.NewEntry(&pfi)
if err != nil {
t.Errorf("Error adding entry: %s", err)
}
err = sess.DeleteEntries()
if err != nil {
t.Errorf("Error deleting entries: %s", err)
}
fullContents, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Error reading file: %s", err)
}
// fix generated date so contents match
replacement := "# GENERATED: 2020-01-13 18:38:19.389637 +0000 UTC"
fullContents = replaceLine(fullContents, "GENERATED", replacement)
got := string(fullContents)
if got != expectedResultDeletionEnd {
t.Errorf("Result not expected. Got: %s", got)
}
defer os.Remove(filename)
}
func TestDeleteEntryInMiddle(t *testing.T) {
filename := "./acfmgr_credfile_test2.txt"
err := writeBaseFile(filename)
if err != nil {
t.Errorf("Error making basefile: %s", err)
}
sess, err := NewCredFileSession(filename)
if err != nil {
t.Errorf("Error making credfile session: %s", err)
}
pfi := ProfileEntryInput{
Credential: getFakeCreds(),
ProfileEntryName: "testing",
}
err = sess.NewEntry(&pfi)
if err != nil {
t.Errorf("Error adding entry: %s", err)
}
err = sess.DeleteEntries()
if err != nil {
t.Errorf("Error deleting entries: %s", err)
}
fullContents, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Error reading file: %s", err)
}
got := string(fullContents)
if got != expectedResultDeletionMiddle {
t.Errorf("Result not expected. Got: %s", got)
}
defer os.Remove(filename)
}
func gimmeUserWindows() (fakeUser *user.User) {
u := user.User{
Uid: "S-1-5-21-693214013-1772980081-1954060963-500",
Gid: "S-1-5-21-693214013-1772980081-1954060963-513",
Username: "EC2AMAZ-K9GK6S2\\Administrator",
Name: "",
HomeDir: "C:\\Users\\Administrator",
}
fakeUser = &u
return fakeUser
}
func gimmeUserLinux() (fakeUser *user.User) {
u := user.User{
Uid: "1001",
Gid: "1001",
Username: "admin",
Name: "",
HomeDir: "/home/admin",
}
fakeUser = &u
return fakeUser
}
func gimmeUserMac() (fakeUser *user.User) {
u := user.User{
Uid: "501",
Gid: "20",
Username: "dudedudem",
Name: "Dude Dudem",
HomeDir: "/Users/dudedudem",
}
fakeUser = &u
return fakeUser
}
func TestExpandPath(t *testing.T) {
cases := []struct {
Path string
Want string
User *user.User
ExpectedErr error
ExpectedRuntime string
PrefixWantCurrentUser bool
}{
{
User: gimmeUserMac(),
Path: "~/.aws/credentials",
Want: "/Users/dudedudem/.aws/credentials",
ExpectedRuntime: "darwin",
},
{
User: gimmeUserMac(),
Path: "/Users/dudedudem/.aws/credentials",
Want: "/Users/dudedudem/.aws/credentials",
ExpectedRuntime: "darwin",
},
// test will always fail because os.ExpandEnv always takes runtime user, ugh
// {
// User: gimmeUserMac(),
// Path: "$HOME/.aws/credentials",
// ExpectedRuntime: "darwin",
// },
{
User: gimmeUserWindows(),
Path: "~\\.aws\\credentials",
Want: "C:\\Users\\Administrator\\.aws\\credentials",
ExpectedRuntime: "windows",
},
// test will probably fail unless test system user is exactly "Administrator"
// {
// User: gimmeUserWindows(),
// Path: "%userprofile%\\.aws\\credentials",
// Want: "C:\\Users\\Administrator\\.aws\\credentials",
// ExpectedRuntime: "windows",
// },
{
User: gimmeUserLinux(),
Path: "~/.aws/credentials",
Want: "/home/admin/.aws/credentials",
ExpectedRuntime: "linux",
},
}
for _, c := range cases {
if runtime.GOOS == c.ExpectedRuntime { // otherwise tests will bomb
have, err := expandPath(c.Path, c.User)
if err != nil {
if c.ExpectedErr == nil {
t.Errorf("Unexpected error: %s\n", err.Error())
}
} else if have != c.Want {
t.Errorf("Unexpected result. Have: '%s', Want: '%s'\n", have, c.Want)
} else {
t.Logf("PASS: Have '%s', Want '%s'\n", have, c.Want)
}
}
}
}
func getFakeCreds() *aws.Credentials {
timeFormat := "2006-01-02T15:04:05Z" // time.RFC3339
t, _ := time.Parse(timeFormat, "2020-01-08T14:03:02Z")
st := "f8sNh8tocFpiabpbOGHfpqSYSgOQcNqvbzyNpAYW9gxWOlAcGpaPJMQoeD" +
"M/0AQjHnvA8qMA8Q2jdxFmPwLHA184JI9YXVXs3a6ig2GMKvtTYXYwe4HK" +
"bymJm4zWxcG7OWwPee8BlZbY+F/T+lmNguge42ePV3mA5uyK5oTgryTG9" +
"TNFBtmh518OCdRXBDwwPWwQbfLWM/95KaOnZRIr/TpkjdWk4iCFXmKTIs5" +
"RKwDrS9mmD66cj6KTNsAGDxw29wYLOXlcB3MXbuEZzgew6tn8vpzonBIRi" +
"Fy74Oym6Ct1sFcNXVKrwmn2Ojnmec3KCAbFwynyTHPxE2PpHlVhQhvb2Az" +
"w2FeLGAw1btiItcvLDrS3cDI3TfQNaa8L2MX3Zfr2yBv9UUS4MfS2pZQ42" +
"Czze7PMRk6LrWh0HA+SdBUG6XeXDHcvXH3rH4GxJHuDhALCgNabFYwuXysXdGP="
cred := aws.Credentials{
AccessKeyID: []string{"AHENVMSKIRUEQNFHGZTA"}[0],
SecretAccessKey: []string{"ZcqCQl34NF8PtXHSdbBk3mZze1plNNSWqnmsz523"}[0],
SessionToken: st,
Expires: t,
}
return &cred
}