Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when delete the secret cause 500 error #10902

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions apisix/secret.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions t/secret/secret_lru.t
Original file line number Diff line number Diff line change
@@ -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")
hanqingwu marked this conversation as resolved.
Show resolved Hide resolved
ngx.say(value)
}
}
--- response_body
nil
Loading