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


##########
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
+             .. "/api/v1/namespaces/" .. namespace
+             .. "/secrets/" .. secret_name
+
+    core.log.info("fetching Kubernetes secret from: ", uri)
+
+    local httpc = http.new()
+    httpc:set_timeout(5000)
+
+    local ssl_verify = conf.ssl_verify
+    if ssl_verify == nil then
+        ssl_verify = true
+    end
+
+    local request_opts = {
+        method = "GET",
+        headers = {
+            ["Authorization"] = "Bearer " .. token,
+            ["Accept"] = "application/json",
+        },
+        ssl_verify = ssl_verify,
+    }
+
+    if ssl_verify then
+        request_opts.ssl_trusted_certificate = DEFAULT_CA_FILE

Review Comment:
   `ssl_trusted_certificate` is not a supported `request_uri` option in 
lua-resty-http (the api7 fork APISIX uses only honors `ssl_verify`, 
`ssl_server_name` and `ssl_send_status_req` in `http_connect.lua`), so this 
line is silently ignored. With `ssl_verify = true`, the handshake verifies 
against the nginx-level `lua_ssl_trusted_certificate`, which defaults to the 
system CA bundle (`apisix.ssl.ssl_trusted_certificate: system`) — and the 
kube-apiserver cert is signed by the cluster CA, not a public one. So the 
default config will fail the TLS handshake in a real cluster, and the obvious 
workaround users will reach for is `ssl_verify: false`, which means sending the 
ServiceAccount token over an unverified connection.
   
   I think this needs to either drop the no-op option and document that 
`apisix.ssl.ssl_trusted_certificate` must be set to 
`/var/run/secrets/kubernetes.io/serviceaccount/ca.crt` (the doc currently 
claims the in-cluster CA is used by default, which is not accurate), or find 
another way to actually load the cluster CA.



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