Skip to content

Commit

Permalink
Merge pull request #1453 from tkan145/THREESCALE-10934-batcher-regrex
Browse files Browse the repository at this point in the history
[THREESCALE-10934] [3scale_batcher] Update regex to match key with special chars
  • Loading branch information
tkan145 authored Apr 4, 2024
2 parents 9bcea54 + 40e9bcb commit 849b5fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Replace luafilesystem-ffi with [luafilesystem](https://github.com/lunarmodules/luafilesystem) [PR #1445](https://github.com/3scale/APIcast/pull/1445) [THREESCALE-10662](https://issues.redhat.com/browse/THREESCALE-10662)

- Fix "Upstream cannot be null" error in APIcast logs [PR #1449](https://github.com/3scale/APIcast/pull/1449) [THREESCALE-5225](https://issues.redhat.com/browse/THREESCALE-5225)
- Fixed 3scale Batcher policy unable to handle base64 encoded `user_key` [PR #1453](https://github.com/3scale/APIcast/pull/1453) [THREESCALE-10934](https://issues.redhat.com/browse/THREESCALE-10934)

### Added

Expand Down
4 changes: 2 additions & 2 deletions gateway/src/apicast/policy/3scale_batcher/keys_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ local function metrics_part_in_key(usage)
end

local regexes_report_key = {
[[service_id:(?<service_id>[\w-]+),user_key:(?<user_key>[\w-]+),metric:(?<metric>[\S-]+)]],
[[service_id:(?<service_id>[\w-]+),user_key:(?<user_key>[\S-]+),metric:(?<metric>[\S-]+)]],
[[service_id:(?<service_id>[\w-]+),access_token:(?<access_token>[\w-]+),metric:(?<metric>[\S-]+)]],
[[service_id:(?<service_id>[\w-]+),app_id:(?<app_id>[\w-]+),app_key:(?<app_key>[\w-]+),metric:(?<metric>[\S-]+)]],
[[service_id:(?<service_id>[\w-]+),app_id:(?<app_id>[\w-]+),app_key:(?<app_key>[\S-]+),metric:(?<metric>[\S-]+)]],
[[service_id:(?<service_id>[\w-]+),app_id:(?<app_id>[\w-]+),metric:(?<metric>[\S-]+)]],
}

Expand Down
23 changes: 23 additions & 0 deletions spec/policy/3scale_batcher/keys_helper_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('Keys Helper', function()

local report = keys_helper.report_from_key_batched_report(key)
assert.same({ service_id = 's1', app_id = 'ai', app_key = 'ak', metric = 'm1' }, report)

-- special chars
key = 'service_id:s1,app_id:ai,app_key:!#$%&\'()*+,-.:;<=>?@[]^_`{|}~,metric:m1'
report = keys_helper.report_from_key_batched_report(key)
assert.same({ service_id = 's1', app_id = 'ai', app_key = '!#$%&\'()*+,-.:;<=>?@[]^_`{|}~', metric = 'm1' }, report)
end)

it('returns a valid metric in case of special chars', function()
Expand All @@ -56,6 +61,24 @@ describe('Keys Helper', function()

local report = keys_helper.report_from_key_batched_report(key)
assert.same({ service_id = 's1', user_key = 'uk', metric = 'm1' }, report)

key = 'service_id:s1,user_key:you-&$#!!!,metric:m1'
report = keys_helper.report_from_key_batched_report(key)
assert.same({ service_id = 's1', user_key = 'you-&$#!!!', metric = 'm1' }, report)

-- Base64
key = 'service_id:s1,user_key:aGVsbG93b3JsZAo=,metric:m1'
report = keys_helper.report_from_key_batched_report(key)
assert.same({ service_id = 's1', user_key = 'aGVsbG93b3JsZAo=', metric = 'm1' }, report)

end)

it('returns an error when user_key has space', function()
local key = 'service_id:s1,app_id:ai,app_key:I have spaces,metric:m%1'
assert.returns_error('credentials not found', keys_helper.report_from_key_batched_report(key))

key = 'service_id:s1,user_key:I have spaces,metric:m1'
assert.returns_error('credentials not found', keys_helper.report_from_key_batched_report(key))
end)

it('returns a report given a key of a batched report with access token', function()
Expand Down

0 comments on commit 849b5fb

Please sign in to comment.