跳到主要内容
版本:3.9.x

权限策略示例

本页按使用场景整理可复用的 API7 网关权限策略示例。每个示例都聚焦于常见的实际访问控制场景。有关确切的 JSON 架构和字段定义,请参阅权限策略和权限边界;有关允许的操作和资源的完整目录,请参阅权限策略操作和资源。

策略文档结构​

通过 Admin API(POST /api/permission_policies)创建权限策略时,请求正文必须是完整的 PermissionPolicy 对象,包含 name、可选的 desc 和 labels,以及包裹 statement 数组的 policy_document:

{
"name": "production-readonly",
"desc": "对生产网关组的只读访问权限。",
"labels": {
"team": "sre"
},
"policy_document": {
"statement": [
{
"effect": "allow",
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>"
],
"actions": [
"<.*>Get<.*>"
]
}
]
}
}

以下示例聚焦于 policy_document 部分,使每个示例简洁且便于比较。通过 Admin API 提交时,请将每个 statement 数组包裹在上方所示的外层对象中。

基本访问模式​

API7 网关访问控制的常见起点:平台范围访问、只读可见性、网关组范围权限、拒绝优先规则以及基于标签的环境管理。

对所有资源的完全访问权限​

此策略授予对 API7 网关中所有资源和操作的无限制访问权限。resources 和 actions 中的 <.*> 通配符表示“全部”。这是权限最宽松的策略,通常仅供超级管理员使用。

{
"statement": [
{
"resources": [
"<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
}
]
}

对所有资源的只读权限​

此策略允许用户查看所有资源但不进行任何更改。actions 限制为包含 Get 的操作,通常对应只读操作。它适用于需要查看整个系统但不需要修改权限的角色。

{
"statement": [
{
"resources": [
"<.*>"
],
"actions": [
"<.*>Get<.*>"
],
"effect": "allow"
}
]
}

对特定网关组的只读权限​

此策略授予对指定网关组列表及其中已发布服务的只读访问权限。它使用唯一的网关组 ID 标识每个资源。对于需要监控生产或测试等特定环境、但不能修改配置的用户而言,此策略非常适用。

{
"statement": [
{
"resources": [
"arn:api7:gateway:gatewaygroup/{gateway_group_id_1}",
"arn:api7:gateway:gatewaygroup/{gateway_group_id_2}"
],
"actions": [
"<.*>Get<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/{gateway_group_id_1}/publishedservice/<.*>",
"arn:api7:gateway:gatewaygroup/{gateway_group_id_2}/publishedservice/<.*>"
],
"actions": [
"<.*>Get<.*>"
],
"effect": "allow"
}
]
}

对特定网关组的完全访问权限​

此策略授予对指定网关组及其关联服务的完整控制权。策略中的多个语句以 OR 关系组合,这意味着只要任一语句允许某项操作,该操作就会被允许。它适用于特定环境的管理员,例如生产运维团队。

{
"statement": [
{
"resources": [
"arn:api7:gateway:gatewaygroup/{gateway_group_id_1}",
"arn:api7:gateway:gatewaygroup/{gateway_group_id_2}"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/{gateway_group_id_1}/publishedservice/<.*>",
"arn:api7:gateway:gatewaygroup/{gateway_group_id_2}/publishedservice/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
}
]
}

对特定网关组的完全访问权限,但不包括消费者凭证​

此策略授予对指定网关组的广泛权限,但明确拒绝管理消费者凭证。它演示了“拒绝优先于允许”原则:对于相同资源和操作,deny 效果优先于任何 allow 语句。这是保护敏感消费者数据的重要安全措施。

{
"statement": [
{
"resources": [
"arn:api7:gateway:gatewaygroup/{gateway_group_id_1}",
"arn:api7:gateway:gatewaygroup/{gateway_group_id_2}"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/{gateway_group_id_1}/publishedservice/<.*>",
"arn:api7:gateway:gatewaygroup/{gateway_group_id_2}/publishedservice/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>/consumer/<.*>"
],
"actions": [
"gateway:GetConsumerCredential",
"gateway:UpdateConsumerCredential",
"gateway:DeleteConsumerCredential"
],
"effect": "deny"
}
]
}

创建和管理生产网关组​

此策略允许用户创建新的网关组,并完全管理带有 production 标签的现有网关组。两个语句都在 gateway_group_label 上使用 MatchLabel 条件:第一个语句将管理范围限定为带有 type: production 标签的现有网关组,第二个语句将创建范围限定为请求正文设置了相同 type: production 标签的请求,因此用户无法在指定范围外创建网关组。

{
"statement": [
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>"
],
"actions": [
"<.*>"
],
"conditions": {
"gateway_group_label": {
"type": "MatchLabel",
"options": {
"key": "type",
"operator": "exact_match",
"value": "production"
}
}
},
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/*"
],
"actions": [
"gateway:CreateGatewayGroup"
],
"conditions": {
"gateway_group_label": {
"type": "MatchLabel",
"options": {
"key": "type",
"operator": "exact_match",
"value": "production"
}
}
},
"effect": "allow"
}
]
}

服务与插件操作​

适用于管理服务模板、各网关组中对应的已发布服务和自定义插件的运维人员。

服务管理员​

此策略面向跨所有网关组管理指定服务的服务管理员角色,包括修改服务模板、发布服务以及在网关组之间同步服务。它对指定服务模板及其对应的已发布服务授予广泛权限,同时授予读取网关组和全局插件规则所需的权限,以避免冲突。

备注

API7 网关区分服务模板 ID 和已发布服务 ID:

  • 服务模板只有 Service Template ID。同一模板可以发布到多个网关组。
  • 模板每次发布到某个网关组时,都会形成一个已发布服务,拥有该网关组内唯一的 Published Service ID。

ARN 中的 publishedservice/<id> 与已发布服务 ID 匹配,而不是服务模板 ID。若要用一条声明覆盖所有网关组中的已发布服务,请使用下例中的 <.*> 通配符。

{
"statement": [
{
"resources": [
"arn:api7:gateway:servicetemplate/{service_template_id}"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>/publishedservice/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>/publishedservice/*"
],
"actions": [
"gateway:CreatePublishedService",
"gateway:PublishServices"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>"
],
"actions": [
"gateway:GetGatewayGroup",
"gateway:GetGlobalPluginRule",
"gateway:GetPluginMetadata"
],
"effect": "allow"
}
]
}

第三条声明针对 gatewaygroup/<.*>/publishedservice/* 授予 gateway:CreatePublishedService 和 gateway:PublishServices 权限,其范围比其他按模板限定的声明更宽:发布时尚不存在已发布服务 ID,因此无法将 ARN 限定到某个具体服务。若需限制哪些人可以发布或同步服务,请像下方按标签限定的示例一样,为这些操作添加 service_label 的 MatchLabel 条件,要求请求正文携带匹配的服务标签。

也可以使用标签,更灵活地管理多个服务。下列策略授予同样的服务管理权限,但通过 MatchLabel 条件,将其限定于带有 team: enterprise 标签的服务。

{
"statement": [
{
"resources": [
"arn:api7:gateway:servicetemplate/<.*>"
],
"actions": [
"<.*>"
],
"conditions": {
"service_label": {
"type": "MatchLabel",
"options": {
"key": "team",
"operator": "exact_match",
"value": "enterprise"
}
}
},
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>/publishedservice/<.*>"
],
"actions": [
"<.*>"
],
"conditions": {
"service_label": {
"type": "MatchLabel",
"options": {
"key": "team",
"operator": "exact_match",
"value": "enterprise"
}
}
},
"effect": "allow"
},
{
"resources": [
"arn:api7:gateway:gatewaygroup/<.*>"
],
"actions": [
"gateway:GetGatewayGroup"
],
"effect": "allow"
}
]
}

管理自定义插件​

此策略授予在网关设置中管理自定义插件的权限。它允许所有包含 CustomPlugin 的操作,从而完整管理自定义插件的生命周期。

{
"statement": [
{
"resources": [
"arn:api7:gateway:gatewaysetting/*"
],
"actions": [
"gateway:<.*>CustomPlugin<.*>"
],
"effect": "allow"
}
]
}

IAM 与治理​

适用于 IAM 管理和治理控制的模式:角色管理访问权限、显式拒绝防护措施以及委派管理员的权限边界。

角色管理员​

此策略面向负责用户和角色管理的角色管理员。它授予管理用户、角色和权限策略的完整权限,允许其邀请或删除用户、重置密码、设计自定义角色以及为用户分配角色。

{
"statement": [
{
"resources": [
"arn:api7:iam:user/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:iam:role/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:iam:permissionpolicy/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
}
]
}

具有许可证更新限制的完全资源访问权限​

此策略为用户提供对所有资源的广泛访问权限,同时明确拒绝更新许可证的权限。这是“拒绝优先于允许”规则的另一个示例,也是防止未经授权更改许可证等关键系统设置的常见安全实践。deny 语句确保即使用户对所有内容拥有完整的 allow 权限,特定的 UpdateLicense 操作也会被禁止。

{
"statement": [
{
"resources": [
"<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:iam:organization/*"
],
"actions": [
"iam:UpdateLicense"
],
"effect": "deny"
}
]
}

委派管理员的权限边界​

权限边界是直接应用于用户的权限策略,用于限制该用户可能拥有的最大权限,而不受其所持角色影响。以下示例使用 AllOfStrings 条件,要求用户的 permission_boundaries 上下文必须恰好包含列出的权限策略 ID,不能多也不能少。在本例中,只有当活动边界集合恰好为 {boundary_policy_id_1, boundary_policy_id_2} 时,用户才能管理 IAM 用户。

通过 PUT /api/users/{user_id}/boundaries 将此策略附加到用户,并传入构成该用户边界集合的权限策略 ID 列表。options 中的值必须是已分配给该用户的边界策略 ID。

{
"statement": [
{
"resources": [
"arn:api7:iam:user/<.*>"
],
"actions": [
"iam:InviteUser",
"iam:UpdateUser",
"iam:DeleteUser"
],
"conditions": {
"permission_boundaries": {
"type": "AllOfStrings",
"options": ["{boundary_policy_id_1}", "{boundary_policy_id_2}"]
}
},
"effect": "allow"
}
]
}

门户管理​

适用于开发者门户管理和 API 产品所有权的模式。

门户管理员​

此策略面向门户管理员角色,负责管理门户、DCR 提供商、API 产品、开发者和订阅等 API 门户资源。该角色可以创建和管理 API 产品、配置 DCR 提供商、审批开发者订阅,并管理整个 API 门户生命周期。

{
"statement": [
{
"resources": [
"arn:api7:portal:portal/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:portal:dcrprovider/<.*>"
],
"actions": [
"<.*>"
],
"effect": "allow"
}
]
}

API 产品管理员​

此策略面向 API 产品管理员角色,该角色可以完全管理 API 产品、查看开发者账户并审批开发者注册,同时对门户配置保持只读可见性。该策略不授予订阅管理操作,也不授予更广泛的门户或 DCR 提供商管理权限,因此适合负责整理 API 产品并引导开发者入驻、但不应改变门户基础设施的产品负责人。

{
"statement": [
{
"resources": [
"arn:api7:portal:portal/<.*>/apiproduct/<.*>"
],
"actions": [
"portal:GetAPIProduct",
"portal:CreateAPIProduct",
"portal:UpdateAPIProduct",
"portal:DeleteAPIProduct"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:portal:portal/<.*>/developer/<.*>"
],
"actions": [
"portal:GetDeveloper",
"portal:ApproveDeveloperSignUp"
],
"effect": "allow"
},
{
"resources": [
"arn:api7:portal:portal/<.*>"
],
"actions": [
"portal:GetPortal"
],
"effect": "allow"
}
]
}

第一个声明仅允许对 API 产品执行创建、读取、更新和删除操作;订阅审批使用 portal:ApproveAPIProductSubscription,此处有意不授予该操作。

其他资源​