diff --git a/CHANGELOG.md b/CHANGELOG.md index 766cd2ab6..3adac38fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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()