文档

使用 RAM 管理 API

更新时间:

API 网关结合阿里云访问控制(RAM)来实现企业内多职员分权管理 API。API 提供者可以为员工建立子账户,并控制不同职员负责不同的 API 管理。

  • 使用 RAM 可以允许子账号,查看、创建、管理、删除 API 分组、API、插件等。但子账号不是资源的所有者,其操作权限随时都可以被主账号收回。

  • 可以根据文档,利用标签鉴权实现主子账号的资源隔离。

  • 在查看本文前,请确保您已经详读了RAM帮助手册和API 网关API 手册。

  • 若您无此业务场景,请跳过此章节。

第一部分:策略管理

授权策略(Policy),来描述授权的具体内容,授权内容主要包含效力(Effect)、资源(Resource)、对资源所授予的操作权限(Action)以及限制条件(Condition)这几个基本元素。

1. 系统授权策略

API 网关已经预置了两个系统权限,AliyunApiGatewayFullAccess和AliyunApiGatewayReadOnlyAccess,可以到 RAM控制台-权限管理-权限策略 进行查看。 image

  • AliyunApiGatewayFullAccess:管理员权限,拥有主账号下包含 API 分组、API、流控策略、应用等所有资源的管理权限。

  • AliyunApiGatewayReadOnlyAccess:可以查看主账号下包含 API 分组、API、流控策略、应用等所有资源,但不可以操作。

2. 自定义授权策略

您可以根据需要自定义管理权限,支持更为精细化的授权,可以为某个操作,也可以是某个资源。如:API GetUsers 的编辑权限。可以在RAM控制台-策略管理中选择策略类型自定义策略,查看已经定义好的自定义授权。

第二部分:授权策略

授权策略是一组权限的集合,它以一种策略语言来描述。通过给用户或群组附加授权策略,用户或群组中的所有用户就能获得授权策略中指定的访问权限。

示例:

{
  "Version": "1",
  "Statement": [
    {
  "Action": "apigateway:Describe*",
      "Resource": "*",
      "Effect": "Allow"
    }
  ]
}
                

此示例表示:允许所有的查看操作。

Action(操作名称列表)格式为:

 "Action":"<service-name>:<action-name>"

其中:

  • service-name 为:阿里云产品名称,请填写 apigateway。

  • action-name 为:API 接口名称,请参照下表,支持通配符*。

    "Action": "apigateway:Describe*" 表示所有的查询操作。

    "Action": "apigateway:*" 表示 API 网关所有操作。

第三部分:Resource(操作对象列表)

Resource 通常指操作对象, API 网关中的 API 分组、流控策略、应用都被称为 Resource,书写格式:

acs:<service-name>:<region>:<account-id>:<relative-id>

其中:

  • acs:Alibaba Cloud Service 的首字母缩写,表示阿里云的公有云平台。

  • service-name 为:阿里云产品名称,请填写 apigateway。

  • region:地区信息,可以使用通配符*号来代替,*表示所有区域。

  • account-id:账号 ID,比如123456789012****,也可以用*代替。

  • relative-id:与 API 网关相关的资源描述部分,这部分的格式描述支持类似于一个文件路径的树状结构。

格式:

acs:apigateway:$regionid:$accountid:apigroup/$groupId

示例:

acs:apigateway:*:*:apigroup/cbd157704e624ab58a204fd3e0b5ad79

请结合 API 网关的API 手册来查看下表:

action-name

接口描述

资源(Resource)

CreateApiGroup

创建分组

acs:apigateway:$regionid:$accountid:apigroup/*

ModifyApiGroup

修改分组

acs:apigateway:$regionid:$accountid:apigroup/$groupId

DeleteApiGroup

删除分组

acs:apigateway:$regionid:$accountid:apigroup/$groupId

DescribeApiGroups

查询分组列表

acs:apigateway:$regionid:$accountid:apigroup/*

CreateApi

创建API

acs:apigateway:$regionid:$accountid:apigroup/$groupId

DeployApi

发布API

acs:apigateway:$regionid:$accountid:apigroup/$groupId

AbolishApi

下线API

acs:apigateway:$regionid:$accountid:apigroup/$groupId

DeleteApi

删除API

acs:apigateway:$regionid:$accountid:apigroup/$groupId

DescribeApis

查询API列表

acs:apigateway:$regionid:$accountid:apigroup/*

CreatePlugin

创建插件

acs:apigateway:$regionid:$accountid:plugin/*

ModifyPlugin

修改插件

acs:apigateway:$regionid:$accountid:plugin/$pluginId

DeletePlugin

删除插件

acs:apigateway:$regionid:$accountid:plugin/$pluginId

AttachPlugin

将插件绑定到API上

acs:apigateway:$regionid:$accountid:plugin/$pluginId

DetachPlugin

将插件和API解绑

acs:apigateway:$regionid:$accountid:plugin/$pluginId

DescribePluginsByApi

查询API上绑定的插件列表

acs:apigateway:$regionid:$accountid:plugin/$pluginId

CreateApp

创建应用

acs:apigateway:$regionid:$accountid:app/*

ModifyApp

修改应用

acs:apigateway:$regionid:$accountid:app/$appId

DeleteApp

删除应用

acs:apigateway:$regionid:$accountid:app/$appId

DescribeAppAttributes

查询应用列表

acs:apigateway:$regionid:$accountid:app/$appId

SetApisAuthorities

给APP授权API访问权限

acs:apigateway:$regionid:$accountid:apigroup/$groupId

DescribeAuthorizedApps

查询API授权列表

acs:apigateway:$regionid:$accountid:apigroup/$groupId

SetVpcAccess

添加VPC授权

acs:apigateway:$regionid:$accountid:vpcaccess/*

RemoveVpcAccess

删除VPC授权

acs:apigateway:$regionid:$accountid:vpcaccess/*

DescribeVpcAccesses

查询VPC授权

acs:apigateway:$regionid:$accountid:vpcaccess/*

DescribeInstances

查询专享实例列表授权

acs:apigateway:$regionid:$accountid:instance/$instanceId

部分场景示例

授权所有API的查询操作:

{
          "Version": "1",
          "Statement": [
            {
                      "Action": "apigateway:Describe*",
                      "Resource":"acs:apigateway:$regionid:$accountid:apigroup/*",
                      "Effect": "Allow"
            }
          ]
}                        

授权打了标签 `version:v1` 的分组查询操作:

{
	  "Version": "1",
	  "Statement": [
	    {
      		"Action": "apigateway:Describe*",
	      	"Resource":"acs:apigateway:$regionid:$accountid:apigroup/*",
	      	"Effect": "Allow",
                 "Condition": {
                        "StringEquals": {
                             "apigateway:tag/version": "v1"
                        }
                 }
             }
	  ]
}			

授权某个分组下所有API的管理操作:

{
          "Version": "1",
          "Statement": [
            {
                      "Action": "apigateway:*",
                      "Resource": [
                              "acs:apigateway:$regionid:$accountid:apigroup/$groupId",
                              "acs:apigateway:$regionid:$accountid:app/$appId",
                              "acs:apigateway:$regionid:$accountid:vpcaccess/*"
                      ],
                      "Effect": "Allow"
            }
          ]
}

备注:以上示例中,变量部分可以根据需要配置成*。

  • 本页导读 (0)
文档反馈