From 53f0f54b6dd4a2dc9235d55ab50737f8e3882d34 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Mon, 25 Dec 2023 18:10:44 +0800 Subject: [PATCH 01/10] fix: fix bkauth request add request-id --- src/apisix/plugins/bk-components/bkauth.lua | 41 +++++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/apisix/plugins/bk-components/bkauth.lua b/src/apisix/plugins/bk-components/bkauth.lua index 63a5903..016f95b 100644 --- a/src/apisix/plugins/bk-components/bkauth.lua +++ b/src/apisix/plugins/bk-components/bkauth.lua @@ -17,6 +17,7 @@ -- local pl_types = require("pl.types") local http = require("resty.http") +local uuid = require("resty.jit-uuid") local core = require("apisix.core") local bk_core = require("apisix.plugins.bk-core.init") @@ -48,6 +49,8 @@ function _M.verify_app_secret(app_code, app_secret) local http_client = http.new() http_client:set_timeout(BKAUTH_TIMEOUT_MS) + + local request_id= uuid.generate_v4() local res, err = http_client:request_uri( url, { method = "POST", @@ -57,16 +60,18 @@ function _M.verify_app_secret(app_code, app_secret) } ), ssl_verify = false, + headers = { ["X-Bk-App-Code"] = _M.app_code, ["X-Bk-App-Secret"] = _M.app_secret, + ["X-Request-Id"] =request_id, ["Content-Type"] = "application/json", }, } ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s, err: %s, response: nil", url, err) + err = string_format("failed to request third-party api, url: %s, request_id: %s, err: %s, response: nil", url,request_id, err) core.log.error(err) return nil, err end @@ -83,24 +88,24 @@ function _M.verify_app_secret(app_code, app_secret) if result == nil then core.log.error( string_format( - "failed to request %s, response is not valid json, status: %s, response: %s", url, res.status, res.body + "failed to request %s, request_id: %s, response is not valid json, status: %s, response: %s", url, request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s, status: %s", url, res.status + "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url,request_id, res.status ) end if result.code ~= 0 or res.status ~= 200 then core.log.error( string_format( - "failed to request %s, result.code!=0 or status!=200, status: %s, response: %s", url, res.status, + "failed to request %s,request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url,request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, bkauth error message: %s, url: %s, status: %s, code: %s", - result.message, url, res.status, result.code + "failed to request third-party api, bkauth error message: %s, url: %s,request_id: %s, status: %s, code: %s", + result.message, url,request_id, res.status, result.code ) end @@ -119,6 +124,7 @@ function _M.list_app_secrets(app_code) local http_client = http.new() http_client:set_timeout(BKAUTH_TIMEOUT_MS) + local request_id= uuid.generate_v4() local res, err = http_client:request_uri( url, { method = "GET", @@ -126,13 +132,14 @@ function _M.list_app_secrets(app_code) headers = { ["X-Bk-App-Code"] = _M.app_code, ["X-Bk-App-Secret"] = _M.app_secret, + ["X-Request-Id"] =request_id, ["Content-Type"] = "application/x-www-form-urlencoded", }, } ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s, err: %s, response: nil", url, err) + err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url,request_id, err) core.log.error(err) return nil, err end @@ -148,24 +155,24 @@ function _M.list_app_secrets(app_code) if result == nil then core.log.error( string_format( - "failed to request %s, response is not valid json, status: %s, response: %s", url, res.status, res.body + "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url,request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s, status: %s", url, res.status + "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url, request_id, res.status ) end if result.code ~= 0 or res.status ~= 200 then core.log.error( string_format( - "failed to request %s, result.code!=0 or status!=200, status: %s, response: %s", url, res.status, + "failed to request %s, request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url,request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, bkauth error message: %s, url: %s, status: %s, code: %s", - result.message, url, res.status, result.code + "failed to request third-party api, bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", + result.message, url,request_id, res.status, result.code ) end @@ -188,6 +195,7 @@ function _M.verify_access_token(access_token) local http_client = http.new() http_client:set_timeout(BKAUTH_TIMEOUT_MS) + local request_id= uuid.generate_v4() local res, err = http_client:request_uri( url, { method = "POST", @@ -200,6 +208,7 @@ function _M.verify_access_token(access_token) headers = { ["X-Bk-App-Code"] = _M.app_code, ["X-Bk-App-Secret"] = _M.app_secret, + ["X-Request-Id"] =request_id, -- ["Authorization"] = "Bearer " .. self.bkauth_access_token ["Content-Type"] = "application/json", }, @@ -207,7 +216,7 @@ function _M.verify_access_token(access_token) ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s, err: %s, response: nil", url, err) + err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url,request_id, err) core.log.error(err) return nil, err end @@ -216,17 +225,17 @@ function _M.verify_access_token(access_token) if result == nil then core.log.error( string_format( - "failed to request %s, response is not valid json, status: %s, response: %s", url, res.status, res.body + "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url,request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s, status: %s", url, res.status + "failed to request third-party api, response is not valid json, url: %s,request_id: %s, status: %s", url,request_id, res.status ) end if result.code ~= 0 or res.status ~= 200 then return nil, string_format( - "bkauth error message: %s, url: %s, status: %s, code: %s", result.message, url, res.status, result.code + "bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", result.message, url, request_id,res.status, result.code ) end From 3dd52892c97ba9d5828fbd5aa8b0dc82c5508618 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Mon, 25 Dec 2023 18:15:38 +0800 Subject: [PATCH 02/10] fix: add test --- src/apisix/tests/bk-components/test-bkauth.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index e1329c0..8fe1f4a 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -59,6 +59,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -79,6 +80,7 @@ describe( assert.is_false(result.existed) assert.is_false(result.verified) assert.is_nil(err) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -93,6 +95,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -115,6 +118,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -174,6 +178,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) it( @@ -196,6 +201,7 @@ describe( } ) assert.is_nil(err) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -210,6 +216,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -232,6 +239,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -295,6 +303,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -309,6 +318,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -347,6 +357,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) From 2752109095aa2d9f1707e394675fa3515727aa4c Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Mon, 25 Dec 2023 18:19:37 +0800 Subject: [PATCH 03/10] fix: fix test --- src/apisix/tests/bk-components/test-bkauth.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 8fe1f4a..8c2ae52 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -80,7 +80,6 @@ describe( assert.is_false(result.existed) assert.is_false(result.verified) assert.is_nil(err) - assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -201,7 +200,6 @@ describe( } ) assert.is_nil(err) - assert.is_true(core.string.has_prefix(err, "request_id")) end ) @@ -338,6 +336,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) + assert.is_true(core.string.has_prefix(err, "request_id")) end ) From 661c664495bcb033c9f5d6889fa7167390a7af3f Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Tue, 26 Dec 2023 10:43:57 +0800 Subject: [PATCH 04/10] fix: fix fmt --- src/apisix/plugins/bk-components/bkauth.lua | 52 ++++++++++++------- .../tests/bk-components/test-bkauth.lua | 1 - 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/apisix/plugins/bk-components/bkauth.lua b/src/apisix/plugins/bk-components/bkauth.lua index 016f95b..1fed127 100644 --- a/src/apisix/plugins/bk-components/bkauth.lua +++ b/src/apisix/plugins/bk-components/bkauth.lua @@ -50,7 +50,7 @@ function _M.verify_app_secret(app_code, app_secret) local http_client = http.new() http_client:set_timeout(BKAUTH_TIMEOUT_MS) - local request_id= uuid.generate_v4() + local request_id = uuid.generate_v4() local res, err = http_client:request_uri( url, { method = "POST", @@ -64,14 +64,15 @@ function _M.verify_app_secret(app_code, app_secret) headers = { ["X-Bk-App-Code"] = _M.app_code, ["X-Bk-App-Secret"] = _M.app_secret, - ["X-Request-Id"] =request_id, + ["X-Request-Id"] = request_id, ["Content-Type"] = "application/json", }, } ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s, request_id: %s, err: %s, response: nil", url,request_id, err) + err = string_format("failed to request third-party api, url: %s, request_id: %s, err: %s, response: nil", url, + request_id, err) core.log.error(err) return nil, err end @@ -88,24 +89,27 @@ function _M.verify_app_secret(app_code, app_secret) if result == nil then core.log.error( string_format( - "failed to request %s, request_id: %s, response is not valid json, status: %s, response: %s", url, request_id, res.status, res.body + "failed to request %s, request_id: %s, response is not valid json, status: %s, response: %s", url, + request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url,request_id, res.status + "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url, + request_id, res.status ) end if result.code ~= 0 or res.status ~= 200 then core.log.error( string_format( - "failed to request %s,request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url,request_id, res.status, + "failed to request %s,request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url, + request_id, res.status, res.body ) ) return nil, string_format( "failed to request third-party api, bkauth error message: %s, url: %s,request_id: %s, status: %s, code: %s", - result.message, url,request_id, res.status, result.code + result.message, url, request_id, res.status, result.code ) end @@ -124,7 +128,7 @@ function _M.list_app_secrets(app_code) local http_client = http.new() http_client:set_timeout(BKAUTH_TIMEOUT_MS) - local request_id= uuid.generate_v4() + local request_id = uuid.generate_v4() local res, err = http_client:request_uri( url, { method = "GET", @@ -132,14 +136,15 @@ function _M.list_app_secrets(app_code) headers = { ["X-Bk-App-Code"] = _M.app_code, ["X-Bk-App-Secret"] = _M.app_secret, - ["X-Request-Id"] =request_id, + ["X-Request-Id"] = request_id, ["Content-Type"] = "application/x-www-form-urlencoded", }, } ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url,request_id, err) + err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url, + request_id, err) core.log.error(err) return nil, err end @@ -155,24 +160,27 @@ function _M.list_app_secrets(app_code) if result == nil then core.log.error( string_format( - "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url,request_id, res.status, res.body + "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url, + request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url, request_id, res.status + "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url, + request_id, res.status ) end if result.code ~= 0 or res.status ~= 200 then core.log.error( string_format( - "failed to request %s, request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url,request_id, res.status, + "failed to request %s, request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url, + request_id, res.status, res.body ) ) return nil, string_format( "failed to request third-party api, bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", - result.message, url,request_id, res.status, result.code + result.message, url, request_id, res.status, result.code ) end @@ -195,7 +203,7 @@ function _M.verify_access_token(access_token) local http_client = http.new() http_client:set_timeout(BKAUTH_TIMEOUT_MS) - local request_id= uuid.generate_v4() + local request_id = uuid.generate_v4() local res, err = http_client:request_uri( url, { method = "POST", @@ -208,7 +216,7 @@ function _M.verify_access_token(access_token) headers = { ["X-Bk-App-Code"] = _M.app_code, ["X-Bk-App-Secret"] = _M.app_secret, - ["X-Request-Id"] =request_id, + ["X-Request-Id"] = request_id, -- ["Authorization"] = "Bearer " .. self.bkauth_access_token ["Content-Type"] = "application/json", }, @@ -216,7 +224,8 @@ function _M.verify_access_token(access_token) ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url,request_id, err) + err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url, + request_id, err) core.log.error(err) return nil, err end @@ -225,17 +234,20 @@ function _M.verify_access_token(access_token) if result == nil then core.log.error( string_format( - "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url,request_id, res.status, res.body + "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url, + request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s,request_id: %s, status: %s", url,request_id, res.status + "failed to request third-party api, response is not valid json, url: %s,request_id: %s, status: %s", url, + request_id, res.status ) end if result.code ~= 0 or res.status ~= 200 then return nil, string_format( - "bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", result.message, url, request_id,res.status, result.code + "bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", result.message, url, + request_idres.status, result.code ) end diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 8c2ae52..8786364 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -21,7 +21,6 @@ local bkauth = require("apisix.plugins.bk-components.bkauth") describe( "bkauth", function() - local response, response_err before_each( From 99f7c3d9847d02cc912d387309140ee248544a6e Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Tue, 26 Dec 2023 12:10:44 +0800 Subject: [PATCH 05/10] fix: fix fmt --- src/apisix/plugins/bk-components/bkauth.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/apisix/plugins/bk-components/bkauth.lua b/src/apisix/plugins/bk-components/bkauth.lua index 1fed127..6639cb5 100644 --- a/src/apisix/plugins/bk-components/bkauth.lua +++ b/src/apisix/plugins/bk-components/bkauth.lua @@ -102,13 +102,13 @@ function _M.verify_app_secret(app_code, app_secret) if result.code ~= 0 or res.status ~= 200 then core.log.error( string_format( - "failed to request %s,request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url, + "failed to request %s, request_id: %s, result.code!=0 or status!=200, status: %s, response: %s", url, request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, bkauth error message: %s, url: %s,request_id: %s, status: %s, code: %s", + "failed to request third-party api, bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", result.message, url, request_id, res.status, result.code ) end @@ -143,7 +143,7 @@ function _M.list_app_secrets(app_code) ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url, + err = string_format("failed to request third-party api, url: %s, request_id: %s, err: %s, response: nil", url, request_id, err) core.log.error(err) return nil, err @@ -160,7 +160,7 @@ function _M.list_app_secrets(app_code) if result == nil then core.log.error( string_format( - "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url, + "failed to request %s, request_id: %s, response is not valid json, status: %s, response: %s", url, request_id, res.status, res.body ) ) @@ -224,7 +224,7 @@ function _M.verify_access_token(access_token) ) if not (res and res.body) then - err = string_format("failed to request third-party api, url: %s,request_id: %s, err: %s, response: nil", url, + err = string_format("failed to request third-party api, url: %s, request_id: %s, err: %s, response: nil", url, request_id, err) core.log.error(err) return nil, err @@ -234,12 +234,12 @@ function _M.verify_access_token(access_token) if result == nil then core.log.error( string_format( - "failed to request %s,request_id: %s, response is not valid json, status: %s, response: %s", url, + "failed to request %s, request_id: %s, response is not valid json, status: %s, response: %s", url, request_id, res.status, res.body ) ) return nil, string_format( - "failed to request third-party api, response is not valid json, url: %s,request_id: %s, status: %s", url, + "failed to request third-party api, response is not valid json, url: %s, request_id: %s, status: %s", url, request_id, res.status ) end From a81e1c06e0cb9608a0d4b9e922ef0da92b716764 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Tue, 26 Dec 2023 12:12:51 +0800 Subject: [PATCH 06/10] fix: fix test --- .../tests/bk-components/test-bkauth.lua | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 8786364..7e5e756 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -58,7 +58,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -93,7 +93,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -116,7 +116,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -176,7 +176,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) it( @@ -213,7 +213,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -236,7 +236,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -287,6 +287,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -300,7 +301,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -315,7 +316,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) @@ -335,7 +336,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) From 29bd191fecf25e0457772027476aa703db570601 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Tue, 26 Dec 2023 12:14:07 +0800 Subject: [PATCH 07/10] fix: fix test --- src/apisix/tests/bk-components/test-bkauth.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 7e5e756..0f89afc 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -356,7 +356,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) - assert.is_true(core.string.has_prefix(err, "request_id")) + assert.is_true(core.string.contains(err, "request_id")) end ) From 03e42617c1a0e9d8a41a2d0a000a66ec2361e9f3 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Tue, 26 Dec 2023 12:36:31 +0800 Subject: [PATCH 08/10] fix: fix test --- src/apisix/tests/bk-components/test-bkauth.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 0f89afc..738dd7f 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -58,7 +58,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")) end ) @@ -93,7 +93,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")) end ) @@ -116,7 +116,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")) end ) From 3af2632444b3769e123490f7fe6a1a496ee487c5 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Wed, 27 Dec 2023 15:23:55 +0800 Subject: [PATCH 09/10] fix: fix test --- src/apisix/plugins/bk-components/bkauth.lua | 10 +++++---- .../tests/bk-components/test-bkauth.lua | 22 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/apisix/plugins/bk-components/bkauth.lua b/src/apisix/plugins/bk-components/bkauth.lua index 6639cb5..b841857 100644 --- a/src/apisix/plugins/bk-components/bkauth.lua +++ b/src/apisix/plugins/bk-components/bkauth.lua @@ -108,7 +108,8 @@ function _M.verify_app_secret(app_code, app_secret) ) ) return nil, string_format( - "failed to request third-party api, bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", + "failed to request third-party api, bkauth error message: %s, url: %s, \ + request_id: %s, status: %s, code: %s", result.message, url, request_id, res.status, result.code ) end @@ -179,7 +180,8 @@ function _M.list_app_secrets(app_code) ) ) return nil, string_format( - "failed to request third-party api, bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", + "failed to request third-party api, bkauth error message: %s, url: %s,\ + request_id: %s, status: %s, code: %s", result.message, url, request_id, res.status, result.code ) end @@ -246,8 +248,8 @@ function _M.verify_access_token(access_token) if result.code ~= 0 or res.status ~= 200 then return nil, string_format( - "bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", result.message, url, - request_idres.status, result.code + "bkauth error message: %s, url: %s, request_id: %s, status: %s, code: %s", + result.message, url, request_id, res.status, result.code ) end diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 738dd7f..1cfb80b 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -58,7 +58,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -93,7 +93,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -116,7 +116,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -176,7 +176,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) it( @@ -213,7 +213,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -236,7 +236,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -287,7 +287,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -301,7 +301,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -316,7 +316,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -336,7 +336,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) @@ -356,7 +356,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) - assert.is_true(core.string.contains(err, "request_id")) + assert.is_true(core.string.find(err, "request_id")~=nil) end ) From cca5ee3cef9cba038ebf89955dd2a16399274ee1 Mon Sep 17 00:00:00 2001 From: Han-Ya-Jun <1581532052@qq.com> Date: Wed, 27 Dec 2023 15:27:06 +0800 Subject: [PATCH 10/10] fix: fix fmt --- .../tests/bk-components/test-bkauth.lua | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/apisix/tests/bk-components/test-bkauth.lua b/src/apisix/tests/bk-components/test-bkauth.lua index 1cfb80b..0ebd3fb 100644 --- a/src/apisix/tests/bk-components/test-bkauth.lua +++ b/src/apisix/tests/bk-components/test-bkauth.lua @@ -58,7 +58,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -93,7 +93,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -116,7 +116,7 @@ describe( local result, err = bkauth.verify_app_secret("fake-app-code", "fake-app-secret") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -176,7 +176,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) it( @@ -213,7 +213,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -236,7 +236,7 @@ describe( local result, err = bkauth.list_app_secrets("fake-app-code") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -287,7 +287,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -301,7 +301,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -316,7 +316,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "failed to request third-party api")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -336,7 +336,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end ) @@ -356,7 +356,7 @@ describe( local result, err = bkauth.verify_access_token("fake-token") assert.is_nil(result) assert.is_true(core.string.has_prefix(err, "bkauth error message: error")) - assert.is_true(core.string.find(err, "request_id")~=nil) + assert.is_true(core.string.find(err, "request_id") ~= nil) end )