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

feat(vault): vault lua module, integration with jwt-auth authentication plugin #5745

Merged
merged 28 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b2788ff
vault-auth init
bisakhmondal Dec 3, 2021
a88c615
vault storage kv engine integration
bisakhmondal Dec 6, 2021
17b3c23
not required file
bisakhmondal Dec 6, 2021
7605c38
integrating vault storage backend with jwt-auth authentication plugin
bisakhmondal Dec 8, 2021
ae240ed
Merge branch 'master' into vault-jwt
bisakhmondal Dec 9, 2021
876cce3
Merge branch 'master' into vault-jwt
bisakhmondal Dec 9, 2021
c3a7d4a
openssl rsa-2048 pem public private keypairs
bisakhmondal Dec 9, 2021
ed628b2
vault integration tests with corner cases
bisakhmondal Dec 9, 2021
9ec682a
minor updates
bisakhmondal Dec 9, 2021
36f0141
adding real vault server into CIs
bisakhmondal Dec 9, 2021
c3aaf8f
lint fix
bisakhmondal Dec 9, 2021
80358b9
suggestions
bisakhmondal Dec 9, 2021
e4d10da
now get doesnot returns vault data
bisakhmondal Dec 9, 2021
f927fb9
update exposed port address
bisakhmondal Dec 9, 2021
ee251aa
documentation
bisakhmondal Dec 9, 2021
6158837
blank commit
bisakhmondal Dec 9, 2021
f9cdc4e
remove custom path support from mvp
bisakhmondal Dec 10, 2021
6729106
trimming down validation and key generation if vault config is enabled
bisakhmondal Dec 10, 2021
83b3fe0
remove redundant codes
bisakhmondal Dec 10, 2021
58292d2
Ci fix
bisakhmondal Dec 10, 2021
55c105d
changing vault kv suffix to /consumer/<username>/jwt-auth
bisakhmondal Dec 10, 2021
1f2ff22
update tests and modify the way http status code were sent
bisakhmondal Dec 10, 2021
cac28d1
fix doc broken link
bisakhmondal Dec 10, 2021
f78cf89
comment out vault config in yaml and update tests accordingly
bisakhmondal Dec 12, 2021
6a28225
Merge branch 'master' into vault-jwt
bisakhmondal Dec 13, 2021
2d44654
change yaml_config to extra_yaml_config
bisakhmondal Dec 13, 2021
66ee305
single extra yaml config
bisakhmondal Dec 13, 2021
a56ed8e
suggestion
bisakhmondal Dec 14, 2021
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
28 changes: 28 additions & 0 deletions apisix/admin/consumers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local core = require("apisix.core")
local plugins = require("apisix.admin.plugins")
local utils = require("apisix.admin.utils")
local plugin = require("apisix.plugin")
local vault = require("apisix.core.vault")
local pairs = pairs

local _M = {
Expand Down Expand Up @@ -102,6 +103,33 @@ function _M.get(consumer_name)
end

utils.fix_count(res.body, consumer_name)

if consumer_name then
-- if data is queried for a single consumer, and there is any plugin where the vault config
-- is enabled - it fetches vault data and returns combined with etcd response.
bisakhmondal marked this conversation as resolved.
Show resolved Hide resolved
local vault_fetch = {}
local attach_response = false
local _plugins = res.body.node.value.plugins or {}
for plugin_name, _schema in pairs(_plugins) do
if _schema.vault then
local res, err = vault.get(_schema.vault.path, _schema.vault.add_prefix)
if not res then
core.log.error("failed to get data from vault for plugin: ", plugin_name,
"err: ", err)
else
attach_response = true
vault_fetch[plugin_name] = res.data
end
end
end

if attach_response then
res.body.vault = {
["data-fetched"] = vault_fetch
}
end
end

return res.status, res.body
end

Expand Down
116 changes: 116 additions & 0 deletions apisix/core/vault.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
--
-- 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 http = require("resty.http")
local json = require("cjson")

local fetch_local_conf = require("apisix.core.config_local").local_conf
local norm_path = require("pl.path").normpath

local _M = {}

local function fetch_vault_conf()
local conf, err = fetch_local_conf()
if not conf then
return nil, "failed to fetch vault configuration from config yaml: " .. err
end

if not conf.vault then
return nil, "accessing vault data requires configuration information"
end
return conf.vault
end


local function make_request_to_vault(method, key, rel_path, data)
local vault, err = fetch_vault_conf()
if not vault then
return nil, err
end

local httpc = http.new()
-- config timeout or default to 5000 ms
httpc:set_timeout((vault.timeout or 5)*1000)

local req_addr = vault.host
if rel_path then
req_addr = req_addr .. norm_path("/v1/"
.. vault.prefix .. "/" .. key)
else
req_addr = req_addr .. norm_path("/" .. key)
end

local res, err = httpc:request_uri(req_addr, {
method = method,
headers = {
["X-Vault-Token"] = vault.token
},
body = core.json.encode(data or {}, true)
})
if not res then
return nil, err
end

return res.body
end

-- key is the vault kv engine path, joined with config yaml vault prefix
local function get(key, rel_path)
core.log.info("fetching data from vault for key: ", key)

local res, err = make_request_to_vault("GET", key, rel_path)
if not res or err then
return nil, "failed to retrtive data from vault kv engine " .. err
end

return json.decode(res)
end

_M.get = get

-- key is the vault kv engine path, data is json key vaule pair
local function set(key, data, rel_path)
core.log.info("stroing data into vault for key: ", key,
"and value: ", core.json.delay_encode(data, true))

local res, err = make_request_to_vault("POST", key, rel_path, data)
if not res or err then
return nil, "failed to store data into vault kv engine " .. err
end

return {status = "success"}
bisakhmondal marked this conversation as resolved.
Show resolved Hide resolved
end
_M.set = set


-- key is the vault kv engine path, joined with config yaml vault prefix
local function delete(key, rel_path)
core.log.info("deleting data from vault for key: ", key)

local res, err = make_request_to_vault("DELETE", key, rel_path)

if not res or err then
return nil, "failed to delete data into vault kv engine " .. err
end

return {status = "success"}
end

_M.delete = delete

return _M
Loading