You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "nic-6443 (via GitHub)" <gi...@apache.org> on 2026/08/16 14:57:45 UTC

[PR] fix(admin): encrypt the inline upstream client key of a stream route [apisix]

nic-6443 opened a new pull request, #13833:
URL: https://github.com/apache/apisix/pull/13833

   ### Description
   
   A stream route takes the same upstream schema as an HTTP one, `tls.client_cert`/`tls.client_key` included. `routes`, `services` and `upstreams` all register an `encrypt_conf` hook that runs `apisix_upstream.encrypt_conf()` before the write, so an inline client key is encrypted at rest; `stream_routes` never registered one, so the same key sent to `/apisix/admin/stream_routes/{id}` is stored in etcd verbatim and returned in plaintext by the Admin API.
   
   This registers the hook for `stream_routes` as well. Keys written before the change keep working — `aes_decrypt_pkey()` passes a PEM through untouched — and the new entry in `t/admin/stream-routes.t` asserts both that etcd no longer holds the plaintext and that the Admin API no longer returns it.
   
   #### Which issue(s) this PR fixes:
   
   N/A
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change — the encryption of inline upstream keys is already documented for the other resources
   - [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   


-- 
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-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(admin): encrypt the inline upstream client key of a stream route [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 merged PR #13833:
URL: https://github.com/apache/apisix/pull/13833


-- 
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-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(admin): encrypt the inline upstream client key of a stream route [apisix]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13833:
URL: https://github.com/apache/apisix/pull/13833#discussion_r3792034793


##########
apisix/admin/stream_routes.lua:
##########
@@ -151,12 +152,18 @@ local function delete_checker(id)
 end
 
 
+local function encrypt_conf(id, conf)
+    apisix_upstream.encrypt_conf(conf.upstream)

Review Comment:
   `encrypt_conf` currently returns `nil`. If the admin `resource` layer expects the hook to return the (possibly mutated) config, this can accidentally replace the stored config with `nil`. Consider explicitly returning `conf` after encrypting (and optionally rename `id` to `_id` if it’s intentionally unused) to make the contract unambiguous.



##########
t/admin/stream-routes.t:
##########
@@ -654,3 +654,51 @@ passed
 GET /t
 --- response_body
 passed
+
+
+
+=== TEST 18: an inline upstream client key is encrypted at rest
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local etcd = require("apisix.core.etcd")
+            local t = require("lib.test_admin")
+
+            local ssl_cert = t.read_file("t/certs/apisix.crt")
+            local ssl_key = t.read_file("t/certs/apisix.key")
+            local code, body = t.test('/apisix/admin/stream_routes/enc',
+                ngx.HTTP_PUT,
+                core.json.encode({
+                    remote_addr = "127.0.0.1",
+                    upstream = {
+                        nodes = { ["127.0.0.1:8080"] = 1 },
+                        type = "roundrobin",
+                        scheme = "tls",
+                        tls = {
+                            client_cert = ssl_cert,
+                            client_key = ssl_key,
+                        },
+                    },
+                })
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            local res = assert(etcd.get('/stream_routes/enc'))
+            local stored = res.body.node.value.upstream.tls.client_key
+            assert(stored ~= ssl_key, "the client key must be encrypted at rest")

Review Comment:
   This assertion can produce false positives: `stored ~= ssl_key` would still pass if the key is merely normalized (e.g., trailing newline stripped) rather than encrypted. To ensure the test fails unless encryption-at-rest is actually applied, assert on an encryption-specific property (e.g., that `stored` does not contain `PRIVATE KEY` / PEM markers, or matches an expected ciphertext wrapper/prefix used by the encryptor).



##########
t/admin/stream-routes.t:
##########
@@ -654,3 +654,51 @@ passed
 GET /t
 --- response_body
 passed
+
+
+
+=== TEST 18: an inline upstream client key is encrypted at rest

Review Comment:
   There are multiple consecutive blank lines before the new test header. Consider reducing to a single blank line to keep the test file formatting consistent and easier to scan.



-- 
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-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(admin): encrypt the inline upstream client key of a stream route [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #13833:
URL: https://github.com/apache/apisix/pull/13833#discussion_r3792152718


##########
apisix/admin/stream_routes.lua:
##########
@@ -151,12 +152,18 @@ local function delete_checker(id)
 end
 
 
+local function encrypt_conf(id, conf)
+    apisix_upstream.encrypt_conf(conf.upstream)

Review Comment:
   The return value isn't part of the contract: `resource.lua` calls `self.encrypt_conf(id, conf)` and ignores what comes back, and the hook mutates `conf` in place — `routes.lua`, `services.lua` and `upstreams.lua` all return nothing the same way. Returning `conf` here would make this one look different from its three siblings without changing behaviour, so I'd rather keep it identical to them.



##########
t/admin/stream-routes.t:
##########
@@ -654,3 +654,51 @@ passed
 GET /t
 --- response_body
 passed
+
+
+
+=== TEST 18: an inline upstream client key is encrypted at rest
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local etcd = require("apisix.core.etcd")
+            local t = require("lib.test_admin")
+
+            local ssl_cert = t.read_file("t/certs/apisix.crt")
+            local ssl_key = t.read_file("t/certs/apisix.key")
+            local code, body = t.test('/apisix/admin/stream_routes/enc',
+                ngx.HTTP_PUT,
+                core.json.encode({
+                    remote_addr = "127.0.0.1",
+                    upstream = {
+                        nodes = { ["127.0.0.1:8080"] = 1 },
+                        type = "roundrobin",
+                        scheme = "tls",
+                        tls = {
+                            client_cert = ssl_cert,
+                            client_key = ssl_key,
+                        },
+                    },
+                })
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            local res = assert(etcd.get('/stream_routes/enc'))
+            local stored = res.body.node.value.upstream.tls.client_key
+            assert(stored ~= ssl_key, "the client key must be encrypted at rest")

Review Comment:
   Right, normalization would have slipped through. Strengthened in 205c07c96: it asserts the stored value no longer contains the PEM `PRIVATE KEY` marker.



##########
t/admin/stream-routes.t:
##########
@@ -654,3 +654,51 @@ passed
 GET /t
 --- response_body
 passed
+
+
+
+=== TEST 18: an inline upstream client key is encrypted at rest

Review Comment:
   That's the convention in this suite: test blocks are separated by three blank lines, and `utils/reindex` (what the CI style check runs) reports this file as clean.



-- 
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-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org