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


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



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