

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例 3：使用 RBAC 進行多租戶存取控制
<a name="avp-mt-abac-examples"></a>

若要詳細說明先前的 RBAC 範例，您可以擴展需求以包含 SaaS 多租戶，這是 SaaS 供應商的常見需求。在多租用戶解決方案中，一律代表指定的租用戶提供資源存取。也就是說，租戶 A 的使用者無法檢視租戶 B 的資料，即使該資料在邏輯上或實際共置在系統中。下列範例說明如何使用多個 [Verified Permissions 政策存放區](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/policy-stores.html)實作租用戶隔離，以及如何使用使用者角色在租用戶內定義許可。

使用每個租用戶政策存放區設計模式是維護租用戶隔離，同時使用 Verified Permissions 實作存取控制的最佳實務。在此案例中，租戶 A 和租戶 B 使用者請求`DATAMICROSERVICE_POLICYSTORE_B`會分別針對個別的政策存放區 `DATAMICROSERVICE_POLICYSTORE_A`和 進行驗證。如需多租用戶 SaaS 應用程式之 Verified Permissions 設計考量事項的詳細資訊，請參閱 [Verified Permissions 多租用戶設計考量](avp-design-considerations.md)事項一節。

![具有 RBAC、Amazon Verified Permissions 和 Cedar 的多租戶存取控制範例](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/saas-multitenant-api-access-authorization/images/avp-example-3.png)


下列政策位於`DATAMICROSERVICE_POLICYSTORE_A`政策存放區中。它會驗證委託人將是類型 `allAccessRole`群組的一部分`Role`。在這種情況下，將允許主體對與租用戶 A 相關聯的所有資源執行 `viewData`和 `updateData`動作。

```
permit (
    principal in MultitenantApp::Role::"allAccessRole",
    action in [
        MultitenantApp::Action::"viewData",
        MultitenantApp::Action::"updateData"
    ],
    resource
);
```

下列政策位於`DATAMICROSERVICE_POLICYSTORE_B`政策存放區中。第一個政策會驗證委託人是類型 `updateDataRole`群組的一部分`Role`。假設是這種情況，它會授予主體許可，以對與租用戶 B 相關聯的資源執行 `updateData`動作。

```
permit (
    principal in MultitenantApp::Role::"updateDataRole",
    action == MultitenantApp::Action::"updateData",
    resource
);
```

第二個政策強制允許屬於 類型`viewDataRole`群組的主體對與租用戶 B 相關聯的資源`Role`執行 `viewData`動作。

```
permit (
    principal in MultitenantApp::Role::"viewDataRole",
    action == MultitenantApp::Action::"viewData",
    resource
);
```

從租用戶 A 發出的授權請求需要傳送到`DATAMICROSERVICE_POLICYSTORE_A`政策存放區，並由屬於該存放區的政策進行驗證。在這種情況下，它是由此範例之前討論的第一個政策進行驗證。在此授權請求中，值`User`為 的 類型主體`Alice`正在請求 執行`viewData`動作。委託人屬於類型 `allAccessRole`的群組`Role`。Alice 正在嘗試對`SampleData`資源執行 `viewData`動作。由於 Alice 具有 `allAccessRole`角色，因此此評估會產生 `ALLOW`決策。

```
{
  "policyStoreId": "DATAMICROSERVICE_POLICYSTORE_A",
  "principal": {
      "entityType": "MultitenantApp::User",
      "entityId": "Alice"
  },
  "action": {
      "actionType": "MultitenantApp::Action",
      "actionId": "viewData"
  },
  "resource": {
      "entityType": "MultitenantApp::Data",
      "entityId": "SampleData"
  },
  "entities": {
    "entityList": [
      {
        "identifier": {
            "entityType": "MultitenantApp::User",
            "entityId": "Alice"
        },
        "attributes": {},
        "parents": [
            {
                "entityType": "MultitenantApp::Role",
                "entityId": "allAccessRole"
            }
        ]
      },
      {
        "identifier": {
            "entityType": "MultitenantApp::Data",
            "entityId": "SampleData"
        },
        "attributes": {},
        "parents": []
      }
    ]
  }
}
```

如果您改為檢視 向租戶 B 提出的請求`User Bob`，您會看到類似以下授權請求的內容。請求會傳送到`DATAMICROSERVICE_POLICYSTORE_B`政策存放區，因為它源自租用戶 B。在此請求中，委託人`Bob`想要`updateData`對資源 執行 動作`SampleData`。不過， `Bob` 不是可存取`updateData`該資源上 動作的群組的一部分。因此，請求會產生`DENY`決策。

```
{
  "policyStoreId": "DATAMICROSERVICE_POLICYSTORE_B",
  "principal": {
      "entityType": "MultitenantApp::User",
      "entityId": "Bob"
  },
  "action": {
      "actionType": "MultitenantApp::Action",
      "actionId": "updateData"
  },
  "resource": {
      "entityType": "MultitenantApp::Data",
      "entityId": "SampleData"
  },
  "entities": {
    "entityList": [
      {
        "identifier": {
            "entityType": "MultitenantApp::User",
            "entityId": "Bob"
        },
        "attributes": {},
        "parents": [
            {
                "entityType": "MultitenantApp::Role",
                "entityId": "viewDataRole"
            }
        ]
      },
      {
        "identifier": {
            "entityType": "MultitenantApp::Data",
            "entityId": "SampleData"
        },
        "attributes": {},
        "parents": []
      }
    ]
  }
}
```

在此第三個範例中， `User Alice `會嘗試對資源 執行 `viewData`動作`SampleData`。此請求會導向至`DATAMICROSERVICE_POLICYSTORE_A`政策存放區，因為主體`Alice`屬於租用戶 A。 `Alice`是 類型 `allAccessRole` 群組的一部分`Role`，允許她對 資源執行`viewData`動作。因此，請求會產生 `ALLOW`決策。

```
{
  "policyStoreId": "DATAMICROSERVICE_POLICYSTORE_A",
  "principal": {
      "entityType": "MultitenantApp::User",
      "entityId": "Alice"
  },
  "action": {
      "actionType": "MultitenantApp::Action",
      "actionId": "viewData"
  },
  "resource": {
      "entityType": "MultitenantApp::Data",
      "entityId": "SampleData"
  },
  "entities": {
    "entityList": [
      {
        "identifier": {
            "entityType": "MultitenantApp::User",
            "entityId": "Alice"
        },
        "attributes": {},
        "parents": [
            {
                "entityType": "MultitenantApp::Role",
                "entityId": "allAccessRole"
            }
        ]
      },
      {
        "identifier": {
            "entityType": "MultitenantApp::Data",
            "entityId": "SampleData"
        },
        "attributes": {},
        "parents": []
      }
    ]
  }
}
```