Skip to content

Commit a797908

Browse files
committed
fix: add token env
1 parent ead0dae commit a797908

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

spec/03-credentials/04-RemoteCredentials_spec.lua

+31
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,34 @@ describe("RemoteCredentials with full URI and token file", function ()
123123
assert.equal(http_records[#http_records].headers["Authorization"], "testtokenabc123")
124124
end)
125125
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+

src/resty/aws/config.lua

+1-7
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,8 @@ local env_vars = {
147147
-- if both are set, the value in AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE will be used
148148
--
149149
-- This is also used by EKS Pod Identity authorization
150+
AWS_CONTAINER_AUTHORIZATION_TOKEN = { name = "AWS_CONTAINER_AUTHORIZATION_TOKEN", default = nil },
150151
AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE = { name = "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE", default = nil },
151-
-- TODO: ---
152-
-- A possible issue is that due to Nginx worker process's envvars isolation
153-
-- the AWS_CONTAINER_AUTHORIZATION_TOKEN may not get refreshed.
154-
-- According to the AWS documentation, the AWS_CONTAINER_AUTHORIZATION_TOKEN is only
155-
-- used in IoT product Greengrass, which is not a common use case.
156-
-- AWS_CONTAINER_AUTHORIZATION_TOKEN = { name = "AWS_CONTAINER_AUTHORIZATION_TOKEN", default = nil },
157-
-- ---------
158152

159153
-- HTTP/HTTPs proxy settings
160154
HTTP_PROXY = { name = "http_proxy", default = nil },

src/resty/aws/credentials/RemoteCredentials.lua

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ local readfile = require("pl.utils").readfile
1717

1818

1919
local FullUri
20+
local AuthToken
2021
local AuthTokenFile
2122

2223

@@ -78,6 +79,11 @@ local function initialize()
7879
({ http = 80, https = 443 })[FullUri.scheme]
7980
end
8081

82+
-- get auth token
83+
if aws_config.global.AWS_CONTAINER_AUTHORIZATION_TOKEN then
84+
AuthToken = aws_config.global.AWS_CONTAINER_AUTHORIZATION_TOKEN
85+
end
86+
8187
-- get auth token file path
8288
if aws_config.global.AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE then
8389
AuthTokenFile = aws_config.global.AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE
@@ -116,6 +122,11 @@ function RemoteCredentials:refresh()
116122

117123

118124
local headers = {}
125+
126+
if AuthToken then
127+
headers["Authorization"] = AuthToken
128+
end
129+
119130
if AuthTokenFile then
120131
local token, err = readfile(AuthTokenFile)
121132
if not token then

0 commit comments

Comments
 (0)