Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support bypassing Admin API Auth by configuration #9147

Merged
merged 21 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ local router

local function check_token(ctx)
local local_conf = core.config.local_conf()

-- check if admin_key is required
if not local_conf.deployment.admin.admin_key_required then
return true
end

local admin_key = core.table.try_read_attr(local_conf, "deployment", "admin", "admin_key")
if not admin_key then
return true
Expand Down Expand Up @@ -395,6 +401,13 @@ function _M.init_worker()
events.register(reload_plugins, reload_event, "PUT")

if ngx_worker_id() == 0 then
-- check if admin_key is required
if not local_conf.deployment.admin.admin_key_required then
core.log.warn("AdminKey is bypassed.",
"If you are deploying APISIX in a production environment,",
"please disable it and set a secure password for the adminKey!")
end

local ok, err = ngx_timer_at(0, function(premature)
if premature then
return
Expand Down
7 changes: 7 additions & 0 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ local function init(env)
and #allow_admin == 1 and allow_admin[1] == "127.0.0.0/24" then
checked_admin_key = true
end
-- check if admin_key is required
if not yaml_conf.deployment.admin.admin_key_required then
checked_admin_key = true
print("Warning! AdminKey is bypassed. "
.. "If you are deploying APISIX in a production environment, "
.. "please disable it and set a secure password for the adminKey!")
end

if yaml_conf.apisix.enable_admin and not checked_admin_key then
local help = [[
Expand Down
3 changes: 3 additions & 0 deletions apisix/cli/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ local admin_schema = {
https_admin = {
type = "boolean",
},
admin_key_required = {
type = "boolean",
},
}
}

Expand Down
4 changes: 4 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ deployment:
role_traditional:
config_provider: etcd
admin:
# If admin_key is required, default value is true.
# Bypass the Admin API authentication by modifying this value to false if needed.
admin_key_required: true

# Default token when use API to call for Admin API.
# *NOTE*: Highly recommended to modify this value to protect APISIX's Admin API.
# Disabling this configuration item means that the Admin API does not
Expand Down
37 changes: 37 additions & 0 deletions t/admin/api.t
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,40 @@ X-API-VERSION: v2
GET /t
--- response_body
passed



=== TEST 10: Access with api key, and admin_key_required=true
--- yaml_config
deployment:
admin:
admin_key_required: true
--- more_headers
X-API-KEY: edd1c9f034335f136f87ad84b625c8f1
--- request
GET /apisix/admin/routes
--- error_code: 200



=== TEST 11: Access without api key, but admin_key_required=true
--- yaml_config
deployment:
admin:
admin_key_required: true
--- request
GET /apisix/admin/routes
--- error_code: 401



=== TEST 12: Access without api key, but admin_key_required=false
--- yaml_config
deployment:
admin:
admin_key_required: false
--- request
GET /apisix/admin/routes
--- error_code: 200
--- error_log
AdminKey is bypassed.
56 changes: 56 additions & 0 deletions t/cli/test_admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,62 @@ fi

echo "pass: missing admin key and only allow 127.0.0.0/24 to access admin api"

# allow any IP to access admin api with empty admin_key, when admin_key_required=true

git checkout conf/config.yaml

echo '
deployment:
admin:
admin_key_required: true
admin_key: ~
allow_admin:
- 0.0.0.0/0
' > conf/config.yaml

make init > output.log 2>&1 | true

if ! grep -E "ERROR: missing valid Admin API token." output.log > /dev/null; then
echo "failed: should show 'ERROR: missing valid Admin API token.'"
exit 1
fi

echo '
deployment:
admin:
admin_key_required: false
admin_key: ~
allow_admin:
- 0.0.0.0/0
' > conf/config.yaml

make init > output.log 2>&1 | true

if grep -E "ERROR: missing valid Admin API token." output.log > /dev/null; then
echo "failed: should not show 'ERROR: missing valid Admin API token.'"
exit 1
fi

if ! grep -E "Warning! AdminKey is bypassed" output.log > /dev/null; then
echo "failed: should show 'Warning! AdminKey is bypassed'"
exit 1
fi

echo '
deployment:
admin:
admin_key_required: invalid-value
' > conf/config.yaml

make init > output.log 2>&1 | true

if grep -E "path[deployment->admin->admin_key_required] expect: boolean, but got: string" output.log > /dev/null; then
echo "failed: should show 'expect: boolean, but got: string'"
exit 1
fi

echo "pass: allow empty admin_key, when admin_key_required=true"

# admin api, allow any IP but use default key

echo '
Expand Down