Skip to content

Commit 8bdc1fa

Browse files
author
Dave Sims
committed
Memoize forest object; added docs & general cleanup
1 parent 03c7d90 commit 8bdc1fa

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

lib/github/ldap/forest_search.rb

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,44 @@ def search(options, &block)
5353

5454
# Internal: Queries configuration for available domains
5555
#
56-
# Membership of local or global groups need to be evaluated by contacting referral Donmain Controllers
56+
# Membership of local or global groups need to be evaluated by contacting referral
57+
# Domain Controllers
5758
#
58-
# Returns all Domain Controllers within the forest
59-
def get_domain_forest
60-
instrument "get_domain_forest.github_ldap" do |payload|
61-
domains = @connection.search(
62-
base: naming_context,
63-
search_referrals: true,
64-
filter: Net::LDAP::Filter.eq("nETBIOSName", "*")
65-
)
66-
unless domains.nil?
67-
return domains.each_with_object({}) do |server, result|
68-
if server[:ncname].any? and server[:dnsroot].any?
69-
result[server[:ncname].first] = Net::LDAP.new({
70-
host: server[:dnsroot].first,
71-
port: @connection.instance_variable_get(:@encryption)? 636 : 389,
72-
auth: @connection.instance_variable_get(:@auth),
73-
encryption: @connection.instance_variable_get(:@encryption),
74-
instrumentation_service: @connection.instance_variable_get(:@instrumentation_service)
75-
})
59+
# returns: A memoized Hash of Domain Controllers from this AD forest in the format:
60+
#
61+
# {<nCNname> => <connection>}
62+
#
63+
# where "nCName" specifies the distinguished name of the naming context for the domain
64+
# controller, and "connection" is an instance of Net::LDAP that represents a connection
65+
# to that domain controller, for instance:
66+
#
67+
# {"DC=ad,DC=ghe,DC=local" => <Net::LDAP:0x007f9c3e20b200>,
68+
# "DC=fu,DC=bar,DC=local" => <Net::LDAP:0x007f9c3e20b890>}
69+
#
70+
def forest
71+
@forest ||= begin
72+
instrument "get_domain_forest.github_ldap" do
73+
domains = @connection.search(
74+
base: naming_context,
75+
search_referrals: true,
76+
filter: Net::LDAP::Filter.eq("nETBIOSName", "*")
77+
)
78+
if domains
79+
domains.each_with_object({}) do |server, result|
80+
if server[:ncname].any? && server[:dnsroot].any?
81+
result[server[:ncname].first] = Net::LDAP.new({
82+
host: server[:dnsroot].first,
83+
port: @connection.instance_variable_get(:@encryption)? 636 : 389,
84+
auth: @connection.instance_variable_get(:@auth),
85+
encryption: @connection.instance_variable_get(:@encryption),
86+
instrumentation_service: @connection.instance_variable_get(:@instrumentation_service)
87+
})
88+
end
7689
end
90+
else
91+
{}
7792
end
7893
end
79-
return {}
8094
end
8195
end
8296

0 commit comments

Comments
 (0)