跳转到内容
云栖回顾 | 2024 云栖大会微服务和网关相关演讲材料点此了解

OPA

功能说明

该插件实现了 OPA 策略控制

运行属性

插件执行阶段:认证阶段 插件执行优先级:225

配置字段

字段数据类型填写要求默认值描述
policystring必填-opa 策略
timeoutstring必填-访问超时时间设置
serviceSourcestring必填-k8s,nacos,ip,route
hoststring非必填-服务主机(serviceSource为ip必填)
serviceNamestring非必填-服务名称(serviceSource为k8s,nacos,ip必填)
servicePortstring非必填-服务端口(serviceSource为k8s,nacos,ip必填)
namespacestring非必填-服务端口(serviceSource为k8s,nacos必填)

配置示例

serviceSource: k8s
serviceName: opa
servicePort: 8181
namespace: higress-backend
policy: example1
timeout: 5s

OPA 服务安装参考

启动 OPA 服务

Terminal window
docker run -d --name opa -p 8181:8181 openpolicyagent/opa:0.35.0 run -s

创建 OPA 策略

Terminal window
curl -X PUT '127.0.0.1:8181/v1/policies/example1' \
-H 'Content-Type: text/plain' \
-d 'package example1
import input.request
default allow = false
allow {
# HTTP method must GET
request.method == "GET"
}'

查询策略

Terminal window
curl -X POST '127.0.0.1:8181/v1/data/example1/allow' \
-H 'Content-Type: application/json' \
-d '{"input":{"request":{"method":"GET"}}}'