Skip to content

Commit

Permalink
[3scale_batcher] Update regrex to match key with special chars
Browse files Browse the repository at this point in the history
Currently, when key is base64 encoded or contains special chars, the
batcher policy will save it to the cache but won't be able to retrieve
it due to the regrex mismatch. This leads to two problems:
* Reports are not sent to the backend
* Report cache filling up over time.

This commit changes the regular expression key to accept any character
except spaces.
  • Loading branch information
tkan145 committed Apr 3, 2024
1 parent 152cd27 commit f3d8b72
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ 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)

- 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

- Detect number of CPU shares when running on Cgroups V2 [PR #1410](https://github.com/3scale/apicast/pull/1410) [THREESCALE-10167](https://issues.redhat.com/browse/THREESCALE-10167)
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 f3d8b72

Please sign in to comment.