@@ -4,6 +4,7 @@ local restore = require "spec.helpers"
4
4
5
5
-- Mock for HTTP client
6
6
local response = {} -- override in tests
7
+ local http_records = {} -- record requests for assertions
7
8
local http = {
8
9
new = function ()
9
10
return {
@@ -12,8 +13,10 @@ local http = {
12
13
set_timeouts = function () return true end ,
13
14
request = function (self , opts )
14
15
if opts .path == " /test/path" then
16
+ table.insert (http_records , opts )
15
17
return { -- the response for the credentials
16
18
status = (response or {}).status or 200 ,
19
+ headers = opts and opts .headers or {},
17
20
read_body = function () return json .encode {
18
21
AccessKeyId = (response or {}).AccessKeyId or " access" ,
19
22
SecretAccessKey = (response or {}).SecretAccessKey or " secret" ,
@@ -30,6 +33,12 @@ local http = {
30
33
end ,
31
34
}
32
35
36
+ local pl_utils = {
37
+ readfile = function ()
38
+ return " testtokenabc123"
39
+ end
40
+ }
41
+
33
42
34
43
describe (" RemoteCredentials" , function ()
35
44
@@ -85,3 +94,63 @@ describe("RemoteCredentials with customized full URI", function ()
85
94
assert .equal (" token" , token )
86
95
end )
87
96
end )
97
+
98
+ describe (" RemoteCredentials with full URI and token file" , function ()
99
+ it (" fetches credentials" , function ()
100
+ local RemoteCredentials
101
+
102
+ restore ()
103
+ restore .setenv (" AWS_CONTAINER_CREDENTIALS_FULL_URI" , " http://localhost:12345/test/path" )
104
+ restore .setenv (" AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE" , " /var/run/secrets/pods.eks.amazonaws.com/serviceaccount/eks-pod-identity-token" )
105
+
106
+ local _ = require (" resty.aws.config" ).global -- load config before mocking http client
107
+ package.loaded [" resty.luasocket.http" ] = http
108
+ package.loaded [" pl.utils" ] = pl_utils
109
+
110
+ RemoteCredentials = require " resty.aws.credentials.RemoteCredentials"
111
+ finally (function ()
112
+ restore ()
113
+ end )
114
+
115
+ local cred = RemoteCredentials :new ()
116
+ local success , key , secret , token = cred :get ()
117
+ assert .equal (true , success )
118
+ assert .equal (" access" , key )
119
+ assert .equal (" secret" , secret )
120
+ assert .equal (" token" , token )
121
+
122
+ assert .not_nil (http_records [# http_records ].headers )
123
+ assert .equal (http_records [# http_records ].headers [" Authorization" ], " testtokenabc123" )
124
+ end )
125
+ end )
126
+
127
+ describe (" RemoteCredentials with full URI and token and token file, file takes higher precedence" , function ()
128
+ it (" fetches credentials" , function ()
129
+ local RemoteCredentials
130
+
131
+ restore ()
132
+ restore .setenv (" AWS_CONTAINER_CREDENTIALS_FULL_URI" , " http://localhost:12345/test/path" )
133
+ restore .setenv (" AWS_CONTAINER_AUTHORIZATION_TOKEN" , " testtoken" )
134
+ restore .setenv (" AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE" , " /var/run/secrets/pods.eks.amazonaws.com/serviceaccount/eks-pod-identity-token" )
135
+
136
+ local _ = require (" resty.aws.config" ).global -- load config before mocking http client
137
+ package.loaded [" resty.luasocket.http" ] = http
138
+ package.loaded [" pl.utils" ] = pl_utils
139
+
140
+ RemoteCredentials = require " resty.aws.credentials.RemoteCredentials"
141
+ finally (function ()
142
+ restore ()
143
+ end )
144
+
145
+ local cred = RemoteCredentials :new ()
146
+ local success , key , secret , token = cred :get ()
147
+ assert .equal (true , success )
148
+ assert .equal (" access" , key )
149
+ assert .equal (" secret" , secret )
150
+ assert .equal (" token" , token )
151
+
152
+ assert .not_nil (http_records [# http_records ].headers )
153
+ assert .equal (http_records [# http_records ].headers [" Authorization" ], " testtokenabc123" )
154
+ end )
155
+ end )
156
+
0 commit comments