Class: GitHub::Ldap
- Inherits:
-
Object
- Object
- GitHub::Ldap
- Extended by:
- Forwardable
- Defined in:
- lib/github/ldap.rb,
lib/github/ldap/group.rb,
lib/github/ldap/domain.rb,
lib/github/ldap/filter.rb,
lib/github/ldap/server.rb,
lib/github/ldap/posix_group.rb,
lib/github/ldap/virtual_group.rb,
lib/github/ldap/virtual_attributes.rb
Defined Under Namespace
Modules: Filter Classes: Domain, Group, PosixGroup, VirtualAttributes, VirtualGroup
Constant Summary collapse
- DEFAULT_FIXTURES_PATH =
Preconfigured user fixtures. If you want to use them for your own tests.
File.('fixtures.ldif', File.dirname(__FILE__))
- DEFAULT_SERVER_OPTIONS =
{ user_fixtures: DEFAULT_FIXTURES_PATH, user_domain: 'dc=github,dc=com', admin_user: 'uid=admin,dc=github,dc=com', admin_password: 'secret', quiet: true, port: 3897 }
Class Attribute Summary collapse
-
.ldap_server ⇒ Object
readonly
ldap_server: is the instance of the testing ldap server, you should never interact with it, but it’s used to grecefully stop it after your tests finalize.
-
.server_options ⇒ Object
readonly
server_options: is the options used to start the server, useful to know in development.
Instance Attribute Summary collapse
-
#search_domains ⇒ Object
readonly
Returns the value of attribute search_domains.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
-
#virtual_attributes ⇒ Object
readonly
Returns the value of attribute virtual_attributes.
Class Method Summary collapse
-
.server_tmp ⇒ Object
Determine the temporal directory where the ldap server lives.
-
.start_server(options = {}) ⇒ Object
Start a testing server.
-
.stop_server ⇒ Object
Stop the testing server.
Instance Method Summary collapse
-
#check_encryption(encryption) ⇒ Object
Internal - Determine whether to use encryption or not.
-
#configure_virtual_attributes(attributes) ⇒ Object
Internal - Configure virtual attributes for this server.
-
#domain(base_name) ⇒ Object
Public - Creates a new domain object to perform operations.
-
#group(base_name) ⇒ Object
Public - Creates a new group object to perform operations.
-
#initialize(options = {}) ⇒ Ldap
constructor
A new instance of Ldap.
-
#load_group(group_entry) ⇒ Object
Public - Create a new group object based on a Net::LDAP::Entry.
-
#posix_support_enabled? ⇒ Boolean
Public - Whether membership checks should include posixGroup filter conditions on ‘memberUid`.
-
#recursive_group_search_fallback? ⇒ Boolean
Public - Whether membership checks should recurse into nested groups when virtual attributes aren’t enabled.
-
#search(options, &block) ⇒ Object
Public - Search entries in the ldap server.
-
#test_connection ⇒ Object
Public - Utility method to check if the connection with the server can be stablished.
Constructor Details
#initialize(options = {}) ⇒ Ldap
Returns a new instance of Ldap.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/github/ldap.rb', line 28 def initialize( = {}) @uid = [:uid] || "sAMAccountName" @connection = Net::LDAP.new({host: [:host], port: [:port]}) if [:admin_user] && [:admin_password] @connection.authenticate([:admin_user], [:admin_password]) end if encryption = check_encryption([:encryption]) @connection.encryption(encryption) end configure_virtual_attributes([:virtual_attributes]) # enable fallback recursive group search unless option is false @recursive_group_search_fallback = ([:recursive_group_search_fallback] != false) # enable posixGroup support unless option is false @posix_support = ([:posix_support] != false) # search_domains is a connection of bases to perform searches # when a base is not explicitly provided. @search_domains = Array([:search_domains]) end |
Class Attribute Details
.ldap_server ⇒ Object (readonly)
ldap_server: is the instance of the testing ldap server,
you should never interact with it,
but it's used to grecefully stop it after your tests finalize.
26 27 28 |
# File 'lib/github/ldap/server.rb', line 26 def ldap_server @ldap_server end |
.server_options ⇒ Object (readonly)
server_options: is the options used to start the server,
useful to know in development.
21 22 23 |
# File 'lib/github/ldap/server.rb', line 21 def @server_options end |
Instance Attribute Details
#search_domains ⇒ Object (readonly)
Returns the value of attribute search_domains.
26 27 28 |
# File 'lib/github/ldap.rb', line 26 def search_domains @search_domains end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
26 27 28 |
# File 'lib/github/ldap.rb', line 26 def uid @uid end |
#virtual_attributes ⇒ Object (readonly)
Returns the value of attribute virtual_attributes.
26 27 28 |
# File 'lib/github/ldap.rb', line 26 def virtual_attributes @virtual_attributes end |
Class Method Details
.server_tmp ⇒ Object
Determine the temporal directory where the ldap server lives. If there is no temporal directory in the environment we create one in the base path.
Returns the path to the temporal directory.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/github/ldap/server.rb', line 55 def self.server_tmp tmp = ENV['TMPDIR'] || ENV['TEMPDIR'] if tmp.nil? tmp = 'tmp' Dir.mkdir(tmp) unless File.directory?('tmp') end tmp end |
.start_server(options = {}) ⇒ Object
Start a testing server. If there is already a server initialized it doesn’t do anything.
options: is a hash with the custom options for the server.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/github/ldap/server.rb', line 33 def self.start_server( = {}) @server_options = DEFAULT_SERVER_OPTIONS.merge() @server_options[:allow_anonymous] ||= false @server_options[:ldif] = @server_options[:user_fixtures] @server_options[:domain] = @server_options[:user_domain] @server_options[:tmpdir] ||= server_tmp @ldap_server = Ladle::Server.new(@server_options) @ldap_server.start end |
.stop_server ⇒ Object
Stop the testing server. If there is no server started this method doesn’t do anything.
47 48 49 |
# File 'lib/github/ldap/server.rb', line 47 def self.stop_server ldap_server && ldap_server.stop end |
Instance Method Details
#check_encryption(encryption) ⇒ Object
Internal - Determine whether to use encryption or not.
encryption: is the encryption method, either ‘ssl’, ‘tls’, ‘simple_tls’ or ‘start_tls’.
Returns the real encryption type.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/github/ldap.rb', line 147 def check_encryption(encryption) return unless encryption case encryption.downcase.to_sym when :ssl, :simple_tls :simple_tls when :tls, :start_tls :start_tls end end |
#configure_virtual_attributes(attributes) ⇒ Object
Internal - Configure virtual attributes for this server. If the option is ‘true`, we’ll use the default virual attributes. If it’s a Hash we’ll map the attributes in the hash.
attributes: is the option set when Ldap is initialized.
Returns a VirtualAttributes.
165 166 167 168 169 170 171 172 173 |
# File 'lib/github/ldap.rb', line 165 def configure_virtual_attributes(attributes) @virtual_attributes = if attributes == true VirtualAttributes.new(true) elsif attributes.is_a?(Hash) VirtualAttributes.new(true, attributes) else VirtualAttributes.new(false) end end |
#domain(base_name) ⇒ Object
Public - Creates a new domain object to perform operations
base_name: is the dn of the base root.
Returns a new Domain object.
90 91 92 |
# File 'lib/github/ldap.rb', line 90 def domain(base_name) Domain.new(self, base_name, @uid) end |
#group(base_name) ⇒ Object
Public - Creates a new group object to perform operations
base_name: is the dn of the base root.
Returns a new Group object. Returns nil if the dn is not in the server.
100 101 102 103 104 105 |
# File 'lib/github/ldap.rb', line 100 def group(base_name) entry = domain(base_name).bind return unless entry load_group(entry) end |
#load_group(group_entry) ⇒ Object
Public - Create a new group object based on a Net::LDAP::Entry.
group_entry: is a Net::LDAP::Entry.
Returns a Group, PosixGroup or VirtualGroup object.
112 113 114 115 116 117 118 119 120 |
# File 'lib/github/ldap.rb', line 112 def load_group(group_entry) if @virtual_attributes.enabled? VirtualGroup.new(self, group_entry) elsif posix_support_enabled? && PosixGroup.valid?(group_entry) PosixGroup.new(self, group_entry) else Group.new(self, group_entry) end end |
#posix_support_enabled? ⇒ Boolean
Public - Whether membership checks should include posixGroup filter conditions on ‘memberUid`. Configurable since some LDAP servers don’t handle unsupported attribute queries gracefully.
Enable by passing :posix_support => true.
Returns true, false, or nil (assumed false).
71 72 73 |
# File 'lib/github/ldap.rb', line 71 def posix_support_enabled? @posix_support end |
#recursive_group_search_fallback? ⇒ Boolean
Public - Whether membership checks should recurse into nested groups when virtual attributes aren’t enabled. The fallback search has poor performance characteristics in some cases, in which case this should be disabled by passing :recursive_group_search_fallback => false.
Returns true or false.
60 61 62 |
# File 'lib/github/ldap.rb', line 60 def recursive_group_search_fallback? @recursive_group_search_fallback end |
#search(options, &block) ⇒ Object
Public - Search entries in the ldap server.
options: is a hash with the same options that Net::LDAP::Connection#search supports. block: is an optional block to pass to the search.
Returns an Array of Net::LDAP::Entry.
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/github/ldap.rb', line 128 def search(, &block) result = if [:base] @connection.search(, &block) else search_domains.each_with_object([]) do |base, result| rs = @connection.search(.merge(:base => base), &block) result.concat Array(rs) unless rs == false end end return [] if result == false Array(result) end |
#test_connection ⇒ Object
Public - Utility method to check if the connection with the server can be stablished. It tries to bind with the ldap auth default configuration.
Returns an OpenStruct with ‘code` and `message`. If `code` is 0, the operation succeeded and there is no message.
80 81 82 83 |
# File 'lib/github/ldap.rb', line 80 def test_connection @connection.bind last_operation_result end |