You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/12/09 01:56:40 UTC
[GitHub] [apisix] spacewander commented on a change in pull request #5745: feat(vault): vault lua module, integration with jwt-auth authentication plugin
spacewander commented on a change in pull request #5745:
URL: https://github.com/apache/apisix/pull/5745#discussion_r765369332
##########
File path: apisix/plugins/jwt-auth.lua
##########
@@ -76,7 +84,23 @@ local consumer_schema = {
},
},
required = {"public_key", "private_key"},
- }
+ },
+ {
+ properties = {
+ vault = {
+ type = "object",
+ properties = {
+ path = {type = "string"},
+ add_prefix = {type = "boolean"}
Review comment:
We should not allow users to specify path & add_prefix in the MVP, which will make thing very complex.
First, people may make mistakes, like using the same path according to multiple consumers.
Second, we need to enforce users to put their data under the same namespace, so we can provide a consistent token for APISIX to read the data.
##########
File path: apisix/admin/consumers.lua
##########
@@ -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.
Review comment:
IMHO, it is not a good idea to merge the vault data with etcd response. Now everyone who can access Admin API could access the vault data, which is against the reason to use vault.
##########
File path: apisix/plugins/jwt-auth.lua
##########
@@ -198,7 +312,11 @@ end
local function sign_jwt_with_HS(key, auth_conf, payload)
- local auth_secret = get_secret(auth_conf)
+ local auth_secret, err = get_secret(auth_conf)
+ if not auth_secret then
+ core.log.error("failed to sign jwt, err: ", err)
+ core.response.exit(500, "failed to sign jwt")
Review comment:
```suggestion
core.response.exit(503, "failed to sign jwt")
```
Use 503 for known failure.
##########
File path: apisix/core/vault.lua
##########
@@ -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"}
Review comment:
For write operation, better to return a single boolean, so the caller can check if the call success without knowing the internal detail.
##########
File path: apisix/plugins/jwt-auth.lua
##########
@@ -119,29 +143,74 @@ function _M.check_schema(conf, schema_type)
if schema_type == core.schema.TYPE_CONSUMER then
ok, err = core.schema.check(consumer_schema, conf)
else
- ok, err = core.schema.check(schema, conf)
+ return core.schema.check(schema, conf)
end
if not ok then
return false, err
end
- if schema_type == core.schema.TYPE_CONSUMER then
- if conf.algorithm ~= "RS256" and not conf.secret then
- conf.secret = ngx_encode_base64(resty_random.bytes(32, true))
+ -- in nginx init_worker_by_lua context API calls are disabled,
+ -- also that is a costly operation during system startup.
+ if ngx.get_phase() == "init_worker" then
Review comment:
We can check the data in Vault, but in the Admin API during creating. Sending a bundle of requests in full sync (which is not only happened during system startup) is too expensive. For instance, we don't connect to aws-lambda in the check schema. Even the data is valid, people can still change it later as the data does not come from the major data source.
--
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