-
Notifications
You must be signed in to change notification settings - Fork 27
Recursive memberOf group search strategy #66
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
107437d
Add GitHub::Ldap::MemberOf search strategies
mtodd 765c8b5
Expose Group.group? for testing if entry is group
mtodd 6942a49
Take attrs option for Member search strategy
mtodd db248e1
Accept optional depth setting
mtodd d33fc08
Test excluded groups, clarify test name
mtodd c5ec886
Merge branch 'master' into member-of-search
mtodd 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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'github/ldap/member_of/classic' | ||
require 'github/ldap/member_of/recursive' | ||
|
||
module GitHub | ||
class Ldap | ||
# Provides various strategies to search for groups a user is a member of. | ||
# | ||
# For example: | ||
# | ||
# group = domain.groups(%w(Engineering)).first | ||
# strategy = GitHub::Ldap::MemberOf::Recursive.new(ldap) | ||
# strategy.perform(user) #=> [#<Net::LDAP::Entry>] | ||
# | ||
module MemberOf | ||
# Internal: Mapping of strategy name to class. | ||
STRATEGIES = { | ||
:classic => GitHub::Ldap::MemberOf::Classic, | ||
:recursive => GitHub::Ldap::MemberOf::Recursive | ||
} | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module GitHub | ||
class Ldap | ||
module MemberOf | ||
# Look up the groups an entry is a member of. | ||
class Classic | ||
include Filter | ||
|
||
# Internal: The GitHub::Ldap object to search domains with. | ||
attr_reader :ldap | ||
|
||
# Public: Instantiate new search strategy. | ||
# | ||
# - ldap: GitHub::Ldap object | ||
# - options: Hash of options (unused) | ||
def initialize(ldap, options = {}) | ||
@ldap = ldap | ||
@options = options | ||
end | ||
|
||
# Public: Performs search for groups an entry is a member of, including | ||
# subgroups. | ||
# | ||
# Returns Array of Net::LDAP::Entry objects. | ||
def perform(entry) | ||
filter = member_filter(entry) | ||
|
||
if ldap.posix_support_enabled? && !entry[ldap.uid].empty? | ||
filter |= posix_member_filter(entry, ldap.uid) | ||
end | ||
|
||
domains.each_with_object([]) do |domain, entries| | ||
entries.concat domain.search(filter: filter) | ||
end | ||
end | ||
|
||
# Internal: Domains to search through. | ||
# | ||
# Returns an Array of GitHub::Ldap::Domain objects. | ||
def domains | ||
@domains ||= ldap.search_domains.map { |base| ldap.domain(base) } | ||
end | ||
private :domains | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module GitHub | ||
class Ldap | ||
module MemberOf | ||
# Look up the groups an entry is a member of, including nested subgroups. | ||
# | ||
# NOTE: this strategy is network and performance intensive. | ||
class Recursive | ||
include Filter | ||
|
||
# Internal: The GitHub::Ldap object to search domains with. | ||
attr_reader :ldap | ||
|
||
# Internal: The maximum depth to search for subgroups. | ||
attr_reader :depth | ||
|
||
# Public: Instantiate new search strategy. | ||
# | ||
# - ldap: GitHub::Ldap object | ||
# - options: Hash of options | ||
def initialize(ldap, options = {}) | ||
@ldap = ldap | ||
@options = options | ||
@depth = options[:depth] | ||
end | ||
|
||
# Public: Performs search for groups an entry is a member of, including | ||
# subgroups. | ||
# | ||
# Returns Array of Net::LDAP::Entry objects. | ||
def perform(entry) | ||
filter = member_filter(entry) | ||
|
||
if ldap.posix_support_enabled? && !entry[ldap.uid].empty? | ||
filter |= posix_member_filter(entry, ldap.uid) | ||
end | ||
|
||
entries = domains.each_with_object([]) do |domain, entries| | ||
entries.concat domain.search(filter: filter) | ||
end | ||
|
||
entries.each_with_object(entries.dup) do |entry, entries| | ||
entries.concat search_strategy.perform(entry) | ||
end.select { |entry| group?(entry) } | ||
end | ||
|
||
# Internal: Domains to search through. | ||
# | ||
# Returns an Array of GitHub::Ldap::Domain objects. | ||
def domains | ||
@domains ||= ldap.search_domains.map { |base| ldap.domain(base) } | ||
end | ||
private :domains | ||
|
||
# Internal: The search strategy to recursively search for nested | ||
# subgroups with. | ||
def search_strategy | ||
@search_strategy ||= | ||
GitHub::Ldap::Members::Recursive.new ldap, | ||
depth: depth, | ||
attrs: %w(objectClass) | ||
end | ||
|
||
# Internal: Returns true if the entry is a group. | ||
def group?(entry) | ||
GitHub::Ldap::Group.group?(entry[:objectclass]) | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require_relative '../test_helper' | ||
|
||
class GitHubLdapClassicMemberOfTest < GitHub::Ldap::Test | ||
def setup | ||
@ldap = GitHub::Ldap.new(options.merge(search_domains: %w(dc=github,dc=com))) | ||
@domain = @ldap.domain("dc=github,dc=com") | ||
@entry = @domain.user?('user1') | ||
@strategy = GitHub::Ldap::MemberOf::Classic.new(@ldap) | ||
end | ||
|
||
def find_group(cn) | ||
@domain.groups([cn]).first | ||
end | ||
|
||
def test_finds_groups_entry_is_a_direct_member_of | ||
member_of = @strategy.perform(@entry) | ||
assert_includes member_of.map(&:dn), find_group("nested-group1").dn | ||
end | ||
|
||
def test_finds_subgroups_entry_is_a_member_of | ||
skip "Classic strategy does not support nested subgroups" | ||
member_of = @strategy.perform(@entry) | ||
assert_includes member_of.map(&:dn), find_group("head-group").dn | ||
assert_includes member_of.map(&:dn), find_group("tail-group").dn | ||
end | ||
|
||
def test_excludes_groups_entry_is_not_a_member_of | ||
member_of = @strategy.perform(@entry) | ||
refute_includes member_of.map(&:dn), find_group("ghe-admins").dn | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require_relative '../test_helper' | ||
|
||
class GitHubLdapRecursiveMemberOfTest < GitHub::Ldap::Test | ||
def setup | ||
@ldap = GitHub::Ldap.new(options.merge(search_domains: %w(dc=github,dc=com))) | ||
@domain = @ldap.domain("dc=github,dc=com") | ||
@entry = @domain.user?('user1') | ||
@strategy = GitHub::Ldap::MemberOf::Recursive.new(@ldap) | ||
end | ||
|
||
def find_group(cn) | ||
@domain.groups([cn]).first | ||
end | ||
|
||
def test_finds_groups_entry_is_a_direct_member_of | ||
member_of = @strategy.perform(@entry) | ||
assert_includes member_of.map(&:dn), find_group("nested-group1").dn | ||
end | ||
|
||
def test_finds_subgroups_entry_is_a_member_of | ||
member_of = @strategy.perform(@entry) | ||
assert_includes member_of.map(&:dn), find_group("head-group").dn | ||
assert_includes member_of.map(&:dn), find_group("tail-group").dn | ||
end | ||
|
||
def test_excludes_groups_entry_is_not_a_member_of | ||
member_of = @strategy.perform(@entry) | ||
refute_includes member_of.map(&:dn), find_group("ghe-admins").dn | ||
end | ||
end |
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.
This looks pretty innocuous, but it can result in N_M *recursive_ queries and exceptionally bad performance for large data sets.
It might make more sense to inline the relevant logic from the search strategy in order to recursively search for multiple entries at a given time (instead of individually). This warrants more consideration and testing.