opa

描述

opa 插件可用于与 Open Policy Agent 进行集成,实现后端服务的认证授权与访问服务等功能解耦,减少系统复杂性。

属性

名称类型必选项默认值有效值描述
hoststringOPA 服务的主机地址,例如 https://localhost:8181
ssl_verifybooleantrue当设置为 true 时,将验证 SSL 证书。
policystringOPA 策略路径,是 packagedecision 配置的组合。当使用高级功能(如自定义响应)时,你可以省略 decision 配置。
timeoutinteger3000ms[1, 60000]ms设置 HTTP 调用超时时间。
keepalivebooleantrue当设置为 true 时,将为多个请求保持连接并处于活动状态。
keepalive_timeoutinteger60000ms[1000, …]ms连接断开后的闲置时间。
keepalive_poolinteger5[1, …]ms连接池限制。
with_routebooleanfalse当设置为 true 时,发送关于当前 Route 的信息。
with_servicebooleanfalse当设置为 true 时,发送关于当前 Service 的信息。
with_consumerbooleanfalse当设置为 true 时,发送关于当前 Consumer 的信息。注意,这可能会发送敏感信息,如 API key。请确保在安全的情况下才打开它。

数据定义

APISIX 向 OPA 发送信息

下述示例代码展示了如何通过 APISIX 向 OPA 服务发送数据:

  1. {
  2. "type": "http",
  3. "request": {
  4. "scheme": "http",
  5. "path": "\/get",
  6. "headers": {
  7. "user-agent": "curl\/7.68.0",
  8. "accept": "*\/*",
  9. "host": "127.0.0.1:9080"
  10. },
  11. "query": {},
  12. "port": 9080,
  13. "method": "GET",
  14. "host": "127.0.0.1"
  15. },
  16. "var": {
  17. "timestamp": 1701234567,
  18. "server_addr": "127.0.0.1",
  19. "server_port": "9080",
  20. "remote_port": "port",
  21. "remote_addr": "ip address"
  22. },
  23. "route": {},
  24. "service": {},
  25. "consumer": {}
  26. }

上述代码具体释义如下:

  • type 代表请求类型(如 httpstream);
  • request 则需要在 typehttp 时使用,包含基本的请求信息(如 URL、头信息等);
  • var 包含关于请求连接的基本信息(如 IP、端口、请求时间戳等);
  • routeserviceconsumer 包含的数据与 APISIX 中存储的数据相同,只有当这些对象上配置了 opa 插件时才会发送。

OPA 向 APISIX 返回数据

下述示例代码展示了 OPA 服务对 APISIX 发送请求后的响应数据:

  1. {
  2. "result": {
  3. "allow": true,
  4. "reason": "test",
  5. "headers": {
  6. "an": "header"
  7. },
  8. "status_code": 401
  9. }
  10. }

上述响应中的代码释义如下:

  • allow 配置是必不可少的,它表示请求是否允许通过 APISIX 进行转发;
  • reasonheadersstatus_code 是可选的,只有当你配置一个自定义响应时才会返回这些选项信息,具体使用方法可查看后续测试用例。

测试插件

首先启动 OPA 环境:

  1. docker run -d --name opa -p 8181:8181 openpolicyagent/opa:0.35.0 run -s

基本用法

一旦你运行了 OPA 服务,就可以进行基本策略的创建:

  1. curl -X PUT '127.0.0.1:8181/v1/policies/example1' \
  2. -H 'Content-Type: text/plain' \
  3. -d 'package example1
  4. import input.request
  5. default allow = false
  6. allow {
  7. # HTTP method must GET
  8. request.method == "GET"
  9. }'

然后在指定路由上配置 opa 插件:

  1. curl -X PUT 'http://127.0.0.1:9080/apisix/admin/routes/r1' \
  2. -H 'X-API-KEY: <api-key>' \
  3. -H 'Content-Type: application/json' \
  4. -d '{
  5. "uri": "/*",
  6. "plugins": {
  7. "opa": {
  8. "host": "http://127.0.0.1:8181",
  9. "policy": "example1"
  10. }
  11. },
  12. "upstream": {
  13. "nodes": {
  14. "httpbin.org:80": 1
  15. },
  16. "type": "roundrobin"
  17. }
  18. }'

使用如下命令进行测试:

  1. curl -i -X GET 127.0.0.1:9080/get
  1. HTTP/1.1 200 OK

如果尝试向不同的端点发出请求,会出现请求失败的状态:

  1. curl -i -X POST 127.0.0.1:9080/post
  1. HTTP/1.1 403 FORBIDDEN

使用自定义响应

除了基础用法外,你还可以为更复杂的使用场景配置自定义响应,参考示例如下:

  1. curl -X PUT '127.0.0.1:8181/v1/policies/example2' \
  2. -H 'Content-Type: text/plain' \
  3. -d 'package example2
  4. import input.request
  5. default allow = false
  6. allow {
  7. request.method == "GET"
  8. }
  9. # custom response body (Accepts a string or an object, the object will respond as JSON format)
  10. reason = "test" {
  11. not allow
  12. }
  13. # custom response header (The data of the object can be written in this way)
  14. headers = {
  15. "Location": "http://example.com/auth"
  16. } {
  17. not allow
  18. }
  19. # custom response status code
  20. status_code = 302 {
  21. not allow
  22. }'

同时,你可以将 opa 插件的策略参数调整为 example2,然后发出请求进行测试:

  1. curl -i -X GET 127.0.0.1:9080/get
  1. HTTP/1.1 200 OK

此时如果你发出一个失败请求,将会收到来自 OPA 服务的自定义响应反馈,如下所示:

  1. curl -i -X POST 127.0.0.1:9080/post
  1. HTTP/1.1 302 FOUND
  2. Location: http://example.com/auth
  3. test

发送 APISIX 数据

如果你的 OPA 服务需要根据 APISIX 的某些数据(如 Route 和 Consumer 的详细信息)来进行后续操作时,则可以通过配置插件来实现。

下述示例展示了一个简单的 echo 策略,它将原样返回 APISIX 发送的数据:

  1. curl -X PUT '127.0.0.1:8181/v1/policies/echo' \
  2. -H 'Content-Type: text/plain' \
  3. -d 'package echo
  4. allow = false
  5. reason = input'

现在就可以在路由上配置插件来发送 APISIX 数据:

  1. curl -X PUT 'http://127.0.0.1:9080/apisix/admin/routes/r1' \
  2. -H 'X-API-KEY: <api-key>' \
  3. -H 'Content-Type: application/json' \
  4. -d '{
  5. "uri": "/*",
  6. "plugins": {
  7. "opa": {
  8. "host": "http://127.0.0.1:8181",
  9. "policy": "echo",
  10. "with_route": true
  11. }
  12. },
  13. "upstream": {
  14. "nodes": {
  15. "httpbin.org:80": 1
  16. },
  17. "type": "roundrobin"
  18. }
  19. }'

此时如果你提出一个请求,则可以通过自定义响应看到来自路由的数据:

  1. curl -X GET 127.0.0.1:9080/get
  1. {
  2. "type": "http",
  3. "request": {
  4. xxx
  5. },
  6. "var": {
  7. xxx
  8. },
  9. "route": {
  10. xxx
  11. }
  12. }

禁用插件

当你需要禁用 opa 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:2379/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/hello",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1980": 1
  10. }
  11. }
  12. }'