-
Notifications
You must be signed in to change notification settings - Fork 2
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
Improve Directory Group lookup error handling #339
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c3d3f2
Improve Directory Group lookup error handling
ghsolomon dc7365b
Changes from CR
ghsolomon 90645c2
Merge remote-tracking branch 'origin/develop' into directoryGroupErro…
ghsolomon 1f7cfbd
Merge branch 'develop' into directoryGroupErrorHandling
lbwexler 4228653
Fix NPE in DefaultRoleService.doLoadUsersForDirectoryGroups
ghsolomon 3ca3026
Merge branch 'fixDefaultRoleServiceNPE' into directoryGroupErrorHandling
ghsolomon 3ce4f76
Add comment + fix use of HashSet.remove
ghsolomon 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -58,6 +58,8 @@ import static io.xh.hoist.util.InstanceConfigUtils.getInstanceConfig | |
* Role. | ||
* | ||
* Applications wishing to extend this feature should override doLoadUsersForDirectoryGroups(). | ||
* If doLoadUsersForDirectoryGroups() throws an exception, this service will use the last successful | ||
* lookup result and log an error. Clearing this service's caches will also clear the cached lookup. | ||
* | ||
* Certain aspects of this service and its Admin Console UI are soft-configurable via a JSON | ||
* `xhRoleModuleConfig`. This service will create this config entry if not found on startup. | ||
|
@@ -81,6 +83,7 @@ class DefaultRoleService extends BaseRoleService { | |
private Timer timer | ||
protected Map<String, Set<String>> _allRoleAssignments = emptyMap() | ||
protected ConcurrentMap<String, Set<String>> _roleAssignmentsByUser = new ConcurrentHashMap<>() | ||
protected Map<String, Object> _usersForDirectoryGroups = emptyMap() | ||
|
||
static clearCachesConfigs = ['xhRoleModuleConfig'] | ||
|
||
|
@@ -89,7 +92,7 @@ class DefaultRoleService extends BaseRoleService { | |
|
||
timer = createTimer( | ||
interval: { config.refreshIntervalSecs as int * DateTimeUtils.SECONDS }, | ||
runFn: this.&refreshCaches, | ||
runFn: this.&refreshRoleAssignments, | ||
runImmediatelyAndBlock: true | ||
) | ||
} | ||
|
@@ -168,6 +171,7 @@ class DefaultRoleService extends BaseRoleService { | |
* b) String describing lookup error. | ||
*/ | ||
protected Map<String, Object> doLoadUsersForDirectoryGroups(Set<String> groups) { | ||
if (!groups) return emptyMap() | ||
if (!ldapService.enabled) { | ||
return groups.collectEntries{[it, 'LdapService not enabled in this application']} | ||
} | ||
|
@@ -182,15 +186,17 @@ class DefaultRoleService extends BaseRoleService { | |
if (group) { | ||
foundGroups << name | ||
} else { | ||
ret[name] = 'No AD group found' | ||
ret[name] = 'Directory Group not found' | ||
} | ||
} | ||
|
||
// 2) Search for members of valid groups | ||
ldapService | ||
.lookupGroupMembers(foundGroups) | ||
.each {name, members -> | ||
ret[name] = members.collect(new HashSet()) { it.samaccountname.toLowerCase()} | ||
ret[name] = members.collect(new HashSet()) { it.samaccountname?.toLowerCase() } | ||
// Exclude members without a samaccountname (e.g. email-only contacts within a DL) | ||
ret[name].remove(null) | ||
} | ||
|
||
return ret | ||
|
@@ -269,18 +275,10 @@ class DefaultRoleService extends BaseRoleService { | |
// Implementation/Framework | ||
//--------------------------- | ||
final Map<String, Object> loadUsersForDirectoryGroups(Set<String> directoryGroups) { | ||
if (!directoryGroups) return emptyMap() | ||
// Wrapped here to avoid failing implementations from ever bringing down app. | ||
try { | ||
return doLoadUsersForDirectoryGroups(directoryGroups) | ||
} catch (Throwable e) { | ||
def errMsg = 'Error resolving Directory Groups' | ||
logError(errMsg, e) | ||
return directoryGroups.collectEntries {[it, errMsg] } | ||
} | ||
doLoadUsersForDirectoryGroups(directoryGroups) | ||
} | ||
|
||
protected void refreshCaches() { | ||
void refreshRoleAssignments() { | ||
withDebug('Refreshing role caches') { | ||
_allRoleAssignments = unmodifiableMap(generateRoleAssignments()) | ||
_roleAssignmentsByUser = new ConcurrentHashMap() | ||
|
@@ -290,16 +288,23 @@ class DefaultRoleService extends BaseRoleService { | |
@ReadOnly | ||
protected Map<String, Set<String>> generateRoleAssignments() { | ||
List<Role> roles = Role.list() | ||
Map<String, Object> usersForDirectoryGroups = [:] | ||
|
||
if (directoryGroupsSupported) { | ||
Set<String> groups = roles.collectMany(new HashSet()) { it.directoryGroups } | ||
loadUsersForDirectoryGroups(groups).each { k, v -> | ||
if (v instanceof Set) { | ||
usersForDirectoryGroups[k] = v | ||
} else { | ||
logError("Error resolving users for directory group", k, v) | ||
// Wrapped here to avoid failing implementations from ever bringing down app. | ||
try { | ||
Map<String, Object> usersForDirectoryGroups = [:] | ||
loadUsersForDirectoryGroups(groups).each { k, v -> | ||
if (v instanceof Set) { | ||
usersForDirectoryGroups[k] = v | ||
} else { | ||
logError("Error resolving users for directory group", k, v) | ||
} | ||
} | ||
_usersForDirectoryGroups = usersForDirectoryGroups | ||
} catch (Throwable e) { | ||
// Leave existing _usersForDirectoryGroups cache in place, log error, and continue. | ||
logError("Error resolving users for directory groups", e) | ||
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. Worth a comment to clarify that leaving the previous |
||
} | ||
} | ||
|
||
|
@@ -313,7 +318,7 @@ class DefaultRoleService extends BaseRoleService { | |
if (directoryGroupsSupported) groups.addAll(effRole.directoryGroups) | ||
} | ||
groups.each { group -> | ||
usersForDirectoryGroups[group]?.each { users << it.toLowerCase() } | ||
_usersForDirectoryGroups[group]?.each { users << it.toLowerCase() } | ||
} | ||
|
||
logTrace("Generated assignments for ${role.name}", "${users.size()} effective users") | ||
|
@@ -347,6 +352,9 @@ class DefaultRoleService extends BaseRoleService { | |
} | ||
|
||
void clearCaches() { | ||
_allRoleAssignments = emptyMap() | ||
_roleAssignmentsByUser = new ConcurrentHashMap() | ||
_usersForDirectoryGroups = emptyMap() | ||
timer.forceRun() | ||
super.clearCaches() | ||
} | ||
|
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.
To confirm - we might still get
errorsForGroups
populated as expected with partial lookup failures, but now only in the case of 'No AD group found' - is that correct?Checking that a search returnign no result does not throw- that would be reasonable but didn't want to assume.
(Would suggest changing to "Directory Group not found" FWIW)
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.
Correct - will double-check this now to make sure. And sounds good re: change to error text
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.
Confirmed! Built a snapshot from this branch and tested in a client app to confirm.