IBM Cloud Docs
以時間和資源屬性為基礎的條件限制存取

以時間和資源屬性為基礎的條件限制存取

您可以設定基於時間的條件,以指定帳戶中資源的臨時存取權,或允許在特定時間視窗內存取資源,也可以設定基於資源屬性的條件,以避免建立多種存取政策來滿足您的存取需求。

您可以建立以時間為基礎的條件,授予特定時間和日期範圍的一次性臨時存取權限,也可以設定每週經常性的存取權限。 例如,您可能希望透過指定週期性存取,讓使用者只能在工作時間存取帳戶資源,或者您可能有承包商或使用者需要示範服務功能,而他們只需要臨時存取權。

對於遵守夏令時間 (DST) 的時區,以時間為基礎的條件不計算夏令時間 (DST) 的變化。 管理員必須根據 DST 變更更新政策,以準確地強制執行基於時間的條件。 例如,東部時區在夏令時間為 UTC-4 小時,而非標準時間的 -5 小時。 標準時間從十一月開始,到三月 DST 開始時結束。

Kubernetes Service 不遵守以時間為基礎的條件。 例如,具有基於時間的條件的政策,授予存取所有啟用身分與存取服務的權限,包括存取 Kubernetes Service 資源。 政策的對象可以在指定的時間條件之外存取某些 Kubernetes Service 資源。

存取政策的時間條件可協助您在指派存取時應用最少權限原則,並在發生安全漏洞時減少攻擊面。

當您建立具有基於資源屬性條件的政策時,可以避免建立多個存取政策來滿足您的存取需求。 相反,您可以使用 OR / AND 運算符號的組合來建立單一政策,這些運算符號會套用在具有字面或通配符值的資源屬性上。 您可以同時對符合多個條件的資源授予存取權 (AND),或在符合幾個條件中的任何一個時授予存取權 (OR)。 例如,使用基於資源屬性的條件,您可以建立一個允許基於下列條件進行存取的策略:Service instance: abc或者attribute-1: xyz或者**(attribute-2: defattribute-3: hij)**。

您必須至少有 2 個使用 OR / AND 和基於資源屬性的條件。 如果您需要新增單一條件,請參閱 在主控台中指定資源存取權限,並在選擇特定資源後新增條件。

若要檢視使用者的存取權限,請參閱 檢視主控台中指定的存取權限

條件模式

以時間為基礎的條件模式

以下模式代表允許的條件排列:

以時間為基礎的條件允許的條件模式。
型樣 範例
基於時間的條件:一次 特定日期上午 9 點至下午 5 點的臨時存取 UTC-5。
基於時間的條件:每週:全日 週一至週五全天重複存取 UTC-5。
基於時間的條件:每週:自訂時間 週一至週五上午 9 點至下午 5 點 UTC-5。

IAM 可防止在相同的政策定義中結合一次性臨時條件和每週經常性條件。

基於資源屬性的條件模式

以下模式代表允許的條件排列:

基於資源屬性的條件的允許條件模式。
型樣 範例
attribute-based-condition:resource:literal-and-wildcard 以具有字面或通配符值的資源屬性為基礎的條件(根據所使用的運算符號)

使用主控台建立臨時時間條件

您可以指定日期和時間範圍,以決定條件何時授予和終止存取權限,從而指定有限的存取期限。 例如,您可能有一位使用者需要在您的帳戶上進行幾小時的示範,或是有一位承包商需要在幾天內暫時存取某項服務。 完成下列步驟,以指定具有臨時時間條件的存取原則:

  1. 在 IBM Cloud 主控台中,移至管理 > 存取 (IAM)

  2. 根據您要指派存取的實體,選擇使用者受信任的設定檔服務 ID存取群組

  3. 按一下清單中的實體名稱,然後進入「存取」。

  4. 按一下指派存取權

  5. 選取服務,然後按一下下一步

    • 如果您希望使用者能夠建立任何服務,請選擇所有已啟用身分與存取的服務
    • 如果要指定使用者對特定服務的存取權,請從清單中選取。
  6. 選擇要指派使用者存取的資源,或選擇所有資源。 按下一步

  7. (選項)選擇資源群組存取角色。 按下一步

  8. 選擇服務存取和平台存取角色的任意組合,然後按一下下一步

  9. 按一下新增條件,然後選擇一次性

  10. 選擇時區。

    舉例來說,假設您要為都柏林的開發人員建立條件政策。 在這種情況下,請選擇 UTC+1,以便您在下一步中選擇的日期和時間範圍可以在該位置的正確時間執行存取。

  11. 完成日期和時間範圍的欄位,以決定條件何時授予和終止存取。

  12. 按一下建立

  13. 按一下審閱

  14. 按一下新增,將您的政策組態新增至政策摘要。

  15. 按一下指派

臨時政策不會自動移除。 為避免達到帳戶中的政策限制,管理員可以在政策到期後手動移除政策。

有關訪問策略基於時間的條件的更多信息,請參閱 訪問策略中的條件

開始之前

請確定您擁有最新版本的 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,請查看 入門教學

  2. 使用下列指令建立存取政策並指定給使用者或服務 ID ibmcloud iam user-policy-create.

  3. Operator 角色指定所有帳戶管理服務的存取權:

    ibmcloud iam service-policy-create [your service ID here] --roles Operator --account-management --api-version v2
    
  4. 使用以下基於時間的臨時原則 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 Policy Management API 及變更記錄

使用 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: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 的臨時政策不會自動移除。 為避免達到帳戶中的政策限制,管理員可以在政策到期後手動移除政策。

有關訪問策略基於時間的條件的更多信息,請參閱 訪問策略中的條件

使用主控台建立重複發生的基於時間的條件

您可以指派每週定期存取。 您可能希望僅允許使用者在工作時間存取帳戶資源。 完成下列步驟,以指定具有重複時間條件的存取政策:

  1. 在 IBM Cloud 主控台中,移至管理 > 存取 (IAM)

  2. 根據您要指派存取的實體,選擇使用者受信任的設定檔服務 ID存取群組

  3. 按一下清單中的身分名稱,然後轉到存取

  4. 按一下指派存取權

  5. 選取服務,然後按一下下一步

    • 如果您希望使用者能夠建立任何服務,請選擇所有已啟用身分與存取的服務
    • 如果要指定使用者對特定服務的存取權,請從清單中選取。
  6. 選擇要指派使用者存取的資源,或選擇所有資源。 按下一步

  7. (選項)選擇資源群組存取角色。 按下一步

  8. 選擇服務存取和平台存取角色的任意組合,然後按一下下一步

  9. 按一下新增條件,然後選擇每週

  10. 選取條件原則的時區。

    舉例來說,假設您要為都柏林的開發人員建立條件政策。 在這種情況下,請選擇 UTC+1,以便您下一步選擇的日期和時間範圍在該位置的正確時間執行。

  11. 選取您希望條件允許存取的一週天數。

    • (可選)將 “全天” 切換設為 “否” 以指定您選擇的日期的時間範圍。
  12. 按一下建立

  13. 按一下審閱

  14. 按一下新增,將您的政策組態新增至政策摘要。

  15. 按一下指派

有關訪問策略基於時間的條件的更多信息,請參閱 訪問策略中的條件

使用 CLI 建立以時間為基礎的重複條件

您可以指派每週定期存取。 您可能希望僅允許使用者在工作時間存取帳戶資源。 以下範例說明如何為使用者建立重複的時間條件。 該政策指派週一至週五工作時間內的存取權,作為所有帳戶管理服務的編輯。

  1. 登入 IBM Cloud® CLI。 如果您有多個帳戶,則系統會提示您選取要使用的帳戶。 如果您未使用 -r 旗標來指定地區,則必須同時選取地區。

    ibmcloud login
    

    如果您的認證遭到拒絕,您可能是使用聯合 ID。 若要使用聯合 ID 來登入,請使用 --sso 旗標。 如需詳細資料,請參閱使用聯合 ID 進行登入

    如果您是第一次使用 IBM Cloud CLI,請查看 入門教學

  2. 使用下列指令建立存取政策並指定給使用者或服務 ID ibmcloud iam user-policy-create.

  3. Editor 角色指定所有帳戶管理服務的存取權:

    ibmcloud iam service-policy-create [your service ID here] --roles Editor --account-management --api-version v2
    
  4. 使用以下以時間為基礎的週期性原則 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 層。

完成下列步驟,以指定具有基於屬性資源條件的存取政策:

  1. 在 IBM Cloud 主控台中,移至管理 > 存取 (IAM)

  2. 根據您要指派存取的實體,選擇使用者受信任的設定檔服務 ID存取群組

  3. 按一下清單中的實體名稱,然後進入「存取」。

  4. 按一下指派存取權

  5. 選取服務,然後按一下下一步

    並非所有服務都支援基於資源屬性的條件。 若要繼續,請選擇支援基於資源屬性條件的服務。 例如,雲端物件儲存空間

  6. 選擇要指派使用者存取的資源,或選擇所有資源。 按下一步

  7. (選項)選擇資源群組存取角色。 按下一步

  8. 選擇服務存取和平台存取角色的任意組合,然後按一下下一步

  9. 按一下新增條件,然後選取進階條件建立程式 > 下一步

  10. 新增資源條件,然後按一下建立

舉例來說,假設您要為開發人員建立一個條件政策,該開發人員需要存取 dev/David/dev/Secret/ 資料夾下的所有內容,或任何以 devOps/cicd/ 開頭的路徑。 在這種情況下,選擇前綴字串匹配任何一個,並新增 dev/David/*dev/Secret/*。 然後,新增 OR 條件並選擇 Path字串匹配任何一個,並新增 devOps/*cicd/*

  1. 按一下建立 > 檢閱 > 加入,將您的存取設定加入存取摘要。
  2. 按一下指派

特定服務文件

如需 Cloud Object Storage 如何使用基於資源屬性的條件的詳細資訊,請參閱 控制水桶中個別物件的存取

開始之前

請確定您呼叫 v2/policies URI,也就是 https://iam.coud.ibm.com/v2/policies,這樣您就可以在存取政策中使用條件。 如需詳細資訊,請參閱 IAM Policy Management 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": "/"
          }
        ]
      }
    ]
  }