This is an automated email from the ASF dual-hosted git repository.

monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new f2c099949 refactor(consumer-restriction): optimize the error log when 
the corresponding resource is not fetched (#9778)
f2c099949 is described below

commit f2c099949c7079196c1acb8fdd1c6c4cb7018ff3
Author: Fucheng Jiang <[email protected]>
AuthorDate: Wed Jul 19 10:15:00 2023 +0800

    refactor(consumer-restriction): optimize the error log when the 
corresponding resource is not fetched (#9778)
---
 apisix/plugins/consumer-restriction.lua |   4 +-
 t/plugin/consumer-restriction.t         |  12 +--
 t/plugin/consumer-restriction2.t        | 155 ++++++++++++++++++++++++++++++--
 3 files changed, 156 insertions(+), 15 deletions(-)

diff --git a/apisix/plugins/consumer-restriction.lua 
b/apisix/plugins/consumer-restriction.lua
index 93f3b9f59..88c2bbd95 100644
--- a/apisix/plugins/consumer-restriction.lua
+++ b/apisix/plugins/consumer-restriction.lua
@@ -128,7 +128,9 @@ function _M.access(conf, ctx)
     local method = ngx.req.get_method()
 
     if not value then
-        return 401, { message = "Missing authentication or identity 
verification."}
+        local err_msg = "The request is rejected, please check the "
+                        .. conf.type .. " for this request"
+        return 401, { message = err_msg}
     end
     core.log.info("value: ", value)
 
diff --git a/t/plugin/consumer-restriction.t b/t/plugin/consumer-restriction.t
index 862e8695d..cc86aacdc 100644
--- a/t/plugin/consumer-restriction.t
+++ b/t/plugin/consumer-restriction.t
@@ -314,7 +314,7 @@ passed
 GET /hello
 --- error_code: 401
 --- response_body
-{"message":"Missing authentication or identity verification."}
+{"message":"The request is rejected, please check the consumer_name for this 
request"}
 
 
 
@@ -325,7 +325,7 @@ GET /hello
 Authorization: Basic amFjazIwMTk6MTIzNDU2
 --- error_code: 401
 --- response_body
-{"message":"Missing authentication or identity verification."}
+{"message":"The request is rejected, please check the consumer_name for this 
request"}
 
 
 
@@ -336,7 +336,7 @@ GET /hello
 Authorization: Basic amFjazIwMjA6MTIzNDU2
 --- error_code: 401
 --- response_body
-{"message":"Missing authentication or identity verification."}
+{"message":"The request is rejected, please check the consumer_name for this 
request"}
 
 
 
@@ -383,7 +383,7 @@ passed
 GET /hello
 --- error_code: 401
 --- response_body
-{"message":"Missing authentication or identity verification."}
+{"message":"The request is rejected, please check the consumer_name for this 
request"}
 
 
 
@@ -394,7 +394,7 @@ GET /hello
 Authorization: Basic amFjazIwMTk6MTIzNDU2
 --- error_code: 401
 --- response_body
-{"message":"Missing authentication or identity verification."}
+{"message":"The request is rejected, please check the consumer_name for this 
request"}
 
 
 
@@ -405,7 +405,7 @@ GET /hello
 Authorization: Basic amFjazIwMjA6MTIzNDU2
 --- error_code: 401
 --- response_body
-{"message":"Missing authentication or identity verification."}
+{"message":"The request is rejected, please check the consumer_name for this 
request"}
 
 
 
diff --git a/t/plugin/consumer-restriction2.t b/t/plugin/consumer-restriction2.t
index 6fdba1daa..febae314d 100644
--- a/t/plugin/consumer-restriction2.t
+++ b/t/plugin/consumer-restriction2.t
@@ -135,7 +135,37 @@ passed
 
 
 
-=== TEST 5: set whitelist
+=== TEST 5: consumer jack3 with no consumer group
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "jack3",
+                    "plugins": {
+                        "basic-auth": {
+                            "username": "jack2021",
+                            "password": "123456"
+                        }
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 6: set whitelist
 --- config
     location /t {
         content_by_lua_block {
@@ -175,7 +205,7 @@ passed
 
 
 
-=== TEST 6: verify unauthorized
+=== TEST 7: verify unauthorized
 --- request
 GET /hello
 --- error_code: 401
@@ -184,7 +214,7 @@ GET /hello
 
 
 
-=== TEST 7: verify jack1
+=== TEST 8: verify jack1
 --- request
 GET /hello
 --- more_headers
@@ -194,7 +224,7 @@ hello world
 
 
 
-=== TEST 8: verify jack2
+=== TEST 9: verify jack2
 --- request
 GET /hello
 --- more_headers
@@ -205,7 +235,7 @@ Authorization: Basic amFjazIwMjA6MTIzNDU2
 
 
 
-=== TEST 9: set blacklist
+=== TEST 10: set blacklist
 --- config
     location /t {
         content_by_lua_block {
@@ -246,7 +276,7 @@ passed
 
 
 
-=== TEST 10: verify unauthorized
+=== TEST 11: verify unauthorized
 --- request
 GET /hello
 --- error_code: 401
@@ -255,7 +285,7 @@ GET /hello
 
 
 
-=== TEST 11: verify jack1
+=== TEST 12: verify jack1
 --- request
 GET /hello
 --- more_headers
@@ -266,10 +296,119 @@ Authorization: Basic amFjazIwMTk6MTIzNDU2
 
 
 
-=== TEST 12: verify jack2
+=== TEST 13: verify jack2
 --- request
 GET /hello
 --- more_headers
 Authorization: Basic amFjazIwMjA6MTIzNDU2
 --- response_body
 hello world
+
+
+
+=== TEST 14: verify jack2
+--- request
+GET /hello
+--- more_headers
+Authorization: Basic amFjazIwMjE6MTIzNDU2
+--- error_code: 401
+--- response_body
+{"message":"The request is rejected, please check the consumer_group_id for 
this request"}
+
+
+
+=== TEST 15: set blacklist with service_id
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "uri": "/hello",
+                        "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                        },
+                        "plugins": {
+                            "consumer-restriction": {
+                                 "type": "service_id",
+                                 "blacklist": [
+                                     "1"
+                                 ],
+                                 "rejected_msg": "request is forbidden"
+                            }
+                        }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 16: hit
+--- request
+GET /hello
+--- error_code: 401
+--- response_body
+{"message":"The request is rejected, please check the service_id for this 
request"}
+
+
+
+=== TEST 17: set whitelist with service_id
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "uri": "/hello",
+                        "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                        },
+                        "plugins": {
+                            "consumer-restriction": {
+                                 "type": "service_id",
+                                 "whitelist": [
+                                     "1"
+                                 ],
+                                 "rejected_msg": "request is forbidden"
+                            }
+                        }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 18: hit
+--- request
+GET /hello
+--- error_code: 401
+--- response_body
+{"message":"The request is rejected, please check the service_id for this 
request"}

Reply via email to