Skip to content

Commit c8d5afb

Browse files
change(jwt-auth): unify apisix/core/vault.lua and apisix/secret/vault.lua (#8660)
Fixes #8424
1 parent fab68d7 commit c8d5afb

File tree

10 files changed

+16
-795
lines changed

10 files changed

+16
-795
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ A/B testing, canary release, blue-green deployment, limit rate, defense against
140140
- [Elasticsearch](docs/en/latest/plugins/elasticsearch-logger.md): push logs to Elasticsearch.
141141
- [Datadog](docs/en/latest/plugins/datadog.md): push custom metrics to the DogStatsD server, comes bundled with [Datadog agent](https://docs.datadoghq.com/agent/), over the UDP protocol. DogStatsD basically is an implementation of StatsD protocol which collects the custom metrics for Apache APISIX agent, aggregates it into a single data point and sends it to the configured Datadog server.
142142
- [Helm charts](https://github.com/apache/apisix-helm-chart)
143-
- [HashiCorp Vault](https://www.vaultproject.io/): Support secret management solution for accessing secrets from Vault secure storage backed in a low trust environment. Currently, RS256 keys (public-private key pairs) or secret keys can be linked from vault in [jwt-auth](docs/en/latest/plugins/jwt-auth.md#enable-jwt-auth-with-vault-compatibility) authentication plugin.
143+
- [HashiCorp Vault](https://www.vaultproject.io/): Support secret management solution for accessing secrets from Vault secure storage backed in a low trust environment. Currently, RS256 keys (public-private key pairs) or secret keys can be linked from vault in jwt-auth authentication plugin using [APISIX Secret](docs/en/latest/terminology/secret.md) resource.
144144

145145
- **Highly scalable**
146146
- [Custom plugins](docs/en/latest/plugin-develop.md): Allows hooking of common phases, such as `rewrite`, `access`, `header filter`, `body filter` and `log`, also allows to hook the `balancer` stage.

apisix/core/vault.lua

Lines changed: 0 additions & 127 deletions
This file was deleted.

apisix/plugins/jwt-auth.lua

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ local core = require("apisix.core")
1818
local jwt = require("resty.jwt")
1919
local consumer_mod = require("apisix.consumer")
2020
local resty_random = require("resty.random")
21-
local vault = require("apisix.core.vault")
2221
local new_tab = require ("table.new")
2322

2423
local ngx_encode_base64 = ngx.encode_base64
@@ -71,10 +70,6 @@ local consumer_schema = {
7170
type = "boolean",
7271
default = false
7372
},
74-
vault = {
75-
type = "object",
76-
properties = {}
77-
},
7873
lifetime_grace_period = {
7974
type = "integer",
8075
minimum = 0,
@@ -102,19 +97,6 @@ local consumer_schema = {
10297
},
10398
required = {"public_key", "private_key"},
10499
},
105-
{
106-
properties = {
107-
vault = {
108-
type = "object",
109-
properties = {}
110-
},
111-
algorithm = {
112-
enum = {"RS256", "ES256"},
113-
},
114-
},
115-
required = {"vault"},
116-
},
117-
118100
}
119101
}
120102
},
@@ -147,11 +129,6 @@ function _M.check_schema(conf, schema_type)
147129
return false, err
148130
end
149131

150-
if conf.vault then
151-
core.log.info("skipping jwt-auth schema validation with vault")
152-
return true
153-
end
154-
155132
if conf.algorithm ~= "RS256" and conf.algorithm ~= "ES256" and not conf.secret then
156133
conf.secret = ngx_encode_base64(resty_random.bytes(32, true))
157134
elseif conf.base64_secret then
@@ -161,8 +138,8 @@ function _M.check_schema(conf, schema_type)
161138
end
162139

163140
if conf.algorithm == "RS256" or conf.algorithm == "ES256" then
164-
-- Possible options are a) both are in vault, b) both in schema
165-
-- c) one in schema, another in vault.
141+
-- Possible options are a) public key is missing
142+
-- b) private key is missing
166143
if not conf.public_key then
167144
return false, "missing valid public key"
168145
end
@@ -243,25 +220,8 @@ local function fetch_jwt_token(conf, ctx)
243220
return val
244221
end
245222

246-
247-
local function get_vault_path(username)
248-
return "consumer/".. username .. "/jwt-auth"
249-
end
250-
251-
252223
local function get_secret(conf, consumer_name)
253224
local secret = conf.secret
254-
if conf.vault then
255-
local res, err = vault.get(get_vault_path(consumer_name))
256-
if not res then
257-
return nil, err
258-
end
259-
260-
if not res.data or not res.data.secret then
261-
return nil, "secret could not found in vault: " .. core.json.encode(res)
262-
end
263-
secret = res.data.secret
264-
end
265225

266226
if conf.base64_secret then
267227
return ngx_decode_base64(secret)
@@ -274,32 +234,16 @@ end
274234
local function get_rsa_or_ecdsa_keypair(conf, consumer_name)
275235
local public_key = conf.public_key
276236
local private_key = conf.private_key
277-
-- if keys are present in conf, no need to query vault (fallback)
237+
278238
if public_key and private_key then
279239
return public_key, private_key
240+
elseif public_key and not private_key then
241+
return nil, nil, "missing private key"
242+
elseif not public_key and private_key then
243+
return nil, nil, "missing public key"
244+
else
245+
return nil, nil, "public and private keys are missing"
280246
end
281-
282-
local vout = {}
283-
if conf.vault then
284-
local res, err = vault.get(get_vault_path(consumer_name))
285-
if not res then
286-
return nil, nil, err
287-
end
288-
289-
if not res.data then
290-
return nil, nil, "key pairs could not found in vault: " .. core.json.encode(res)
291-
end
292-
vout = res.data
293-
end
294-
295-
if not public_key and not vout.public_key then
296-
return nil, nil, "missing public key, not found in config/vault"
297-
end
298-
if not private_key and not vout.private_key then
299-
return nil, nil, "missing private key, not found in config/vault"
300-
end
301-
302-
return public_key or vout.public_key, private_key or vout.private_key
303247
end
304248

305249

apisix/secret/vault.lua

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ local _M = {
4545
schema = schema
4646
}
4747

48-
-- This code is copied from apisix/core/vault.lua.
49-
-- The functions in apisix/core/vault.lua are currently only used in the jwt-auth plugin,
50-
-- and it is not suitable to be placed in the core module of APISIX.
51-
--
52-
-- When KMS is fully functional, we will remove apisix/core/vault.lua.
53-
--
5448
local function make_request_to_vault(conf, method, key, data)
5549
local httpc = http.new()
5650
-- config timeout or default to 5000 ms

conf/config-default.yaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,6 @@ nginx_config: # config for render the template to generate n
250250
tars: 1m
251251
cas-auth: 10m
252252

253-
# HashiCorp Vault storage backend for sensitive data retrieval. The config shows an example of what APISIX expects if you
254-
# wish to integrate Vault for secret (sensetive string, public private keys etc.) retrieval. APISIX communicates with Vault
255-
# server HTTP APIs. By default, APISIX doesn't need this configuration.
256-
# vault:
257-
# host: "http://0.0.0.0:8200" # The host address where the vault server is running.
258-
# timeout: 10 # request timeout 30 seconds
259-
# token: root # Authentication token to access Vault HTTP APIs
260-
# prefix: kv/apisix # APISIX supports vault kv engine v1, where sensitive data are being stored
261-
# and retrieved through vault HTTP APIs. enabling a prefix allows you to better enforcement of
262-
# policies, generate limited scoped tokens and tightly control the data that can be accessed
263-
# from APISIX.
264-
265-
266253
#discovery: # service discovery center
267254
# dns:
268255
# servers:

docs/en/latest/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ APISIX facilitates interface traffic handling for websites, mobile and IoT appli
5050
- Multi-platform support: APISIX can run from bare-metal machines to Kubernetes providing a vendor neutral, multi-platform solution. It also provides integration to cloud services like AWS Lambda, Azure Function, Lua functions and Apache OpenWhisk.
5151
- Fully dynamic: APISIX supports hot-reloading, meaning you don't need to restart the service to reflect changes in the configuration.
5252
- Fine-grained routing: APISIX supports using all [built-in NGINX variables](http://nginx.org/en/docs/varindex.html) for routing. You can define custom matching functions to filter requests and match Route.
53-
- Ops-friendly: APISIX is renowned for its ops-friendliness by DevOps teams. It integrates with tools and platforms like [HashiCorp Vault](./plugins/jwt-auth.md#usage-with-hashicorp-vault), [Zipkin](./plugins/zipkin.md), [Apache SkyWalking](./plugins/skywalking.md), [Consul](./discovery/consul_kv.md), [Nacos](./discovery/nacos.md) and [Eureka](./discovery.md). With [APISIX Dashboard](https://github.com/apache/apisix-dashboard), operators can configure APISIX through an easy-to-use and intuitive UI.
53+
- Ops-friendly: APISIX is renowned for its ops-friendliness by DevOps teams. It integrates with tools and platforms like [HashiCorp Vault](./terminology/secret.md#use-vault-to-manage-secrets), [Zipkin](./plugins/zipkin.md), [Apache SkyWalking](./plugins/skywalking.md), [Consul](./discovery/consul_kv.md), [Nacos](./discovery/nacos.md) and [Eureka](./discovery.md). With [APISIX Dashboard](https://github.com/apache/apisix-dashboard), operators can configure APISIX through an easy-to-use and intuitive UI.
5454
- Multi-language Plugin support: APISIX supports multiple programming languages for Plugin development. Developers can choose a language-specific SDK to write custom Plugins.
5555

5656
## Key concepts

0 commit comments

Comments
 (0)