コンテキスト・ベースの制限の作成
コンテキスト・ベースの制限により、特定のクラウド・リソースへのユーザー・アクセスとサービス・アクセスを管理できます。 ネットワーク・ゾーンやエンドポイント・タイプなどのコンテキストに基づき、リソースに対して制限を定義できます。 詳しくは、コンテキスト・ベースの制限とはを参照してください。
ユーザーおよびアカウント IPアドレス制限も、ユーザーのリソースへのアクセス能力に影響を与える可能性がある。 アカウント IPアドレス制限は、 「設定」 ページで確認できます。 個々のユーザー設定を表示するには、 ユーザー ページに移動し、詳細タブで各ユーザーのIPアドレス制限を表示します。
開始前に
- ルールアクションを完了するには、対象サービスに Identity and Access Management (IAM)ポリシーが割り当てられている必要があります。
- ネットワークゾーンのアクションを完了するには、コンテキストベースの制限サービスのIAMポリシーが割り当てられている必要があります。
詳細については、 コンテキストベースの制限の役割とアクションを 参照してください。
ネットワーク・ゾーンの作成
ネットワークゾーンを作成することで、アクセス要求が発生する許可された場所のリストを確立します。 1 つ以上のネットワーク・ロケーションのセットを、個々のアドレス、範囲、またはサブネットなどの IP アドレス、および VPC ID で指定できます。 ネットワーク・ゾーンを作成したら、それをルールに追加できます。
ネットワーク・ゾーンを作成するには、以下の手順を実行します。
-
IBM Cloud コンソールで**「管理」** > **「コンテキスト・ベースの制限 (Context-based restrictions)」をクリックし、「ネットワーク・ゾーン」**を選択します。
-
「作成」 をクリックします。
UI 入力を使用してゾーンを作成する代わりに、JSON コード・フォームを使用して JSON を直接入力し、**「JSON コードとして入力」**をクリックしてゾーンを作成できます。
-
固有の名前と説明を入力します。
-
アクセス要求を発信できる、許可される IP アドレスを入力します。 必要に応じて、拒否リストに IP アドレス例外を含めます。
-
許可される VPC を入力します。
VPCからパブリック・エンドポイントへのアクセスをルールで許可する場合は、VPCとともにパブリック・ゲートウェイのIPアドレスをゾーン定義に含めます。
-
サービスを参照します。 サービスタイプを選択し、サービスを選択します。 サービスのIPアドレスをネットワークゾーンに関連付けるには、「 追加 」をクリックします。
サービスの種類がわからない場合は、「 コンテキストベースの制限と統合されたサービス 」の表をご覧ください。
-
**「次へ」**をクリックしてネットワーク・ゾーンを確認します。
-
「作成」 をクリックします。
続いて、ネットワーク・ゾーンの作成やルールの作成を行うことができます。
CLIを使用したネットワーク・ゾーンの作成
ネットワークゾーンを作成することで、アクセス要求が発生する許可された場所のリストを確立します。 1 つ以上のネットワーク・ロケーションのセットを、個々のアドレス、範囲、またはサブネットなどの IP アドレス、および VPC ID で指定できます。 ネットワーク・ゾーンを作成したら、それをルールに追加できます。
-
以下のコマンドを実行して、 Context-based restrictions 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を使用したネットワーク・ゾーンの作成
ネットワークゾーンを作成することで、アクセス要求が発生する許可された場所のリストを確立します。 1つまたは複数のネットワークロケーションのセットは、個々のアドレス、範囲またはサブネット、VPC ID、およびサービス参照などのIPアドレスによって指定できます。 ネットワーク・ゾーンを作成したら、それをルールに追加できます。
ネットワークゾーンを作成するには、以下の例に示すように、 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によるネットワークゾーンの作成
ネットワークゾーンを作成することで、アクセス要求が発生する許可された場所のリストを確立します。 1つまたは複数のネットワークロケーションのセットは、個々のアドレス、範囲またはサブネット、VPC ID、およびサービス参照などのIPアドレスによって指定できます。 ネットワーク・ゾーンを作成したら、それをルールに追加できます。
ネットワークゾーンを作成するには、Terraformリソース cbr_zoneを 使用します。
-
Terraform CLI をインストールし、Terraform 用の IBM Cloud Provider プラグインを構成するために、Terraform on IBM Cloud® 入門のチュートリアルに従ってください。 このプラグインは、以下のタスクを実行するために使用される IBM Cloud API を抽象化します。
-
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 モジュールを 参照のこと。
ルールの作成
ルールを作成して、クラウド・リソースに対する制限を定義します。
ルールを作成するには、以下の手順を実行します。
-
IBM Cloud コンソールで管理>**コンテキスト・ベースの制限 (Context-based restrictions)**をクリックし、ルールを選択します。
-
「作成」 をクリックします。
-
ルールのターゲットにするサービスを選択します。 そして、「次へ」 をクリックします。
IAM Access Groups サービスにコンテキストベースの制限を作成すると、そのルールを満たさないユーザーは、パブリックアクセスグループを含め、 アカウント内のグループを表示できなくなります。
-
(オプション)ルールによって操作が制限されるAPIの範囲を選択します。 詳しくは、 ルールの範囲を定義するを 参照のこと。
すべてのサービスがAPIによるルールのスコープ機能をサポートしているわけではない。
-
選択した属性に基づいて、 すべてのリソースまたは特定のリソースに制限を適用します。
-
「確認」>**「続行」**をクリックします。
-
1つ以上のコンテキストを追加する。 エンドポイント・タイプとネットワーク・ゾーンを選択し、**「追加」**をクリックします。
- デフォルトでは、トグルが「No」に設定されている場合、サービスがサポートするすべてのエンドポイントタイプからのアクセスが許可される。 特定のエンドポイント・タイプのみを許可するには、トグルを「Yes」に設定する。
VPCからパブリック・エンドポイントへのアクセスをルールで許可する場合は、VPCとともにパブリック・ゲートウェイのIPアドレスをゾーン定義に含めてください。
- 既存のネットワーク・ゾーンをルールに追加するか、新規ゾーンを作成してルールに追加することができます。 詳細については、 ネットワークゾーンの作成を 参照してください。
-
「続行」 をクリックします。
-
ユニークな説明を提供する。
-
ルールの適用方法を選択します。 ルール作成時にルールの適用方法を決定し、いつでもルールの適用方法を更新できる。
- 有効にする :ルールを有効にします。 拒否されたアクセス試行は 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 の使用によるルールの作成
ルールを作成してクラウドリソースの制限を作成するには、 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を 使用します。
-
Terraform CLI をインストールし、Terraform 用の IBM Cloud Provider プラグインを構成するために、Terraform on IBM Cloud® 入門のチュートリアルに従ってください。 このプラグインは、以下のタスクを実行するために使用される IBM Cloud API を抽象化します。
-
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" } } }