Skip to content

Commit faf7eea

Browse files
Merge branch 'master' into fix-paged-search
2 parents 0c2023f + 46c7d9e commit faf7eea

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ Lint/UselessAssignment:
277277
# Offense count: 38
278278
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
279279
Metrics/AbcSize:
280-
Max: 120
280+
Max: 124
281281

282282
# Offense count: 3
283283
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
@@ -298,12 +298,12 @@ Metrics/ClassLength:
298298
# Offense count: 20
299299
# Configuration parameters: AllowedMethods, AllowedPatterns.
300300
Metrics/CyclomaticComplexity:
301-
Max: 44
301+
Max: 45
302302

303303
# Offense count: 74
304304
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
305305
Metrics/MethodLength:
306-
Max: 128
306+
Max: 130
307307

308308
# Offense count: 1
309309
# Configuration parameters: CountComments, CountAsOne.
@@ -313,7 +313,7 @@ Metrics/ModuleLength:
313313
# Offense count: 12
314314
# Configuration parameters: AllowedMethods, AllowedPatterns.
315315
Metrics/PerceivedComplexity:
316-
Max: 44
316+
Max: 46
317317

318318
# Offense count: 1
319319
Naming/AccessorMethodName:

lib/net/ldap.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ def self.result2string(code) #:nodoc:
480480
# server says it supports them. This is a fix for MS Active Directory
481481
# * :instrumentation_service => An object responsible for instrumenting
482482
# operations, compatible with ActiveSupport::Notifications' public API.
483+
# * :connect_timeout => The TCP socket timeout (in seconds) to use when
484+
# connecting to the LDAP server (default 5 seconds).
483485
# * :encryption => specifies the encryption to be used in communicating
484486
# with the LDAP server. The value must be a Hash containing additional
485487
# parameters, which consists of two keys:

lib/net/ldap/connection.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ def search(args = nil)
425425
# this breaks when calling to_ber. (Can't force binary data to UTF-8)
426426
# we have to disable paging (even though server supports it) to get around this...
427427

428+
user_controls = args.fetch(:controls, [])
428429
controls = []
429430
controls <<
430431
[
@@ -434,7 +435,12 @@ def search(args = nil)
434435
[rfc2696_cookie[0].to_ber, rfc2696_cookie[1].to_ber_bin].to_ber_sequence.to_ber_bin,
435436
].to_ber_sequence if paged
436437
controls << ber_sort if ber_sort
437-
controls = controls.empty? ? nil : controls.to_ber_contextspecific(0)
438+
if controls.empty? && user_controls.empty?
439+
controls = nil
440+
else
441+
controls += user_controls
442+
controls = controls.to_ber_contextspecific(0)
443+
end
438444

439445
write(request, controls, message_id)
440446

lib/net/ldap/dn.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def each_pair
8181
value << char
8282
when ',' then
8383
state = :key
84-
yield key.string.strip, value.string.rstrip
84+
yield key.string.strip, value.string
8585
key = StringIO.new
8686
value = StringIO.new;
8787
else
@@ -93,7 +93,7 @@ def each_pair
9393
when '\\' then state = :value_normal_escape
9494
when ',' then
9595
state = :key
96-
yield key.string.strip, value.string.rstrip
96+
yield key.string.strip, value.string
9797
key = StringIO.new
9898
value = StringIO.new;
9999
else value << char
@@ -142,7 +142,7 @@ def each_pair
142142
when ' ' then state = :value_end
143143
when ',' then
144144
state = :key
145-
yield key.string.strip, value.string.rstrip
145+
yield key.string.strip, value.string
146146
key = StringIO.new
147147
value = StringIO.new;
148148
else raise Net::LDAP::InvalidDNError, "DN badly formed"
@@ -159,7 +159,7 @@ def each_pair
159159
when ' ' then state = :value_end
160160
when ',' then
161161
state = :key
162-
yield key.string.strip, value.string.rstrip
162+
yield key.string.strip, value.string
163163
key = StringIO.new
164164
value = StringIO.new;
165165
else raise Net::LDAP::InvalidDNError, "DN badly formed"
@@ -172,7 +172,7 @@ def each_pair
172172
raise Net::LDAP::InvalidDNError, "DN badly formed" unless
173173
[:value, :value_normal, :value_hexstring, :value_end].include? state
174174

175-
yield key.string.strip, value.string.rstrip
175+
yield key.string.strip, value.string
176176
end
177177

178178
##

test/test_dn.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ def test_escape_space
1414
assert_equal '\\ before_after\\ ', Net::LDAP::DN.escape(' before_after ')
1515
end
1616

17+
def test_retain_spaces
18+
dn = Net::LDAP::DN.new('CN=Foo.bar.baz, OU=Foo \ ,OU=\ Bar, O=Baz')
19+
assert_equal "CN=Foo.bar.baz, OU=Foo \\ ,OU=\\ Bar, O=Baz", dn.to_s
20+
assert_equal ["CN", "Foo.bar.baz", "OU", "Foo ", "OU", " Bar", "O", "Baz"], dn.to_a
21+
end
22+
1723
def test_escape_on_initialize
1824
dn = Net::LDAP::DN.new('cn', ',+"\\<>;', 'ou=company')
1925
assert_equal 'cn=\\,\\+\\"\\\\\\<\\>\\;,ou=company', dn.to_s
@@ -26,7 +32,7 @@ def test_to_a
2632

2733
def test_to_a_parenthesis
2834
dn = Net::LDAP::DN.new('cn = \ James , ou = "Comp\28ny" ')
29-
assert_equal ['cn', ' James', 'ou', 'Comp(ny'], dn.to_a
35+
assert_equal ['cn', ' James ', 'ou', 'Comp(ny'], dn.to_a
3036
end
3137

3238
def test_to_a_hash_symbol

test/test_ldap_connection.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,43 @@ def test_search_net_ldap_connection_event
501501
# ensure no unread
502502
assert unread.empty?, "should not have any leftover unread messages"
503503
end
504+
505+
def test_search_with_controls
506+
# search data
507+
search_data_ber = Net::BER::BerIdentifiedArray.new([1, [
508+
"uid=user1,ou=People,dc=rubyldap,dc=com",
509+
[["uid", ["user1"]]],
510+
]])
511+
search_data_ber.ber_identifier = Net::LDAP::PDU::SearchReturnedData
512+
search_data = [1, search_data_ber]
513+
# search result (end of results)
514+
search_result_ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
515+
search_result_ber.ber_identifier = Net::LDAP::PDU::SearchResult
516+
search_result = [1, search_result_ber]
517+
@tcp_socket.should_receive(:read_ber).and_return(search_data)
518+
.and_return(search_result)
519+
520+
events = @service.subscribe "search.net_ldap_connection"
521+
unread = @service.subscribe "search_messages_unread.net_ldap_connection"
522+
523+
all_but_sacl_flag = 0x1 | 0x2 | 0x4 # OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION
524+
control_values = [all_but_sacl_flag].map(&:to_ber).to_ber_sequence.to_s.to_ber
525+
controls = []
526+
# LDAP_SERVER_SD_FLAGS constant definition, taken from https://ldapwiki.com/wiki/LDAP_SERVER_SD_FLAGS_OID
527+
ldap_server_sd_flags = '1.2.840.113556.1.4.801'.freeze
528+
controls << [ldap_server_sd_flags.to_ber, true.to_ber, control_values].to_ber_sequence
529+
530+
result = @connection.search(filter: "(uid=user1)", base: "ou=People,dc=rubyldap,dc=com", controls: controls)
531+
assert result.success?, "should be success"
532+
533+
# a search event
534+
payload, result = events.pop
535+
assert payload.key?(:result)
536+
assert payload.key?(:filter)
537+
assert_equal "(uid=user1)", payload[:filter].to_s
538+
assert result
539+
540+
# ensure no unread
541+
assert unread.empty?, "should not have any leftover unread messages"
542+
end
504543
end

0 commit comments

Comments
 (0)