使用 API 来管理 API 安全性
您可以访问 API 来创建、配置和部署 API 安全性。
注意:
要了解如何使用 API 安全性 API 来配置该功能,请参阅 Nitro API 文档。
| 步骤 | 资源 URL | ||
|---|---|---|---|
| 1 | 创建 API 定义 | https://adm.cloud.com/{customerid}/apisec/nitro/v1/config/apidefs | |
| 2 | 添加 API 代理 | https://adm.cloud.com/apiproxies | |
| 3 | 使用 API 代理部署 API 实例 | https://adm.cloud.com/apiproxies/{customerid}/deployments | |
| 4 | 添加 API 策略 | https://adm.cloud.com/{customerid}/apisec/nitro/v1/config/policies/{id} | |
每个 API 策略都有不同的 config_spec 对象。它是一个不透明的对象,它包含一个 JSON 字典,用于使用特定值配置 policytype。
在此对象中,您可以使用以下选项选择 API 资源及其方法:
-
api-resource-paths- 指定 API 定义中定义的 API 资源路径和方法。示例:
{ "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": false, "put": false, "delete": false } <!--NeedCopy--> -
custom-rules- 指定 API 定义中可能不存在的自定义 API 资源路径和方法。示例:
{ "endpoints": ["/pet/categories", "/pet/findByName"], "get": true, "post": false, "put": false, "delete": false } <!--NeedCopy-->
使用此配置,策略会筛选与指定 API 资源路径匹配的传入流量请求。
有关每种策略类型的 config_spec 的信息,请参阅策略类型的 API 示例。
策略类型的 API 示例
本节介绍支持的 API 策略类型及其配置:
速率限制
以下是 Ratelimit 策略类型的示例配置。在 config_spec 对象中指定以下配置:
{ "policytype": "Ratelimit", "config_spec": { "api-resource-paths": { "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": false, "put": false, "delete": false }, "custom-rules": {}, "threshold": "10", "timeslice": "20000", "limittype": "BURSTY", "api-respondertype": "DROP", "header_name": "x-api-key", "per_client_ip": true }, "order_index": 1, "policy_name": "ratelimit_policy" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅 速率限制策略。
OAuth
下面是 JWT Auth validation 策略类型的 API 配置示例。在 config_spec 对象中指定以下配置:
{ "policytype": "JWT Auth Validation", "config_spec": { "api-resource-paths": { "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": true, "put": false, "delete": false }, "custom-rules": {}, "jwks-uri": "https://uri.petstore.com", "issuer": "https://issuer.petstore.com", "audience": "petstore", "introspect-uri": "https://introspect.uri.com", "clientid": "client", "clientsecret": "clientsecret", "claims-to-save": ["scope", "scope2"], "allowed-algorithms": { "hs256": true, "rs256": true, "rs512": true } }, "order_index": 2, "policy_name": "Jwt_auth_policy" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅 OAuth 策略
基本认证
以下是 BasicAuth 策略类型的 API 配置示例:
{ "config_spec": { "api-resource-paths": { "delete": false, "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": true, "put": false }, "custom-rules": {} }, "order_index": 3, "policy_name": "Auth_BaSIC", "policytype": "BasicAuth" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅 基本身份验证策略。
未进行身份验证
以下是 NoAuth 策略类型的 API 配置示例:
{ "config_spec": { "api-resource-paths": { "delete": false, "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": false, "put": false }, "custom-rules": {} }, "order_index": 4, "policy_name": "no_auth_policy", "policytype": "NoAuth" } <!--NeedCopy--> 机器人
以下是 Bot 策略类型的 API 配置示例:
{ "config_spec": { "api-resource-paths": { "delete": false, "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": false, "post": false, "put": false }, "bot-prof-name": "apisec_test_profile", "custom-rules": {} }, "order_index": 5, "policy_name": "bot_policy", "policytype": "Bot" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅机器人策略。
WAF
以下是 WAF 策略类型的 API 配置示例:
{ "config_spec": { "api-resource-paths": { "delete": false, "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": false, "post": false, "put": false }, "waf-prof-name": "apisec_waf_profile", "custom-rules": {} }, "order_index": 6, "policy_name": "waf_policy", "policytype": "WAF" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅 WAF 策略。
标题重写
以下是 Header Rewrite 策略类型的 API 配置示例,请在 config_spec 对象中指定此配置:
{ "policytype": "Header Rewrite", "config_spec": { "api-resource-paths": { "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": true, "put": false, "delete": false }, "custom-rules": {}, "rewrite-policy-header-field-name": "org", "rewrite-policy-header-field-val": "Citrix", "rewrite-policy-header-field-new-val": "Citrite" }, "order_index": 7, "policy_name": "header_rewrite_pol" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅标题重写策略。
URI 路径重写
以下是 URI 路径重写策略类型的 API 配置示例:
{ "config_spec": { "api-resource-paths": { "endpoints": ["/store/order", "/store/inventory"], "delete": false, "get": true, "post": true, "patch": false, "put": false }, "custom-rules": { "delete": false, "endpoints": [], "get": false, "post": false, "patch": false, "put": true }, "path-rewrite-params": [ { "insert-segment-position": "beginning", "new-path-value": "v3", "old-path-value": "v2", "action-type": "replace path segment" }, { "insert-segment-position": "beginning", "new-path-value": "begin", "action-type": "insert path segment" }, { "insert-segment-position": "end", "new-path-value": "end", "action-type": "insert path segment" }, { "insert-segment-position": "before", "new-path-value": "before", "old-path-value": "store", "action-type": "insert path segment" }, { "insert-segment-position": "after", "new-path-value": "after", "old-path-value": "store", "action-type": "insert path segment" } ] }, "order_index": 24, "policy_name": "eats_uripathrewrite", "policytype": "URI Path Rewrite” } <!--NeedCopy--> 有关每个属性的更多信息,请参阅 URI 路径重写策略。
Authorization(授权)
下面是 Authorization 策略类型的 API 配置示例。在 config_spec 对象中指定以下配置:
{ "policytype": "Authorization", "config_spec": { "api-resource-paths": { "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": true, "put": false, "delete": false }, "custom-rules": {}, "claims": [{ "name": "scope", "values": ["value1", "value2"] }] }, "order_index": 8, "policy_name": "authorization" } <!--NeedCopy--> 有关每个属性的更多信息,请参阅授权策略。
拒绝
下面是 Deny 策略类型的 API 配置示例。在 config_spec 对象中指定以下配置:
{ "policytype": "Deny", "config_spec": { "api-resource-paths": { "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"], "get": true, "post": true, "put": false, "delete": false }, "custom-rules": {}, "api-denytype": "RESPONDWITH" }, "order_index": 9, "policy_name": "deny_policy" } <!--NeedCopy--> 在 api-denytype 中,您可以指定以下值之一:
- RESPONDWITH
- RESET
有关每个属性的更多信息,请参阅 拒绝规则。