Skip to content

Commit dd093a7

Browse files
feat: assertion header is now configurable (#45)
* the header used for the assertion is now configurable * new config parameter
1 parent e0bdca4 commit dd093a7

File tree

6 files changed

+64
-22
lines changed

6 files changed

+64
-22
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
runs-on: ubuntu-latest
3636
steps:
3737
- uses: actions/checkout@v3
38-
- uses: leafo/gh-actions-lua@v9
39-
- uses: leafo/gh-actions-luarocks@v4
38+
- uses: leafo/gh-actions-lua@v12
39+
- uses: leafo/gh-actions-luarocks@v6
4040
- run: luarocks make
4141

4242
- name: luarocks pack

.github/workflows/publish-rock.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v3
16-
- uses: leafo/gh-actions-lua@v9
17-
- uses: leafo/gh-actions-luarocks@v4
16+
- uses: leafo/gh-actions-lua@v12
17+
- uses: leafo/gh-actions-luarocks@v6
1818
- run: luarocks make
1919

2020
- name: luarocks pack

Readme.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ Note that this plugin cannot be used in combination with Kong [upstreams](https:
1717
## Plugin configuration parameters
1818

1919
```lua
20-
aws_assume_role_arn -- ARN of the IAM role that the plugin will try to assume
20+
aws_assume_role_arn -- ARN of the IAM role that the plugin will try to assume, cannot be supplied together with `aws_account_id`. At least one must be specified.
2121
type = "string"
22-
required = true
22+
required = false
23+
24+
aws_account_id -- ID of the AWS account the lambda is deployed to. Used to generate the ARN of the IAM role to be assumed. Cannot be specified together with `aws_assume_role_arn`. At least one must be specified.
25+
type = "number"
26+
required = false
2327

2428
aws_assume_role_name -- Name of the role above.
2529
type = "string"
@@ -33,6 +37,10 @@ aws_service -- AWS Service you are trying to access (lambda and s3 were tested)
3337
type = "string"
3438
required = true
3539

40+
auth_header -- The header key used to fetch the value sent to AWS STS as the 'WebIdentityToken' parameter. Defaults to 'authorization'
41+
type = "string"
42+
required = false
43+
3644
override_target_host -- To be used when deploying multiple lambdas on a single Kong service (because lambdas have different URLs)
3745
type = "string"
3846
required = false
@@ -56,12 +64,12 @@ type = "boolean"
5664
required = true
5765
default = false
5866

59-
preserve_auth_header -- Controls if the bearer token will be passed to the upstream
67+
preserve_auth_header -- Controls if the header value will be passed to the upstream
6068
type = "boolean"
6169
required = true
6270
default = true
6371

64-
preserve_auth_header_key -- The header key where the bearer token will be saved and passed to the upstream. works only if 'preserve_auth_header' parameter above is set to true.
72+
preserve_auth_header_key -- The header key where the header value will be saved and passed to the upstream. works only if 'preserve_auth_header' parameter above is set to true.
6573
type = "string"
6674
required = true
6775
default = "x-authorization"

kong/plugins/aws-request-signing/handler.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,20 @@ function AWSLambdaSTS:access(conf)
109109
return kong.response.exit(500, { message = "The plugin must be bound to a service!" })
110110
end
111111

112+
local auth_header_key = conf.auth_header or "authorization"
113+
local auth_header_value = request_headers[auth_header_key]
114+
if not auth_header_value then
115+
kong.log.notice("header value missing for: '" .. auth_header_key .. "', skipping signing")
116+
return
117+
end
118+
112119
if conf.preserve_auth_header then
113120
kong.service.request.set_headers({
114-
[conf.preserve_auth_header_key] = request_headers.authorization
121+
[conf.preserve_auth_header_key] = auth_header_value
115122
})
116123
end
124+
-- removing the header, we either do not need it or we set it to the signed value later.
125+
kong.service.request.clear_header(auth_header_key)
117126

118127
local target_altered = false
119128

@@ -155,8 +164,9 @@ function AWSLambdaSTS:access(conf)
155164

156165

157166
local sts_conf = {
158-
RoleArn = conf.aws_assume_role_arn,
159-
WebIdentityToken = retrieve_token(request_headers["authorization"]),
167+
RoleArn = conf.aws_assume_role_arn or
168+
('arn:aws:iam::' .. conf.aws_account_id .. ':role/' .. conf.aws_assume_role_name),
169+
WebIdentityToken = retrieve_token(auth_header_value),
160170
RoleSessionName = conf.aws_assume_role_name,
161171
}
162172

@@ -171,10 +181,7 @@ function AWSLambdaSTS:access(conf)
171181
["content-type"] = request_headers["content-type"]
172182
}
173183

174-
-- removing the authorization, we either do not need it or we set it again later.
175-
kong.service.request.clear_header("authorization")
176-
177-
-- might fail if too big. is controlled by the folowing nginx params:
184+
-- might fail if too big. is controlled by the following nginx params:
178185
-- nginx_http_client_max_body_size
179186
-- nginx_http_client_body_buffer_size
180187
local req_body, get_body_err = kong.request.get_raw_body()

kong/plugins/aws-request-signing/schema.lua

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ return {
1919
aws_assume_role_arn = {
2020
type = "string",
2121
encrypted = true, -- Kong Enterprise-exclusive feature, does nothing in Kong CE
22-
required = true,
22+
required = false,
23+
}
24+
},
25+
{
26+
aws_account_id = {
27+
type = "number",
28+
required = false,
2329
}
2430
},
2531
{
@@ -67,9 +73,9 @@ return {
6773
required = true,
6874
default = false,
6975
description =
70-
"Instructs the plugin to use the context target if its host or port were altered "..
71-
" (by other plugins) during the signing, bypassing the override_target_host "..
72-
"and override_target_port parameters. Works by comparing the service target parameters"..
76+
"Instructs the plugin to use the context target if its host or port were altered " ..
77+
" (by other plugins) during the signing, bypassing the override_target_host " ..
78+
"and override_target_port parameters. Works by comparing the service target parameters" ..
7379
" with the context target parameters. Ignored if the target was not altered."
7480
}
7581
},
@@ -87,6 +93,12 @@ return {
8793
default = false,
8894
}
8995
},
96+
{
97+
auth_header = {
98+
type = "string",
99+
required = false,
100+
}
101+
},
90102
{
91103
preserve_auth_header = {
92104
type = "boolean",
@@ -106,5 +118,17 @@ return {
106118
}
107119
},
108120
entity_checks = {
121+
{
122+
mutually_exclusive = {
123+
"config.aws_account_id",
124+
"config.aws_assume_role_arn",
125+
},
126+
},
127+
{
128+
at_least_one_of = {
129+
"config.aws_account_id",
130+
"config.aws_assume_role_arn",
131+
},
132+
},
109133
}
110134
}

spec/02-integration_spec.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ for _, strategy in helpers.all_strategies() do
194194
method = "GET",
195195
path = "/status/200",
196196
headers = {
197-
["Host"] = "test1.com"
197+
["Host"] = "test1.com",
198+
authorization = "header.body.sig",
198199
}
199200
})
200201
local body = assert.res_status(200, res)
@@ -210,7 +211,8 @@ for _, strategy in helpers.all_strategies() do
210211
it("should override host when configured", function()
211212
local res = proxy_client:get("/testoverride", {
212213
headers = {
213-
["Host"] = "test2.com"
214+
["Host"] = "test2.com",
215+
authorization = "header.body.sig",
214216
}
215217
})
216218
local body = assert.res_status(200, res)
@@ -223,7 +225,8 @@ for _, strategy in helpers.all_strategies() do
223225
method = "GET",
224226
path = "/status/200",
225227
headers = {
226-
["Host"] = "test3.com"
228+
["Host"] = "test3.com",
229+
authorization = "header.body.sig",
227230
}
228231
})
229232
local body = assert.res_status(200, res)

0 commit comments

Comments
 (0)