本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
範例 3:使用 RBAC 進行多租戶存取控制
若要詳細說明先前的 RBAC 範例,您可以擴展您的需求以包含 SaaS 多租戶,這是 SaaS 供應商的常見需求。在多租戶解決方案中,一律代表指定的租戶提供資源存取。也就是說,租用戶 A 的使用者無法檢視租用戶 B 的資料,即使該資料在邏輯上或實體上位於系統中。下列範例說明如何使用多個 Verified Permissions 政策存放區來實作租用戶隔離,以及如何使用使用者角色來定義租用戶內的許可。
使用每個租用戶政策存放區設計模式是維護租用戶隔離,同時透過 Verified Permissions 實作存取控制的最佳實務。在此案例中,租用戶 A 和租用戶 B 使用者請求DATAMICROSERVICE_POLICYSTORE_B
會分別針對個別的政策存放區DATAMICROSERVICE_POLICYSTORE_A
和 進行驗證。如需多租用戶 SaaS 應用程式之 Verified Permissions 設計考量事項的詳細資訊,請參閱 Verified Permissions 多租用戶設計考量事項一節。

下列政策位於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 );
第二個政策要求Role
應允許屬於 類型viewDataRole
群組的主體對與租用戶 B 相關聯的資源執行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": [] } ] } }