From f3d8b7272a904178a1818f9e0e2a02a3e79d0689 Mon Sep 17 00:00:00 2001 From: An Tran Date: Tue, 2 Apr 2024 14:09:25 +1000 Subject: [PATCH] [3scale_batcher] Update regrex to match key with special chars 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. --- CHANGELOG.md | 2 ++ .../policy/3scale_batcher/keys_helper.lua | 4 ++-- .../3scale_batcher/keys_helper_spec.lua | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4df35d515..1accd5f81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/gateway/src/apicast/policy/3scale_batcher/keys_helper.lua b/gateway/src/apicast/policy/3scale_batcher/keys_helper.lua index ef8e5a61a..8b6a80035 100644 --- a/gateway/src/apicast/policy/3scale_batcher/keys_helper.lua +++ b/gateway/src/apicast/policy/3scale_batcher/keys_helper.lua @@ -39,9 +39,9 @@ local function metrics_part_in_key(usage) end local regexes_report_key = { - [[service_id:(?[\w-]+),user_key:(?[\w-]+),metric:(?[\S-]+)]], + [[service_id:(?[\w-]+),user_key:(?[\S-]+),metric:(?[\S-]+)]], [[service_id:(?[\w-]+),access_token:(?[\w-]+),metric:(?[\S-]+)]], - [[service_id:(?[\w-]+),app_id:(?[\w-]+),app_key:(?[\w-]+),metric:(?[\S-]+)]], + [[service_id:(?[\w-]+),app_id:(?[\w-]+),app_key:(?[\S-]+),metric:(?[\S-]+)]], [[service_id:(?[\w-]+),app_id:(?[\w-]+),metric:(?[\S-]+)]], } diff --git a/spec/policy/3scale_batcher/keys_helper_spec.lua b/spec/policy/3scale_batcher/keys_helper_spec.lua index 50199d00c..ba061a5a0 100644 --- a/spec/policy/3scale_batcher/keys_helper_spec.lua +++ b/spec/policy/3scale_batcher/keys_helper_spec.lua @@ -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() @@ -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()