Skip to content

Commit c800766

Browse files
committed
Enable TLS validation
enable by setting `validate_encryption: true` on initialization options
1 parent cb093d2 commit c800766

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/github/ldap.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Ldap
6565
# which to attempt opening connections (default [[host, port]]). Overrides
6666
# host and port if set.
6767
# encryption: optional string. `ssl` or `tls`. nil by default
68+
# validate_encryption: optional boolean. nil by default
6869
# admin_user: optional string ldap administrator user dn for authentication
6970
# admin_password: optional string ldap administrator user password
7071
#
@@ -99,7 +100,7 @@ def initialize(options = {})
99100
@connection.authenticate(options[:admin_user], options[:admin_password])
100101
end
101102

102-
if encryption = check_encryption(options[:encryption])
103+
if encryption = check_encryption(options[:encryption], options[:validate_encryption])
103104
@connection.encryption(encryption)
104105
end
105106

@@ -236,16 +237,18 @@ def capabilities
236237
# Internal - Determine whether to use encryption or not.
237238
#
238239
# encryption: is the encryption method, either 'ssl', 'tls', 'simple_tls' or 'start_tls'.
240+
# validate: is true to enable certificate validation
239241
#
240242
# Returns the real encryption type.
241-
def check_encryption(encryption)
243+
def check_encryption(encryption, validate = false)
242244
return unless encryption
243245

246+
tls_options = validate == true ? OpenSSL::SSL::VERIFY_PEER : {}
244247
case encryption.downcase.to_sym
245248
when :ssl, :simple_tls
246-
:simple_tls
249+
{ method: :simple_tls, tls_options: tls_options }
247250
when :tls, :start_tls
248-
:start_tls
251+
{ method: :start_tls, tls_options: tls_options }
249252
end
250253
end
251254

test/ldap_test.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,30 @@ def test_connection_with_list_of_hosts_with_first_invalid
2525
end
2626

2727
def test_simple_tls
28-
assert_equal :simple_tls, @ldap.check_encryption(:ssl)
29-
assert_equal :simple_tls, @ldap.check_encryption('SSL')
30-
assert_equal :simple_tls, @ldap.check_encryption(:simple_tls)
28+
expected = { method: :simple_tls, tls_options: {} }
29+
assert_equal expected, @ldap.check_encryption(:ssl)
30+
assert_equal expected, @ldap.check_encryption('SSL')
31+
assert_equal expected, @ldap.check_encryption(:simple_tls)
3132
end
3233

3334
def test_start_tls
34-
assert_equal :start_tls, @ldap.check_encryption(:tls)
35-
assert_equal :start_tls, @ldap.check_encryption('TLS')
36-
assert_equal :start_tls, @ldap.check_encryption(:start_tls)
35+
expected = { method: :start_tls, tls_options: {} }
36+
assert_equal expected, @ldap.check_encryption(:tls)
37+
assert_equal expected, @ldap.check_encryption('TLS')
38+
assert_equal expected, @ldap.check_encryption(:start_tls)
39+
end
40+
41+
def test_tls_validation
42+
assert_equal({ method: :start_tls, tls_options: OpenSSL::SSL::VERIFY_PEER },
43+
@ldap.check_encryption(:tls, true))
44+
assert_equal({ method: :start_tls, tls_options: {} },
45+
@ldap.check_encryption(:tls, false))
46+
assert_equal({ method: :start_tls, tls_options: {} },
47+
@ldap.check_encryption(:tls, nil))
48+
assert_equal({ method: :start_tls, tls_options: {} },
49+
@ldap.check_encryption(:tls, 'true'))
50+
assert_equal({ method: :start_tls, tls_options: {} },
51+
@ldap.check_encryption(:tls))
3752
end
3853

3954
def test_search_delegator

0 commit comments

Comments
 (0)