IBM Cloud Docs
管理基于情境的限制

管理基于情境的限制

您可以通过更新描述随时管理上下文规则,这有助于确定规则的目的,或者选择新的资源和网络环境列表。 您还可以删除基于上下文的限制,以删除规则中由上下文定义的限制。

基于上下文的限制可以定义和执行对其自身 IBM Cloud® 资源的访问限制。 您可以根据网络区域和端点类型等环境来定义这些限制。 更多信息,请参阅 “基于情境的限制是什么”。

基于上下文的限制服务管理规则和网络区域,因此,如果您不能满足基于上下文的限制服务上的规则,则可能会失去管理这些资源的所有能力。 只有在请求的上下文符合新规则或修改后的规则时,才允许尝试创建或更新此类规则。

如果您无法满足基于情境的限制服务的规则,请提交支持案例 并提供能够满足要求的情境,以便恢复访问权限。

准备工作

要管理基于情境的限制,您必须在账户管理服务中拥有管理员角色。

使用控制台更新规则

要编辑云资源的基于上下文的限制,请完成以下步骤:

  1. 在 IBM Cloud 控制台中,点击管理 > 基于上下文的限制,然后选择规则
  2. 选择您要更新的规则上的 行为 图标 操作,然后选择 编辑
  3. 要更新受规则限制的操作的API范围,请选择 “所有API”“特定API”。 然后点击 “应用”“继续”。
  4. 要更新限制的资源范围,您可以根据可用属性(如资源组或位置)选择 “所有资源”“特定资源”。 然后点击 “应用”“继续”。
  5. 点击摘要面板中的 编辑 图标 编辑,更新现有内容。
    1. 更新允许的端点类型。
      • 将切换开关设置为“否”,以允许所有支持的服务端点类型。
      • 将切换开关设置为“是”,仅允许特定端点类型。
    2. 更新网络区域。 您可以选择新的网络区域或取消选择网络区域以将其删除。
  6. 然后点击 “应用”。
  7. 点击摘要面板中的 删除 图标 删除,即可删除上下文。
  8. 通过选择所有端点或特定端点以及网络区域来配置新的上下文。 然后,单击添加
  9. 点击 “应用”或 “继续”。
  10. 请为您的规则提供新的描述。 点击 “应用” 更新描述,或点击 “继续”。
  11. 要更新规则的实施,请点击编辑图标编辑图标。 您可以启用 、禁用规则,或将其设置为仅报告
  12. 点击 “应用” 完成操作。

使用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更新规则

要创建规则更新对云资源的限制,请调用 基于上下文的限制API

  1. 获取您想要替换的规则。 在响应正文中复制规则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))
    
  2. 以下示例用更新后的版本替换了规则。 替换请求的 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或服务参考来指定一个或多个网络位置。 您可以更新规则中使用的网络区域,也可以稍后将更新的网络区域整合到规则中。

  1. 在 IBM Cloud 控制台中,点击管理 > 基于上下文的限制,然后选择网络区域
  2. 选择网络区域中您想要更新的 行为 图标 操作,然后选择 编辑
  3. 您可以更新您的区域名称和描述。
  4. 您可以编辑允许访问请求的IP地址列表。 如有必要,将例外情况列入拒绝列表。
  5. 您可以添加或删除允许的VPC。
  6. 您可以添加或删除服务参考。 选择服务以将其 IP 地址与您的网络区域相关联。
  7. 点击 “下一步” 查看您的新配置。
  8. 如需应用更改,请点击 “更新”。

使用CLI更新网络区域

要更新网络区域,请完成以下步骤。

  1. 使用ibmcloud cbr zones命令 列出账户中的所有区域,以获取要更新的网络区域的区域ID。
    ibmcloud cbr zones
    
  2. 使用 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
    

使用API更新网络区域

要更新网络区域,请完成以下步骤。

  1. 获取您想要替换的区域。 在响应正文中复制区域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))
    
  2. 使用 “替换区域”方法 更新网络区域。 替换请求的 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))
    

使用控制台删除规则

删除规则会从给定资源中移除基于上下文的限制,如果用户拥有正确的权限,则允许任何上下文中的请求。 您可以通过完成以下步骤来删除云资源上的规则:

  1. 在 IBM Cloud 控制台中,依次选择 “管理” > “基于情境的限制”,然后选择 “规则”。
  2. 点击包含规则的行中的 行为 图标 操作,然后点击 删除

使用CLI删除规则

您可以通过完成以下步骤来删除云资源上的规则:

  1. 使用 基于上下文的限制规则命令,检索要删除的规则的规则ID。 您可以通过指定属性作为命令选项来缩小列表结果。
    ibmcloud cbr rules --serviceName "iam-identity"
    
  2. 使用 cbr rule-delete命令 删除指定规则ID的规则。
    ibmcloud cbr rule-delete 30fd58c9b75f40e854b89c432318b4a2
    

使用API删除规则

您可以通过完成以下步骤来删除云资源上的规则:

  1. 使用 基于上下文的限制列表规则 方法,获取要删除的规则的规则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))
    
  2. 删除指定规则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)
     }
    

使用控制台删除网络区域

删除网络区域即删除允许访问请求的已设置网络位置。 如果将网络区域添加到规则中,则必须先从规则中删除该区域。 请完成以下步骤以移除网络区域:

  1. 在 IBM Cloud 控制台中,依次选择 “管理” > “基于情境的限制”,然后选择 “网络区域”。
  2. 点击包含网络区域的行中的 行为 图标 操作,然后点击 删除

使用 CLI 删除网络区域

删除网络区域即删除允许访问请求的已设置网络位置。 如果将网络区域添加到规则中,则必须先从规则中删除该区域。 有关从规则中移除区域的更多信息,请参阅 更新基于上下文的限制。 然后,完成以下步骤:

  1. 使用 基于上下文的限制区域命令,检索要删除的网络区域的区域ID。 您可以通过指定区域名称来缩小列表结果的范围。
    ibmcloud cbr zones --name "Example zone"
    
  2. 使用 cbr zone-delete命令 删除指定区域ID的网络区域。
    ibmcloud cbr zone-delete 65810ac762004f22ac19f8f8edf70a34
    

使用API删除网络区域

删除网络区域即删除允许访问请求的已设置网络位置。 如果将网络区域添加到规则中,则必须先从规则中删除该区域。 请参阅 “更新基于情境的限制”,了解从规则中移除区域的更多信息。 然后,完成以下步骤:

  1. 使用 基于上下文的限制列表区域方法,检索要删除的规则的规则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))
    
  2. 删除指定区域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"
}

仅指定 accountIdserviceName 资源属性的规则适用于该服务当前和未来管理的所有资源。 如果您想限制特定资源的操作,请包含相应的 resourceType 资源属性。 基于上下文的限制服务的有效 resourceType 值为 rulezone

为了完成任何规则或网络区域管理操作,必须为用户分配正确的角色,并为其制定 IAM 访问策略,同时用户必须满足基于上下文的限制规则。