nic-6443 commented on code in PR #13509:
URL: https://github.com/apache/apisix/pull/13509#discussion_r3393130220


##########
apisix/secret/kubernetes.lua:
##########
@@ -0,0 +1,242 @@
+--
+-- 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.
+--
+
+--- Kubernetes Secret Manager.
+--  Fetches secrets from the Kubernetes API server using the pod's 
ServiceAccount
+--  token. This allows APISIX to read Kubernetes Secrets directly from the 
cluster
+--  it is running in, without requiring an external secrets management service.
+--
+--  URI format:
+--    $secret://kubernetes/{manager-id}/{namespace}/{secret-name}/{data-key}
+--
+--  Example:
+--    $secret://kubernetes/my-k8s/default/my-secret/password
+--
+--  The manager is configured once via the Admin API:
+--    PUT /apisix/admin/secrets/kubernetes/my-k8s
+--    {
+--      "service_account_file": 
"/var/run/secrets/kubernetes.io/serviceaccount/token",
+--      "kubernetes_host": "kubernetes.default.svc",
+--      "kubernetes_port": "443",
+--      "ssl_verify": true
+--    }
+--
+--  All fields have sensible defaults for in-cluster usage, so the minimal
+--  valid configuration is an empty object `{}`.
+
+local core = require("apisix.core")
+local http = require("resty.http")
+local env  = core.env
+
+local io_open = io.open
+local find    = core.string.find
+local sub     = core.string.sub
+local ngx_decode_base64 = ngx.decode_base64
+
+local DEFAULT_SA_FILE = "/var/run/secrets/kubernetes.io/serviceaccount/token"
+local DEFAULT_CA_FILE = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
+
+local schema = {
+    type = "object",
+    properties = {
+        service_account_file = {
+            type = "string",
+            description = "Path to the ServiceAccount token file. "
+                       .. "Defaults to the standard in-cluster path.",
+            default = DEFAULT_SA_FILE,
+        },
+        kubernetes_host = {
+            type = "string",
+            description = "Kubernetes API server hostname or IP. "
+                       .. "Defaults to the KUBERNETES_SERVICE_HOST environment 
variable.",
+        },
+        kubernetes_port = {
+            type = "string",
+            description = "Kubernetes API server port. "
+                       .. "Defaults to the KUBERNETES_SERVICE_PORT environment 
variable.",
+        },
+        ssl_verify = {
+            type = "boolean",
+            description = "Verify the Kubernetes API server TLS certificate.",
+            default = true,
+        },
+    },
+    required = {},
+}
+
+local _M = {
+    schema = schema,
+}
+
+
+local function read_file(path)
+    local f, err = io_open(path, "r")
+    if not f then
+        return nil, "failed to open file " .. path .. ": " .. err
+    end
+    local content = f:read("*a")
+    f:close()
+    if not content or content == "" then
+        return nil, "file is empty: " .. path
+    end
+    return content
+end
+
+
+local function make_request_to_k8s(conf, namespace, secret_name)
+    local sa_file = conf.service_account_file or DEFAULT_SA_FILE
+    local token, err = read_file(sa_file)
+    if not token then
+        return nil, err
+    end
+    -- strip trailing newline
+    token = token:gsub("%s+$", "")
+
+    local k8s_host = conf.kubernetes_host
+                  or env.fetch_by_uri("$ENV://KUBERNETES_SERVICE_HOST")
+                  or os.getenv("KUBERNETES_SERVICE_HOST")
+    if not k8s_host then
+        return nil, "kubernetes_host is not set and KUBERNETES_SERVICE_HOST 
env var is missing"
+    end
+
+    local k8s_port = conf.kubernetes_port
+                  or env.fetch_by_uri("$ENV://KUBERNETES_SERVICE_PORT")
+                  or os.getenv("KUBERNETES_SERVICE_PORT")
+                  or "443"
+
+    local uri = "https://"; .. k8s_host .. ":" .. k8s_port

Review Comment:
   Hardcoding `https://` makes the bundled tests impossible to pass: the mock 
locations in t/secret/kubernetes.t serve plain HTTP on port 1984, so TESTs 
10-14 will fail at the TLS handshake before ever reaching the mock. gcp.lua 
handles this by letting the full endpoint be configured (`entries_uri`), which 
is exactly what its tests use to mock with `http://127.0.0.1:1984`. I would 
suggest the same here — e.g. an optional scheme/endpoint override defaulting to 
https — rather than rewriting the tests around a TLS mock.
   
   Separately, TEST 14 will still fail after that: the manager registered in 
TEST 13 does not set `service_account_file`, so `get()` falls back to 
`/var/run/secrets/kubernetes.io/serviceaccount/token`, which does not exist in 
CI. Worth running the suite locally before the next push.



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

Reply via email to