

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

# 一個共用多租戶政策存放區
<a name="avp-design-shared-store"></a>

一個共用多租用戶政策存放區設計模型針對 SaaS 解決方案中的所有租用戶，使用 Amazon Verified Permissions 中的單一多租用戶政策存放區。此方法的主要優點是簡化管理和操作，特別是因為您不必在租戶加入期間建立額外的政策存放區。此方法的缺點包括因政策更新或部署中的任何失敗或錯誤而增加影響範圍，以及更容易受到[雜訊鄰近](https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/noisy-neighbor.html)影響。此外，如果您的解決方案需要每個租用戶的唯一政策，我們不建議使用此方法。在此情況下，請改用每個租用戶的政策存放區模型，以確保使用正確租用戶的政策。

一個共用的多租戶政策存放區方法類似於 SaaS [集區隔離](https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/pool-isolation.html)模型。如果您的 SaaS 應用程式需要，它可以提供租戶隔離的集區方法。如果您的 SaaS 解決方案將[孤立隔離](https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/silo-isolation.html)套用至其微服務，您也可以使用此模型。當您選擇模型時，您應該評估租戶資料隔離的需求，以及 SaaS 應用程式獨立所需的 Verified Permissions 政策結構。

若要在整個 SaaS 解決方案中強制執行一致的租用戶識別符共用方式，最佳實務是在使用者註冊期間將識別符映射到使用者的 SaaS 身分，如先前所述。您可以將此映射作為 IdP 的一部分或在 DynamoDB 等外部資料來源中維護，以將此映射提供給 SaaS 應用程式。我們也建議您將共用政策存放區 ID 映射至使用者。雖然 ID 不會用作租戶隔離的一部分，但這是很好的做法，因為它有助於未來的變更。

下列範例顯示 API 端點如何為`Bob`屬於不同租用戶但與政策存放區 ID 共用以進行`store-multi-tenant`授權的使用者`Alice`和 傳送 JWT。由於所有租戶共用單一政策存放區，您不需要在字符或資料庫中維護政策存放區 ID。由於所有租用戶共用單一政策存放區 ID，因此您可以提供 ID 做為環境變數，供應用程式用來呼叫政策存放區。

![\[Verified Permissions 共用設計模型\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/saas-multitenant-api-access-authorization/images/avp-shared.png)


下列範例政策說明一個共用的多租用戶政策設計範例。在此政策中，具有父系 `MultiTenantApp::User`的委託人`MultiTenantApp::Role``Admin`具有檢視所有資源資料的許可。

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

由於單一政策存放區正在使用中，Verified Permissions 政策存放區必須確保與委託人相關聯的租用屬性符合與資源相關聯的租用屬性。這可以透過在政策存放區中包含下列政策來完成，以確保資源和主體上沒有相符租用屬性的所有授權請求都會遭到拒絕。

```
forbid(
    principal, 
    action, 
    resource
)
unless {
    resource.Tenant == principal.Tenant
};
```

對於使用一個共用多租用戶政策存放區模型的授權請求，政策存放區 ID 是共用政策存放區的識別符。在下列請求中，`User``Alice`允許存取 ，因為她具有 `Role`的 `Admin`，並且與資源和主體相關聯的`Tenant`屬性都是 `TenantA`。

```
{
   "policyStoreId":"store-multi-tenant",
   "principal":{
      "entityType":"MultiTenantApp::User",
      "entityId":"Alice"
   },
   "action":{
      "actionType":"MultiTenantApp::Action",
      "actionId":"viewData"
   },
   "resource":{
      "entityType":"MultiTenantApp::Data",
      "entityId":"my_example_data"
   },
   "entities":{
      "entityList":[
         {
            "identifier":{
               "entityType":"MultiTenantApp::User",
               "entityId":"Alice"
            },
            "attributes": {
                {
                    "Tenant": {
                        "entityIdentifier": {
                            "entityType":"MultitenantApp::Tenant",
                            "entityId":"TenantA"
                        }
                    }
                }
            },
            "parents":[
               {
                  "entityType":"MultiTenantApp::Role",
                  "entityId":"Admin"
               }
            ]
         },
         {
            "identifier":{
               "entityType":"MultiTenantApp::Data",
               "entityId":"my_example_data"
            },
            "attributes": {
                {
                    "Tenant": {
                        "entityIdentifier": {
                            "entityType":"MultitenantApp::Tenant",
                            "entityId":"TenantA"
                        }
                    }
                }
            },
            "parents":[]
         }
      ]
   }
}
```

使用 Verified Permissions，可以但不需要將 IdP 與政策存放區整合。此整合可讓 政策明確參考身分存放區中的委託人做為政策的委託人。如需如何將 與 Amazon Cognito 整合為 Verified Permissions IdP 的詳細資訊，請參閱 [Verified Permissions 文件](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/identity-providers.html)和 [Amazon Cognito 文件](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-authorization-with-avp.html)。

當您將政策存放區與 IdP 整合時，每個政策存放區只能使用一個[身分來源](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/identity-providers.html)。例如，如果您選擇將 Verified Permissions 與 Amazon Cognito 整合，則必須鏡像用於租戶隔離 Verified Permissions 政策存放區和 Amazon Cognito 使用者集區的策略。政策存放區和使用者集區也必須位於相同的 中 AWS 帳戶。

![\[在共用設計模型中整合 Verified Permissions 與 Amazon Cognito\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/saas-multitenant-api-access-authorization/images/cognito-shared.png)


從操作和稽核的觀點來看，一個共用的多租用戶政策存放區模型具有缺點，因為 [中記錄的活動 AWS CloudTrail](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/monitoring-overview.html)需要更多涉及的查詢來篩選租用戶上的個別活動，因為每個記錄的 CloudTrail 呼叫都使用相同的政策存放區。在此案例中，將每個租用戶維度上的其他自訂指標記錄到 Amazon CloudWatch，以確保適當的可觀測性和稽核功能。

一種共用的多租用戶政策存放區方法也需要密切注意[已驗證許可配額](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/quotas.html)，以確保它們不會干擾 SaaS 解決方案的操作。特別是，我們建議您監控*每個區域每個帳戶配額每秒的 IsAuthorized 請求*，以確保不超過其限制。您可以請求提高此配額。