创建基于上下文的限制
基于上下文的限制允许您管理对特定云资源的用户和服务访问权。 您可以根据上下文 (例如网络区域和端点类型) 来定义对资源的限制。 有关更多信息,请参阅 什么是基于上下文的限制。
用户和帐户级别的 IP 地址限制也会影响用户访问资源的能力。 您可以在 设置 页。 要查看单个用户设置,请转到 用户 页面并在详细信息选项卡中查看每个用户的 IP 地址限制。
准备工作
- 要完成规则操作,必须在目标服务上为您分配 Identity and Access Management (IAM) 策略。
- 要完成网络区域操作,必须在基于上下文的限制服务上为您分配 IAM 策略。
有关更多信息,请参阅 基于上下文的限制角色和操作。
创建网络区域
通过创建网络区域,可以建立产生访问请求的允许位置的列表。 一个或多个网络位置的集合可以由 IP 地址指定,例如个别地址,范围或子网以及 VPC 标识。 创建网络区域后,可以将其添加到规则。
要创建网络区域,请完成以下步骤。
-
在 IBM Cloud 控制台中,单击 管理 > 基于上下文的限制,然后选择 网络区域。
-
单击创建。
您可以使用 JSON 代码表单直接输入 JSON 以通过单击 作为 JSON 代码输入来创建区域,而不是使用 UI 输入来创建区域。
-
输入唯一的名称和描述。
-
输入可发起访问请求的允许 IP 地址。 如果需要,请在拒绝列表中包含 IP 地址异常。
-
输入允许的 VPC。
如果要允许从 VPC 访问规则中的公共端点,请在区域定义中包含任何公共网关 IP 地址以及 VPC。
-
引用服务。 选择服务类型,然后选择服务。 单击 添加 以将服务的 IP 地址与网络区域相关联。
如果不确定服务类型,请查看 与基于上下文的限制集成的服务 表。
-
单击 下一步 以查看网络区域。
-
单击创建。
您可以通过创建更多网络区域或通过创建规则来继续。
使用 CLI 创建网络区域
通过创建网络区域,可以建立产生访问请求的允许位置的列表。 一个或多个网络位置的集合可以由 IP 地址指定,例如个别地址,范围或子网以及 VPC 标识。 创建网络区域后,可以将其添加到规则。
-
通过运行以下命令来安装 基于上下文的限制 CLI 插件:
ibmcloud plugin install cbr
-
要创建网络区域,请使用 cbr zone-create 命令。
以下示例创建具有允许的网络位置列表的网络区域。
ibmcloud cbr zone-create --name example-zone --description "Example zone description" --addresses 192.0.2.1,192.2.3.5-192.2.3.10
以下示例创建具有服务引用的网络区域。 有关更多信息,请参阅 服务参考。
ibmcloud cbr zone-create --name example-zone-1 --description "Kube zone" --service-ref service_name=containers-kubernetes
要查找可用服务引用的列表,请运行 ibmcloud cbr service-ref-targets 命令。
使用 API 创建网络区域
通过创建网络区域,可以建立产生访问请求的允许位置的列表。 一组一个或多个网络位置可以由 IP 地址 (例如,个别地址,范围或子网,VPC 标识和服务引用) 指定。 创建网络区域后,可以将其添加到规则。
要创建网络区域,请调用 基于上下文的限制 API,如以下示例中所示:
curl -X POST --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" --header "Content-Type: application/json" --data '{ "name": "an example of zone", "description": "this is an example of zone", "account_id": "12ab34cd56ef78ab90cd12ef34ab56cd", "addresses": [ { "type": "ipAddress", "value": "169.23.56.234" }, { "type": "ipRange", "value": "169.23.22.0-169.23.22.255" }, { "type": "subnet", "value": "192.0.2.0/24" }, { "type": "vpc", "value": "crn:v1:bluemix:public:is:us-south:a/12ab34cd56ef78ab90cd12ef34ab56cd::vpc:r134-d98a1702-b39a-449a-86d4-ef8dbacf281e" }, { "type": "serviceRef", "ref": { "account_id": "12ab34cd56ef78ab90cd12ef34ab56cd", "service_name": "cloud-object-storage" } } ], "excluded": [ { "type": "ipAddress", "value": "169.23.22.127" } ] }' "{base_url}/v1/zones"
AddressIPAddress ipAddressModel = new AddressIPAddress.Builder()
.type("ipAddress")
.value("169.23.56.234")
.build();
AddressIPAddressRange ipRangeAddressModel = new AddressIPAddressRange.Builder()
.type("ipRange")
.value("169.23.22.0-169.23.22.255")
.build();
AddressSubnet subnetAddressModel = new AddressSubnet.Builder()
.type("subnet")
.value("192.0.2.0/24")
.build();
AddressVPC vpcAddressModel = new AddressVPC.Builder()
.type("vpc")
.value(vpcCRN)
.build();
ServiceRefValue serviceRefValueModel = new ServiceRefValue.Builder()
.accountId(accountID)
.serviceName("cloud-object-storage")
.build();
AddressServiceRef serviceRefAddressModel = new AddressServiceRef.Builder()
.type("serviceRef")
.ref(serviceRefValueModel)
.build();
AddressIPAddress excludedIPAddressModel = new AddressIPAddress.Builder()
.type("ipAddress")
.value("169.23.22.127")
.build();
CreateZoneOptions createZoneOptions = new CreateZoneOptions.Builder()
.name("an example of zone")
.accountId(accountID)
.description("this is an example of zone")
.addresses(java.util.Arrays.asList(ipAddressModel, ipRangeAddressModel, subnetAddressModel, vpcAddressModel, serviceRefAddressModel))
.excluded(java.util.Arrays.asList(excludedIPAddressModel))
.build();
Response<Zone> response = contextBasedRestrictionsService.createZone(createZoneOptions).execute();
Zone zone = response.getResult();
System.out.println(zone);
// Request models needed by this operation.
// AddressIPAddress
const ipAddressModel = {
type: 'ipAddress',
value: '169.23.56.234',
};
// AddressIPAddressRange
const ipRangeAddressModel = {
type: 'ipRange',
value: '169.23.22.0-169.23.22.255',
};
// AddressSubnet
const subnetAddressModel = {
type: 'subnet',
value: '192.0.2.0/24',
};
// AddressVPC
const vpcAddressModel = {
type: 'vpc',
value: vpcCRN,
};
// AddressServiceRef
const serviceRefAddressModel = {
type: 'serviceRef',
ref: {
account_id: accountId,
service_name: 'cloud-object-storage',
},
};
// AddressIPAddress
const excludedIPAddressModel = {
type: 'ipAddress',
value: '169.23.22.127',
};
const params = {
name: 'an example of zone',
accountId,
addresses: [ipAddressModel, ipRangeAddressModel, subnetAddressModel, vpcAddressModel, serviceRefAddressModel],
excluded: [excludedIPAddressModel],
description: 'this is an example of zone',
};
try {
const res = await contextBasedRestrictionsService.createZone(params);
zoneId = res.result.id;
zoneRev = res.headers.etag;
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
ip_address_model = {
'type': 'ipAddress',
'value': '169.23.56.234',
}
ip_range_address_model = {
'type': 'ipRange',
'value': '169.23.22.0-169.23.22.255',
}
subnet_address_model = {
'type': 'subnet',
'value': '192.0.2.0/24',
}
vpc_address_model = {
'type': 'vpc',
'value': vpc_crn,
}
service_ref_address_model = {
'type': 'serviceRef',
'ref': {
'account_id': account_id,
'service_name': 'cloud-object-storage',
}
}
excluded_ip_address_model = {
'type': 'ipAddress',
'value': '169.23.22.127',
}
zone = context_based_restrictions_service.create_zone(
name='an example of zone',
account_id=account_id,
addresses=[ip_address_model, ip_range_address_model, subnet_address_model, vpc_address_model, service_ref_address_model],
excluded=[excluded_ip_address_model],
description='this is an example of zone',
).get_result()
print(json.dumps(zone, indent=2))
ipAddressModel := &contextbasedrestrictionsv1.AddressIPAddress{
Type: core.StringPtr("ipAddress"),
Value: core.StringPtr("169.23.56.234"),
}
ipRangeAddressModel := &contextbasedrestrictionsv1.AddressIPAddressRange{
Type: core.StringPtr("ipRange"),
Value: core.StringPtr("169.23.22.0-169.23.22.255"),
}
subnetAddressModel := &contextbasedrestrictionsv1.AddressSubnet{
Type: core.StringPtr("subnet"),
Value: core.StringPtr("192.0.2.0/24"),
}
vpcAddressModel := &contextbasedrestrictionsv1.AddressVPC{
Type: core.StringPtr("vpc"),
Value: core.StringPtr(vpcCRN),
}
serviceRefAddressModel := &contextbasedrestrictionsv1.AddressServiceRef{
Type: core.StringPtr("serviceRef"),
Ref: &contextbasedrestrictionsv1.ServiceRefValue{
AccountID: core.StringPtr(accountID),
ServiceName: core.StringPtr("cloud-object-storage"),
},
}
excludedIPAddressModel := &contextbasedrestrictionsv1.AddressIPAddress{
Type: core.StringPtr("ipAddress"),
Value: core.StringPtr("169.23.22.127"),
}
createZoneOptions := contextBasedRestrictionsService.NewCreateZoneOptions()
createZoneOptions.SetName("an example of zone")
createZoneOptions.SetAccountID(accountID)
createZoneOptions.SetDescription("this is an example of zone")
createZoneOptions.SetAddresses([]contextbasedrestrictionsv1.AddressIntf{ipAddressModel, ipRangeAddressModel, subnetAddressModel, vpcAddressModel, serviceRefAddressModel})
createZoneOptions.SetExcluded([]contextbasedrestrictionsv1.AddressIntf{excludedIPAddressModel})
zone, response, err := contextBasedRestrictionsService.CreateZone(createZoneOptions)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(zone, "", " ")
fmt.Println(string(b))
要查找可用服务引用列表,请调用 ListAvailableServicerefTargets 方法。
使用 Terraform 创建网络区域
通过创建网络区域,可以建立产生访问请求的允许位置的列表。 一组一个或多个网络位置可以由 IP 地址 (例如,个别地址,范围或子网,VPC 标识和服务引用) 指定。 创建网络区域后,可以将其添加到规则。
要创建网络区域,请使用 Terraform 资源 cbr_zone。
-
要安装 Terraform CLI 并配置 IBM Cloud Provider Plug-in for Terraform,请遵循 IBM Cloud® 上的 Terraform 入门 教程。 该插件对用于完成此任务的 IBM Cloud API 进行抽象。
-
创建名为
main.tf
的 Terraform 配置文件。 在此文件中,您将添加配置以使用 HashiCorp 配置语言创建网络区域。 有关更多信息,请参阅 Terraform 文档。以下示例创建允许单个 IP 地址的网络区域,并显式排除签署者 IP 地址。
resource "ibm_cbr_zone" "cbr_zone" { account_id = "12ab34cd56ef78ab90cd12ef34ab56cd" addresses { type = "ipAddress" value = "169.23.56.234" } description = "this is an example of zone" excluded { type = "ipAddress" value = "202.38.89.897" } name = "an example of zone" }
创建规则
通过创建规则来定义对云资源的限制。
要创建规则,请完成以下步骤。
-
在 IBM Cloud 控制台中,单击 管理 > 基于上下文的限制,然后选择 规则。
-
单击创建。
-
选择要在规则中作为目标的服务。 然后,单击下一步。
为 IAM 访问组服务创建基于上下文的限制时,不满足规则的用户无法查看帐户中的任何组 (包括公共访问组)。
-
(可选)选择规则限制操作的 API 范围。 有关更多信息,请参阅 定义规则的作用域。
并非所有服务都支持通过 API 确定规则范围的能力。
-
根据所选属性,将限制范围限定为 所有资源 或 特定资源。
-
单击 复审 > 继续。
-
添加一个或多个上下文。 选择端点类型和网络区域,然后单击 添加。
- 缺省情况下,当切换设置为“否”时,允许从所有服务支持的端点类型进行访问。 将切换设置为“是”,以便只允许特定的端点类型。
如果要允许从 VPC 访问规则中的公共端点,请在区域定义中包含任何公共网关 IP 地址以及 VPC。
- 您可以将现有网络区域添加到规则,或者创建要添加到规则的新区域。 有关更多信息,请参阅 创建网络区域。
-
单击继续。
-
请提供唯一描述。
-
选择执行规则的方式。 您可以在创建规则时决定要如何强制实施规则,并随时更新规则强制实施。
- 启用: 强制实施规则。 被拒绝的访问尝试会在 Activity Tracker Event Routing 中报告。
- 禁用: 不强制实施规则。 限制不会应用到您的帐户资源。 如果您尚未准备好启用此规则,请选择此选项。
- 仅报告:监控规则对用户的影响,但不强制执行。 所有访问账户资源的尝试都会被记录在 Activity Tracker Event Routing 中。 建议在强制实施规则之前进行 30 天的监视。
-
单击创建。
使用 CLI 创建规则
要通过创建规则来定义对云资源的限制,请使用 ibmcloud cbr rule-create 命令。 以下示例创建以 Kubernetes Service 为目标的规则,并且仅允许来自指定网络区域的专用端点访问服务。
ibmcloud cbr rule-create --description 'Example Rule Description' --service-name kms --context-attributes endpointType=private --zone-id 93de8d3f588ab2c457ff576c364d1145 --enforcement-mode report
对于 enforcement-mode
选项,CLI 接受值 enabled
,disabled
和 report
。 如果未指定强制实施,那么缺省情况下将启用该规则。 有关更多信息,请参阅 规则实施。
使用 API 创建规则
要通过创建规则来创建对云资源的限制,请调用 基于上下文的限制 API。 以下示例创建以 Kubernetes Service 为目标的已启用规则,并仅允许来自指定网络区域的请求访问服务。
curl -X POST --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" --header "Content-Type: application/json" --data '{ "description": "this is an example of rule", "resources": [ { "attributes": [ { "name": "accountId", "value": "12ab34cd56ef78ab90cd12ef34ab56cd" }, { "name": "serviceName", "value": "kms" } ] } ], "contexts": [ { "attributes": [ { "name": "networkZoneId", "value": "65810ac762004f22ac19f8f8edf70a34" } ] } ], "enforcement_mode": "enabled" }' "{base_url}/v1/rules"
RuleContextAttribute ruleContextAttributeModel = new RuleContextAttribute.Builder()
.name("networkZoneId")
.value(zoneID)
.build();
RuleContext ruleContextModel = new RuleContext.Builder()
.attributes(java.util.Arrays.asList(ruleContextAttributeModel))
.build();
ResourceAttribute resourceAttributeModelAccountID = new ResourceAttribute.Builder()
.name("accountId")
.value(accountID)
.build();
ResourceAttribute resourceAttributeModelServiceName = new ResourceAttribute.Builder()
.name("serviceName")
.value(serviceName)
.build();
ResourceTagAttribute resourceTagAttributeModel = new ResourceTagAttribute.Builder()
.name("tagName")
.value("tagValue")
.build();
Resource resourceModel = new Resource.Builder()
.addAttributes(resourceAttributeModelAccountID)
.addAttributes(resourceAttributeModelServiceName)
.tags(java.util.Arrays.asList(resourceTagAttributeModel))
.build();
CreateRuleOptions createRuleOptions = new CreateRuleOptions.Builder()
.description("this is an example of rule")
.addContexts(ruleContextModel)
.addResources(resourceModel)
.enforcementMode("enabled")
.build();
Response<Rule> response = contextBasedRestrictionsService.createRule(createRuleOptions).execute();
Rule rule = response.getResult();
System.out.println(rule);
ruleID = rule.getId();
ruleRev = response.getHeaders().values("Etag").get(0);
// Request models needed by this operation.
// RuleContextAttribute
const ruleContextAttributeModel = {
name: 'networkZoneId',
value: zoneId,
};
// RuleContext
const ruleContextModel = {
attributes: [ruleContextAttributeModel],
};
// ResourceAttribute
const resourceAttributeAccountIdModel = {
name: 'accountId',
value: accountId,
};
// Resource Attribute
const resourceAttributeServiceNameModel = {
name: 'serviceName',
value: serviceName,
operator: 'stringEquals',
};
// Resource
const resourceModel = {
attributes: [resourceAttributeAccountIdModel, resourceAttributeServiceNameModel],
};
const params = {
contexts: [ruleContextModel],
resources: [resourceModel],
description: 'this is an example of rule',
enforcementMode: 'enabled',
};
try {
const res = await contextBasedRestrictionsService.createRule(params);
ruleId = res.result.id;
ruleRev = res.headers.etag;
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
rule_context_attribute_model = {
'name': 'networkZoneId',
'value': zone_id,
}
rule_context_model = {
'attributes': [rule_context_attribute_model],
}
resource_attribute_account_id_model = {
'name': 'accountId',
'value': account_id,
}
resource_attribute_service_name_model = {
'name': 'serviceName',
'value': service_name,
}
resource_model = {
'attributes': [resource_attribute_account_id_model, resource_attribute_service_name_model],
}
rule = context_based_restrictions_service.create_rule(
contexts=[rule_context_model],
resources=[resource_model],
description='this is an example of rule',
enforcement_mode='enabled'
).get_result()
print(json.dumps(rule, indent=2))
ruleContextAttributeModel := &contextbasedrestrictionsv1.RuleContextAttribute{
Name: core.StringPtr("networkZoneId"),
Value: core.StringPtr(zoneID),
}
ruleContextModel := &contextbasedrestrictionsv1.RuleContext{
Attributes: []contextbasedrestrictionsv1.RuleContextAttribute{*ruleContextAttributeModel},
}
resourceModel := &contextbasedrestrictionsv1.Resource{
Attributes: []contextbasedrestrictionsv1.ResourceAttribute{
{
Name: core.StringPtr("accountId"),
Value: core.StringPtr(accountID),
},
{
Name: core.StringPtr("serviceName"),
Value: core.StringPtr(serviceName),
},
},
Tags: []contextbasedrestrictionsv1.ResourceTagAttribute{
{
Name: core.StringPtr("tagName"),
Value: core.StringPtr("tagValue"),
},
},
}
createRuleOptions := contextBasedRestrictionsService.NewCreateRuleOptions()
createRuleOptions.SetDescription("this is an example of rule")
createRuleOptions.SetContexts([]contextbasedrestrictionsv1.RuleContext{*ruleContextModel})
createRuleOptions.SetResources([]contextbasedrestrictionsv1.Resource{*resourceModel})
createRuleOptions.SetEnforcementMode(contextbasedrestrictionsv1.CreateRuleOptionsEnforcementModeEnabledConst)
rule, response, err := contextBasedRestrictionsService.CreateRule(createRuleOptions)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(rule, "", " ")
fmt.Println(string(b))
使用 Terraform 创建规则
要通过创建规则来定义对云资源的限制,请使用 Terraform 资源 cbr_rule。
-
要安装 Terraform CLI 并配置 IBM Cloud Provider Plug-in for Terraform,请遵循 IBM Cloud® 上的 Terraform 入门 教程。 该插件对用于完成此任务的 IBM Cloud API 进行抽象。
-
创建名为
main.tf
的 Terraform 配置文件。 在此文件中,您将添加配置以使用 HashiCorp 配置语言创建基于上下文的限制规则。 有关更多信息,请参阅 Terraform 文档。以下示例创建以特定 Kubernetes Service API 为目标的规则,并且仅允许来自指定网络区域的专用端点调用与该 API 关联的操作。
resource "ibm_cbr_rule" "cbr_rule" { contexts { attributes { name = "endpointType" value = "private" } } description = "this is an example of rule" enforcement_mode = "enabled" operations { api_types { api_type_id = "api_type_id" } } resources { attributes { name = "serviceName" value = "containers-kubernetes" operator = "equals" } } }