時間とリソースの属性に基づく条件でアクセスを制限する
時間ベースの条件を設定して、アカウント内のリソースへの一時的なアクセスを指定したり、特定の時間枠内でリソースへのアクセスを許可したり、リソースの属性ベースの条件を設定して、アクセスニーズを満たすために複数のアクセスポリシーを作成する手間を省くことができます。
特定の日付と時間帯に1回限りの一時的なアクセスを許可する時間ベースの条件を作成したり、毎週定期的なアクセスを設定したりすることができます。 たとえば、繰り返しアクセスを指定することで、勤務時間中のみユーザーにアカウントリソースへのアクセス権を与えることができます。また、サービスの特徴をデモする必要がある契約社員やユーザーが一時的なアクセス権を必要としている場合もあります。
時間に基づく条件では、夏時間(DST)を採用しているタイムゾーンにおける夏時間(DST)の変更は考慮されません。 管理者は、時間に基づく条件を正確に適用するために、DSTの変更に合わせてポリシーを更新する必要があります。 例えば、東部標準時の夏時間は、標準時の -5 時間ではなく、 UTC-4 時間となります。 標準時は11月に始まり、夏時間が始まる3月に終わります。
Kubernetes Service は時間ベースの条件に従いません。 例えば、すべてのアイデンティティおよびアクセスが有効なサービスへのアクセスを許可する時間ベースの条件付きポリシーには、 Kubernetes Service リソースへのアクセスが含まれます。 ポリシーの対象者は、時間ベースの条件外でも、 Kubernetes Service リソースにアクセスできます。
アクセスポリシーの時間ベースの条件は、アクセス権限を割り当てる際に最小特権の原則を適用し、セキュリティ侵害が発生した場合の攻撃対象領域を縮小するのに役立ちます。
リソース属性に基づく条件でポリシーを作成すると、アクセスニーズを満たすために複数のアクセスポリシーを作成する必要がなくなります。 代わりに、リテラル値またはワイルドカード値を持つリソース属性に適用される OR
/ AND
演算子の組み合わせを使用することで、単一のポリシーを作成することができます。 複数の条件を同時に満たすリソースへのアクセス権を付与することもできます(AND)、または、いくつかの条件のいずれかが満たされた場合にアクセス権を付与することもできます(OR)。
たとえば、リソース属性ベースの条件を使用すると、次の条件に基づいてアクセスを許可する単一のポリシーを作成できます。 Service instance: abc
、 またはattribute-1: xyz
、 または**(attribute-2: def
そしてattribute-3: hij
)**。
OR
/ AND
を使用する条件と、リソース属性に基づく条件を最低2つは設定する必要があります。 単一の条件を追加する必要がある場合は 、「コンソールでのリソースへのアクセスの割り当て 」を参照し 、「特定のリソース」 を選択した後で条件を追加します。
ユーザーのアクセスを確認するには、 「コンソールで割り当てられたアクセスを確認する」 を参照してください。
条件パターン
時間ベースの条件パターン
以下のパターンは、許可された条件の並び替えを表します
パターン | 例 |
---|---|
時間ベースの条件:一度 | 特定の日の午前9時から午後5時までの一時的なアクセス UTC-5。 |
時間ベースの条件: 週単位:終日 | 月~金曜日の定期的なアクセスは、 UTC-5 で終日ご利用いただけます。 |
時間ベースの条件: 週単位: カスタム時間 | 月~金曜日の午前9時から午後5時までの定期的なアクセス UTC-5。 |
IAMは、同じポリシー定義の中で、一時的な1回限りの条件と毎週繰り返される条件を組み合わせないようにします。
リソース属性に基づく条件パターン
以下のパターンは、許可された条件の並び替えを表します
パターン | 例 |
---|---|
attribute-based-condition:resource:literal-and-wildcard |
リテラル値またはワイルドカード値を持つリソース属性に基づく条件(使用される演算子に基づく) |
コンソールを使用して一時的な時間ベースの条件を作成する
条件がアクセスを許可し、終了する日時を決定する日付と時間帯を指定することで、限定的な期間のアクセス権限を割り当てることができます。 例えば、数時間、貴社のアカウントでデモンストレーションを行う必要があるユーザーや、数日間、サービスへの一時的なアクセスが必要な契約者がいるかもしれません。 一時的な時間ベースの条件付きアクセスポリシーを割り当てるには、以下の手順に従います
-
IBM Cloud コンソールで 、「管理」 > 「アクセス(IAM)」 に移動します
-
アクセスを割り当てる対象に応じて、 ユーザー、 信頼されたプロファイル、 サービスID、またはアクセスグループを選択します。
-
リストからエンティティ名をクリックし、 アクセスに進みます。
-
「アクセス権限の割り当て」 をクリックします。
-
サービスを選択し 、「次へ」 をクリックします。
- 任意のサービスをユーザーが作成できるようにする場合は、すべての ID およびアクセス対応サービスを選択します。
- 特定のサービスに対するアクセス権限をユーザーに割り当てる場合は、リストからサービスを選択します。
-
ユーザーにアクセス権を付与したいリソースを選択するか、または「すべてのリソース」を選択します。 次へ をクリックします。
-
(オプション)リソースグループへのアクセス権限を選択します。 次へ をクリックします。
-
サービスアクセスとプラットフォームアクセスの役割を任意に選択し 、「次へ」 をクリックします。
-
条件を追加をクリックし、 一度だけを選択します。
-
時間帯を選択します。
例えば、ダブリンを拠点とする開発者向けの条件付きポリシーを作成するとします。 この場合、
UTC+1
を選択すると、次のステップで選択する日時範囲が、その場所に適切な時間でのアクセスを強制します。 -
条件がアクセスを許可し、終了する日時を決定する日付と時間帯のフィールドを記入してください。
-
「作成」 をクリックします。
-
**「レビュー」**をクリックします。
-
追加をクリックして、ポリシー構成をポリシー・サマリーに追加します。
-
割り当て をクリックします。
一時的なポリシーは自動的に削除されません。 アカウントでポリシーの上限に達しないようにするには、ポリシーの有効期限が切れた後に、管理者は手動でポリシーを削除することができます。
アクセスポリシーの時間ベースの条件についての詳細は、 アクセスポリシーの条件 を参照してください。
開始前に
アクセスポリシーで条件を使用できるように、 IBM Cloud CLI の最新バージョンを使用していることを確認してください。
使用している IBM Cloud CLI バージョンを判別するには、以下のコマンドを実行します。
ibmcloud -v
最新バージョンの CLI を使用する必要があります。 最新バージョンを使用していない場合、以下のコマンドを実行して CLI を更新します。
ibmcloud update
現行リリースを実行している場合には、以下の出力が表示されます。
Checking for updates...
No update required. Your CLI is already up-to-date.
詳細については 、「スタンドアロン版 IBM Cloud CLIのインストール」 を参照してください。
CLIを使用して一時的な時間ベースの条件を作成する
条件がアクセスを許可し、終了する日時を決定する日付と時間帯を指定することで、限定的な期間のアクセス権限を割り当てることができます。 例えば、数時間、貴社のアカウントでデモンストレーションを行う必要があるユーザーや、数日間、サービスへの一時的なアクセスが必要な契約者がいるかもしれません。 次の例では、ユーザーにアカウント管理サービスすべてに対する一時的なオペレーター権限を付与することで、アカウントに1回限りの時間ベースの条件を作成する方法を示しています。
-
IBM Cloud® CLI にログインします。 複数のアカウントを持っている場合、使用するアカウントを選択するように求めるプロンプトが出されます。
-r
フラグを使用してリージョンを指定していない場合、リージョンも選択しなければなりません。ibmcloud login
資格情報が拒否された場合、統合 ID を使用している可能性があります。 フェデレーテッド ID を使用してログインするには、
--sso
フラグを使用します。 詳しくは、フェデレーテッド ID を使用したログインを参照してください。IBM Cloud CLI を初めて使用する場合は、 入門チュートリアル をご覧ください。
-
コマンドを使用して、アクセスポリシーを作成し、それをユーザーまたはサービスIDに割り当てます
ibmcloud iam user-policy-create
。 -
Operator
の役割を持つすべてのアカウント管理サービスへのアクセス権限を割り当てますibmcloud iam service-policy-create [your service ID here] --roles Operator --account-management --api-version v2
-
一時的な時間ベースのポリシーJSONスキーマの次の例を参考に、独自の条件を作成してください
{ "type": "access", "description": "time-based conditions policy example restricting access to the full day of 2022-12-23 UTC", "control": { "grant": { "roles": [ { "role_id": "crn:v1:bluemix:public:iam::::role:Operator" } ] } }, "resource": { "attributes": [ { "operator": "stringEquals", "value": "d4b763ad0cbd4dca8dd1edb427d7a77e", "key": "accountId" }, { "value": "platform_service", "operator": "stringEquals", "key": "serviceType" } ] }, "pattern": "time-based-conditions:once", "rule": { "operator": "and", "conditions": [ { "key": "{{environment.attributes.current_date_time}}", "operator": "dateTimeGreaterThanOrEquals", "value": "2022-12-23T00:00:00+00:00" }, { "key": "{{environment.attributes.current_date_time}}", "operator": "dateTimeLessThanOrEquals", "value": "2022-12-23T23:59:59+00:00" } ] }, "subject": { "attributes": [ { "key": "iam_id", "operator": "stringEquals", "value": "IBMid-550000HFVV" } ] } }
time-based-conditions:once
というパターンを使用する一時的なポリシーは、自動的に削除されません。 アカウントでポリシーの上限に達しないようにするには、ポリシーの有効期限が切れた後に、管理者は手動でポリシーを削除することができます。
アクセスポリシーの時間ベースの条件についての詳細は、 アクセスポリシーの条件 を参照してください。
開始前に
v2/policies
URI( https://iam.coud.ibm.com/v2/policies
)に確実にアクセスし、アクセスポリシーで条件を使用できるようにしてください。 詳細については、 IAMポリシー管理API および 変更履歴 を参照してください。
APIを使用して一時的な時間ベースの条件を作成する
条件がアクセスを許可し、終了する日時を決定する日付と時間帯を指定することで、限定的な期間のアクセス権限を割り当てることができます。 例えば、数時間、貴社のアカウントでデモンストレーションを行う必要があるユーザーや、数日間、サービスへの一時的なアクセスが必要な契約者がいるかもしれません。 以下の例では、ユーザーにすべてのアカウント管理サービスに対する一時的なオペレーター権限を付与することで、アカウントに1回限りの時間ベースの条件を作成する方法を説明します。
subjectAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("iam_id"),
Operator: core.StringPtr("stringEquals"),
Value: &exampleUserID,
}
policySubject := &iampolicymanagementv1.V2PolicyBaseSubject{
Attributes: []iampolicymanagementv1.V2PolicyAttribute{*subjectAttribute},
}
policyRole := &iampolicymanagementv1.PolicyRole{
RoleID: core.StringPtr("crn:v1:bluemix:public:iam::::role:Operator"),
}
v2PolicyGrant := &iampolicymanagementv1.V2PolicyBaseControlGrant{
Roles: []iampolicymanagementv1.PolicyRole{*policyRole},
}
v2PolicyControl := &iampolicymanagementv1.V2PolicyBaseControl{
Grant: v2PolicyGrant,
}
accountIDResourceAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("accountId"),
Operator: core.StringPtr("stringEquals"),
Value: core.StringPtr(exampleAccountID),
}
serviceNameResourceAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("serviceType"),
Operator: core.StringPtr("stringEquals"),
Value: core.StringPtr("service"),
}
policyResource := &iampolicymanagementv1.V2PolicyBaseResource{
Attributes: []iampolicymanagementv1.V2PolicyAttribute{
*accountIDResourceAttribute, *serviceNameResourceAttribute},
}
startConditionAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("{{environment.attributes.current_time}}"),
Operator: core.StringPtr("dateTimeGreaterThanOrEquals"),
Value: core.StringPtr("2022-12-23T00:00:00+00:00"),
}
endConditionAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("{{environment.attributes.current_time}}"),
Operator: core.StringPtr("dateTimeLessThanOrEquals"),
Value: core.StringPtr("2022-12-23T23:59:59+00:00"),
}
policyRule := &iampolicymanagementv1.V2PolicyBaseRule{
Operator: core.StringPtr("and"),
Conditions: []iampolicymanagementv1.V2PolicyAttribute{
*startConditionAttribute, *endConditionAttribute},
}
options := iamPolicyManagementService.NewV2CreatePolicyOptions(
"access",
v2PolicyControl,
)
options.SetSubject(policySubject)
options.SetResource(policyResource)
options.SetRule(policyRule)
options.SetPattern(*core.StringPtr("time-based-conditions:once"))
policy, response, err := iamPolicyManagementService.V2CreatePolicy(options)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(policy, "", " ")
fmt.Println(string(b))
V2PolicyAttribute subjectAttribute = new V2PolicyAttribute.Builder()
.key("iam_id")
.value(EXAMPLE_USER_ID)
.operator("stringEquals")
.build();
V2PolicyBaseSubject policySubject = new V2PolicyBaseSubject.Builder()
.addAttributes(subjectAttribute)
.build();
V2PolicyAttribute accountIdResourceAttribute = new V2PolicyAttribute.Builder()
.key("accountId")
.value(exampleAccountId)
.operator("stringEquals")
.build();
V2PolicyAttribute serviceNameResourceAttribute = new V2PolicyAttribute.Builder()
.key("serviceName")
.value("iam-groups")
.operator("stringEquals")
.build();
V2PolicyBaseResource policyResource = new V2PolicyBaseResource.Builder()
.addAttributes(accountIdResourceAttribute)
.addAttributes(serviceNameResourceAttribute)
.build();
PolicyRole policyRoles = new PolicyRole.Builder()
.roleId("crn:v1:bluemix:public:iam::::role:Operator")
.build();
V2PolicyBaseControlGrant policyGrant = new V2PolicyBaseControlGrant.Builder()
.roles(Arrays.asList(policyRoles))
.build();
V2PolicyBaseControl policyControl = new V2PolicyBaseControl.Builder()
.grant(policyGrant)
.build();
V2PolicyAttribute startConditionAttribute = new V2PolicyAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("2022-12-23T00:00:00+00:00")
.operator("dateTimeGreaterThanOrEquals")
.build();
V2PolicyAttribute endConditionAttribute = new V2PolicyAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("2022-12-23T23:59:59+00:00")
.operator("dateTimeLessThanOrEquals")
.build();
V2PolicyBaseRuleV2RuleWithConditions policyRule = new V2PolicyBaseRuleV2RuleWithConditions.Builder()
.operator("and")
.conditions(new ArrayList<V2PolicyAttribute>(Arrays.asList(startConditionAttribute, endConditionAttribute)))
.build();
V2CreatePolicyOptions options = new V2CreatePolicyOptions.Builder()
.type("access")
.subject(policySubject)
.control(policyControl)
.resource(policyResource)
.rule(policyRule)
.pattern("time-based-conditions:once")
.build();
Response<V2Policy> response = service.v2CreatePolicy(options).execute();
V2Policy policy = response.getResult();
const policySubject = {
attributes: [
{
key: 'iam_id',
operator: 'stringEquals',
value: exampleUserId,
},
],
};
const policyResourceAccountAttribute = {
key: 'accountId',
value: exampleAccountId,
operator: 'stringEquals',
};
const policyResourceServiceAttribute = {
key: 'serviceName',
operator: 'stringEquals',
value: 'iam-groups',
};
const policyResource = {
attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute]
};
const policyControl = {
grant: {
roles: [{
role_id: 'crn:v1:bluemix:public:iam::::role:Operator',
}],
}
};
const policyRule = {
operator: 'and',
conditions: [
{
key: '{{environment.attributes.current_time}}',
operator: 'dateTimeGreaterThanOrEquals',
value: '2022-12-23T00:00:00+00:00',
},
{
key: '{{environment.attributes.current_time}}',
operator: 'dateTimeLessThanOrEquals',
value: '2022-12-23T23:59:59+00:00',
},
],
}
const policyPattern = 'time-based-conditions:once'
const params = {
type: 'access',
subject: policySubject,
control: policyControl,
resource: policyResource,
rule: policyRule,
pattern: policyPattern,
};
try {
const res = await iamPolicyManagementService.v2CreatePolicy(params);
examplePolicyId = res.result.id;
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err)
}
policy_subject = V2PolicyBaseSubject(
attributes=[V2PolicyAttribute(key='iam_id', value=example_user_id, operator='stringEquals')]
)
policy_role = PolicyRole(role_id='crn:v1:bluemix:public:iam::::role:Operator')
account_id_resource_attribute = V2PolicyAttribute(
key='accountId', value=example_account_id, operator='stringEquals'
)
service_name_resource_attribute = V2PolicyAttribute(
key='serviceType', value='service', operator='stringEquals'
)
policy_resource = PolicyResource(
attributes=[account_id_resource_attribute, service_name_resource_attribute],
)
policy_control = V2PolicyBaseControl(grant=V2PolicyBaseControlGrant(roles=[policy_role]))
policy_rule = V2PolicyBaseRuleV2RuleWithConditions(
operator='and',
conditions=[
V2PolicyAttribute(
key='{{environment.attributes.current_time}}',
operator='dateTimeGreaterThanOrEquals',
value='2022-12-23T00:00:00+00:00',
),
V2PolicyAttribute(
key='{{environment.attributes.current_time}}',
operator='dateTimeLessThanOrEquals',
value='2022-12-23T23:59:59+00:00',
),
],
)
policy_pattern = 'time-based-conditions:once'
policy = iam_policy_management_service.v2_create_policy(
type='access',
subject=policy_subject,
control=policy_control,
resource=policy_resource,
rule=policy_rule,
pattern=policy_pattern,
).get_result()
print(json.dumps(policy, indent=2))
time-based-conditions:once
というパターンを使用する一時的なポリシーは、自動的に削除されません。 アカウントでポリシーの上限に達しないようにするには、ポリシーの有効期限が切れた後に、管理者は手動でポリシーを削除することができます。
アクセスポリシーの時間ベースの条件についての詳細は、 アクセスポリシーの条件 を参照してください。
コンソールを使用して定期的な時間ベースの条件を作成する
毎週定期的なアクセスを割り当てることができます。 ユーザーにアカウントリソースへのアクセス権を与えるのは、勤務時間中だけにしたい場合もあるでしょう。 繰り返し発生する時間ベースの条件付きアクセスポリシーを割り当てるには、以下の手順に従います
-
IBM Cloud コンソールで 、「管理」 > 「アクセス(IAM)」 に移動します
-
アクセスを割り当てる対象に応じて、 ユーザー、 信頼されたプロファイル、 サービスID、またはアクセスグループを選択します。
-
リストからアイデンティティの名前をクリックし、 アクセスに進みます。
-
「アクセス権限の割り当て」 をクリックします。
-
サービスを選択し 、「次へ」 をクリックします。
- 任意のサービスをユーザーが作成できるようにする場合は、すべての ID およびアクセス対応サービスを選択します。
- 特定のサービスに対するアクセス権限をユーザーに割り当てる場合は、リストからサービスを選択します。
-
ユーザーにアクセス権を付与したいリソースを選択するか、または「すべてのリソース」を選択します。 次へ をクリックします。
-
(オプション)リソースグループへのアクセス権限を選択します。 次へ をクリックします。
-
サービスアクセスとプラットフォームアクセスの役割を任意に選択し 、「次へ」 をクリックします。
-
条件を追加をクリックし、 週を選択します。
-
条件付きポリシーのタイムゾーンを選択します。
例えば、ダブリンを拠点とする開発者向けの条件付きポリシーを作成するとします。 この場合、
UTC+1
を選択すると、次に選択する日付と時刻の範囲がその場所の正しい時間に強制されます。 -
条件を満たすユーザーにアクセス権を付与したい曜日を選択します。
- (オプション) 終日トグル を「いいえ」 に設定し、選択した日の時間枠を指定します。
-
「作成」 をクリックします。
-
**「レビュー」**をクリックします。
-
追加をクリックして、ポリシー構成をポリシー・サマリーに追加します。
-
割り当て をクリックします。
アクセスポリシーの時間ベースの条件についての詳細は、 アクセスポリシーの条件 を参照してください。
CLI を使用して定期的な時間ベースの条件を作成する
毎週定期的なアクセスを割り当てることができます。 ユーザーにアカウントリソースへのアクセス権を与えるのは、勤務時間中だけにしたい場合もあるでしょう。 次の例では、ユーザーに対して時間ベースの繰り返し条件を作成する方法を示します。 このポリシーでは、アカウント管理サービスすべてについて、月曜日から金曜日の就業時間中のアクセス権をエディターとして割り当てています。
-
IBM Cloud® CLI にログインします。 複数のアカウントを持っている場合、使用するアカウントを選択するように求めるプロンプトが出されます。
-r
フラグを使用してリージョンを指定していない場合、リージョンも選択しなければなりません。ibmcloud login
資格情報が拒否された場合、統合 ID を使用している可能性があります。 フェデレーテッド ID を使用してログインするには、
--sso
フラグを使用します。 詳しくは、フェデレーテッド ID を使用したログインを参照してください。IBM Cloud CLI を初めて使用する場合は、入門チュートリアルを参照してください。
-
コマンドを使用して、アクセスポリシーを作成し、それをユーザーまたはサービスIDに割り当てます
ibmcloud iam user-policy-create
。 -
Editor
の役割を持つすべてのアカウント管理サービスへのアクセス権限を割り当てますibmcloud iam service-policy-create [your service ID here] --roles Editor --account-management --api-version v2
-
時間ベースの繰り返しポリシーのJSONスキーマの例を参考に、独自の条件を作成してください
ibmcloud iam service-policy-create [your service ID here] --file [your JSON file name]
{ "type": "access", "description": "time-based conditions policy example restricting access to the full day of 2022-12-23 UTC", "control": { "grant": { "roles": [ { "role_id": "crn:v1:bluemix:public:iam::::role:Editor" } ] } }, "resource": { "attributes": [ { "operator": "stringEquals", "value": "d4b763ad0cbd4dca8dd1edb427d7a77e", "key": "accountId" }, { "value": "platform_service", "operator": "stringEquals", "key": "serviceType" } ] }, "pattern": "time-based-conditions:weekly", "rule": { "operator": "and", "conditions": [ { "key": "{{environment.attributes.day_of_week}}", "operator": "dayOfWeekAnyOf", "value": [ 1, 2, 3, 4, 5 ] }, { "key": "{{environment.attributes.current_time}}", "operator": "timeGreaterThanOrEquals", "value": "00:00:00+00:00" }, { "key": "{{environment.attributes.current_time}}", "operator": "timeLessThanOrEquals", "value": "23:59:59+00:00" } ] }, "subject": { "attributes": [ { "key": "iam_id", "operator": "stringEquals", "value": "IBMid-550000HFVV" } ] } }
アクセスポリシーの時間ベースの条件についての詳細は、 アクセスポリシーの条件 を参照してください。
APIを使用して定期的な時間ベースの条件を作成する
毎週定期的なアクセスを割り当てることができます。 ユーザーにアカウントリソースへのアクセス権を与えるのは、勤務時間中だけにしたい場合もあるでしょう。 以下の例では、ユーザーに対して時間ベースの繰り返し条件を作成する方法を説明します。 このポリシーでは、アカウント管理サービスすべてについて、月曜日から金曜日の就業時間中のアクセス権をエディターとして割り当てています。
subjectAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("iam_id"),
Operator: core.StringPtr("stringEquals"),
Value: &exampleUserID,
}
policySubject := &iampolicymanagementv1.V2PolicyBaseSubject{
Attributes: []iampolicymanagementv1.V2PolicyAttribute{*subjectAttribute},
}
policyRole := &iampolicymanagementv1.PolicyRole{
RoleID: core.StringPtr("crn:v1:bluemix:public:iam::::role:Editor"),
}
v2PolicyGrant := &iampolicymanagementv1.V2PolicyBaseControlGrant{
Roles: []iampolicymanagementv1.PolicyRole{*policyRole},
}
v2PolicyControl := &iampolicymanagementv1.V2PolicyBaseControl{
Grant: v2PolicyGrant,
}
accountIDResourceAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("accountId"),
Operator: core.StringPtr("stringEquals"),
Value: core.StringPtr(exampleAccountID),
}
serviceNameResourceAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("serviceType"),
Operator: core.StringPtr("stringEquals"),
Value: core.StringPtr("service"),
}
policyResource := &iampolicymanagementv1.V2PolicyBaseResource{
Attributes: []iampolicymanagementv1.V2PolicyAttribute{
*accountIDResourceAttribute, *serviceNameResourceAttribute},
}
weeklyConditionAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("{{environment.attributes.day_of_week}}"),
Operator: core.StringPtr("dayOfWeekAnyOf"),
Value: []int{1,2,3,4,5},
}
startConditionAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("{{environment.attributes.current_time}}"),
Operator: core.StringPtr("timeGreaterThanOrEquals"),
Value: core.StringPtr("09:00:00+00:00"),
}
endConditionAttribute := &iampolicymanagementv1.V2PolicyAttribute{
Key: core.StringPtr("{{environment.attributes.current_time}}"),
Operator: core.StringPtr("timeLessThanOrEquals"),
Value: core.StringPtr("17:00:00+00:00"),
}
policyRule := &iampolicymanagementv1.V2PolicyBaseRule{
Operator: core.StringPtr("and"),
Conditions: []iampolicymanagementv1.V2PolicyAttribute{
*weeklyConditionAttribute, *startConditionAttribute, *endConditionAttribute},
}
options := iamPolicyManagementService.NewV2CreatePolicyOptions(
"access",
v2PolicyControl,
)
options.SetSubject(policySubject)
options.SetResource(policyResource)
options.SetRule(policyRule)
options.SetPattern(*core.StringPtr("time-based-conditions:weekly"))
policy, response, err := iamPolicyManagementService.V2CreatePolicy(options)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(policy, "", " ")
fmt.Println(string(b))
V2PolicyAttribute subjectAttribute = new V2PolicyAttribute.Builder()
.key("iam_id")
.value(EXAMPLE_USER_ID)
.operator("stringEquals")
.build();
V2PolicyBaseSubject policySubject = new V2PolicyBaseSubject.Builder()
.addAttributes(subjectAttribute)
.build();
V2PolicyAttribute accountIdResourceAttribute = new V2PolicyAttribute.Builder()
.key("accountId")
.value(exampleAccountId)
.operator("stringEquals")
.build();
V2PolicyAttribute serviceNameResourceAttribute = new V2PolicyAttribute.Builder()
.key("serviceName")
.value("iam-groups")
.operator("stringEquals")
.build();
V2PolicyBaseResource policyResource = new V2PolicyBaseResource.Builder()
.addAttributes(accountIdResourceAttribute)
.addAttributes(serviceNameResourceAttribute)
.build();
PolicyRole policyRoles = new PolicyRole.Builder()
.roleId("crn:v1:bluemix:public:iam::::role:Editor")
.build();
V2PolicyBaseControlGrant policyGrant = new V2PolicyBaseControlGrant.Builder()
.roles(Arrays.asList(policyRoles))
.build();
V2PolicyBaseControl policyControl = new V2PolicyBaseControl.Builder()
.grant(policyGrant)
.build();
V2PolicyAttribute weeklyConditionAttribute = new V2PolicyAttribute.Builder()
.key("{{environment.attributes.day_of_week}}")
.value(new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5)))
.operator("dayOfWeekAnyOf")
.build();
V2PolicyAttribute startConditionAttribute = new V2PolicyAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("09:00:00+00:00")
.operator("timeGreaterThanOrEquals")
.build();
V2PolicyAttribute endConditionAttribute = new V2PolicyAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("17:00:00+00:00")
.operator("timeLessThanOrEquals")
.build();
V2PolicyBaseRuleV2RuleWithConditions policyRule = new V2PolicyBaseRuleV2RuleWithConditions.Builder()
.operator("and")
.conditions(new ArrayList<V2PolicyAttribute>(Arrays.asList(weeklyConditionAttribute, startConditionAttribute, endConditionAttribute)))
.build();
V2CreatePolicyOptions options = new V2CreatePolicyOptions.Builder()
.type("access")
.subject(policySubject)
.control(policyControl)
.resource(policyResource)
.rule(policyRule)
.pattern("time-based-conditions:weekly")
.build();
Response<V2Policy> response = service.v2CreatePolicy(options).execute();
V2Policy policy = response.getResult();
System.out.println(policy);
const policySubject = {
attributes: [
{
key: 'iam_id',
operator: 'stringEquals',
value: exampleUserId,
},
],
};
const policyResourceAccountAttribute = {
key: 'accountId',
value: exampleAccountId,
operator: 'stringEquals',
};
const policyResourceServiceAttribute = {
key: 'serviceName',
operator: 'stringEquals',
value: 'iam-groups',
};
const policyResource = {
attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute]
};
const policyControl = {
grant: {
roles: [{
role_id: 'crn:v1:bluemix:public:iam::::role:Editor',
}],
}
};
const policyRule = {
operator: 'and',
conditions: [
{
key: '{{environment.attributes.day_of_week}}',
operator: 'dayOfWeekAnyOf',
value: [1, 2, 3, 4, 5],
},
{
key: '{{environment.attributes.current_time}}',
operator: 'timeGreaterThanOrEquals',
value: '09:00:00+00:00',
},
{
key: '{{environment.attributes.current_time}}',
operator: 'timeLessThanOrEquals',
value: '17:00:00+00:00',
},
],
}
const policyPattern = 'time-based-conditions:weekly'
const params = {
type: 'access',
subject: policySubject,
control: policyControl,
resource: policyResource,
rule: policyRule,
pattern: policyPattern,
};
try {
const res = await iamPolicyManagementService.v2CreatePolicy(params);
examplePolicyId = res.result.id;
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err)
}
policy_subject = V2PolicyBaseSubject(
attributes=[V2PolicyAttribute(key='iam_id', value=example_user_id, operator='stringEquals')]
)
policy_role = PolicyRole(role_id='crn:v1:bluemix:public:iam::::role:Editor')
account_id_resource_attribute = V2PolicyAttribute(
key='accountId', value=example_account_id, operator='stringEquals'
)
service_name_resource_attribute = V2PolicyAttribute(
key='serviceName', value='iam-groups', operator='stringEquals'
)
policy_resource = PolicyResource(
attributes=[account_id_resource_attribute, service_name_resource_attribute],
)
policy_control = V2PolicyBaseControl(grant=V2PolicyBaseControlGrant(roles=[policy_role]))
policy_rule = V2PolicyBaseRuleV2RuleWithConditions(
operator='and',
conditions=[
V2PolicyAttribute(
key='{{environment.attributes.day_of_week}}', operator='dayOfWeekAnyOf', value=[1, 2, 3, 4, 5]
),
V2PolicyAttribute(
key='{{environment.attributes.current_time}}',
operator='timeGreaterThanOrEquals',
value='09:00:00+00:00',
),
V2PolicyAttribute(
key='{{environment.attributes.current_time}}',
operator='timeLessThanOrEquals',
value='17:00:00+00:00',
),
],
)
policy_pattern = 'time-based-conditions:weekly'
policy = iam_policy_management_service.v2_create_policy(
type='access',
subject=policy_subject,
control=policy_control,
resource=policy_resource,
rule=policy_rule,
pattern=policy_pattern,
).get_result()
print(json.dumps(policy, indent=2))
アクセスポリシーの時間ベースの条件についての詳細は、 アクセスポリシーの条件 を参照してください。
コンソールを使用してリソース属性に基づく条件を作成する
条件がアクセスを許可するリソースを決定するリソース属性を指定することで、アクセスを割り当てることができます。 例えば、特定のリソースを使用してアカウント内でデモンストレーションを行う必要があるユーザーがいるかもしれません。 利用可能な演算子に関する詳細情報および例については 、「リソース属性に基づく条件 」を参照してください。
OR
を使用することで、最大10個の条件と最大2レベルのネストが可能です。
以下の手順に従って、属性リソースベースの条件付きアクセスポリシーを割り当てます
-
IBM Cloud コンソールで 、「管理」 > 「アクセス(IAM)」 に移動します
-
アクセスを割り当てる対象に応じて、 ユーザー、 信頼されたプロファイル、 サービスID、またはアクセスグループを選択します。
-
リストからエンティティ名をクリックし、 アクセスに進みます。
-
「アクセス権限の割り当て」 をクリックします。
-
サービスを選択し 、「次へ」 をクリックします。
すべてのサービスがリソース属性に基づく条件をサポートしているわけではありません。 続行するには、リソース属性に基づく条件をサポートするサービスを選択します。 例えば、Cloud Object Storage です。
-
ユーザーにアクセス権を付与したいリソースを選択するか、または 「すべてのリソース」 を選択します。 **「次へ」**をクリックします。
-
(オプション)リソースグループへのアクセス権限を選択します。 次へ をクリックします。
-
サービスアクセスとプラットフォームアクセスの役割を任意に選択し 、「次へ」 をクリックします。
-
条件を追加をクリックし、 詳細な条件ビルダー > 次へを選択します。
-
リソース条件を追加し 、「作成」 をクリックします。
例えば、 dev/David/
および dev/Secret/
フォルダ以下にあるすべて、または devOps/
または cicd/
で始まるパスへのアクセスが必要な開発者向けの条件付きポリシーを作成するとします。 この場合 、「接頭辞」 を選択し 、「文字列がいずれかに一致する 」を選択し、 dev/David/*
と dev/Secret/*
を追加します。 次に、OR条件を追加し 、「パス」 を選択し 、「文字列がいずれかに一致する 」を選択し、 devOps/*
と cicd/*
を追加します。
- 「作成」 をクリックし 、「レビュー」 をクリックし 、「追加」をクリックして、アクセス設定をアクセスサマリーに追加します。
- 割り当て をクリックします。
サービス固有のドキュメント
Cloud Object Storage がリソース属性に基づく条件をどのように使用するかについての詳細は 、「バケット内の個々のオブジェクトへのアクセス制御」 を参照してください。
開始前に
v2/policies
URI( https://iam.coud.ibm.com/v2/policies
)に確実にアクセスし、アクセスポリシーで条件を使用できるようにしてください。 詳細については、 IAMポリシー管理API および 変更履歴 を参照してください。
APIを使用してリソース属性に基づく条件を作成する
条件がアクセスを許可するリソースを決定するリソース属性を指定することで、アクセスを割り当てることができます。 例えば、 dev/David/*
と devA/*
フォルダ以下にあるすべて、または dev/David/*
または devA/*
で始まるパスへのアクセスを必要とする開発者向けの条件付きポリシーを作成するとします。 次の例では、 dev/David/
フォルダ以下にあるすべてのもの、および devA
または devA/
フォルダで始まるすべてのパスへのユーザーアクセスを許可するリソース属性ベースの条件を作成する方法を示します。
OR
を使用することで、最大10個の条件と最大2レベルのネストが可能です。
利用可能な演算子に関する詳細情報および例については 、「リソース属性に基づく条件 」を参照してください。
"pattern": "attribute-based-condition:resource:literal-and-wildcard",
"rule": {
"operator": "or",
"conditions": [
{
"key": "{{resource.attributes.prefix}}",
"operator": "stringMatchAnyOf",
"value": [
"dev/David/*",
"devA*"
]
},
{
"key": "{{resource.attributes.path}}",
"operator": "stringMatchAnyOf",
"value": [
"dev/David/*",
"devA/*"
]
},
{
"operator": "and",
"conditions": [
{
"key": "{{resource.attributes.prefix}}",
"operator": "stringMatchAnyOf",
"value": [
"dev/David/*",
"devA/*"
]
},
{
"key": "{{resource.attributes.delimiter}}",
"operator": "stringEquals",
"value": "/"
}
]
}
]
}