This is an automated email from the ASF dual-hosted git repository.

shreemaanabhishek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 1d57986c5 fix: when delete the secret cause 500 error  (#10902)
1d57986c5 is described below

commit 1d57986c5f9c76eebd364c7fe2c0f43e749f52f7
Author: cooper.wu <[email protected]>
AuthorDate: Wed Feb 21 22:52:50 2024 +0800

    fix: when delete the secret cause 500 error  (#10902)
---
 apisix/secret.lua     | 24 +++++++------
 t/secret/secret_lru.t | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 11 deletions(-)

diff --git a/apisix/secret.lua b/apisix/secret.lua
index ca9b09190..90a99d321 100644
--- a/apisix/secret.lua
+++ b/apisix/secret.lua
@@ -58,20 +58,22 @@ local function create_secret_kvs(values)
     local secret_managers = {}
 
     for _, v in ipairs(values) do
-        local path = v.value.id
-        local idx = find(path, "/")
-        if not idx then
-            core.log.error("no secret id")
-            return nil
-        end
+        if v then
+            local path = v.value.id
+            local idx = find(path, "/")
+            if not idx then
+                core.log.error("no secret id")
+                return nil
+            end
 
-        local manager = sub(path, 1, idx - 1)
-        local id = sub(path, idx + 1)
+            local manager = sub(path, 1, idx - 1)
+            local id = sub(path, idx + 1)
 
-        if not secret_managers[manager] then
-            secret_managers[manager] = {}
+            if not secret_managers[manager] then
+                secret_managers[manager] = {}
+            end
+            secret_managers[manager][id] = v.value
         end
-        secret_managers[manager][id] = v.value
     end
 
     return secret_managers
diff --git a/t/secret/secret_lru.t b/t/secret/secret_lru.t
new file mode 100644
index 000000000..3ff3386fc
--- /dev/null
+++ b/t/secret/secret_lru.t
@@ -0,0 +1,98 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+log_level("info");
+run_tests;
+
+__DATA__
+
+=== TEST 1: add secret  && consumer && check
+--- request
+GET /t
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- put secret vault config
+            local code, body = t('/apisix/admin/secrets/vault/mysecret',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "http://127.0.0.1:8200";,
+                    "prefix": "kv-v1/apisix",
+                    "token": "root"
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+            -- change consumer with secrets ref: vault
+            code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "jack",
+                    "plugins": {
+                          "key-auth": {
+                            "key": "$secret://vault/mysecret/jack/auth-key"
+                        }
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+
+            local secret = require("apisix.secret")
+            local value = 
secret.fetch_by_uri("$secret://vault/mysecret/jack/auth-key")
+
+
+            local code, body = t('/apisix/admin/secrets/vault/mysecret', 
ngx.HTTP_DELETE)
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+            code, body = t('/apisix/admin/consumers',
+                ngx.HTTP_PUT,
+                [[{
+                    "username": "jack",
+                    "plugins": {
+                          "key-auth": {
+                            "key": "$secret://vault/mysecret/jack/auth-key"
+                        }
+                    }
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return ngx.say(body)
+            end
+
+            local secret = require("apisix.secret")
+            local value = 
secret.fetch_by_uri("$secret://vault/mysecret/jack/auth-key")
+            ngx.say(value)
+        }
+    }
+--- response_body
+nil

Reply via email to