Debian Codegraph @

cmd/desync/credentials_test.go

raw
1 package main
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 var now = time.Now
14
15 func TestNewRefreshableSharedCredentials(t *testing.T) {
16 currentTime := time.Now()
17 mockNow := func() time.Time {
18 return currentTime.Add(time.Minute * 2)
19 }
20
21 c := NewRefreshableSharedCredentials("testdata/example.ini", "", mockNow)
22
23 assert.True(t, c.IsExpired(), "Expect creds to be expired before retrieve")
24
25 _, err := c.Get()
26 assert.Nil(t, err, "Expect no error")
27
28 assert.False(t, c.IsExpired(), "Expect creds to not be expired after retrieve")
29 }
30
31 func TestRefreshableSharedCredentialsProvider(t *testing.T) {
32 defer restoreEnv(os.Environ())
33 os.Clearenv()
34
35 p := RefreshableSharedCredentialsProvider{Filename: "testdata/example.ini", Profile: "", exp: now().Add(time.Minute), now: now}
36 creds, err := p.Retrieve()
37 assert.Nil(t, err, "Expect no error")
38
39 assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match")
40 assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match")
41 assert.Equal(t, "token", creds.SessionToken, "Expect session token to match")
42 }
43
44 func TestRefreshableSharedCredentialsProviderIsExpired(t *testing.T) {
45 defer restoreEnv(os.Environ())
46 os.Clearenv()
47 currentTime := time.Now()
48 mockNow := func() time.Time {
49 return currentTime.Add(time.Minute * 2)
50 }
51
52 p := RefreshableSharedCredentialsProvider{Filename: "testdata/example.ini", Profile: "", exp: currentTime.Add(time.Minute), now: mockNow}
53
54 assert.True(t, p.IsExpired(), "Expect creds to be expired before retrieve")
55
56 _, err := p.Retrieve()
57 assert.Nil(t, err, "Expect no error")
58
59 assert.False(t, p.IsExpired(), "Expect creds to not be expired after retrieve")
60 }
61
62 func TestRefreshableSharedCredentialsProviderWithAWS_SHARED_CREDENTIALS_FILE(t *testing.T) {
63 defer restoreEnv(os.Environ())
64 os.Clearenv()
65 os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "testdata/example.ini")
66
67 p := RefreshableSharedCredentialsProvider{exp: now().Add(time.Minute), now: now}
68 creds, err := p.Retrieve()
69
70 assert.Nil(t, err, "Expect no error")
71
72 assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match")
73 assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match")
74 assert.Equal(t, "token", creds.SessionToken, "Expect session token to match")
75 }
76
77 func TestRefreshableSharedCredentialsProviderWithAWS_SHARED_CREDENTIALS_FILEAbsPath(t *testing.T) {
78 defer restoreEnv(os.Environ())
79 os.Clearenv()
80
81 wd, err := os.Getwd()
82 assert.NoError(t, err)
83 os.Setenv("AWS_SHARED_CREDENTIALS_FILE", filepath.Join(wd, "testdata/example.ini"))
84 p := RefreshableSharedCredentialsProvider{exp: now().Add(time.Minute), now: now}
85 creds, err := p.Retrieve()
86 assert.Nil(t, err, "Expect no error")
87
88 assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match")
89 assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match")
90 assert.Equal(t, "token", creds.SessionToken, "Expect session token to match")
91 }
92
93 func TestRefreshableSharedCredentialsProviderWithAWS_PROFILE(t *testing.T) {
94 defer restoreEnv(os.Environ())
95 os.Clearenv()
96 os.Setenv("AWS_PROFILE", "no_token")
97
98 p := RefreshableSharedCredentialsProvider{Filename: "testdata/example.ini", Profile: "", exp: now().Add(time.Minute), now: now}
99 creds, err := p.Retrieve()
100 assert.Nil(t, err, "Expect no error")
101
102 assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match")
103 assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match")
104 assert.Empty(t, creds.SessionToken, "Expect no token")
105 }
106
107 func TestRefreshableSharedCredentialsProviderWithoutTokenFromProfile(t *testing.T) {
108 defer restoreEnv(os.Environ())
109 os.Clearenv()
110
111 p := RefreshableSharedCredentialsProvider{Filename: "testdata/example.ini", Profile: "no_token", exp: now().Add(time.Minute), now: now}
112 creds, err := p.Retrieve()
113 assert.Nil(t, err, "Expect no error")
114
115 assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match")
116 assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match")
117 assert.Empty(t, creds.SessionToken, "Expect no token")
118 }
119
120 func TestRefreshableSharedCredentialsProviderColonInCredFile(t *testing.T) {
121 defer restoreEnv(os.Environ())
122 os.Clearenv()
123
124 p := RefreshableSharedCredentialsProvider{Filename: "testdata/example.ini", Profile: "with_colon", exp: now().Add(time.Minute), now: now}
125 creds, err := p.Retrieve()
126 assert.Nil(t, err, "Expect no error")
127
128 assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match")
129 assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match")
130 assert.Empty(t, creds.SessionToken, "Expect no token")
131 }
132
133 func TestRefreshableSharedCredentialsProvider_DefaultFilename(t *testing.T) {
134 defer restoreEnv(os.Environ())
135 os.Clearenv()
136 os.Setenv("USERPROFILE", "profile_dir")
137 os.Setenv("HOME", "home_dir")
138
139 // default filename and profile
140 p := RefreshableSharedCredentialsProvider{exp: now().Add(time.Minute), now: now}
141
142 filename, err := p.filename()
143
144 if err != nil {
145 t.Fatalf("expect no error, got %v", err)
146 }
147 expectedFilename, err := SharedCredentialsFilename()
148 assert.NoError(t, err)
149 if expectedFilename != filename {
150 t.Errorf("expect %q filename, got %q", expectedFilename, filename)
151 }
152 }
153
154 func restoreEnv(env []string) {
155 for _, e := range env {
156 kv := strings.SplitN(e, "=", 2)
157 os.Setenv(kv[0], kv[1])
158 }
159 }