starsz commented on code in PR #11080:
URL: https://github.com/apache/apisix/pull/11080#discussion_r1546117644


##########
apisix/core/id.lua:
##########
@@ -62,18 +67,78 @@ local function write_file(path, data)
 end
 
 
+local function generate_yaml(table)
+    -- By default lyaml will parse null values as []
+    -- The following logic is a workaround so that null values are parsed as 
null
+    local function replace_null(tbl)
+        for k, v in pairs(tbl) do
+            if type(v) == "table" then
+                replace_null(v)
+            elseif v == nil then
+                tbl[k] = "<PLACEHOLDER>"
+            end
+        end
+    end
+
+    -- Replace null values with "<PLACEHOLDER>"
+    replace_null(table)
+    local yaml = lyaml.dump({ table })
+    yaml = yaml:gsub("<PLACEHOLDER>", "null"):gsub("%[%s*%]", "null")
+    return yaml
+end
+
+
 _M.gen_uuid_v4 = uuid.generate_v4
 
 
+--- This will autogenerate the admin key if it's passed as an empty string in 
the configuration.
+local function autogenerate_admin_key(default_conf)
+    local changed = false
+   -- Check if deployment.role is either traditional or control_plane
+    local deployment_role = default_conf.deployment and 
default_conf.deployment.role
+    if deployment_role and (deployment_role == "traditional" or
+       deployment_role == "control_plane") then
+        -- Check if deployment.admin.admin_key is not nil and it's an empty 
string
+        local admin_keys = default_conf.deployment
+            and default_conf.deployment.admin
+            and default_conf.deployment.admin.admin_key

Review Comment:
   I think you can try `core.table.try_read_attr` to improve your code.



##########
apisix/core/id.lua:
##########
@@ -62,18 +67,78 @@ local function write_file(path, data)
 end
 
 
+local function generate_yaml(table)
+    -- By default lyaml will parse null values as []
+    -- The following logic is a workaround so that null values are parsed as 
null
+    local function replace_null(tbl)
+        for k, v in pairs(tbl) do
+            if type(v) == "table" then
+                replace_null(v)
+            elseif v == nil then
+                tbl[k] = "<PLACEHOLDER>"
+            end
+        end
+    end
+
+    -- Replace null values with "<PLACEHOLDER>"
+    replace_null(table)
+    local yaml = lyaml.dump({ table })
+    yaml = yaml:gsub("<PLACEHOLDER>", "null"):gsub("%[%s*%]", "null")
+    return yaml
+end
+
+
 _M.gen_uuid_v4 = uuid.generate_v4
 
 
+--- This will autogenerate the admin key if it's passed as an empty string in 
the configuration.
+local function autogenerate_admin_key(default_conf)
+    local changed = false
+   -- Check if deployment.role is either traditional or control_plane
+    local deployment_role = default_conf.deployment and 
default_conf.deployment.role
+    if deployment_role and (deployment_role == "traditional" or
+       deployment_role == "control_plane") then
+        -- Check if deployment.admin.admin_key is not nil and it's an empty 
string
+        local admin_keys = default_conf.deployment
+            and default_conf.deployment.admin
+            and default_conf.deployment.admin.admin_key
+        if admin_keys and type(admin_keys) == "table" then
+            for i, admin_key in ipairs(admin_keys) do
+                if admin_key.role == "admin" and admin_key.key == "" then
+                    changed = true
+                    admin_keys[i].key = ""
+                    for _ = 1, 32 do
+                        admin_keys[i].key = admin_keys[i].key ..
+                        string.char(math.random(65, 90) + math.random(0, 1) * 
32)
+                    end
+                end
+            end
+        end
+    end
+    return default_conf,changed
+end
+
+
 function _M.init()
+    local local_conf = fetch_local_conf()
+
+    local local_conf, changed = autogenerate_admin_key(local_conf)
+    if changed then
+        local yaml_conf = generate_yaml(local_conf)
+        local local_conf_path = profile:yaml_path("config")
+        local ok, err = write_file(local_conf_path, yaml_conf)
+        if not ok then
+            log.error(err)

Review Comment:
   I think you should panic if we can't write file.



##########
apisix/core/id.lua:
##########
@@ -61,19 +63,88 @@ local function write_file(path, data)
     return true
 end
 
+local function generate_yaml(table)
+    -- By default lyaml will parse null values as []
+    -- The following logic is a workaround so that null values are parsed as 
null
+    local function replace_null(tbl)
+        for k, v in pairs(tbl) do
+            if type(v) == "table" then
+                replace_null(v)
+            elseif v == nil then
+                tbl[k] = "<PLACEHOLDER>"
+            end
+        end
+    end
+
+    -- Replace null values with "<PLACEHOLDER>"
+    replace_null(table)
+
+    -- Convert Lua table to YAML string without parsing null values
+    local yaml = lyaml.dump({ table }, { no_nil = true })
+
+    -- Replace "<PLACEHOLDER>" with null except for empty arrays
+    yaml = yaml:gsub("<PLACEHOLDER>", "null"):gsub("%[%s*%]", "null")
+
+    -- Ensure boolean values remain intact
+    yaml = yaml:gsub(":%s*true%s*true", ": true"):gsub(":%s*false%s*true", ": 
false")
+
+    -- Replace *no_nil with true
+    yaml = yaml:gsub("&no_nil", "true")
+
+    -- Remove any occurrences of *no_nil
+    yaml = yaml:gsub(":%s*%*no_nil", ": true")
+
+    -- Remove duplicates for boolean values
+    yaml = yaml:gsub("true%s*true", "true"):gsub("false%s*false", "false")
+
+    return yaml
+end
+
 
 _M.gen_uuid_v4 = uuid.generate_v4
 
+--- This will autogenerate the admin key if it's passed as an empty string in 
the configuration.
+local function autogenerate_admin_key(default_conf)
+    local changed = false
+    -- Check if deployment.admin.admin_key is not nil and it's an array
+    local admin_keys = default_conf.deployment
+        and default_conf.deployment.admin
+        and default_conf.deployment.admin.admin_key
+    if admin_keys and type(admin_keys) == "table" then
+        for i, admin_key in ipairs(admin_keys) do
+            if admin_key.role == "admin" and admin_key.key == "" then
+                changed = true
+                admin_keys[i].key = ""
+                for _ = 1, 32 do
+                    admin_keys[i].key = admin_keys[i].key ..
+                    string.char(math.random(65, 90) + math.random(0, 1) * 32)
+                end

Review Comment:
   > Which function or module do you suggest to generate a uuid?
   
   You can review the `request-id` plugin.



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