Skip to content

Commit 0468d78

Browse files
authored
feat: support hcv namespace (#11277)
1 parent 693d2aa commit 0468d78

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

apisix/secret/vault.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ local schema = {
3737
token = {
3838
type = "string",
3939
},
40+
namespace = {
41+
type = "string",
42+
},
4043
},
4144
required = {"uri", "prefix", "token"},
4245
}
@@ -58,11 +61,18 @@ local function make_request_to_vault(conf, method, key, data)
5861
token = conf.token
5962
end
6063

64+
local headers = {
65+
["X-Vault-Token"] = token
66+
}
67+
if conf.namespace then
68+
-- The namespace rule is referenced in
69+
-- https://developer.hashicorp.com/vault/docs/enterprise/namespaces#vault-api-and-namespaces
70+
headers["X-Vault-Namespace"] = conf.namespace
71+
end
72+
6173
local res, err = httpc:request_uri(req_addr, {
6274
method = method,
63-
headers = {
64-
["X-Vault-Token"] = token
65-
},
75+
headers = headers,
6676
body = core.json.encode(data or {}, true)
6777
})
6878

docs/en/latest/admin-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@ When `{secretmanager}` is `vault`:
14661466
| uri | True | URI | URI of the vault server. | |
14671467
| prefix | True | string | key prefix
14681468
| token | True | string | vault token. | |
1469+
| namespace | False | string | Vault namespace, no default value | `admin` |
14691470
14701471
Example Configuration:
14711472

docs/en/latest/terminology/secret.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ curl http://127.0.0.1:9180/apisix/admin/consumers \
123123

124124
Through the above steps, the `key` configuration in the `key-auth` plugin can be saved in the environment variable instead of being displayed in plain text when configuring the plugin.
125125

126-
## Use Vault to manage secrets
126+
## Use HashiCorp Vault to manage secrets
127127

128-
Using Vault to manage secrets means that you can store secrets information in the Vault service and refer to it through variables in a specific format when configuring plugins. APISIX currently supports [Vault KV engine version V1](https://developer.hashicorp.com/vault/docs/secrets/kv/kv-v1).
128+
Using HashiCorp Vault to manage secrets means that you can store secrets information in the Vault service and refer to it through variables in a specific format when configuring plugins. APISIX currently supports [Vault KV engine version V1](https://developer.hashicorp.com/vault/docs/secrets/kv/kv-v1).
129129

130130
### Usage
131131

132132
```
133133
$secret://$manager/$id/$secret_name/$key
134134
```
135135

136-
- manager: secrets management service, could be the Vault, AWS, etc.
136+
- manager: secrets management service, could be the HashiCorp Vault, AWS, etc.
137137
- id: APISIX Secrets resource ID, which needs to be consistent with the one specified when adding the APISIX Secrets resource
138138
- secret_name: the secret name in the secrets management service
139139
- key: the key corresponding to the secret in the secrets management service
@@ -168,6 +168,12 @@ secrets:
168168
uri: 127.0.0.1:8200
169169
```
170170
171+
:::tip
172+
173+
It now supports the use of the [`namespace` field](../admin-api.md#request-body-parameters-11) to set the multi-tenant namespace concepts supported by [HashiCorp Vault Enterprise](https://developer.hashicorp.com/vault/docs/enterprise/namespaces#vault-api-and-namespaces) and HCP Vault.
174+
175+
:::
176+
171177
Step 3: Reference the APISIX Secrets resource in the `key-auth` plugin and fill in the key information:
172178

173179
```shell

docs/zh/latest/admin-api.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ Route 对象 JSON 配置示例:
358358
"desc": "hello world",
359359
"remote_addrs": ["127.0.0.1"], # 一组客户端请求 IP 地址
360360
"vars": [["http_user", "==", "ios"]], # 由一个或多个 [var, operator, val] 元素组成的列表
361-
"upstream_id": "1", # upstream 对象在 etcd 中的 id ,建议使用此值
361+
"upstream_id": "1", # upstream 对象在 etcd 中的 id,建议使用此值
362362
"upstream": {}, # upstream 信息对象,建议尽量不要使用
363363
"timeout": { # 为 route 设置 upstream 的连接、发送消息、接收消息的超时时间。
364364
"connect": 3,
@@ -652,7 +652,7 @@ Service 对象 JSON 配置示例:
652652
{
653653
"id": "1", # id
654654
"plugins": {}, # 指定 service 绑定的插件
655-
"upstream_id": "1", # upstream 对象在 etcd 中的 id ,建议使用此值
655+
"upstream_id": "1", # upstream 对象在 etcd 中的 id,建议使用此值
656656
"upstream": {}, # upstream 信息对象,不建议使用
657657
"name": "test svc", # service 名称
658658
"desc": "hello world", # service 描述
@@ -1476,6 +1476,7 @@ Secret 资源请求地址:/apisix/admin/secrets/{secretmanager}/{id}
14761476
| uri | 是 | URI | Vault 服务器的 URI | |
14771477
| prefix | 是 | 字符串 | 密钥前缀
14781478
| token | 是 | 字符串 | Vault 令牌 | |
1479+
| namespace | 否 | 字符串 | Vault 命名空间,该字段无默认值 | `admin` |
14791480
14801481
配置示例:
14811482

docs/zh/latest/terminology/secret.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ secrets:
169169
uri: 127.0.0.1:8200
170170
```
171171
172+
:::tip
173+
174+
它现在支持使用 [`namespace` 字段](../admin-api.md#secret-config-body-requset-parameters] 设置 [HashiCorp Vault Enterprise](https://developer.hashicorp.com/vault/docs/enterprise/namespaces#vault-api-and-namespaces) 和 HCP Vault 所支持的多租户命名空间概念。
175+
176+
:::
177+
172178
第三步:在 `key-auth` 插件中引用 APISIX Secret 资源,填充秘钥信息:
173179

174180
```shell

t/secret/vault.t

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,60 @@ failed to decode result, res: \{\"errors\":\[\"permission denied\"\]}\n
236236
GET /t
237237
--- response_body_like
238238
failed to decode result, res: \{\"errors\":\[\"permission denied\"\]}\n
239+
240+
241+
242+
=== TEST 10: setup route
243+
--- config
244+
location /t {
245+
content_by_lua_block {
246+
local t = require("lib.test_admin").test
247+
local code, body = t('/apisix/admin/routes/1',
248+
ngx.HTTP_PUT,
249+
[[{
250+
"plugins": {
251+
"serverless-pre-function": {
252+
"phase": "access",
253+
"functions": [
254+
"return function(conf, ctx) ngx.log(ngx.ERR, 'HCV_NAMESAPCE:'..(ctx.var.http_x_vault_namespace or '_')); require('apisix.core').response.exit(200); end"
255+
]
256+
}
257+
},
258+
"uri": "/*"
259+
}]]
260+
)
261+
262+
if code >= 300 then
263+
ngx.status = code
264+
end
265+
ngx.say(body)
266+
}
267+
}
268+
--- request
269+
GET /t
270+
--- response_body
271+
passed
272+
273+
274+
275+
=== TEST 11: hit route (test namespace header)
276+
--- config
277+
location /t {
278+
content_by_lua_block {
279+
local vault = require("apisix.secret.vault")
280+
local conf = {
281+
prefix = "kv/apisix",
282+
token = "test",
283+
uri = "http://localhost:1984/mock",
284+
namespace = "apisix",
285+
}
286+
local value, err = vault.get(conf, "/apisix-key/jack/key")
287+
if err then
288+
return ngx.say(err)
289+
end
290+
}
291+
}
292+
--- request
293+
GET /t
294+
--- error_log
295+
HCV_NAMESAPCE:apisix

0 commit comments

Comments
 (0)