Skip to content

Accept depth as option for Recursive membership validator strategy constructor #73

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/github/ldap/membership_validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class Base
#
# - ldap: GitHub::Ldap object
# - groups: Array of Net::LDAP::Entry group objects
def initialize(ldap, groups)
@ldap = ldap
@groups = groups
# - options: Hash of options
def initialize(ldap, groups, options = {})
@ldap = ldap
@groups = groups
@options = options
end

# Abstract: Performs the membership validation check.
Expand Down
28 changes: 26 additions & 2 deletions lib/github/ldap/membership_validators/recursive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,31 @@ class Recursive < Base
DEFAULT_MAX_DEPTH = 9
ATTRS = %w(dn cn)

def perform(entry, depth = DEFAULT_MAX_DEPTH)
# Internal: The maximum depth to search for membership.
attr_reader :depth

# Public: Instantiate new search strategy.
#
# - ldap: GitHub::Ldap object
# - groups: Array of Net::LDAP::Entry group objects
# - options: Hash of options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document the depth attribute here.

# depth: Integer limit of recursion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jch documented depth option.

#
# NOTE: This overrides default behavior to configure `depth`.
def initialize(ldap, groups, options = {})
super
@depth = options[:depth] || DEFAULT_MAX_DEPTH
end

def perform(entry, depth_override = nil)
if depth_override
warn "DEPRECATION WARNING: Calling Recursive#perform with a second argument is deprecated."
warn "Usage:"
warn " strategy = GitHub::Ldap::MembershipValidators::Recursive.new \\"
warn " ldap, depth: 5"
warn " strategy#perform(entry)"
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jch added deprecation warning.


# short circuit validation if there are no groups to check against
return true if groups.empty?

Expand All @@ -36,7 +60,7 @@ def perform(entry, depth = DEFAULT_MAX_DEPTH)
next if membership.empty?

# recurse to at most `depth`
depth.times do |n|
(depth_override || depth).times do |n|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jch use param if set, falling back to configured depth.

# find groups whose members include membership groups
membership = domain.search(filter: membership_filter(membership), attributes: ATTRS)

Expand Down
8 changes: 4 additions & 4 deletions test/membership_validators/recursive_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def setup
@validator = GitHub::Ldap::MembershipValidators::Recursive
end

def make_validator(groups)
def make_validator(groups, options = {})
groups = @domain.groups(groups)
@validator.new(@ldap, groups)
@validator.new(@ldap, groups, options)
end

def test_validates_user_in_group
Expand All @@ -34,8 +34,8 @@ def test_validates_user_in_great_grandchild_group
end

def test_does_not_validate_user_in_great_granchild_group_with_depth
validator = make_validator(%w(n-depth-nested-group3))
refute validator.perform(@entry, 2)
validator = make_validator(%w(n-depth-nested-group3), depth: 2)
refute validator.perform(@entry)
end

def test_does_not_validate_user_not_in_group
Expand Down