建立以情境為基礎的限制

基於情境的限制可讓您管理使用者及服務對特定雲端資源的存取。 您可以根據情境 (例如網路區域和端點類型) 定義對資源的限制。 如需詳細資訊,請參閱 什麼是基於上下文的限制

使用者和帳號層級的 IP 位址限制也會影響使用者存取資源的能力。 您可以在「設定」頁面檢視帳號層級的 IP 位址限制。 若要檢視個別使用者設定,請移至 使用者 頁面,並在詳細資料索引標籤中檢視每個使用者的 IP 位址限制。

開始之前

  • 若要完成規則動作,您必須在目標服務上指定 Identity and Access Management (IAM) 政策。
  • 若要完成網路區域動作,您必須在基於情境的限制服務上指定 IAM 政策。

如需詳細資訊,請參閱 基於內容的限制角色和動作

建立網路區域

透過建立網路區域,您可以建立存取請求來源的允許位置清單。 一組一個或多個網路位置可以由 IP 位址指定,例如個別位址、範圍或子網路,以及 VPC ID。 建立網路區域後,您可以將它新增到規則中。

若要建立網路區域,請完成下列步驟。

  1. 在 IBM Cloud 主控台中,按一下管理 > 基於上下文的限制,然後選擇網路區域

  2. 按一下建立

    與其使用 UI 輸入來建立區域,您可以使用 JSON 代碼表單,直接輸入 JSON 來建立區域,只要按一下 Enter as JSON code 即可。

  3. 輸入唯一的名稱和說明。

  4. 輸入允許的 IP 位址,存取要求可以從這些位址開始。 必要時,在拒絕清單中包含 IP 位址例外。

  5. 輸入允許的 VPC。

    如果要在規則中允許從 VPC 存取公共端點,請在區域定義中包含任何公共閘道 IP 位址以及 VPC。

  6. 參考服務。 選擇服務類型,然後選擇服務。 按一下新增,將服務的 IP 位址與您的網路區域相關聯。

    如果您不確定服務類型,請檢視表格 整合了上下文限制的服務

  7. 一下下一步,檢視您的網路區域。

  8. 按一下建立

您可以繼續建立更多的網路區域,或建立規則。

使用 CLI 建立網路區域

透過建立網路區域,您可以建立存取請求來源的允許位置清單。 一組一個或多個網路位置可以由 IP 位址指定,例如個別位址、範圍或子網路,以及 VPC ID。 建立網路區域後,您可以將它新增到規則中。

  1. 執行下列指令,安裝 Context-based restrictions CLI 外掛程式

    ibmcloud plugin install cbr
    
  2. 若要建立網路區域,請使用 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 ID 以及服務參考。 建立網路區域後,您可以將它新增到規則中。

若要建立網路區域,請如以下範例所示,呼叫 Context-based restrictions 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 ID 以及服務參考。 建立網路區域後,您可以將它新增到規則中。

若要建立網路區域,請使用 Terraform 資源 cbr_zone

  1. 要為 Terraform 安裝 Terraform CLI 並配置 IBM Cloud Provider 插件,請遵循 IBM Cloud® 上的 Terraform 入門 教學。 外掛程式抽象出 IBM Cloud API,用來完成這項任務。

  2. 建立名為 main.tf 的 Terraform 配置檔案。 在此檔案中,您使用 HashiCorp Configuration Language 新增建立網路區域的組態。 如需詳細資訊,請參閱 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"
    }
    

另外,您也可以使用 Terraform IBM Modules(TIM)for CBR Zone 來為基於上下文的限制建立一個區域,或更新現有區域中的位址。 瞭解 Terraform IBM 模組

以下範例說明如何使用模組定義網路區域:

module "cbr" {
  source  = "terraform-ibm-modules/cbr/ibm"
  version = "X.X.X" # Replace with the latest version

  cbr_zones = [
    {
      name             = "my-network-zone"
      zone_description = "Zone for approved IP ranges and VPCs"
      addresses = [
        { type = "ipRange", value = "10.0.0.0/8" }, # use your valid/supported CIDR range.
        { type = "vpc",     value = "abcxxxx..." } # provide vpc crn
      ]
    }
  ]
}

如需完整的輸入清單和使用範例,請參閱 GitHub 上的 Context-Based Restrictions 模組

建立規則

透過建立規則,定義對雲端資源的限制。

若要建立規則,請完成下列步驟。

  1. 在 IBM Cloud 主控台中,按一下管理 > 基於上下文的限制,然後選擇規則

  2. 按一下建立

  3. 選取您要在規則中針對的服務。 然後按下一步

    當您為 IAM 存取群組服務建立基於情境的限制時,不符合規則的使用者無法檢視帳戶中的任何群組,包括公用存取群組。

  4. (可選)選取操作受規則限制的 API 範圍。 如需詳細資訊,請參閱 定義規則的範圍

    並非所有服務都支援透過 API 來設定規則範圍的功能。

  5. 根據選取的屬性,將限制範圍擴大為所有資源特定資源

  6. 按一下檢閱 > 繼續

  7. 新增一個或多個上下文。 選取端點類型和網路區域,然後按一下新增

    • 預設情況下,當切換設定為否時,允許從所有服務支援的端點類型存取。 將切換設為是,以僅允許特定端點類型。

    如果要在規則中允許從 VPC 存取公共端點,請在區域定義中包含任何公共閘道 IP 位址以及 VPC。

    • 您可以將現有的網路區域加入規則,或建立新的區域加入規則。 如需詳細資訊,請參閱 建立網路區域
  8. 按一下繼續

  9. 提供獨特的描述。

  10. 選擇執行規則的方式。 您可以在建立規則時決定執行規則的方式,並隨時更新規則的執行方式。

    • 啟用:執行規則。 被拒絕的存取嘗試會在 Activity Tracker Event Routing 中報告。
    • 停用:不執行該規則。 限制不會套用到您的帳戶資源。 如果您不準備啟用規則,請選取此選項。
    • 僅報告:監控規則對使用者的影響,但不強制執行。 所有嘗試存取帳戶中資源的動作都會記錄在 Activity Tracker Event Routing 中。 建議先監視 30 天,然後再強制執行該規則。
  11. 按一下建立

使用 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 接受 enableddisabledreport。 如果未指定執行,則預設會啟用該規則。 如需詳細資訊,請參閱 規則執行

使用 API 建立規則

若要透過建立規則來對您的雲端資源建立限制,請呼叫 Context-based restrictions 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

  1. 要為 Terraform 安裝 Terraform CLI 並配置 IBM Cloud Provider 插件,請遵循 IBM Cloud® 上的 Terraform 入門 教學。 外掛程式抽象出 IBM Cloud API,用來完成這項任務。

  2. 建立名為 main.tf 的 Terraform 配置檔案。 在此檔案中,您可使用 HashiCorp Configuration Language 新增設定,以建立基於上下文的限制規則。 如需詳細資訊,請參閱 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"
            }
      }
    }