Copilot commented on code in PR #12484:
URL: https://github.com/apache/apisix/pull/12484#discussion_r2252958540


##########
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

Review Comment:
   The magic number '32' (key length) should be defined as a named constant. 
This makes the key generation parameters more explicit and maintainable.
   ```suggestion
               for _ = 1, ADMIN_KEY_LENGTH do
   ```



##########
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:
   The ASCII values 65, 90, and 32 for character generation should be defined 
as named constants (e.g., ASCII_A = 65, ASCII_Z = 90, CASE_OFFSET = 32). This 
makes the character range generation more readable and maintainable.
   ```suggestion
                   key = key .. string.char(math.random(ASCII_A, ASCII_Z) + 
math.random(0, 1) * CASE_OFFSET)
   ```



##########
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

Review Comment:
   This could result in a nil access error if local_conf.deployment.admin is 
nil. The function should use try_read_attr or add nil checks to safely access 
nested configuration values.
   ```suggestion
       return try_read_attr(local_conf, {"deployment", "admin", 
"admin_key_required"})
   ```



##########
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

Review Comment:
   Without synchronization, workers that don't claim initialization may call 
get_admin_keys() before the initializing worker completes, potentially 
returning empty keys. Consider adding a brief retry mechanism or checking for 
completion status.



##########
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)

Review Comment:
   The magic number '5' (TTL for initialization claim) should be defined as a 
named constant. This makes the timeout duration more explicit and easier to 
modify if needed.
   ```suggestion
       local claimed_init, err = admin_keys_shm:safe_add("init_claimed", 
ngx.worker.id() or 0, INIT_CLAIM_TTL)
   ```



-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to