Skip to content

Commit 125d768

Browse files
author
Zhang Wenhao
committed
<feature>[identity]: introduce resource-viewer roles
* Add resource-viewer roles for supporting to query all resources; * Change multiple permission checks from “Administrator/Synchronization Call Detection Only to the broade “Full Resource Read Access / Read-Only API” detection. Resolves: ZSV-11447 Change-Id: I6d666a686e776b70617474617767676175626d67
1 parent 1e7e0c9 commit 125d768

7 files changed

Lines changed: 48 additions & 3 deletions

File tree

header/src/main/java/org/zstack/header/RBACInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public void roles() {
3232
.actions("org.zstack.header.**")
3333
.build();
3434

35+
roleBuilder()
36+
.name("resource-viewer")
37+
.uuid(AccountConstant.ALL_RESOURCES_READABLE_ROLE_UUID)
38+
.build();
39+
3540
roleBuilder()
3641
.name("sod-system-administrator")
3742
.uuid(AccountConstant.SOD_SYSTEM_ADMIN_ROLE_UUID)

header/src/main/java/org/zstack/header/identity/AccountConstant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public interface AccountConstant {
4141
String OTHER_ROLE_UUID = "80315b1f85314917826b182bf6def552";
4242
String LEGACY_ROLE_UUID = "85cfac2138494b2db6501881e1e68045";
4343

44+
/**
45+
* Allow querying all resources.
46+
* Querying audit data is not allowed
47+
*/
48+
String ALL_RESOURCES_READABLE_ROLE_UUID = "8550153cd5474c79850566787fe0f055";
49+
4450
// for: Separation of Duties
4551
String SOD_SYSTEM_ADMIN_ROLE_UUID = "8550125df53c54edb33d1b8ae83ded55";
4652
String SOD_SECURITY_ADMIN_ROLE_UUID = "855013d87cf55944b4a6c6ae729b3f55";

header/src/main/java/org/zstack/header/message/APIMessage.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ private static void validateValue(String[] validValues, String value, String fie
223223
}
224224
}
225225

226+
public static boolean isReadOnlyApi(Class<?> apiClass) {
227+
// TODO: will add RestRequest.readOnly()
228+
// Note: APIGenerateSshKeyPairMsg is not a read-only api, but it is a sync api
229+
if (apiClass.getSimpleName().equals("APIGenerateSshKeyPairMsg")
230+
|| apiClass.getSimpleName().equals("APIBatchSyncVolumeSizeMsg")
231+
|| apiClass.getSimpleName().equals("APICheckElaborationContentMsg")) {
232+
return false;
233+
}
234+
return APISyncCallMessage.class.isAssignableFrom(apiClass);
235+
}
236+
226237
public String getOperator() {
227238
return null;
228239
}

identity/src/main/java/org/zstack/identity/Account.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.zstack.header.identity.role.RoleAccountRefVO;
66
import org.zstack.header.identity.role.RoleAccountRefVO_;
77

8+
import static org.zstack.header.identity.AccountConstant.ALL_RESOURCES_READABLE_ROLE_UUID;
89
import static org.zstack.header.identity.AccountConstant.SOD_AUDITOR_ROLE_UUID;
910
import static org.zstack.header.identity.AccountConstant.SOD_SYSTEM_ADMIN_ROLE_UUID;
1011

@@ -40,6 +41,21 @@ static boolean isAdmin(String accountUuid) {
4041
return AccountConstant.isAdmin(accountUuid);
4142
}
4243

44+
static boolean isAllResourcesReadable(SessionInventory session) {
45+
return isAllResourcesReadable(session.getAccountUuid());
46+
}
47+
48+
static boolean isAllResourcesReadable(String accountUuid) {
49+
if (isAdminPermission(accountUuid)) {
50+
return true;
51+
}
52+
53+
return Q.New(RoleAccountRefVO.class)
54+
.eq(RoleAccountRefVO_.accountUuid, accountUuid)
55+
.eq(RoleAccountRefVO_.roleUuid, ALL_RESOURCES_READABLE_ROLE_UUID)
56+
.isExists();
57+
}
58+
4359
static boolean supportToQueryAuditsFromAllAccounts(SessionInventory session) {
4460
return supportToQueryAuditsFromAllAccounts(session.getAccountUuid());
4561
}

identity/src/main/java/org/zstack/identity/AccountSubQueryExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AccountSubQueryExtension extends AbstractMysqlQuerySubQueryExtensio
1313

1414
@Override
1515
public String makeSubquery(APIQueryMessage msg, Class inventoryClass) {
16-
if (Account.isAdminPermission(msg.getSession())) {
16+
if (Account.isAllResourcesReadable(msg.getSession())) {
1717
return null;
1818
}
1919

identity/src/main/java/org/zstack/identity/rbac/RBACAPIRequestChecker.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public void check(APIMessage message) {
7676
apiClass.getName()));
7777
}
7878

79+
if (APIMessage.isReadOnlyApi(apiClass) && Account.isAllResourcesReadable(session)) {
80+
// exclude audits / event api
81+
if (!"APIGetAuditDataMsg".equals(apiClass.getSimpleName())
82+
&& !"APIGetEventDataMsg".equals(apiClass.getSimpleName())) {
83+
return;
84+
}
85+
}
86+
7987
if (!check()) {
8088
permissionDenied();
8189
}

identity/src/main/java/org/zstack/identity/rbac/RBACResourceRequestChecker.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.zstack.header.message.APIMessage;
1111
import org.zstack.header.message.APIParam;
1212
import org.zstack.header.message.APIResourceScope;
13-
import org.zstack.header.message.APISyncCallMessage;
1413
import org.zstack.header.tag.SystemTagVO;
1514
import org.zstack.header.tag.SystemTagVO_;
1615
import org.zstack.header.vo.ResourceVO;
@@ -107,7 +106,7 @@ private void checkOperationTarget(APIMessage.FieldParam param) {
107106
return;
108107
}
109108

110-
if (message instanceof APISyncCallMessage) {
109+
if (APIMessage.isReadOnlyApi(message.getClass())) {
111110
// no check to read api
112111
return;
113112
}

0 commit comments

Comments
 (0)