SkyeYoung commented on code in PR #12484: URL: https://github.com/apache/apisix/pull/12484#discussion_r2281104772
########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} Review Comment: `The "version" seems not very necessary, but the other files in the "core" folder are also not consistent. It can be considered to remove it or keep it.` ########## .github/workflows/source-install.yml: ########## @@ -89,10 +89,24 @@ jobs: run: | wget https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64 -O /usr/bin/yq && sudo chmod +x /usr/bin/yq get_admin_key() { + # First try to get the key from config.yaml local admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml) + # If the key is empty (auto-generated), extract it from logs + if [ -z "$admin_key" ] || [ "$admin_key" = "''" ] || [ "$admin_key" = "null" ]; then + admin_key=$(grep -A 10 "Generated admin keys for this session:" logs/error.log | grep -E "^ [A-Za-z0-9]{32}$" | head -1 | sed 's/^ //') + fi echo "$admin_key" } export admin_key=$(get_admin_key); echo $admin_key + # Validate that we have a valid admin key + if [ -z "$admin_key" ] || [ "$admin_key" = "null" ]; then + echo "ERROR: Could not obtain valid admin key" + echo "Config file content:" + cat conf/config.yaml + echo "Error log content:" + cat logs/error.log Review Comment: Other parts are used lowercase letters, and the style is recommended to be unified with other parts. https://github.com/apache/apisix/pull/12484/files#diff-628f0f4af8b34393590078f846cb1dc4c219a073a129f609f09f21df198f7eb0R122-R130 <img width="1698" height="992" alt="Image" src="https://github.com/user-attachments/assets/b1bd5cf5-3929-4f61-99a1-854aca57a0fb" /> ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] Review Comment: Please assert that the shared dict exists. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then Review Comment: ```suggestion if key_value and role then local name = admin_keys_shm:get(key_id .. "_name") local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false ``` better 🤔 ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 Review Comment: If `admin_key_1` exists, then `total_keys` will be `> 0`. So I think this `total_keys` is unnecessary. You can directly check in the loop and exit if the `key_value` corresponding to `key_id` does not exist. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { + name = name ~= "" and name or nil, + role = role, + key = key_value, + autogenerated = autogenerated + } + end + end + if #keys > 0 then + admin_keys_cache = keys + end + end + + return admin_keys_cache +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + if not local_conf or not local_conf.deployment then + return Review Comment: I think it's necessary to add some logs here, because there are too many branch conditions, and logs can help people determine the problem. 🤔 ########## .github/workflows/source-install.yml: ########## @@ -89,10 +89,24 @@ jobs: run: | wget https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64 -O /usr/bin/yq && sudo chmod +x /usr/bin/yq get_admin_key() { + # First try to get the key from config.yaml local admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml) + # If the key is empty (auto-generated), extract it from logs + if [ -z "$admin_key" ] || [ "$admin_key" = "''" ] || [ "$admin_key" = "null" ]; then Review Comment: Looks like `|| [ "$admin_key" = "''" ]` can be removed ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do Review Comment: weird. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { Review Comment: use `core.table.insert` ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { + name = name ~= "" and name or nil, + role = role, + key = key_value, + autogenerated = autogenerated + } + end + end + if #keys > 0 then + admin_keys_cache = keys + end + end + + return admin_keys_cache +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + if not local_conf or not local_conf.deployment then + return + end + local deployment_role = local_conf.deployment.role + + if local_conf.deployment.admin and local_conf.deployment.admin.admin_key_required == false then + return Review Comment: add logs. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { + name = name ~= "" and name or nil, + role = role, + key = key_value, + autogenerated = autogenerated + } + end + end + if #keys > 0 then + admin_keys_cache = keys + end + end + + return admin_keys_cache +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + if not local_conf or not local_conf.deployment then + return + end + local deployment_role = local_conf.deployment.role + + if local_conf.deployment.admin and local_conf.deployment.admin.admin_key_required == false then + return + end + + if not deployment_role or + (deployment_role ~= "traditional" and deployment_role ~= "control_plane") then + return + end + + local config_admin_keys = try_read_attr(local_conf, "deployment", "admin", "admin_key") + if not config_admin_keys or type(config_admin_keys) ~= "table" then + return + end + + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if not admin_keys_shm then + log.error("admin-keys shared memory zone not configured") + return + end + + -- Only one worker can claim initialization, this flag will only be cleared on full restart + local claimed_init = admin_keys_shm:safe_add("init_claimed", ngx.worker.id() or 0) Review Comment: It looks like this and `completed` are also a bit redundant. You can totally use a `status` instead. Recording the worker ID doesn't seem very important either. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { + name = name ~= "" and name or nil, + role = role, + key = key_value, + autogenerated = autogenerated + } + end + end + if #keys > 0 then + admin_keys_cache = keys + end + end + + return admin_keys_cache +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + if not local_conf or not local_conf.deployment then + return + end + local deployment_role = local_conf.deployment.role + + if local_conf.deployment.admin and local_conf.deployment.admin.admin_key_required == false then + return + end + + if not deployment_role or + (deployment_role ~= "traditional" and deployment_role ~= "control_plane") then + return Review Comment: add logs. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { + name = name ~= "" and name or nil, + role = role, + key = key_value, + autogenerated = autogenerated + } + end + end + if #keys > 0 then + admin_keys_cache = keys + end + end + + return admin_keys_cache +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + if not local_conf or not local_conf.deployment then + return + end + local deployment_role = local_conf.deployment.role + + if local_conf.deployment.admin and local_conf.deployment.admin.admin_key_required == false then + return + end + + if not deployment_role or + (deployment_role ~= "traditional" and deployment_role ~= "control_plane") then + return + end + + local config_admin_keys = try_read_attr(local_conf, "deployment", "admin", "admin_key") + if not config_admin_keys or type(config_admin_keys) ~= "table" then + return + end + + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if not admin_keys_shm then + log.error("admin-keys shared memory zone not configured") + return + end + + -- Only one worker can claim initialization, this flag will only be cleared on full restart + local claimed_init = admin_keys_shm:safe_add("init_claimed", ngx.worker.id() or 0) + if not claimed_init then + -- Another worker is handling initialization, no waiting needed + -- Keys will be loaded lazily when first accessed via get_admin_keys() + return + end + + -- This worker won the initialization race + local generated_keys = {} + local has_autogenerated = false + + for i, admin_key in ipairs(config_admin_keys) do + local key_id = "admin_key_" .. i + local key_value = admin_key.key + + if admin_key.role == "admin" and admin_key.key == "" then + has_autogenerated = true + local key = "" + for _ = 1, 32 do + key = key .. string.char(math.random(65, 90) + math.random(0, 1) * 32) + end + key_value = key + generated_keys[#generated_keys + 1] = key Review Comment: `core.table.insert` ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,167 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type +local ngx = ngx + +local admin_key_shm_name = "admin-keys" +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.get_admin_keys() + -- First check if admin key is required at all + if not _M.admin_key_required() then + -- When admin keys are not required, return empty table + return {} + end + + -- First, try to load from cache + if #admin_keys_cache > 0 then + return admin_keys_cache + end + + -- Try loading from shared memory + local admin_keys_shm = ngx.shared[admin_key_shm_name] + if admin_keys_shm and admin_keys_shm:get("completed") then + local total_keys = admin_keys_shm:get("total_keys") or 0 + local keys = {} + + for i = 1, total_keys do + local key_id = "admin_key_" .. i + local key_value = admin_keys_shm:get(key_id) + local role = admin_keys_shm:get(key_id .. "_role") + local name = admin_keys_shm:get(key_id .. "_name") + local autogenerated = admin_keys_shm:get(key_id .. "_autogenerated") or false + + if key_value and role then + keys[#keys + 1] = { + name = name ~= "" and name or nil, + role = role, + key = key_value, + autogenerated = autogenerated + } + end + end + if #keys > 0 then + admin_keys_cache = keys + end + end + + return admin_keys_cache +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + if not local_conf or not local_conf.deployment then + return Review Comment: add logs? ########## t/cli/common.sh: ########## @@ -39,5 +39,33 @@ exit_if_not_customed_nginx() { openresty -V 2>&1 | grep apisix-nginx-module || exit 0 } +get_admin_key() { + # First try to get the key from config.yaml + local admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g') + echo "DEBUG: config.yaml admin_key: '$admin_key'" >&2 + # If the key is empty (auto-generated), extract it from logs + if [ -z "$admin_key" ] || [ "$admin_key" = "''" ] || [ "$admin_key" = "null" ]; then Review Comment: ```suggestion if [ -z "$admin_key" ] || [ "$admin_key" = "null" ]; then ``` Looks like it can be removed. ########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,162 @@ +-- +-- 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. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type + +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.get_admin_keys() + -- Lazy loading: only load keys when actually needed + if #admin_keys_cache == 0 then + _M.load_keys_from_shared_memory() + end + return admin_keys_cache +end + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + local deployment_role = local_conf.deployment and local_conf.deployment.role + + if local_conf.deployment.admin.admin_key_required == false then + return + end + + if not deployment_role or (deployment_role ~= "traditional" and deployment_role ~= "control_plane") then + return + end + + local config_admin_keys = try_read_attr(local_conf, "deployment", "admin", "admin_key") + if not config_admin_keys or type(config_admin_keys) ~= "table" then + return + end + + local admin_keys_shm = ngx.shared.admin_keys + if not admin_keys_shm then + log.error("admin_keys shared memory zone not configured") + return + end + + -- Check if already initialized by another worker + if admin_keys_shm:get("completed") then + _M.load_keys_from_shared_memory() + return + end + + -- Atomic check-and-set: only one worker can claim initialization + local claimed_init, err = admin_keys_shm:safe_add("init_claimed", ngx.worker.id() or 0, 5) + if not claimed_init then + -- Another worker is handling initialization, no waiting needed + -- Keys will be loaded lazily when first accessed via get_admin_keys() + return + end + + -- This worker won the initialization race + local generated_keys = {} + local has_autogenerated = false + + for i, admin_key in ipairs(config_admin_keys) do + local key_id = "admin_key_" .. i + local key_value = admin_key.key + + if admin_key.role == "admin" and admin_key.key == "" then + has_autogenerated = true + local key = "" + for _ = 1, 32 do + key = key .. string.char(math.random(65, 90) + math.random(0, 1) * 32) Review Comment: It's best not to resolve this comment, so others will know your code comes from `id.lua`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
