Key Auth
key-auth 插件支持使用身份认证密钥作为客户端在访问上游资源之前进行自我身份认证的机制。
要使用该插件,你需要在消费者(Consumer)上配置身份认证密钥,并在路由或服务上启用该插件。密钥可以包含在请求 URL 查询字符串或请求头中。APISIX 将验证密钥,以确定是否允许请求访问上游资源。
消费者成功通过身份认证后,APISIX 会在将请求代理到上游服务之前添加额外的请求头,例如 X-Consumer-Username、X-Credential-Identifier;如果配置了消费者自定义请求头,也会一并添加。上游服务可以据此区分消费者并执行额外逻辑。如果这些值不可用,则不会添加相应的请求头。
使用 Ingress Controller 配置消费者时,消费者名称会生成为 namespace_consumername 格式。因此,X-Consumer-Username 请求头也会采用此格式,而不只是 consumername。
示例
以下示例展示了如何在不同场景下使用 key-auth 插件。
在路由上实现密钥认证
以下示例展示了如何在路由上实现密钥认证,并在请求头中携带密钥。
- Admin API
- ADC
- Ingress Controller
创建一个消费者 jack:
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "jack"
}'
为该消费者创建 key-auth 凭证:
curl "http://127.0.0.1:9180/apisix/admin/consumers/jack/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-jack-key-auth",
"plugins": {
"key-auth": {
"key": "jack-key"
}
}
}'
创建一个启用 key-auth 的路由:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "key-auth-route",
"uri": "/anything",
"plugins": {
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
创建一个配置了 key-auth 凭证的消费者,以及一个配置了 key-auth 插件的路由:
consumers:
- username: jack
credentials:
- name: key-auth
type: key-auth
config:
key: jack-key
services:
- name: key-auth-service
routes:
- name: key-auth-route
uris:
- /anything
plugins:
key-auth: {}
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更并确认仅影响本指南的资源:
adc diff -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=key-auth
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=key-auth
创建一个配置了 key-auth 凭证的消费者,以及一个配置了 key-auth 插件的路由:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: jack
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: primary-cred
config:
key: jack-key
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: key-auth-plugin-config
spec:
plugins:
- name: key-auth
config:
_meta:
disable: false
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: key-auth-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: key-auth-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: jack
spec:
ingressClassName: apisix
authParameter:
keyAuth:
value:
key: jack-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: key-auth-route
spec:
ingressClassName: apisix
http:
- name: key-auth-route
match:
paths:
- /anything
upstreams:
- name: httpbin-external-domain
plugins:
- name: key-auth
enable: true
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
使用有效密钥进行验证
使用有效密钥发送请求:
curl -i "http://127.0.0.1:9080/anything" -H 'apikey: jack-key'
你应该会收到 HTTP/1.1 200 OK 响应。
使用无效密钥进行验证
使用无效密钥发送请求:
curl -i "http://127.0.0.1:9080/anything" -H 'apikey: wrong-key'
你应该会看到包含以下内容的 HTTP/1.1 401 Unauthorized 响应:
{"message":"Invalid API key in request"}
未提供密钥进行验证
未提供密钥发送请求:
curl -i "http://127.0.0.1:9080/anything"
你应该会看到包含以下内容的 HTTP/1.1 401 Unauthorized 响应:
{"message":"Missing API key found in request"}
向上游隐藏认证信息
以下示例展示了如何通过配置 hide_credentials 来防止密钥被发送到上游服务。默认情况下,认证密钥会被转发到上游服务,这在某些情况下可能会导致安全风险。
- Admin API
- ADC
- Ingress Controller
创建一个消费者 jack:
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "jack"
}'
为该消费者创建 key-auth 凭证:
curl "http://127.0.0.1:9180/apisix/admin/consumers/jack/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-jack-key-auth",
"plugins": {
"key-auth": {
"key": "jack-key"
}
}
}'
不隐藏凭证
创建一个启用 key-auth 的路由,并将 hide_credentials 配置为 false(这是默认配置):
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "key-auth-route",
"uri": "/anything",
"plugins": {
"key-auth": {
"hide_credentials": false
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
创建一个配置了 key-auth 凭证的消费者,以及一个配置了 key-auth 插件的路由:
consumers:
- username: jack
credentials:
- name: key-auth
type: key-auth
config:
key: jack-key
services:
- name: key-auth-service
routes:
- name: key-auth-route
uris:
- /anything
plugins:
key-auth:
hide_credentials: false
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更并确认仅影响本指南的资源:
adc diff -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=key-auth
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=key-auth
创建一个配置了 key-auth 凭证的消费者,以及一个配置了 key-auth 插件的路由:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: jack
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: primary-cred
config:
key: jack-key
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: key-auth-plugin-config
spec:
plugins:
- name: key-auth
config:
_meta:
disable: false
hide_credentials: false
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: key-auth-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: key-auth-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: jack
spec:
ingressClassName: apisix
authParameter:
keyAuth:
value:
key: jack-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: key-auth-route
spec:
ingressClassName: apisix
http:
- name: key-auth-route
match:
paths:
- /anything
upstreams:
- name: httpbin-external-domain
plugins:
- name: key-auth
enable: true
config:
hide_credentials: false
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
使用有效密钥发送请求:
curl -i "http://127.0.0.1:9080/anything?apikey=jack-key"
你应该会看到包含以下内容的 HTTP/1.1 200 OK 响应:
{
"args": {
"apikey": "jack-key"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.2.1",
"X-Consumer-Username": "jack",
"X-Credential-Identifier": "cred-jack-key-auth",
"X-Amzn-Trace-Id": "Root=1-6502d8a5-2194962a67aa21dd33f94bb2",
"X-Forwarded-Host": "127.0.0.1"
},
"json": null,
"method": "GET",
"origin": "127.0.0.1, 103.248.35.179",
"url": "http://127.0.0.1/anything?apikey=jack-key"
}
注意,凭证 jack-key 对上游服务可见。
隐藏凭证
- Admin API
- ADC
- Ingress Controller
将插件的 hide_credentials 更新为 true:
curl "http://127.0.0.1:9180/apisix/admin/routes/key-auth-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"key-auth": {
"hide_credentials": true
}
}
}'
更新路由配置:
services:
- name: key-auth-service
routes:
- name: key-auth-route
uris:
- /anything
plugins:
key-auth:
hide_credentials: true
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更并确认仅影响本指南的资源:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=key-auth
将配置同步到网关:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=key-auth
- Gateway API
- APISIX CRD
更新 PluginConfig,将 hide_credentials 设置为 true:
# 其他配置
# ---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: key-auth-plugin-config
spec:
plugins:
- name: key-auth
config:
_meta:
disable: false
hide_credentials: true
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
更新 ApisixRoute,将 hide_credentials 设置为 true:
# 其他配置
# ---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: key-auth-route
spec:
ingressClassName: apisix
http:
- name: key-auth-route
match:
paths:
- /anything
upstreams:
- name: httpbin-external-domain
plugins:
- name: key-auth
enable: true
config:
hide_credentials: true
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
使用有效密钥发送请求:
curl -i "http://127.0.0.1:9080/anything?apikey=jack-key"
你应该会看到包含以下内容的 HTTP/1.1 200 OK 响应:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "127.0.0.1",
"User-Agent": "curl/8.2.1",
"X-Consumer-Username": "jack",
"X-Credential-Identifier": "cred-jack-key-auth",
"X-Amzn-Trace-Id": "Root=1-6502d85c-16f34dbb5629a5960183e803",
"X-Forwarded-Host": "127.0.0.1"
},
"json": null,
"method": "GET",
"origin": "127.0.0.1, 103.248.35.179",
"url": "http://127.0.0.1/anything"
}
注意,凭证 jack-key 不再对上游服务可见。
演示请求头和查询参数中密钥的优先级
以下示例展示了如何在路由上按消费者实现密钥认证,并自定义用于携带密钥的 URL 参数。该示例还表明,当 API 密钥同时出现在请求头和查询字符串中时,请求头具有更高的优先级。
- Admin API
- ADC
- Ingress Controller
创建一个消费者 jack:
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "jack"
}'
为该消费者创建 key-auth 凭证:
curl "http://127.0.0.1:9180/apisix/admin/consumers/jack/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-jack-key-auth",
"plugins": {
"key-auth": {
"key": "jack-key"
}
}
}'
创建一个启用 key-auth 的路由:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "key-auth-route",
"uri": "/anything",
"plugins": {
"key-auth": {
"query": "auth"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
创建一个配置了 key-auth 凭证的消费者,以及一个配置了 key-auth 插件的路由:
consumers:
- username: jack
credentials:
- name: key-auth
type: key-auth
config:
key: jack-key
services:
- name: key-auth-service
routes:
- name: key-auth-route
uris:
- /anything
plugins:
key-auth:
query: auth
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览变更并确认仅影响本指南的资源:
adc diff -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=key-auth
将配置同步到 网关:
adc sync -f adc.yaml \
--include-resource-type consumer \
--include-resource-type service \
--label-selector docs-example=key-auth
创建一个配置了 key-auth 凭证的消费者,以及一个配置了 key-auth 插件的路由:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: jack
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: primary-cred
config:
key: jack-key
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: key-auth-plugin-config
spec:
plugins:
- name: key-auth
config:
_meta:
disable: false
query: auth
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: key-auth-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: key-auth-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: jack
spec:
ingressClassName: apisix
authParameter:
keyAuth:
value:
key: jack-key
---
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: key-auth-route
spec:
ingressClassName: apisix
http:
- name: key-auth-route
match:
paths:
- /anything
upstreams:
- name: httpbin-external-domain
plugins:
- name: key-auth
enable: true
config:
query: auth
将配置应用到集群:
kubectl apply -f key-auth-ic.yaml
使用有效密钥进行验证
使用有效密钥发送请求:
curl -i "http://127.0.0.1:9080/anything?auth=jack-key"
你应该会收到 HTTP/1.1 200 OK 响应。
使用无效密钥进行验证
使用无效密钥发送请求:
curl -i "http://127.0.0.1:9080/anything?auth=wrong-key"
你应该会看到包含以下内容的 HTTP/1.1 401 Unauthorized 响应:
{"message":"Invalid API key in request"}