yuqi1129 commented on code in PR #10908:
URL: https://github.com/apache/gravitino/pull/10908#discussion_r3171858914
##########
server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizer.java:
##########
@@ -440,20 +447,59 @@ private boolean loadPrivilegeAndAuthorize(
return false;
}
loadRolePrivilege(metalake, username, userId, requestContext);
- return authorizeByJcasbin(userId, metadataObject, metadataId, privilege);
+ return authorizeByIndex(userId, metadataObject, metadataId, privilege,
requestContext);
}
- private boolean authorizeByJcasbin(
- Long userId, MetadataObject metadataObject, Long metadataId, String
privilege) {
+ /**
+ * Resolve a single privilege probe against the per-role policy index.
Replaces the previous
+ * {@code enforcer.enforce} call, which scanned every policy line in the
enforcer for each
+ * probe. Per-request cost goes from {@code O(total_policies)} to {@code
O(roles_per_user)} hash
+ * probes.
+ */
+ private boolean authorizeByIndex(
+ Long userId,
+ MetadataObject metadataObject,
+ Long metadataId,
+ String privilege,
+ AuthorizationRequestContext requestContext) {
if (AuthConstants.OWNER.equals(privilege)) {
Optional<Long> owner = ownerRel.getIfPresent(metadataId);
return Objects.equals(Optional.of(userId), owner);
}
- return enforcer.enforce(
- String.valueOf(userId),
- String.valueOf(metadataObject.type()),
- String.valueOf(metadataId),
- privilege);
+ Set<Long> roleIds = requestContext.getUserRoleIds();
+ if (roleIds.isEmpty()) {
+ return false;
+ }
+ PolicyKey key = new PolicyKey(metadataObject.type().name(), metadataId,
privilege);
+ if (authorizationMode == AuthorizationMode.DENY) {
+ for (Long roleId : roleIds) {
+ Map<PolicyKey, Effect> idx = loadedRoles.getIfPresent(roleId);
+ if (idx != null && idx.get(key) == Effect.DENY) {
Review Comment:
You can describe clearly what the problem is, and about this point, you are
more familiar with it than me. if it has problems, you can directly give me
advice.
##########
server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizer.java:
##########
@@ -440,20 +447,59 @@ private boolean loadPrivilegeAndAuthorize(
return false;
}
loadRolePrivilege(metalake, username, userId, requestContext);
- return authorizeByJcasbin(userId, metadataObject, metadataId, privilege);
+ return authorizeByIndex(userId, metadataObject, metadataId, privilege,
requestContext);
}
- private boolean authorizeByJcasbin(
- Long userId, MetadataObject metadataObject, Long metadataId, String
privilege) {
+ /**
+ * Resolve a single privilege probe against the per-role policy index.
Replaces the previous
+ * {@code enforcer.enforce} call, which scanned every policy line in the
enforcer for each
+ * probe. Per-request cost goes from {@code O(total_policies)} to {@code
O(roles_per_user)} hash
+ * probes.
+ */
+ private boolean authorizeByIndex(
+ Long userId,
+ MetadataObject metadataObject,
+ Long metadataId,
+ String privilege,
+ AuthorizationRequestContext requestContext) {
if (AuthConstants.OWNER.equals(privilege)) {
Optional<Long> owner = ownerRel.getIfPresent(metadataId);
return Objects.equals(Optional.of(userId), owner);
}
- return enforcer.enforce(
- String.valueOf(userId),
- String.valueOf(metadataObject.type()),
- String.valueOf(metadataId),
- privilege);
+ Set<Long> roleIds = requestContext.getUserRoleIds();
+ if (roleIds.isEmpty()) {
+ return false;
+ }
+ PolicyKey key = new PolicyKey(metadataObject.type().name(), metadataId,
privilege);
+ if (authorizationMode == AuthorizationMode.DENY) {
Review Comment:
This is to replace the original `true/false` for `allowEnforcer.`. The
original is just a boolean value which indicates whether it's an '
allowEnforcer `allowEnforcer`or not.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]