Skip to content

Commit

Permalink
fix(cli): prevent non-127.0.0.0/24 to access admin api with empty a…
Browse files Browse the repository at this point in the history
…dmin_key (#9146)
  • Loading branch information
An-DJ authored Mar 29, 2023
1 parent 81149cd commit 01f0498
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
9 changes: 3 additions & 6 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,9 @@ local function init(env)
local checked_admin_key = false
local allow_admin = yaml_conf.deployment.admin and
yaml_conf.deployment.admin.allow_admin
if yaml_conf.apisix.enable_admin and allow_admin then
for _, allow_ip in ipairs(allow_admin) do
if allow_ip == "127.0.0.0/24" then
checked_admin_key = true
end
end
if yaml_conf.apisix.enable_admin and allow_admin
and #allow_admin == 1 and allow_admin[1] == "127.0.0.0/24" then
checked_admin_key = true
end

if yaml_conf.apisix.enable_admin and not checked_admin_key then
Expand Down
35 changes: 35 additions & 0 deletions t/cli/test_admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,41 @@ fi

echo "pass: missing admin key and show ERROR message"

# missing admin key, only allow 127.0.0.0/24 to access admin api

echo '
deployment:
admin:
admin_key: ~
allow_admin:
- 127.0.0.0/24
' > 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

echo '
deployment:
admin:
admin_key: ~
allow_admin:
- 0.0.0.0/0
- 127.0.0.0/24
' > 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 "pass: missing admin key and only allow 127.0.0.0/24 to access admin api"

# admin api, allow any IP but use default key

echo '
Expand Down

0 comments on commit 01f0498

Please sign in to comment.