consumer-restriction
Allows or denies requests by consumer, group, route or service.
consumer-restrictionPriority 2400What it does
consumer-restriction lets a request through or refuses it by who is calling. It reads the consumer that a plugin on the same route, usually key-auth, has named, by its username or by the ID of its consumer group. Whitelist lists who may pass, Blacklist who may not.
A refused consumer is answered 403 with {"message":"The consumer_name is forbidden."}, or with your Rejected message, even though its key is valid. Without a plugin on the route that names the consumer, every request is answered 401.
When to use it
- Only some clients may call a route, the mobile app for example, while every other consumer is refused even with a valid key.
- A team whose consumers change: gather them in a consumer group and let the group through, so a consumer that joins it later needs no change on the route.
- Keeping one named consumer away from a route every other consumer may use, under Blacklist.
- Limiting a consumer to some methods, reads only for example, under Allowed by methods.
Not for allowing or refusing consumers by a label they carry, their team for example — see acl (Allows or denies consumers by the labels they carry) or allowing or refusing clients by their address — see ip-restriction (Allows or denies clients by IP address or CIDR range).
Guides
Pages that walk through a job with consumer-restriction, step by step.
- Allow only named clientsLet only the consumers you list call a route, by name or by consumer group.
Set it up
Let only the mobile app's consumer call the route
Needs a plugin on the same route that names the consumer, such as key-auth.
Leave Type at consumer_name and fill Whitelist as in the JSON below.
{
"type": "consumer_name",
"whitelist": [
"mobile-app"
]
}
What you will see
- A request with the key of mobile-app passes and reaches your backend, which receives
X-Consumer-Username: mobile-app. - A request with the key of partner-portal, a consumer the list does not name, is answered
403with{"message":"The consumer_name is forbidden."}, though its key is valid. - A request without a key is answered
401with{"message":"Missing API key in request"}by key-auth, before consumer-restriction reads anything. - On a route with no plugin that names the consumer, every request is answered
401with{"message":"The request is rejected, please check the consumer_name for this request"}, one with the key of mobile-app included.
More scenarios
Let several consumers through and answer the others with your own message
Needs a plugin on the same route that names the consumer, such as key-auth.
Leave Type at consumer_name, fill Whitelist as in the JSON below and set Rejected message to This API key is not allowed on this route..
{
"type": "consumer_name",
"whitelist": [
"mobile-app",
"partner-portal"
],
"rejected_msg": "This API key is not allowed on this route."
}
What you will see
- Each consumer the list names passes with its own key: requests with the keys of mobile-app and partner-portal are answered
200. - Any other consumer, here payments-app, is answered
403with{"message":"This API key is not allowed on this route."}, the message this example sets.
Let only the consumers of one consumer group call the route
Needs a plugin on the same route that names the consumer, such as key-auth; the list holds the group's ID, mobile-team.
Set Type to consumer_group_id and fill Whitelist as in the JSON below.
{
"type": "consumer_group_id",
"whitelist": [
"mobile-team"
]
}
What you will see
- A consumer in the group, here mobile-app, passes with its key and reaches your backend, which receives
X-Consumer-Username: mobile-app. - A consumer in another group, here payments-app, is answered
403with{"message":"The consumer_group_id is forbidden."}. - A consumer in no group at all, here partner-portal, is answered
401with{"message":"The request is rejected, please check the consumer_group_id for this request"}, though its key is valid. - The list holds group IDs, not names: a consumer whose group carries the name mobile-team under another ID is answered
403.
Things to know
- Type — With no plugin on the route that names a consumer, such as key-auth, there is no consumer name, and every request is refused with 401. The value Type names has to be on the request.
- Type — With
consumer_group_id, a consumer in no group at all is answered401, not403, with{"message":"The request is rejected, please check the consumer_group_id for this request"}. - Whitelist — With Type
consumer_group_idthe list holds group IDs, and a group's name is not read: a consumer whose group is namedmobile-teamunder another ID is refused. - Whitelist — The three lists can be used together. The deny list is read first, and a name on the allow list skips the method limits.
- Rejected message — Sent as the
messageof the JSON body in place ofThe consumer_name is forbidden., with the Rejected code. - Rejected code — Sent exactly as written: 499 closes the connection without an answer, and 200 makes a refusal look like a success.
- AdviceAllow by name for one or two clients. For a team whose members change, allow its consumer group instead, so the route stays as it is when a consumer joins or leaves.
Settings
The fields as the console shows them, section by section.
typestringdefault consumer_namewhitelistarray of stringsRequired unless Blacklist or Allowed by methods is setNames let through, as Type reads them (consumer names by default), such as alice. Every other name is refused.
blacklistarray of stringsRequired unless Whitelist or Allowed by methods is setNames refused, as Type reads them (consumer names by default), such as mallory.
allowed_by_methodsarray of objectsRequired unless Blacklist or Whitelist is setLimit a name to some methods: a row names it and lists the methods it may use. Names without a row are not limited.
allowed_by_methods[].methodsarray of stringsRequiredallowed_by_methods[].userstringThe name this row limits, as Type reads it. A name no row lists is not limited.
rejected_codeintegerdefault 403The status sent on a refusal, such as 403.
rejected_msgstring