This is an automated email from the ASF dual-hosted git repository.
moonming 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 d817af6f6a fix(admin)!: ignore empty Admin API keys and accept only
the admin and viewer roles (#14203)
d817af6f6a is described below
commit d817af6f6a30b65c5c00e3e7892debd70c89e44b
Author: Ming Wen <[email protected]>
AuthorDate: Wed Nov 18 17:54:10 2026 +0800
fix(admin)!: ignore empty Admin API keys and accept only the admin and
viewer roles (#14203)
---
apisix/admin/init.lua | 3 +-
apisix/cli/ops.lua | 28 +++++++-
apisix/cli/schema.lua | 39 ++++++++---
docs/en/latest/admin-api.md | 2 +
docs/zh/latest/admin-api.md | 2 +
t/admin/token-empty-key-and-role.t | 135 +++++++++++++++++++++++++++++++++++++
t/cli/test_admin.sh | 106 ++++++++++++++++++++++++++++-
7 files changed, 300 insertions(+), 15 deletions(-)
diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua
index e8f8266520..1c52837cb9 100644
--- a/apisix/admin/init.lua
+++ b/apisix/admin/init.lua
@@ -96,7 +96,8 @@ local function check_token(ctx)
local req_token = ctx.var.arg_api_key or ctx.var.http_x_api_key
or ctx.var.cookie_x_api_key
- if not req_token then
+ -- an empty token, such as `?api_key=` or an empty X-API-KEY header, is no
token
+ if not req_token or req_token == "" then
return false, "missing apikey"
end
diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index 77228d8235..12d20e7e16 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -275,6 +275,15 @@ local function get_lua_path(conf)
end
+-- the name of an entry of deployment.admin.admin_key, for messages
+local function admin_key_name(admin, index)
+ if type(admin.name) == "string" and admin.name ~= "" then
+ return admin.name
+ end
+ return "#" .. index
+end
+
+
local function init(env)
if env.is_root_path then
print('Warning! Running apisix under /root is only suitable for '
@@ -342,6 +351,22 @@ local function init(env)
.. "please enable `admin_key_required` and set a secure admin
key!")
end
+ -- report empty keys of the viewer role, which never match a request
+ local admin_conf = yaml_conf.deployment.admin
+ if yaml_conf.apisix.enable_admin and admin_conf
+ and admin_conf.admin_key_required ~= false
+ and type(admin_conf.admin_key) == "table" then
+ for i, admin in ipairs(admin_conf.admin_key) do
+ -- only the key of the admin role is generated, and an empty key
+ -- never matches a request
+ if admin.role ~= "admin" and (admin.key == nil or admin.key == ""
+ or type(admin.key) == "table") then
+ stderr:write("WARNING: Admin API key ", admin_key_name(admin,
i),
+ " is empty, so it does not match any request.\n")
+ end
+ end
+ end
+
if yaml_conf.apisix.enable_admin and not checked_admin_key then
local help = [[
@@ -359,6 +384,7 @@ Please modify "admin_key" in conf/config.yaml .
util.die(help:format("ERROR: missing valid Admin API token."))
end
+ -- the schema has checked entries when the key check is enabled
for _, admin in ipairs(admin_key) do
if type(admin.key) == "table" then
admin.key = ""
@@ -366,7 +392,7 @@ Please modify "admin_key" in conf/config.yaml .
admin.key = tostring(admin.key)
end
- if admin.key == "" then
+ if admin.key == "" and admin.role == "admin" then
stderr:write(
help:format([[WARNING: using empty Admin API.
This will trigger APISIX to automatically generate a
random Admin API token.]]),
diff --git a/apisix/cli/schema.lua b/apisix/cli/schema.lua
index 84c2fba248..1140e9409f 100644
--- a/apisix/cli/schema.lua
+++ b/apisix/cli/schema.lua
@@ -16,6 +16,7 @@
--
local jsonschema = require("jsonschema")
+local yaml = require("lyaml")
local pairs = pairs
local pcall = pcall
local require = require
@@ -384,15 +385,6 @@ local admin_schema = {
properties = {
admin_key = {
type = "array",
- properties = {
- items = {
- properties = {
- name = {type = "string"},
- key = {type = "string"},
- role = {type = "string"},
- }
- }
- }
},
admin_listen = {
properties = {
@@ -410,7 +402,30 @@ local admin_schema = {
admin_key_required = {
type = "boolean",
},
- }
+ },
+ -- keep entry checks disabled when admin_key_required is false
+ anyOf = {
+ {
+ properties = {
+ admin_key_required = {enum = {false}},
+ },
+ required = {"admin_key_required"},
+ },
+ {
+ properties = {
+ admin_key = {
+ items = {
+ type = "object",
+ properties = {
+ key = {type = {"string", "null"}},
+ role = {enum = {"admin", "viewer"}},
+ },
+ required = {"role"},
+ },
+ },
+ },
+ },
+ },
}
local deployment_schema = {
@@ -482,7 +497,9 @@ function _M.validate(yaml_conf)
end
local role = yaml_conf.deployment.role
- local validator = jsonschema.generate_validator(deployment_schema[role])
+ local validator = jsonschema.generate_validator(deployment_schema[role], {
+ null = yaml.null,
+ })
local ok, err = validator(yaml_conf.deployment)
if not ok then
return false, "invalid deployment " .. role .. " configuration: " ..
err
diff --git a/docs/en/latest/admin-api.md b/docs/en/latest/admin-api.md
index 9d0e8e81d9..d3afeda3be 100644
--- a/docs/en/latest/admin-api.md
+++ b/docs/en/latest/admin-api.md
@@ -63,6 +63,8 @@ deployment:
port: 9180 # Specific port, which must be
different from node_listen's port.
```
+The `role` of each key is `admin`, which can read and change the
configuration, or `viewer`, which can only read it. Unless `admin_key_required`
is `false`, which turns off the key check, configuration loading refuses any
other value and a missing one. When key checks are enabled, each entry must be
a map, and its key must be a string or `null` if present. An empty, missing or
`null` key matches no request. APISIX generates a key when it starts only for a
key of the `admin` role that is [...]
+
### Using environment variables
To configure via environment variables, you can use the `${{VAR}}` syntax. For
instance:
diff --git a/docs/zh/latest/admin-api.md b/docs/zh/latest/admin-api.md
index 7bb6528cc1..e42af0e9b2 100644
--- a/docs/zh/latest/admin-api.md
+++ b/docs/zh/latest/admin-api.md
@@ -65,6 +65,8 @@ deployment:
port: 9180 # Admin API 监听的 端口,必须使用与 node_listen
不同的端口。
```
+每个 key 的 `role` 取值为 `admin`(可以读取和修改配置)或 `viewer`(只能读取配置)。除非
`admin_key_required` 为 `false`(即关闭 key 校验),否则 加载配置时会拒绝其他取值,也会拒绝没有 `role` 的
key。启用 key 校验时,每个条目必须是映射,提供的 key 必须是字符串或 `null`。空字符串、缺失或 `null` 的 key
都不会匹配任何请求。只有 `admin` 角色且 key 为 `''` 时,APISIX 才会在启动时生成 key,并写入配置文件。
+
### 使用环境变量 {#using-environment-variables}
要通过环境变量进行配置,可以使用 `${{VAR}}` 语法。例如:
diff --git a/t/admin/token-empty-key-and-role.t
b/t/admin/token-empty-key-and-role.t
new file mode 100644
index 0000000000..2bbdcd6b7e
--- /dev/null
+++ b/t/admin/token-empty-key-and-role.t
@@ -0,0 +1,135 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level("info");
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ my $user_yaml_config = <<_EOC_;
+deployment:
+ admin:
+ admin_key:
+ - name: admin
+ role: admin
+ key: edd1c9f034335f136f87ad84b625c8f1
+ - name: viewer
+ role: viewer
+ key: ''
+ - name: reader
+ role: viewer
+ key: reader-key-for-the-role-check
+ - name: missing
+ role: viewer
+ - name: null
+ role: viewer
+ key: null
+apisix:
+ node_listen: 1984
+_EOC_
+
+ $block->set_value("yaml_config", $user_yaml_config);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: an empty api_key argument does not match an entry with an empty key
+--- request
+GET /apisix/admin/routes?api_key=
+--- error_code: 401
+--- response_body_like eval
+qr/"description":"missing apikey"/
+
+
+
+=== TEST 2: an empty X-API-KEY header does not match an entry with an empty key
+--- request
+GET /apisix/admin/routes
+--- more_headers
+X-API-KEY:
+--- error_code: 401
+--- response_body_like eval
+qr/"description":"missing apikey"/
+
+
+
+=== TEST 3: an empty x_api_key cookie does not match an entry with an empty key
+--- request
+GET /apisix/admin/routes
+--- more_headers
+Cookie: x_api_key=
+--- error_code: 401
+--- response_body_like eval
+qr/"description":"missing apikey"/
+
+
+
+=== TEST 4: the admin key still works
+--- request
+GET /apisix/admin/routes
+--- more_headers
+X-API-KEY: edd1c9f034335f136f87ad84b625c8f1
+--- error_code: 200
+
+
+
+=== TEST 5: the viewer key can read routes
+--- request
+GET /apisix/admin/routes
+--- more_headers
+X-API-KEY: reader-key-for-the-role-check
+--- error_code: 200
+
+
+
+=== TEST 6: the viewer key cannot delete a route
+--- request
+DELETE /apisix/admin/routes/1
+--- more_headers
+X-API-KEY: reader-key-for-the-role-check
+--- error_code: 401
+--- response_body_like eval
+qr/"description":"invalid method for role viewer"/
+
+
+
+=== TEST 7: a missing configured key is not converted to a string
+--- request
+GET /apisix/admin/routes
+--- more_headers
+X-API-KEY: nil
+--- error_code: 401
+--- response_body_like eval
+qr/"description":"wrong apikey"/
+
+
+
+=== TEST 8: a null configured key is not converted to a string
+--- request
+GET /apisix/admin/routes
+--- more_headers
+X-API-KEY: null
+--- error_code: 401
+--- response_body_like eval
+qr/"description":"wrong apikey"/
diff --git a/t/cli/test_admin.sh b/t/cli/test_admin.sh
index 1298cc1dd4..2eee354d11 100755
--- a/t/cli/test_admin.sh
+++ b/t/cli/test_admin.sh
@@ -250,7 +250,7 @@ echo "pass: allow empty admin_key, when
admin_key_required=false"
# admin api, allow any IP but use default key
-echo '
+cat > conf/config.yaml <<'EOF'
deployment:
admin:
allow_admin: ~
@@ -258,7 +258,7 @@ deployment:
- name: "admin"
key: ''
role: admin
-' > conf/config.yaml
+EOF
make init > output.log 2>&1 | true
@@ -490,3 +490,105 @@ fi
make stop
echo "pass: accept changes to /apisix/plugins successfully"
+
+# validate Admin API key entries when loading the configuration
+
+assert_invalid_admin_key() {
+ local label="$1"
+ local entry="$2"
+ cat > conf/config.yaml <<EOF
+deployment:
+ admin:
+ admin_key:
+$entry
+EOF
+
+ if make init > output.log 2>&1; then
+ echo "failed: make init should refuse $label"
+ exit 1
+ fi
+
+ if ! grep -F "invalid deployment traditional configuration:" output.log >
/dev/null; then
+ echo "failed: should report a configuration error for $label"
+ exit 1
+ fi
+}
+
+assert_invalid_admin_key "the role Admin" ' - name: ops
+ key: ops-key-for-the-role-check
+ role: Admin'
+assert_invalid_admin_key "a missing role" ' - name: ops
+ key: ops-key-for-the-role-check'
+assert_invalid_admin_key "an entry that is not a map" ' - entry'
+assert_invalid_admin_key "a numeric key" ' - role: admin
+ key: 123'
+assert_invalid_admin_key "a boolean key" ' - role: admin
+ key: false'
+assert_invalid_admin_key "a map as a key" ' - role: admin
+ key: {value: example}'
+assert_invalid_admin_key "an array as a key" ' - role: admin
+ key: [example]'
+
+echo "pass: refuse invalid Admin API key entries in the configuration schema"
+
+cat > conf/config.yaml <<'EOF'
+deployment:
+ admin:
+ admin_key_required: false
+ admin_key:
+ - name: ops
+ key: ops-key-for-the-role-check
+ role: Admin
+ - name: norole
+ key: norole-key-for-the-role-check
+EOF
+
+if ! make init > output.log 2>&1; then
+ echo "failed: should accept unused roles when admin_key_required is false"
+ exit 1
+fi
+
+echo "pass: do not check key entries when admin_key_required is false"
+
+# an empty key of another role than admin is reported, also with the default
allow_admin
+
+git checkout conf/config.yaml
+
+echo '
+deployment:
+ admin:
+ admin_key:
+ - name: admin
+ key: admin-key-for-the-empty-key-check
+ role: admin
+ - name: viewer
+ key: ""
+ role: viewer
+ - name: viewer2
+ role: viewer
+ - name: viewer3
+ key: null
+ role: viewer
+' > conf/config.yaml
+
+if ! make init > output.log 2>&1; then
+ echo "failed: make init should accept an empty viewer key"
+ exit 1
+fi
+
+if ! grep -F "WARNING: Admin API key viewer is empty, so it does not match any
request." output.log > /dev/null; then
+ echo "failed: should warn about the empty viewer key"
+ exit 1
+fi
+
+if ! grep -F "WARNING: Admin API key viewer2 is empty, so it does not match
any request." output.log > /dev/null; then
+ echo "failed: should warn about a viewer entry without a key"
+ exit 1
+fi
+
+if ! grep -F "WARNING: Admin API key viewer3 is empty, so it does not match
any request." output.log > /dev/null; then
+ echo "failed: should warn about a null viewer key"
+ exit 1
+fi
+
+echo "pass: warn about an empty, missing or null key of the viewer role"