This is an automated email from the ASF dual-hosted git repository.
wenming 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 1a24c2a refactor(consumer): remove id and use username only (#2679)
1a24c2a is described below
commit 1a24c2af39d9df8e8a41e0ee437cd79b628a214c
Author: 罗泽轩 <[email protected]>
AuthorDate: Wed Nov 18 17:44:40 2020 +0800
refactor(consumer): remove id and use username only (#2679)
Close #2656
Close #2653
Close #2651
---
apisix/admin/consumers.lua | 13 ++++---------
apisix/balancer/chash.lua | 2 +-
apisix/consumer.lua | 4 +++-
apisix/core/ctx.lua | 4 ++--
apisix/plugin.lua | 2 +-
apisix/plugins/basic-auth.lua | 10 +++++-----
apisix/plugins/consumer-restriction.lua | 2 +-
apisix/plugins/hmac-auth.lua | 10 +++++-----
apisix/plugins/jwt-auth.lua | 10 +++++-----
apisix/plugins/key-auth.lua | 10 +++++-----
apisix/plugins/limit-req.lua | 4 ++--
apisix/plugins/prometheus/exporter.lua | 12 ++++++------
apisix/plugins/wolf-rbac.lua | 8 ++++----
apisix/schema_def.lua | 1 -
doc/admin-api.md | 14 ++++++--------
doc/architecture-design.md | 8 ++++----
doc/plugins/consumer-restriction.md | 4 ++--
doc/zh-cn/admin-api.md | 7 +++----
doc/zh-cn/architecture-design.md | 8 ++++----
doc/zh-cn/plugins/consumer-restriction.md | 4 ++--
t/admin/consumers.t | 2 +-
t/core/ctx.t | 16 ++++++++--------
t/plugin/key-auth.t | 4 ++--
t/plugin/prometheus.t | 2 +-
24 files changed, 77 insertions(+), 84 deletions(-)
diff --git a/apisix/admin/consumers.lua b/apisix/admin/consumers.lua
index 354ed7e..feaa708 100644
--- a/apisix/admin/consumers.lua
+++ b/apisix/admin/consumers.lua
@@ -25,17 +25,12 @@ local _M = {
}
-local function check_conf(consumer_name, conf)
+local function check_conf(conf)
-- core.log.error(core.json.encode(conf))
if not conf then
return nil, {error_msg = "missing configurations"}
end
- local consumer_name = conf.username or consumer_name
- if not consumer_name then
- return nil, {error_msg = "missing consumer name"}
- end
-
core.log.info("schema: ", core.json.delay_encode(core.schema.consumer))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.consumer, conf)
@@ -65,12 +60,12 @@ local function check_conf(consumer_name, conf)
end
end
- return consumer_name
+ return conf.username
end
-function _M.put(consumer_name, conf)
- local consumer_name, err = check_conf(consumer_name, conf)
+function _M.put(_, conf)
+ local consumer_name, err = check_conf(conf)
if not consumer_name then
return 400, err
end
diff --git a/apisix/balancer/chash.lua b/apisix/balancer/chash.lua
index 38831cd..c8215a5 100644
--- a/apisix/balancer/chash.lua
+++ b/apisix/balancer/chash.lua
@@ -31,7 +31,7 @@ local function fetch_chash_hash_key(ctx, upstream)
local chash_key
if hash_on == "consumer" then
- chash_key = ctx.consumer_id
+ chash_key = ctx.consumer_name
elseif hash_on == "vars" then
chash_key = ctx.var[key]
elseif hash_on == "header" then
diff --git a/apisix/consumer.lua b/apisix/consumer.lua
index 35f0132..a631544 100644
--- a/apisix/consumer.lua
+++ b/apisix/consumer.lua
@@ -51,7 +51,9 @@ local function plugin_consumer()
end
local new_consumer = core.table.clone(consumer.value)
- new_consumer.consumer_id = new_consumer.id
+ -- Note: the id here is the key of consumer data, which
+ -- is 'username' field in admin
+ new_consumer.consumer_name = new_consumer.id
new_consumer.auth_conf = config
core.log.info("consumer:",
core.json.delay_encode(new_consumer))
core.table.insert(plugins[name].nodes, new_consumer)
diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index b9509f0..0f6f1b5 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -96,8 +96,8 @@ do
elseif key == "service_id" then
val = ngx.ctx.api_ctx and ngx.ctx.api_ctx.service_id
- elseif key == "consumer_name" or key == "consumer_id" then
- val = ngx.ctx.api_ctx and ngx.ctx.api_ctx.consumer_id
+ elseif key == "consumer_name" then
+ val = ngx.ctx.api_ctx and ngx.ctx.api_ctx.consumer_name
else
val = get_var(key, t._request)
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 85a8f5b..b5fe014 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -436,7 +436,7 @@ function _M.merge_consumer_route(route_conf, consumer_conf,
api_ctx)
api_ctx.conf_type = api_ctx.conf_type .. "&consumer"
api_ctx.conf_version = api_ctx.conf_version .. "&" ..
api_ctx.consumer_ver
- api_ctx.conf_id = api_ctx.conf_id .. "&" .. api_ctx.consumer_id
+ api_ctx.conf_id = api_ctx.conf_id .. "&" .. api_ctx.consumer_name
return new_conf, new_conf ~= route_conf
end
diff --git a/apisix/plugins/basic-auth.lua b/apisix/plugins/basic-auth.lua
index 9a55592..1171537 100644
--- a/apisix/plugins/basic-auth.lua
+++ b/apisix/plugins/basic-auth.lua
@@ -110,18 +110,18 @@ end
local create_consume_cache
do
- local consumer_ids = {}
+ local consumer_names = {}
function create_consume_cache(consumers)
- core.table.clear(consumer_ids)
+ core.table.clear(consumer_names)
for _, cur_consumer in ipairs(consumers.nodes) do
core.log.info("consumer node: ",
core.json.delay_encode(cur_consumer))
- consumer_ids[cur_consumer.auth_conf.username] = cur_consumer
+ consumer_names[cur_consumer.auth_conf.username] = cur_consumer
end
- return consumer_ids
+ return consumer_names
end
end
@@ -164,7 +164,7 @@ function _M.access(conf, ctx)
end
ctx.consumer = cur_consumer
- ctx.consumer_id = cur_consumer.consumer_id
+ ctx.consumer_name = cur_consumer.consumer_name
ctx.consumer_ver = consumer_conf.conf_version
core.log.info("hit basic-auth access")
diff --git a/apisix/plugins/consumer-restriction.lua
b/apisix/plugins/consumer-restriction.lua
index 68b1711..a6ee467 100644
--- a/apisix/plugins/consumer-restriction.lua
+++ b/apisix/plugins/consumer-restriction.lua
@@ -71,7 +71,7 @@ local fetch_val_funcs = {
return ctx.service_id
end,
["consumer_name"] = function(ctx)
- return ctx.consumer_id
+ return ctx.consumer_name
end
}
diff --git a/apisix/plugins/hmac-auth.lua b/apisix/plugins/hmac-auth.lua
index a4457ee..772c4ca 100644
--- a/apisix/plugins/hmac-auth.lua
+++ b/apisix/plugins/hmac-auth.lua
@@ -130,17 +130,17 @@ end
local create_consumer_cache
do
- local consumer_ids = {}
+ local consumer_names = {}
function create_consumer_cache(consumers)
- core.table.clear(consumer_ids)
+ core.table.clear(consumer_names)
for _, consumer in ipairs(consumers.nodes) do
core.log.info("consumer node: ", core.json.delay_encode(consumer))
- consumer_ids[consumer.auth_conf.access_key] = consumer
+ consumer_names[consumer.auth_conf.access_key] = consumer
end
- return consumer_ids
+ return consumer_names
end
end -- do
@@ -404,7 +404,7 @@ function _M.rewrite(conf, ctx)
local consumer_conf = consumer.plugin(plugin_name)
ctx.consumer = validated_consumer
- ctx.consumer_id = validated_consumer.consumer_id
+ ctx.consumer_name = validated_consumer.consumer_name
ctx.consumer_ver = consumer_conf.conf_version
core.log.info("hit hmac-auth rewrite")
end
diff --git a/apisix/plugins/jwt-auth.lua b/apisix/plugins/jwt-auth.lua
index df1998c..9ba6932 100644
--- a/apisix/plugins/jwt-auth.lua
+++ b/apisix/plugins/jwt-auth.lua
@@ -75,17 +75,17 @@ local _M = {
local create_consume_cache
do
- local consumer_ids = {}
+ local consumer_names = {}
function create_consume_cache(consumers)
- core.table.clear(consumer_ids)
+ core.table.clear(consumer_names)
for _, consumer in ipairs(consumers.nodes) do
core.log.info("consumer node: ", core.json.delay_encode(consumer))
- consumer_ids[consumer.auth_conf.key] = consumer
+ consumer_names[consumer.auth_conf.key] = consumer
end
- return consumer_ids
+ return consumer_names
end
end -- do
@@ -265,7 +265,7 @@ function _M.rewrite(conf, ctx)
end
ctx.consumer = consumer
- ctx.consumer_id = consumer.consumer_id
+ ctx.consumer_name = consumer.consumer_name
ctx.consumer_ver = consumer_conf.conf_version
core.log.info("hit jwt-auth rewrite")
end
diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua
index 0f713f0..10b59e7 100644
--- a/apisix/plugins/key-auth.lua
+++ b/apisix/plugins/key-auth.lua
@@ -52,17 +52,17 @@ local _M = {
local create_consume_cache
do
- local consumer_ids = {}
+ local consumer_names = {}
function create_consume_cache(consumers)
- core.table.clear(consumer_ids)
+ core.table.clear(consumer_names)
for _, consumer in ipairs(consumers.nodes) do
core.log.info("consumer node: ", core.json.delay_encode(consumer))
- consumer_ids[consumer.auth_conf.key] = consumer
+ consumer_names[consumer.auth_conf.key] = consumer
end
- return consumer_ids
+ return consumer_names
end
end -- do
@@ -98,7 +98,7 @@ function _M.rewrite(conf, ctx)
core.log.info("consumer: ", core.json.delay_encode(consumer))
ctx.consumer = consumer
- ctx.consumer_id = consumer.consumer_id
+ ctx.consumer_name = consumer.consumer_name
ctx.consumer_ver = consumer_conf.conf_version
core.log.info("hit key-auth rewrite")
end
diff --git a/apisix/plugins/limit-req.lua b/apisix/plugins/limit-req.lua
index dbbd5d2..7a55d86 100644
--- a/apisix/plugins/limit-req.lua
+++ b/apisix/plugins/limit-req.lua
@@ -73,11 +73,11 @@ function _M.access(conf, ctx)
local key
if conf.key == "consumer_name" then
- if not ctx.consumer_id then
+ if not ctx.consumer_name then
core.log.error("consumer not found.")
return 500, { message = "Consumer not found."}
end
- key = ctx.consumer_id .. ctx.conf_type .. ctx.conf_version
+ key = ctx.consumer_name .. ctx.conf_type .. ctx.conf_version
else
key = (ctx.var[conf.key] or "") .. ctx.conf_type .. ctx.conf_version
diff --git a/apisix/plugins/prometheus/exporter.lua
b/apisix/plugins/prometheus/exporter.lua
index 78c2af6..3519199 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -126,7 +126,7 @@ function _M.log(conf, ctx)
local route_id = ""
local balancer_ip = ctx.balancer_ip or ""
local service_id
- local consumer_id = ctx.consumer_id or ""
+ local consumer_name = ctx.consumer_name or ""
local matched_route = ctx.matched_route and ctx.matched_route.value
if matched_route then
@@ -145,24 +145,24 @@ function _M.log(conf, ctx)
metrics.status:inc(1,
gen_arr(vars.status, route_id, matched_uri, matched_host,
- service_id, consumer_id, balancer_ip))
+ service_id, consumer_name, balancer_ip))
local latency = (ngx.now() - ngx.req.start_time()) * 1000
metrics.latency:observe(latency,
- gen_arr("request", service_id, consumer_id, balancer_ip))
+ gen_arr("request", service_id, consumer_name, balancer_ip))
local overhead = latency
if ctx.var.upstream_response_time then
overhead = overhead - tonumber(ctx.var.upstream_response_time) * 1000
end
metrics.overhead:observe(overhead,
- gen_arr("request", service_id, consumer_id, balancer_ip))
+ gen_arr("request", service_id, consumer_name, balancer_ip))
metrics.bandwidth:inc(vars.request_length,
- gen_arr("ingress", route_id, service_id, consumer_id, balancer_ip))
+ gen_arr("ingress", route_id, service_id, consumer_name, balancer_ip))
metrics.bandwidth:inc(vars.bytes_sent,
- gen_arr("egress", route_id, service_id, consumer_id, balancer_ip))
+ gen_arr("egress", route_id, service_id, consumer_name, balancer_ip))
end
diff --git a/apisix/plugins/wolf-rbac.lua b/apisix/plugins/wolf-rbac.lua
index 993e7e4..4966601 100644
--- a/apisix/plugins/wolf-rbac.lua
+++ b/apisix/plugins/wolf-rbac.lua
@@ -66,17 +66,17 @@ local _M = {
local create_consume_cache
do
- local consumer_ids = {}
+ local consumer_names = {}
function create_consume_cache(consumers)
- core.table.clear(consumer_ids)
+ core.table.clear(consumer_names)
for _, consumer in ipairs(consumers.nodes) do
core.log.info("consumer node: ", core.json.delay_encode(consumer))
- consumer_ids[consumer.auth_conf.appid] = consumer
+ consumer_names[consumer.auth_conf.appid] = consumer
end
- return consumer_ids
+ return consumer_names
end
end -- do
diff --git a/apisix/schema_def.lua b/apisix/schema_def.lua
index c904ac5..df24ace 100644
--- a/apisix/schema_def.lua
+++ b/apisix/schema_def.lua
@@ -544,7 +544,6 @@ _M.service = {
_M.consumer = {
type = "object",
properties = {
- id = id_schema,
username = {
type = "string", minLength = 1, maxLength = 32,
pattern = [[^[a-zA-Z0-9_]+$]]
diff --git a/doc/admin-api.md b/doc/admin-api.md
index 6f8b146..22faa50 100644
--- a/doc/admin-api.md
+++ b/doc/admin-api.md
@@ -417,7 +417,7 @@ Return response from etcd currently.
## Consumer
-*API*:/apisix/admin/consumers/{id}
+*API*:/apisix/admin/consumers/{username}
*Description*:Consumers are consumers of certain types of services and can
only be used in conjunction with a user authentication system. Consumer regards
the `username` property as the identity, so only the HTTP `PUT` method is
supported for creating a new consumer.
@@ -425,9 +425,9 @@ Return response from etcd currently.
|Method |Request URI|Request Body|Description |
|---------|-------------------------|--|------|
-|GET |/apisix/admin/consumers/{id}|NULL|Fetch resource|
-|PUT |/apisix/admin/consumers/{id}|{...}|Create resource by ID|
-|DELETE |/apisix/admin/consumers/{id}|NULL|Remove resource|
+|GET |/apisix/admin/consumers/{username}|NULL|Fetch resource|
+|PUT |/apisix/admin/consumers|{...}|Create resource by username|
+|DELETE |/apisix/admin/consumers/{username}|NULL|Remove resource|
> Request Body Parameters:
@@ -444,7 +444,6 @@ Config Example:
```shell
{
- "id": "1", # id
"plugins": {}, # Bound plugin
"username": "name", # Consumer name
"desc": "hello world", # Consumer desc
@@ -456,9 +455,8 @@ The binding authentication and authorization plug-in is a
bit special. When it n
Example:
```shell
-$ curl http://127.0.0.1:9080/apisix/admin/consumers/2 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+$ curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
- "username": "jack",
"plugins": {
"key-auth": {
"key": "auth-one"
@@ -511,7 +509,7 @@ In addition to the basic complex equalization algorithm
selection, APISIX's Upst
|nodes |required if `k8s_deployment_info` not configured|Hash table,
the key of the internal element is the upstream machine address list, the
format is `Address + Port`, where the address part can be IP or domain name,
such as `192.168.1.100:80`, `foo.com:80`, etc. Value is the weight of the node.
In particular, when the weight value is `0`, it has a special meaning, which
usually means that the upstream node is invalid and never wants to be selected.|
|k8s_deployment_info|required if `nodes` not configured|fields:
`namespace`、`deploy_name`、`service_name`、`port`、`backend_type`, `port` is
number, `backend_type` is `pod` or `service`, others is string. |
|hash_on |optional|This option is only valid if the `type` is `chash`.
Supported types `vars`(Nginx variables), `header`(custom header), `cookie`,
`consumer`, the default value is `vars`.|
-|key |optional|This option is only valid if the `type` is `chash`.
Find the corresponding node `id` according to `hash_on` and `key`. When
`hash_on` is set as `vars`, `key` is the required parameter, for now, it
support nginx built-in variables like `uri, server_name, server_addr,
request_uri, remote_port, remote_addr, query_string, host, hostname, arg_***`,
`arg_***` is arguments in the request line, [Nginx variables
list](http://nginx.org/en/docs/varindex.html). When `hash_ [...]
+|key |optional|This option is only valid if the `type` is `chash`.
Find the corresponding node `id` according to `hash_on` and `key`. When
`hash_on` is set as `vars`, `key` is the required parameter, for now, it
support nginx built-in variables like `uri, server_name, server_addr,
request_uri, remote_port, remote_addr, query_string, host, hostname, arg_***`,
`arg_***` is arguments in the request line, [Nginx variables
list](http://nginx.org/en/docs/varindex.html). When `hash_ [...]
|checks |optional|Configure the parameters of the health check. For
details, refer to [health-check](health-check.md).|
|retries |optional|Pass the request to the next upstream using the
underlying Nginx retry mechanism, the retry mechanism is enabled by default and
set the number of retries according to the number of backend nodes. If
`retries` option is explicitly set, it will override the default value. `0`
means disable retry mechanism.|
|timeout|optional| Set the timeout for connection, sending and receiving
messages. |
diff --git a/doc/architecture-design.md b/doc/architecture-design.md
index 4a6ef9b..1a24391 100644
--- a/doc/architecture-design.md
+++ b/doc/architecture-design.md
@@ -269,7 +269,7 @@ In addition to the basic complex equalization algorithm
selection, APISIX's Upst
|service_name |required if `nodes` and `k8s_deployment_info` not configured
|The name of the upstream service and used with the registry, refer to
[Integration service discovery registry](discovery.md).|
|k8s_deployment_info |required if `nodes` and `service_name` not
configured|fields:
`namespace`、`deploy_name`、`service_name`、`port`、`backend_type`, `port` is
number, `backend_type` is `pod` or `service`, others is string. |
|hash_on |optional|This option is only valid if the `type` is `chash`.
Supported types `vars`(Nginx variables), `header`(custom header), `cookie`,
`consumer`, the default value is `vars`.|
-|key |required|This option is only valid if the `type` is `chash`.
Find the corresponding node `id` according to `hash_on` and `key`. When
`hash_on` is set as `vars`, `key` is the required parameter, for now, it
support nginx built-in variables like `uri, server_name, server_addr,
request_uri, remote_port, remote_addr, query_string, host, hostname, arg_***`,
`arg_***` is arguments in the request line, [Nginx variables
list](http://nginx.org/en/docs/varindex.html). When `hash_ [...]
+|key |required|This option is only valid if the `type` is `chash`.
Find the corresponding node `id` according to `hash_on` and `key`. When
`hash_on` is set as `vars`, `key` is the required parameter, for now, it
support nginx built-in variables like `uri, server_name, server_addr,
request_uri, remote_port, remote_addr, query_string, host, hostname, arg_***`,
`arg_***` is arguments in the request line, [Nginx variables
list](http://nginx.org/en/docs/varindex.html). When `hash_ [...]
|checks |optional|Configure the parameters of the health check. For
details, refer to [health-check](health-check.md).|
|retries |optional|Pass the request to the next upstream using the
underlying Nginx retry mechanism, the retry mechanism is enabled by default and
set the number of retries according to the number of backend nodes. If
`retries` option is explicitly set, it will override the default value.|
|timeout|optional| Set the timeout for connection, sending and receiving
messages. |
@@ -415,7 +415,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H
'X-API-KEY: edd1c9f034335f13
}'
```
-Test request, the `consumer_id` after authentication is passed will be used as
the hash value of the load balancing hash algorithm:
+Test request, the `consumer_name` after authentication is passed will be used
as the hash value of the load balancing hash algorithm:
```shell
curl http://127.0.0.1:9080/server_port -H "apikey: auth-jack"
@@ -512,7 +512,7 @@ In APISIX, the process of identifying a Consumer is as
follows:
<img src="./images/consumer-internal.png" width="50%" height="50%">
1. Authorization certification: e.g [key-auth](./plugins/key-auth.md),
[JWT](./plugins/jwt-auth.md), etc.
-2. Get consumer_id: By authorization, you can naturally get the corresponding
Consumer `id`, which is the unique identifier of the Consumer object.
+2. Get consumer_name: By authorization, you can naturally get the
corresponding Consumer `id`, which is the unique identifier of the Consumer
object.
3. Get the Plugin or Upstream information bound to the Consumer: Complete the
different configurations for different Consumers.
To sum up, Consumer is a consumer of certain types of services and needs to be
used in conjunction with the user authentication system.
@@ -525,7 +525,7 @@ How to enable a specific plugin for a Consumer, you can see
the following exampl
```shell
# Create a Consumer, specify the authentication plugin key-auth, and enable
the specific plugin limit-count
-$ curl http://127.0.0.1:9080/apisix/admin/consumers/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+$ curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"username": "jack",
"plugins": {
diff --git a/doc/plugins/consumer-restriction.md
b/doc/plugins/consumer-restriction.md
index 590917d..4cc3152 100644
--- a/doc/plugins/consumer-restriction.md
+++ b/doc/plugins/consumer-restriction.md
@@ -52,7 +52,7 @@ For the `type` field is an enumerated type, it can be
`consumer_name` or `servic
The following is an example. The `consumer-restriction` plugin is enabled on
the specified route to restrict consumer access.
```shell
-curl http://127.0.0.1:9080/apisix/admin/consumers/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"username": "jack1",
"plugins": {
@@ -63,7 +63,7 @@ curl http://127.0.0.1:9080/apisix/admin/consumers/1 -H
'X-API-KEY: edd1c9f034335
}
}'
-curl http://127.0.0.1:9080/apisix/admin/consumers/2 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"username": "jack2",
"plugins": {
diff --git a/doc/zh-cn/admin-api.md b/doc/zh-cn/admin-api.md
index a8bb27a..a126e1c 100644
--- a/doc/zh-cn/admin-api.md
+++ b/doc/zh-cn/admin-api.md
@@ -430,7 +430,7 @@ HTTP/1.1 200 OK
## Consumer
-*地址*:/apisix/admin/consumers/{id}
+*地址*:/apisix/admin/consumers/{username}
*说明*:Consumer 是某类服务的消费者,需与用户认证体系配合才能使用。Consumer 使用 `username` 作为唯一标识,只支持使用
HTTP `PUT` 方法创建 Consumer。
@@ -439,7 +439,7 @@ HTTP/1.1 200 OK
|名字 |请求 uri|请求 body|说明 |
|---------|-------------------------|--|------|
|GET |/apisix/admin/consumers/{id}|无|获取资源|
-|PUT |/apisix/admin/consumers/{id}|{...}|根据 id 创建资源|
+|PUT |/apisix/admin/consumers|{...}|创建资源|
|DELETE |/apisix/admin/consumers/{id}|无|删除资源|
> body 请求参数:
@@ -457,7 +457,6 @@ consumer 对象 json 配置内容:
```shell
{
- "id": "1", # id
"plugins": {}, # 指定 consumer 绑定的插件
"username": "name", # 必填
"desc": "hello world", # consumer 描述
@@ -470,7 +469,7 @@ consumer 对象 json 配置内容:
```shell
# 创建 Consumer ,指定认证插件 key-auth ,并开启特定插件 limit-count
-$ curl http://127.0.0.1:9080/apisix/admin/consumers/2 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+$ curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"username": "jack",
"plugins": {
diff --git a/doc/zh-cn/architecture-design.md b/doc/zh-cn/architecture-design.md
index 1f4a553..23ff790 100644
--- a/doc/zh-cn/architecture-design.md
+++ b/doc/zh-cn/architecture-design.md
@@ -291,7 +291,7 @@ APISIX 的 Upstream 除了基本的复杂均衡算法选择外,还支持对上
1. 设为 `vars` 时,`key` 为必传参数,目前支持的 Nginx 内置变量有 `uri, server_name, server_addr,
request_uri, remote_port, remote_addr, query_string, host, hostname,
arg_***`,其中 `arg_***` 是来自URL的请求参数,[Nginx
变量列表](http://nginx.org/en/docs/varindex.html)
1. 设为 `header` 时, `key` 为必传参数,其值为自定义的 header name, 即 "http_`key`"
1. 设为 `cookie` 时, `key` 为必传参数,其值为自定义的 cookie name,即 "cookie_`key`"
-1. 设为 `consumer` 时,`key` 不需要设置。此时哈希算法采用的 `key` 为认证通过的 `consumer_id`。
+1. 设为 `consumer` 时,`key` 不需要设置。此时哈希算法采用的 `key` 为认证通过的 `consumer_name`。
1. 如果指定的 `hash_on` 和 `key` 获取不到值时,就是用默认值:`remote_addr`。
创建上游对象用例:
@@ -431,7 +431,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H
'X-API-KEY: edd1c9f034335f13
}'
```
-测试请求,认证通过后的`consumer_id`将作为负载均衡哈希算法的哈希值:
+测试请求,认证通过后的`consumer_name`将作为负载均衡哈希算法的哈希值:
```shell
curl http://127.0.0.1:9080/server_port -H "apikey: auth-jack"
@@ -529,7 +529,7 @@ APISIX 区别于其他 API 网关的一大特点是允许用户选择不同 Rout
<img src="../images/consumer-internal.png" width="50%" height="50%">
1. 授权认证:比如有 [key-auth](../plugins/key-auth.md)、[JWT](plugins/jwt-auth.md) 等。
-2. 获取 consumer_id:通过授权认证,即可自然获取到对应的 Consumer `id`,它是 Consumer 对象的唯一识别标识。
+2. 获取 consumer_name:通过授权认证,即可自然获取到对应的 Consumer name,它是 Consumer 对象的唯一识别标识。
3. 获取 Consumer 上绑定的 Plugin 或 Upstream 信息:完成对不同 Consumer 做不同配置的效果。
概括一下,Consumer 是某类服务的消费者,需与用户认证体系配合才能使用。
@@ -541,7 +541,7 @@ APISIX 区别于其他 API 网关的一大特点是允许用户选择不同 Rout
```shell
# 创建 Consumer ,指定认证插件 key-auth ,并开启特定插件 limit-count
-$ curl http://127.0.0.1:9080/apisix/admin/consumers/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+$ curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"username": "jack",
"plugins": {
diff --git a/doc/zh-cn/plugins/consumer-restriction.md
b/doc/zh-cn/plugins/consumer-restriction.md
index 5283de1..06b31b2 100644
--- a/doc/zh-cn/plugins/consumer-restriction.md
+++ b/doc/zh-cn/plugins/consumer-restriction.md
@@ -51,7 +51,7 @@
下面是一个示例,在指定的 route 上开启了 `consumer-restriction` 插件,限制 consumer 访问:
```shell
-curl http://127.0.0.1:9080/apisix/admin/consumers/1 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"username": "jack1",
"plugins": {
@@ -62,7 +62,7 @@ curl http://127.0.0.1:9080/apisix/admin/consumers/1 -H
'X-API-KEY: edd1c9f034335
}
}'
-curl http://127.0.0.1:9080/apisix/admin/consumers/2 -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
+curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"username": "jack2",
"plugins": {
diff --git a/t/admin/consumers.t b/t/admin/consumers.t
index 2f012e3..9ba284b 100644
--- a/t/admin/consumers.t
+++ b/t/admin/consumers.t
@@ -234,7 +234,7 @@ GET /t
GET /t
--- error_code: 400
--- response_body
-{"error_msg":"missing consumer name"}
+{"error_msg":"invalid configuration: property \"username\" is required"}
--- no_error_log
[error]
diff --git a/t/core/ctx.t b/t/core/ctx.t
index 5b04290..5c04829 100644
--- a/t/core/ctx.t
+++ b/t/core/ctx.t
@@ -484,7 +484,7 @@ consumer_name: consumer_name is nil
-=== TEST 19: create route and consumer_id is consumer_jack
+=== TEST 19: create route and consumer_name is consumer_jack
--- config
location /t {
content_by_lua_block {
@@ -504,7 +504,7 @@ consumer_name: consumer_name is nil
"key-auth": {},
"serverless-pre-function": {
"phase": "access",
- "functions" : ["return function()
ngx.log(ngx.INFO, \"consumer_id: \", ngx.ctx.api_ctx.var.consumer_id) end"]
+ "functions" : ["return function()
ngx.log(ngx.INFO, \"consumer_name: \", ngx.ctx.api_ctx.var.consumer_name) end"]
}
}
}]]
@@ -525,7 +525,7 @@ passed
-=== TEST 20: consumer_id is `consumer_jack`
+=== TEST 20: consumer_name is `consumer_jack`
--- request
GET /hello
--- more_headers
@@ -533,13 +533,13 @@ apikey: auth-jack
--- response_body
hello world
--- error_log
-consumer_id: consumer_jack
+consumer_name: consumer_jack
--- no_error_log
[error]
-=== TEST 21: update the route, and the consumer_id is nil
+=== TEST 21: update the route, and the consumer_name is nil
--- config
location /t {
content_by_lua_block {
@@ -558,7 +558,7 @@ consumer_id: consumer_jack
"plugins": {
"serverless-pre-function": {
"phase": "access",
- "functions" : ["return function()
ngx.log(ngx.INFO, \"consumer_id: \", ngx.ctx.api_ctx.var.consumer_id or
'consumer_id is nil') end"]
+ "functions" : ["return function()
ngx.log(ngx.INFO, \"consumer_name: \", ngx.ctx.api_ctx.var.consumer_name or
'consumer_name is nil') end"]
}
}
}]]
@@ -579,12 +579,12 @@ passed
-=== TEST 22: consumer_id is nil
+=== TEST 22: consumer_name is nil
--- request
GET /hello
--- response_body
hello world
--- error_log
-consumer_id: consumer_id is nil
+consumer_name: consumer_name is nil
--- no_error_log
[error]
diff --git a/t/plugin/key-auth.t b/t/plugin/key-auth.t
index 767c625..01303cf 100644
--- a/t/plugin/key-auth.t
+++ b/t/plugin/key-auth.t
@@ -75,7 +75,7 @@ done
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
- local code, body = t('/apisix/admin/consumers/1',
+ local code, body = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
[[{
"username": "jack",
@@ -196,7 +196,7 @@ GET /hello
for i = 1, 20 do
username = "user_" .. tostring(i)
key = "auth-" .. tostring(i)
- code, body = t(string.format('/apisix/admin/consumers/%d',
tostring(i)),
+ code, body = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
string.format('{"username":"%s","plugins":{"key-auth":{"key":"%s"}}}',
username, key),
string.format('{"node":{"value":{"username":"%s","plugins":{"key-auth":{"key":"%s"}}}},"action":"set"}',
username, key)
diff --git a/t/plugin/prometheus.t b/t/plugin/prometheus.t
index 3bb73e1..e9f0e0f 100644
--- a/t/plugin/prometheus.t
+++ b/t/plugin/prometheus.t
@@ -874,7 +874,7 @@
qr/apisix_bandwidth\{type="egress",route="1",service="",consumer="",node="127.0.
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
- local code, body = t('/apisix/admin/consumers/1',
+ local code, body = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
[[{
"username": "jack",