Repository navigation
RANGER-5850: Validate nested role membership for cycles and avoid rec… #1324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vyommani
wants to merge
1
commit into
apache:master
Choose a base branch
from
vyommani:RANGER-5850
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,9 +51,12 @@ | |
| import org.springframework.stereotype.Component; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import javax.ws.rs.WebApplicationException; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Deque; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
@@ -113,6 +116,8 @@ public RangerRole createRole(RangerRole role, Boolean createNonExistUserGroupRol | |
| throw restErrorUtil.createRESTException("role with name: " + role.getName() + " already exists", MessageEnums.ERROR_DUPLICATE_OBJECT); | ||
| } | ||
|
|
||
| ensureRoleNotNestedInItself(role); | ||
|
|
||
| Runnable roleVersionUpdater = new RoleVersionUpdater(daoMgr); | ||
| transactionSynchronizationAdapter.executeOnTransactionCommit(roleVersionUpdater); | ||
|
|
||
|
|
@@ -149,6 +154,8 @@ public RangerRole updateRole(RangerRole role, Boolean createNonExistUserGroupRol | |
| oldRole = JsonUtils.jsonToObject(xxRole.getRoleText(), RangerRole.class); | ||
| } | ||
|
|
||
| ensureNoRoleNestingCycle(role, xxRole, oldRole); | ||
|
|
||
| Runnable roleVersionUpdater = new RoleVersionUpdater(daoMgr); | ||
|
|
||
| transactionSynchronizationAdapter.executeOnTransactionCommit(roleVersionUpdater); | ||
|
|
@@ -454,6 +461,117 @@ private void ensureRoleNameUpdateAllowed(String roleName) throws Exception { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Rejects a new role whose nested roles contain it, directly or transitively. | ||
| * | ||
| * The ref table can't be used here: when an earlier save referenced a not-yet-existing role with | ||
| * createNonExistUserGroupRole=true (e.g. earlier in the same import), its x_role_ref_role row is only written | ||
| * after commit, so that edge exists only in the stored role JSON. So walk the new role's nested roles downwards | ||
| * using their stored definitions. Each role is loaded at most once; a role with no nested roles costs nothing. | ||
| */ | ||
| private void ensureRoleNotNestedInItself(RangerRole role) { | ||
| Set<String> visited = new HashSet<>(); | ||
| Deque<String> pending = new ArrayDeque<>(); | ||
|
|
||
| for (RangerRole.RoleMember member : role.getRoles()) { | ||
| if (member != null && StringUtils.isNotEmpty(member.getName())) { | ||
| pending.add(member.getName()); | ||
| } | ||
| } | ||
|
|
||
| while (!pending.isEmpty()) { | ||
| String nestedRoleName = pending.poll(); | ||
|
|
||
| if (StringUtils.equals(nestedRoleName, role.getName())) { | ||
| throw createNestingCycleException(role.getName()); | ||
| } | ||
|
|
||
| if (!visited.add(nestedRoleName)) { | ||
| continue; | ||
| } | ||
|
|
||
| XXRole xxNestedRole = daoMgr.getXXRole().findByRoleName(nestedRoleName); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider replacing 493 to 508 with a new method in XXRoleDao to retrieve the list of refereced roles: |
||
|
|
||
| if (xxNestedRole == null) { // will be created empty | ||
| continue; | ||
| } | ||
|
|
||
| RangerRole nestedRole = roleService.read(xxNestedRole.getId()); | ||
|
|
||
| if (nestedRole != null) { | ||
| for (RangerRole.RoleMember member : nestedRole.getRoles()) { | ||
| if (member != null && StringUtils.isNotEmpty(member.getName())) { | ||
| pending.add(member.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Rejects an update that would make the role contain itself, directly or through nested roles. | ||
| * | ||
| * Only nested roles added by this update are checked: an edge role -> N closes a cycle iff N is the role | ||
| * itself or one of its ancestors. Ancestors are found by walking x_role_ref_role upwards by id (the same walk | ||
| * updatePolicyVersions() already performs on every update), so no role JSON is loaded. Updates that do not add | ||
| * a nested role (e.g. user/group membership changes) do no extra DB work. | ||
| */ | ||
| private void ensureNoRoleNestingCycle(RangerRole role, XXRole xxRole, RangerRole oldRole) { | ||
| Set<String> addedRoleNames = new HashSet<>(); | ||
|
|
||
| for (RangerRole.RoleMember member : role.getRoles()) { | ||
| if (member != null && StringUtils.isNotEmpty(member.getName())) { | ||
| addedRoleNames.add(member.getName()); | ||
| } | ||
| } | ||
|
|
||
| if (oldRole != null) { | ||
| for (RangerRole.RoleMember member : oldRole.getRoles()) { | ||
| if (member != null) { | ||
| addedRoleNames.remove(member.getName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (addedRoleNames.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| if (addedRoleNames.contains(role.getName()) || addedRoleNames.contains(xxRole.getName())) { | ||
| throw createNestingCycleException(role.getName()); | ||
| } | ||
|
|
||
| // roles that don't exist yet are created empty, so they can't close a cycle | ||
| Set<Long> addedRoleIds = new HashSet<>(daoMgr.getXXRole().getIdsByRoleNames(addedRoleNames).values()); | ||
|
|
||
| if (addedRoleIds.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| Set<Long> visited = new HashSet<>(); | ||
| Deque<Long> pending = new ArrayDeque<>(); | ||
|
|
||
| pending.add(xxRole.getId()); | ||
|
|
||
| while (!pending.isEmpty()) { | ||
| Long roleId = pending.poll(); | ||
|
|
||
| if (addedRoleIds.contains(roleId)) { | ||
| throw createNestingCycleException(role.getName()); | ||
| } | ||
|
|
||
| if (visited.add(roleId)) { | ||
| pending.addAll(daoMgr.getXXRoleRefRole().getContainingRoles(roleId)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private WebApplicationException createNestingCycleException(String roleName) { | ||
| LOG.error("Role {} can not be saved: nested roles would make it contain itself", roleName); | ||
|
|
||
| return restErrorUtil.createRESTException("Invalid role members for role " + roleName + ": a role can not contain itself, directly or through nested roles", MessageEnums.INVALID_INPUT_DATA); | ||
| } | ||
|
|
||
| private void ensureRoleDeleteAllowed(String roleName) throws Exception { | ||
| boolean roleNotInPolicy = ensureRoleNotInPolicy(roleName); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nestedRoleName=>memberRoleName