-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat(admin): add kms admin api #8394
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0df2f44
feat(admin): add kms admin api
kingluo 140cd09
add code
kingluo 8f38567
fix lint
kingluo 5141f6f
fix PR
kingluo e87027b
fix PR
kingluo 128b88d
fix PR
kingluo 512c7f4
use /kms, read all without dir indicator
kingluo 2619b8d
add test case for schema check
kingluo 3074631
Revert "use /kms, read all without dir indicator"
kingluo a7c86d1
add both /kms and /kms/vault in constants.lua
kingluo 48d762d
add doc
kingluo b9c458d
Revert "add both /kms and /kms/vault in constants.lua"
kingluo 49f374c
refactoring
kingluo b711b03
fix kms id as {secretmanager}/{id}
kingluo ab9f642
fix PR
kingluo a7564f2
fix PR
kingluo d9dae5e
fix PR
kingluo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
-- | ||
-- 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. | ||
-- | ||
local core = require("apisix.core") | ||
local utils = require("apisix.admin.utils") | ||
local type = type | ||
local tostring = tostring | ||
|
||
|
||
local _M = { | ||
need_v3_filter = true, | ||
} | ||
|
||
|
||
local function check_conf(id, conf, need_id, typ) | ||
if not conf then | ||
return nil, {error_msg = "missing configurations"} | ||
end | ||
|
||
id = id or conf.id | ||
if need_id and not id then | ||
return nil, {error_msg = "missing id"} | ||
end | ||
|
||
if not need_id and id then | ||
return nil, {error_msg = "wrong id, do not need it"} | ||
end | ||
|
||
if need_id and conf.id and tostring(conf.id) ~= tostring(id) then | ||
return nil, {error_msg = "wrong id"} | ||
end | ||
|
||
conf.id = id | ||
|
||
core.log.info("conf: ", core.json.delay_encode(conf)) | ||
local ok, err = core.schema.check(core.schema["kms_" .. typ], conf) | ||
if not ok then | ||
return nil, {error_msg = "invalid configuration: " .. err} | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
||
local function split_typ_and_id(id, sub_path) | ||
local uri_segs = core.utils.split_uri(sub_path) | ||
local typ = id | ||
local id = nil | ||
if #uri_segs > 0 then | ||
id = uri_segs[1] | ||
end | ||
return typ, id | ||
end | ||
|
||
|
||
function _M.put(id, conf, sub_path) | ||
local typ, id = split_typ_and_id(id, sub_path) | ||
if not id then | ||
return 400, {error_msg = "no kms id in uri"} | ||
end | ||
|
||
local ok, err = check_conf(typ .. "/" .. id, conf, true, typ) | ||
if not ok then | ||
return 400, err | ||
end | ||
|
||
local key = "/kms/" .. typ .. "/" .. id | ||
|
||
local ok, err = utils.inject_conf_with_prev_conf("kms", key, conf) | ||
if not ok then | ||
return 503, {error_msg = err} | ||
end | ||
|
||
local res, err = core.etcd.set(key, conf) | ||
if not res then | ||
core.log.error("failed to put kms [", key, "]: ", err) | ||
return 503, {error_msg = err} | ||
end | ||
|
||
return res.status, res.body | ||
end | ||
|
||
|
||
function _M.get(id, conf, sub_path) | ||
local typ, id = split_typ_and_id(id, sub_path) | ||
|
||
local key = "/kms/" | ||
if id then | ||
key = key .. typ | ||
key = key .. "/" .. id | ||
end | ||
|
||
local res, err = core.etcd.get(key, not id) | ||
if not res then | ||
core.log.error("failed to get kms [", key, "]: ", err) | ||
return 503, {error_msg = err} | ||
end | ||
|
||
utils.fix_count(res.body, id) | ||
return res.status, res.body | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
end | ||
|
||
|
||
function _M.delete(id, conf, sub_path) | ||
local typ, id = split_typ_and_id(id, sub_path) | ||
if not id then | ||
return 400, {error_msg = "no kms id in uri"} | ||
end | ||
|
||
local key = "/kms/" .. typ .. "/" .. id | ||
|
||
local res, err = core.etcd.delete(key) | ||
if not res then | ||
core.log.error("failed to delete kms [", key, "]: ", err) | ||
return 503, {error_msg = err} | ||
end | ||
|
||
return res.status, res.body | ||
end | ||
|
||
|
||
function _M.patch(id, conf, sub_path) | ||
local uri_segs = core.utils.split_uri(sub_path) | ||
if #uri_segs < 2 then | ||
return 400, {error_msg = "no kms id and/or sub path in uri"} | ||
end | ||
local typ = id | ||
id = uri_segs[1] | ||
sub_path = core.table.concat(uri_segs, "/", 2) | ||
|
||
if not id then | ||
return 400, {error_msg = "missing kms id"} | ||
end | ||
|
||
if not conf then | ||
return 400, {error_msg = "missing new configuration"} | ||
end | ||
|
||
if not sub_path or sub_path == "" then | ||
if type(conf) ~= "table" then | ||
return 400, {error_msg = "invalid configuration"} | ||
end | ||
end | ||
|
||
local key = "/kms/" .. typ .. "/" .. id | ||
local res_old, err = core.etcd.get(key) | ||
if not res_old then | ||
core.log.error("failed to get kms [", key, "]: ", err) | ||
return 503, {error_msg = err} | ||
end | ||
|
||
if res_old.status ~= 200 then | ||
return res_old.status, res_old.body | ||
end | ||
core.log.info("key: ", key, " old value: ", | ||
core.json.delay_encode(res_old, true)) | ||
|
||
local node_value = res_old.body.node.value | ||
local modified_index = res_old.body.node.modifiedIndex | ||
|
||
if sub_path and sub_path ~= "" then | ||
local code, err, node_val = core.table.patch(node_value, sub_path, conf) | ||
node_value = node_val | ||
if code then | ||
return code, err | ||
end | ||
utils.inject_timestamp(node_value, nil, true) | ||
else | ||
node_value = core.table.merge(node_value, conf) | ||
utils.inject_timestamp(node_value, nil, conf) | ||
end | ||
|
||
core.log.info("new conf: ", core.json.delay_encode(node_value, true)) | ||
|
||
local ok, err = check_conf(typ .. "/" .. id, node_value, true, typ) | ||
if not ok then | ||
return 400, {error_msg = err} | ||
end | ||
|
||
local res, err = core.etcd.atomic_set(key, node_value, nil, modified_index) | ||
if not res then | ||
core.log.error("failed to set new kms[", key, "]: ", err) | ||
return 503, {error_msg = err} | ||
end | ||
|
||
return res.status, res.body | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kingluo, @soulbird
conf
is unused.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to be consistent with the operation functions of other resources, and they will be called uniformly here https://github.com/apache/apisix/blob/master/apisix/admin/init.lua#L201
In fact, we are refactoring this part, you are welcome to participate in the review #8611