soulbird commented on code in PR #8390: URL: https://github.com/apache/apisix/pull/8390#discussion_r1031317866
########## apisix/core/env.lua: ########## @@ -0,0 +1,99 @@ +-- +-- 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. +-- +local ffi = require "ffi" + +local json = require("apisix.core.json") +local log = require("apisix.core.log") +local string = require("apisix.core.string") + +local os = os +local type = type +local upper = string.upper +local find = string.find +local sub = string.sub +local str = ffi.string + +local _M = {} + +local ENV_PREFIX = "$ENV://" + +local apisix_env_vars = {} + +ffi.cdef [[ + extern char **environ; +]] + + +function _M.init() + local e = ffi.C.environ + if not e then + log.warn("could not access environment variables") + return + end + + local i = 0 + while e[i] ~= nil do + local var = str(e[i]) + local p = find(var, "=") Review Comment: Already use `local string = require("apisix.core.string")`, it's `plain` mode. ########## apisix/core/env.lua: ########## @@ -0,0 +1,99 @@ +-- +-- 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. +-- +local ffi = require "ffi" + +local json = require("apisix.core.json") +local log = require("apisix.core.log") +local string = require("apisix.core.string") + +local os = os +local type = type +local upper = string.upper +local find = string.find +local sub = string.sub +local str = ffi.string + +local _M = {} + +local ENV_PREFIX = "$ENV://" + +local apisix_env_vars = {} + +ffi.cdef [[ + extern char **environ; +]] + + +function _M.init() + local e = ffi.C.environ + if not e then + log.warn("could not access environment variables") + return + end + + local i = 0 + while e[i] ~= nil do + local var = str(e[i]) + local p = find(var, "=") + if p then + apisix_env_vars[sub(var, 1, p - 1)] = sub(var, p + 1) + end + + i = i + 1 + end +end + + +local function is_env_ref(ref) + return type(ref) == "string" and sub(upper(ref), 1, 7) == ENV_PREFIX +end + + +local function parse_ref(ref) + local path = sub(ref, 8) + local idx = find(path, "/") Review Comment: ditto ########## apisix/core/utils.lua: ########## @@ -329,4 +332,57 @@ end _M.resolve_var = resolve_var +local secrets_lrucache = lrucache.new({ + ttl = 300, count = 512 +}) + +local retrieve_secrets_ref +do + local retrieve_ref + function retrieve_ref(refs) + for k, v in pairs(refs) do + local typ = type(v) + if typ == "string" then + refs[k] = env.get(v) or v + elseif typ == "table" then + retrieve_ref(v) + end + end + return refs + end + + local function retrieve(refs) + log.info(string.format("retrieve secrets refs: %p", refs)) Review Comment: Not a hot path, but it would be better to use cache. -- 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]
