The consumer-restriction makes corresponding access restrictions based on different objects selected.
| Name | Type | Requirement | Default | Valid | Description |
|---|---|---|---|---|---|
| type | string | optional | consumer_name | [“consumer_name”, “service_id”, “route_id”] | According to different objects, corresponding restrictions, support consumer_name, service_id, route_id. |
| whitelist | array[string] | required | Grant full access to all users specified in the provided list , has the priority over allowed_by_methods | ||
| blacklist | array[string] | required | Reject connection to all users specified in the provided list , has the priority over whitelist | ||
| rejected_code | integer | optional | 403 | [200,...] | The HTTP status code returned when the request is rejected. |
| allowed_by_methods | array[object] | optional | Set a list of allowed HTTP methods for the selected user , HTTP methods can be ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE"] |
For the type field is an enumerated type, it can be consumer_name or service_id. They stand for the following meanings:
username of consumer to a whitelist or blacklist (supporting single or multiple consumers) to restrict access to services or routes.id of the service to a whitelist or blacklist (supporting one or more services) to restrict access to the service. It needs to be used in conjunction with authorized plugins.consumer_nameThe following is an example. The consumer-restriction plugin is enabled on the specified route to restrict consumer access.
curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d ' { "username": "jack1", "plugins": { "basic-auth": { "username":"jack2019", "password": "123456" } } }' curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d ' { "username": "jack2", "plugins": { "basic-auth": { "username":"jack2020", "password": "123456" } } }' curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }, "plugins": { "basic-auth": {}, "consumer-restriction": { "whitelist": [ "jack1" ] } } }'
Test Plugin
Requests from jack1:
curl -u jack2019:123456 http://127.0.0.1:9080/index.html HTTP/1.1 200 OK ...
Requests from jack2:
curl -u jack2020:123456 http://127.0.0.1:9080/index.html -i HTTP/1.1 403 Forbidden ... {"message":"The consumer_name is forbidden."}
allowed_by_methodsThis example restrict the user jack1 to only POST on the resource
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }, "plugins": { "basic-auth": {}, "consumer-restriction": { "allowed_by_methods":[{ "user": "jack1", "methods": ["POST"] }] } } }'
Test Plugin
Requests from jack1:
curl -u jack2019:123456 http://127.0.0.1:9080/index.html HTTP/1.1 403 Forbidden ... {"message":"The consumer_name is forbidden."}
Add the capability for jack1 to get the resource
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }, "plugins": { "basic-auth": {}, "consumer-restriction": { "allowed_by_methods":[{ "user": "jack1", "methods": ["POST","GET"] }] } } }'
Requests from jack1:
curl -u jack2019:123456 http://127.0.0.1:9080/index.html HTTP/1.1 200 OK
service_idThe service_id method needs to be used together with the authorization plug-in. Here, the key-auth authorization plug-in is taken as an example.
curl http://127.0.0.1:9080/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "upstream": { "nodes": { "127.0.0.1:1980": 1 }, "type": "roundrobin" }, "desc": "new service 001" }' curl http://127.0.0.1:9080/apisix/admin/services/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "upstream": { "nodes": { "127.0.0.1:1980": 1 }, "type": "roundrobin" }, "desc": "new service 002" }'
consumer-restriction plugin on the consumer (need to cooperate with an authorized plugin to bind), and add the service_id whitelist list.curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "username": "new_consumer", "plugins": { "key-auth": { "key": "auth-jack" }, "consumer-restriction": { "type": "service_id", "whitelist": [ "1" ], "rejected_code": 403 } } }'
key-auth plugin on the route and bind the service_id to 1.curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }, "service_id": 1, "plugins": { "key-auth": { } } }'
Test Plugin
curl http://127.0.0.1:9080/index.html -H 'apikey: auth-jack' -i HTTP/1.1 200 OK ...
The service_id in the whitelist column allows access, and the plug-in configuration takes effect.
key-auth plugin on the route and bind the service_id to 2.curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }, "service_id": 2, "plugins": { "key-auth": { } } }'
Test Plugin
curl http://127.0.0.1:9080/index.html -H 'apikey: auth-jack' -i HTTP/1.1 403 Forbidden ... {"message":"The service_id is forbidden."}
It means that the service_id that is not in the whitelist is denied access, and the plug-in configuration takes effect.
When you want to disable the consumer-restriction plugin, it is very simple, you can delete the corresponding json configuration in the plugin configuration, no need to restart the service, it will take effect immediately:
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { "uri": "/index.html", "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }, "plugins": { "basic-auth": {} } }'
The consumer-restriction plugin has been disabled now. It works for other plugins.