管理基于上下文的限制
您可以随时更新描述(有助于确定规则的目的)或选择新的资源和网络环境列表,从而管理上下文规则。 您还可以删除基于上下文的限制,以删除由规则中的上下文定义的限制。
基于上下文的限制可定义和执行其自身 IBM Cloud® 资源的访问限制。 您可以根据上下文(如网络区域和端点类型)定义这些限制。 更多信息,请参阅 什么是基于上下文的限制。
基于上下文的限制服务管理规则和网络区域,因此,如果无法满足基于上下文的限制服务上的规则,就有可能完全丧失管理这些资源的能力。 只有当请求的上下文符合新规则或修改后的规则时,才允许尝试创建或更新此类规则。
如果您无法再满足以基于上下文的限制服务为目标的规则,请 打开支持案例 并提供您可以满足的上下文以恢复访问。
准备工作
要管理基于上下文的限制,必须在账户管理服务中分配管理员角色。
使用控制台更新规则
要编辑云资源上基于上下文的限制,请完成以下步骤:
- 在 IBM Cloud 控制台中,单击管理 > 基于上下文的限制,然后选择规则。
- 在要更新的规则上选择操作图标
,然后选择编辑。
- 要更新操作受规则限制的 API 的范围,请选择“**所有 API **”或“特定 API”。 然后,单击“应用”或“继续”。
- 要更新限制的资源范围,可根据可用属性(如资源组或位置)选择“所有资源”或“特定资源”。 然后,单击“应用”或“继续”。
- 单击摘要面板中的编辑图标
,更新现有上下文。
- 更新允许的端点类型。
- 将切换设置为“否”,以允许所有服务支持的端点类型。
- 将切换设置为“是”,以便只允许特定的端点类型。
- 更新工作区。 您可以选择新的网络区域或取消选择网络区域以删除它们。
- 更新允许的端点类型。
- 然后点击应用。
- 单击摘要面板中的删除图标
上下文。
- 通过选择所有端点或特定端点以及网络区域来配置新上下文。 然后,单击添加。
- 单击“应用”或“继续”。
- 为您的规则提供新的描述。 单击“应用”更新描述,或单击“继续”。
- 要更新规则的执行情况,请单击 编辑 图标
。 您可以启用、禁用或将规则设置为仅报告。
- 单击“应用”完成。
使用 CLI 更新规则
要更新云资源上基于上下文的限制,请使用 ibmcloud cbr rule-update 命令。 下面的示例更新了 ID 为 30fd58c9b75f40e854b89c432318b4a2 的规则的描述、允许的端点类型和网络区域。
ibmcloud cbr rule-update 30fd58c9b75f40e854b89c432318b4a2 --description 'Example rule description' --service-name kms --context-attributes endpointType=private --zone-id 93de8d3f588ab2c457ff576c364d1145
使用应用程序接口更新规则
要通过创建规则更新云资源限制,请调用 基于上下文的限制 API。
-
获取规则 您想替换的 在响应正文中复制规则 ID,在响应标头中复制 ETag 标头。
curl -X GET --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" "https://cbr.cloud.ibm.com/v1/rules/{rule_id}"GetRuleOptions getRuleOptions = new GetRuleOptions.Builder() .ruleId(ruleID) .build(); Response<Rule> response = contextBasedRestrictionsService.getRule(getRuleOptions).execute(); Rule rule = response.getResult(); System.out.println(rule);const params = { ruleId, }; try { const res = await contextBasedRestrictionsService.getRule(params); console.log(JSON.stringify(res.result, null, 2)); } catch (err) { console.warn(err); }rule = context_based_restrictions_service.get_rule( rule_id=rule_id ) rule = rule.get_result() print(json.dumps(rule, indent=2))getRuleOptions := contextBasedRestrictionsService.NewGetRuleOptions( ruleID, ) rule, response, err := contextBasedRestrictionsService.GetRule(getRuleOptions) if err != nil { panic(err) } b, _ := json.MarshalIndent(rule, "", " ") fmt.Println(string(b)) -
下面的示例用更新版本替换了一条规则。 替换请求的
If-Match标头中需要 ETag 值。curl -X PUT --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" --header "If-Match: {if_match}" --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": "76921bd873115033bd2a0909fe081b45" } ] } ], "enforcement_mode": "disabled" }' "{base_url}/v1/rules/{rule_id}"RuleContextAttribute ruleContextAttributeModel = new RuleContextAttribute.Builder() .name("networkZoneId") .value("76921bd873115033bd2a0909fe081b45") .build(); RuleContext ruleContextModel = new RuleContext.Builder() .attributes(new java.util.ArrayList<RuleContextAttribute>(java.util.Arrays.asList(ruleContextAttributeModel))) .build(); ResourceAttribute resourceAttributeModel = new ResourceAttribute.Builder() .name("accountId") .value("12ab34cd56ef78ab90cd12ef34ab56cd") .build(); Resource resourceModel = new Resource.Builder() .attributes(new java.util.ArrayList<ResourceAttribute>(java.util.Arrays.asList(resourceAttributeModel))) .build(); ReplaceRuleOptions replaceRuleOptions = new ReplaceRuleOptions.Builder() .ruleId("testString") .ifMatch("testString") .description("this is an example of rule") .enforcementMode("disabled") .contexts(new java.util.ArrayList<RuleContext>(java.util.Arrays.asList(ruleContextModel))) .resources(new java.util.ArrayList<Resource>(java.util.Arrays.asList(resourceModel))) .build(); Response<OutRule> response = contextBasedRestrictionsService.replaceRule(replaceRuleOptions).execute(); OutRule outRule = response.getResult(); System.out.println(outRule);// Request models needed by this operation. // RuleContextAttribute const ruleContextAttributeModel = { name: 'networkZoneId', value: '76921bd873115033bd2a0909fe081b45', }; // RuleContext const ruleContextModel = { attributes: [ruleContextAttributeModel], }; // ResourceAttribute const resourceAttributeModel = { name: 'accountId', value: '12ab34cd56ef78ab90cd12ef34ab56cd', }; // Resource const resourceModel = { attributes: [resourceAttributeModel], }; const params = { ruleId: 'testString', ifMatch: 'testString', contexts: [ruleContextModel], resources: [resourceModel], description: 'this is an example of rule', enforcementMode: 'disabled', }; contextBasedRestrictionsService.replaceRule(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) });rule_context_attribute_model = { 'name': 'networkZoneId', 'value': '76921bd873115033bd2a0909fe081b45', } rule_context_model = { 'attributes': [rule_context_attribute_model], } resource_attribute_model = { 'name': 'accountId', 'value': '12ab34cd56ef78ab90cd12ef34ab56cd', } resource_model = { 'attributes': [resource_attribute_model], } out_rule = context_based_restrictions_service.replace_rule( rule_id='testString', if_match='testString', contexts=[rule_context_model], resources=[resource_model], description='this is an example of rule', enforcement_mode='disabled' ).get_result() print(json.dumps(out_rule, indent=2))ruleContextAttributeModel := &contextbasedrestrictionsv1.RuleContextAttribute{ Name: core.StringPtr("networkZoneId"), Value: core.StringPtr("76921bd873115033bd2a0909fe081b45"), } ruleContextModel := &contextbasedrestrictionsv1.RuleContext{ Attributes: []contextbasedrestrictionsv1.RuleContextAttribute{*ruleContextAttributeModel}, } resourceAttributeModel := &contextbasedrestrictionsv1.ResourceAttribute{ Name: core.StringPtr("accountId"), Value: core.StringPtr("12ab34cd56ef78ab90cd12ef34ab56cd"), } resourceModel := &contextbasedrestrictionsv1.Resource{ Attributes: []contextbasedrestrictionsv1.ResourceAttribute{*resourceAttributeModel}, } replaceRuleOptions := contextBasedRestrictionsService.NewReplaceRuleOptions( "testString", "testString", ) replaceRuleOptions.SetDescription("this is an example of rule") replaceRuleOptions.SetContexts([]contextbasedrestrictionsv1.RuleContext{*ruleContextModel}) replaceRuleOptions.SetResources([]contextbasedrestrictionsv1.Resource{*resourceModel}) replaceRuleOptions.SetEnforcementMode(contextbasedrestrictionsv1.ReplaceRuleOptionsEnforcementModeDisabledConst) outRule, response, err := contextBasedRestrictionsService.ReplaceRule(replaceRuleOptions) if err != nil { panic(err) } b, _ := json.MarshalIndent(outRule, "", " ") fmt.Println(string(b))
使用控制台更新网络区域
您可以修改允许发起访问请求的位置列表。 可以通过 IP 地址(单个地址、范围或子网)、VPC 或服务引用来指定一组或多个网络位置。 您可以更新规则中使用的网络区域,或稍后将新更新的网络区域整合到规则中。
- 在 IBM Cloud 控制台中,单击管理 > 基于上下文的限制,然后选择网络区域。
- 在要更新的网络区域上选择操作图标
,然后选择编辑。
- 您可以更新区域名称和描述。
- 您可以编辑允许发出访问请求的 IP 地址列表。 必要时,将例外情况列入拒绝名单。
- 您可以添加或删除允许的 VPC。
- 您可以添加或删除服务引用。 选择服务以将其 IP 地址与您的网络区域相关联。
- 单击“下一步”查看新配置。
- 要应用更改,请单击更新。
使用 CLI 更新网络区域
要更新网络区域,请完成以下步骤。
- 使用 ibmcloud cbr zones 命令列出账户中的所有区域,从而获取要更新的网络区域的区域 ID。
ibmcloud cbr zones - 使用 ibmcloud cbr zone-update 命令更新网络区域。 下面的示例更新了 ID 为
65810ac762004f22ac19f8f8edf70a34的网络区的区名、允许地址和排除地址。ibmcloud cbr zone-update 65810ac762004f22ac19f8f8edf70a34 --name 'Example Zone Name' --addresses 166.22.23.0-166.22.23.108 --excluded 166.22.23.100
使用应用程序接口更新网络区域
要更新网络区域,请完成以下步骤。
-
获取区域 您想替换的 在响应正文中复制区 ID,在响应标头中复制 ETag 标头。
curl -X GET --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" "https://cbr.cloud.ibm.com/v1/zones/{zone_id}"GetZoneOptions getZoneOptions = new GetZoneOptions.Builder() .zoneId(zoneID) .build(); Response<Zone> response = contextBasedRestrictionsService.getZone(getZoneOptions).execute(); Zone zone = response.getResult(); System.out.println(zone);const params = { zoneId, }; try { const res = await contextBasedRestrictionsService.getZone(params); console.log(JSON.stringify(res.result, null, 2)); } catch (err) { console.warn(err); }get_zone_response = context_based_restrictions_service.get_zone( zone_id=zone_id ) zone = get_zone_response.get_result() print(json.dumps(zone, indent=2))getZoneOptions := contextBasedRestrictionsService.NewGetZoneOptions( zoneID, ) zone, response, err := contextBasedRestrictionsService.GetZone(getZoneOptions) if err != nil { panic(err) } b, _ := json.MarshalIndent(zone, "", " ") fmt.Println(string(b)) -
使用 替换区域 方法更新网络区域。 替换请求的
If-Match标头中需要 ETag 值。curl -X PUT --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" --header "If-Match: {if_match}" --header "Content-Type: application/json" --data '{ "name": "new zone name", "description": "new zone description", "account_id": "12ab34cd56ef78ab90cd12ef34ab56cd", "addresses": [ { "type": "ipAddress", "value": "169.23.56.234" }, { "type": "ipRange", "value": "169.23.22.0-169.23.22.255" }, { "type": "vpc", "value": "crn:v1:bluemix:public:is:us-south:a/12ab34cd56ef78ab90cd12ef34ab56cd::vpc:r134-d98a1702-b39a-449a-86d4-ef8dbacf281e" } ] }' "{base_url}/v1/zones/{zone_id}"AddressIPAddress addressModel = new AddressIPAddress.Builder() .type("ipAddress") .value("169.23.56.234") .build(); ReplaceZoneOptions replaceZoneOptions = new ReplaceZoneOptions.Builder() .zoneId("testString") .ifMatch("testString") .name("an example of zone") .accountId("12ab34cd56ef78ab90cd12ef34ab56cd") .description("this is an example of zone") .addresses(new java.util.ArrayList<Address>(java.util.Arrays.asList(addressModel))) .build(); Response<OutZone> response = contextBasedRestrictionsService.replaceZone(replaceZoneOptions).execute(); OutZone outZone = response.getResult(); System.out.println(outZone);// Request models needed by this operation. // AddressIPAddress const addressModel = { type: 'ipAddress', value: '169.23.56.234', }; const params = { zoneId: 'testString', ifMatch: 'testString', name: 'an example of zone', accountId: '12ab34cd56ef78ab90cd12ef34ab56cd', addresses: [addressModel], description: 'this is an example of zone', }; contextBasedRestrictionsService.replaceZone(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) });address_model = { 'type': 'ipAddress', 'value': '169.23.56.234', } out_zone = context_based_restrictions_service.replace_zone( zone_id='testString', if_match='testString', name='an example of zone', account_id='12ab34cd56ef78ab90cd12ef34ab56cd', addresses=[address_model], description='this is an example of zone' ).get_result() print(json.dumps(out_zone, indent=2))addressModel := &contextbasedrestrictionsv1.AddressIPAddress{ Type: core.StringPtr("ipAddress"), Value: core.StringPtr("169.23.56.234"), } replaceZoneOptions := contextBasedRestrictionsService.NewReplaceZoneOptions( "testString", "testString", ) replaceZoneOptions.SetName("an example of zone") replaceZoneOptions.SetAccountID("12ab34cd56ef78ab90cd12ef34ab56cd") replaceZoneOptions.SetDescription("this is an example of updated zone") replaceZoneOptions.SetAddresses([]contextbasedrestrictionsv1.AddressIntf{addressModel}) outZone, response, err := contextBasedRestrictionsService.ReplaceZone(replaceZoneOptions) if err != nil { panic(err) } b, _ := json.MarshalIndent(outZone, "", " ") fmt.Println(string(b))
使用控制台删除规则
删除规则会删除给定资源中基于上下文的限制,如果用户拥有正确的权限,则允许来自任何上下文的请求。 完成以下步骤即可删除云资源上的规则:
- 在 IBM Cloud 控制台中,转到管理 > 基于上下文的限制,然后选择规则。
- 单击包含规则的行中的操作图标
,然后单击删除。
使用 CLI 删除规则
完成以下步骤即可删除云资源上的规则:
- 使用 基于上下文的限制规则 命令,检索要删除的规则的规则 ID。 您可以通过指定属性作为命令选项来缩小列表结果的范围。
ibmcloud cbr rules --serviceName "iam-identity" - 使用 cbr rule-delete 命令删除指定规则 ID 的规则。
ibmcloud cbr rule-delete 30fd58c9b75f40e854b89c432318b4a2
使用 API 删除规则
完成以下步骤即可删除云资源上的规则:
- 使用 基于上下文的限制列表规则 方法,获取要删除的规则的规则 ID。
curl -X GET --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" "{base_url}/v1/rules?account_id={account_id}"ListRulesOptions listRulesOptions = new ListRulesOptions.Builder() .accountId("testString") .build(); Response<OutRulePage> response = contextBasedRestrictionsService.listRules(listRulesOptions).execute(); OutRulePage outRulePage = response.getResult(); System.out.println(outRulePage);const params = { accountId: 'testString', }; contextBasedRestrictionsService.listRules(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) });out_rule_page = context_based_restrictions_service.list_rules( account_id='testString' ).get_result() print(json.dumps(out_rule_page, indent=2))listRulesOptions := contextBasedRestrictionsService.NewListRulesOptions( "testString", ) ruleList, response, err := contextBasedRestrictionsService.ListRules(listRulesOptions) if err != nil { panic(err) } b, _ := json.MarshalIndent(ruleList, "", " ") fmt.Println(string(b)) - 删除指定规则 ID 的规则。
curl -X DELETE --location --header "Authorization: Bearer {iam_token}" "{base_url}/v1/rules/{rule_id}"DeleteRuleOptions deleteRuleOptions = new DeleteRuleOptions.Builder() .ruleId("testString") .build(); Response<Void> response = contextBasedRestrictionsService.deleteRule(deleteRuleOptions).execute();const params = { ruleId: 'testString', }; contextBasedRestrictionsService.deleteRule(params) .then(res => { done(); }) .catch(err => { console.warn(err) });response = context_based_restrictions_service.delete_rule( rule_id='testString' )deleteRuleOptions := contextBasedRestrictionsService.NewDeleteRuleOptions( "testString", ) response, err := contextBasedRestrictionsService.DeleteRule(deleteRuleOptions) if err != nil { panic(err) } if response.StatusCode != 204 { fmt.Printf("\nUnexpected response status code received from DeleteRule(): %d\n", response.StatusCode) }
使用控制台删除网络区域
删除网络区域会移除允许创建访问请求的网络位置集。 如果在规则中添加了网络区域,首先必须从规则中删除该区域。 完成以下步骤删除网络区域:
- 在 IBM Cloud 控制台中,转到管理 > 基于上下文的限制,然后选择网络区域。
- 单击包含网络区域的行中的操作图标
,然后单击删除。
使用 CLI 删除网络区域
删除网络区域会移除允许创建访问请求的网络位置集。 如果在规则中添加了网络区域,首先必须从规则中删除该区域。 有关从规则中删除区域的更多信息,请参阅 更新基于上下文的限制。 然后,完成以下步骤:
- 使用 contxt-based restrictions zones 命令获取要删除的网络区域的区域 ID。 您可以通过指定区域名称来缩小列表结果的范围。
ibmcloud cbr zones --name "Example zone" - 使用 cbr zone-delete 命令删除指定区域 ID 的网络区域。
ibmcloud cbr zone-delete 65810ac762004f22ac19f8f8edf70a34
使用 API 删除网络区域
删除网络区域会移除允许创建访问请求的网络位置集。 如果在规则中添加了网络区域,首先必须从规则中删除该区域。 有关从规则中删除区域的更多信息,请参阅 更新基于上下文的限制。 然后,完成以下步骤:
- 使用 基于上下文的限制列表区 方法,检索要删除的规则的规则 ID。
curl -X GET --location --header "Authorization: Bearer {iam_token}" --header "Accept: application/json" "{base_url}/v1/zones?account_id={account_id}"ListZonesOptions listZonesOptions = new ListZonesOptions.Builder() .accountId("testString") .build(); Response<OutZonePage> response = contextBasedRestrictionsService.listZones(listZonesOptions).execute(); OutZonePage outZonePage = response.getResult(); System.out.println(outZonePage);const params = { accountId: 'testString', }; contextBasedRestrictionsService.listZones(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) });out_zone_page = context_based_restrictions_service.list_zones( account_id='testString' ).get_result() print(json.dumps(out_zone_page, indent=2))listZonesOptions := contextBasedRestrictionsService.NewListZonesOptions( "testString", ) outZonePage, response, err := contextBasedRestrictionsService.ListZones(listZonesOptions) if err != nil { panic(err) } b, _ := json.MarshalIndent(outZonePage, "", " ") fmt.Println(string(b)) - 删除指定区域 ID 的网络区域。
curl -X DELETE --location --header "Authorization: Bearer {iam_token}" "{base_url}/v1/zones/{zone_id}"DeleteZoneOptions deleteZoneOptions = new DeleteZoneOptions.Builder() .zoneId("testString") .build(); Response<Void> response = contextBasedRestrictionsService.deleteZone(deleteZoneOptions).execute();const params = { zoneId: 'testString', }; contextBasedRestrictionsService.deleteZone(params) .then(res => { done(); }) .catch(err => { console.warn(err) });response = context_based_restrictions_service.delete_zone( zone_id='testString' )deleteZoneOptions := contextBasedRestrictionsService.NewDeleteZoneOptions( "testString", ) response, err := contextBasedRestrictionsService.DeleteZone(deleteZoneOptions) if err != nil { panic(err) } if response.StatusCode != 204 { fmt.Printf("\nUnexpected response status code received from DeleteZone(): %d\n", response.StatusCode) }
限制使用控制台管理规则和网络区域的能力
要配置此规则,请将基于上下文的限制服务作为目标。 有关设置规则步骤的更多信息,请参阅 创建规则。 作用域为 所有资源的规则适用于服务管理的所有当前和未来资源。 如果要限制对特定资源的操作,请将规则范围设为特定资源 > 资源类型。 要完成任何规则或网络区域管理操作,必须通过 IAM 访问策略为用户分配正确的角色,而且用户必须满足基于上下文的限制规则。
限制使用 API 管理规则和网络区域的能力
下面的示例显示了一个 JSON 格式的规则,该规则可保护规则和网络区域管理操作:
{
"resources": [
{
"attributes": [
{
"name": "accountId",
"value": "my-AccountID"
},
{
"name": "serviceName",
"value": "context-based-restrictions"
}
]
}
],
"description": "",
"contexts": [
{
"attributes": [
{
"name": "networkZoneId",
"value": "my-zoneID"
}
]
}
],
"enforcement_mode": "report"
}
仅指定 accountId 和 serviceName 资源属性的规则适用于服务管理的所有当前和未来资源。 如果要限制对特定资源的操作,请包含相应的 resourceType 资源属性。 基于上下文的限制服务 resourceType 的有效值为 rule 和 zone。
要完成任何规则或网络区域管理操作,必须通过 IAM 访问策略为用户分配正确的角色,而且用户必须满足基于上下文的限制规则。