Skip to content
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

Only set encryption option to net-ldap when needed. #16954

Merged
merged 2 commits into from
Feb 6, 2018
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
7 changes: 5 additions & 2 deletions lib/miq_ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ def initialize(options = {})
options[:host] ||= ::Settings.authentication.ldaphost
options[:port] ||= ::Settings.authentication.ldapport
options[:host] = resolve_host(options[:host], options[:port])
options[:encryption] = mode == "ldaps" ? {:method => :simple_tls} : {}
options.store_path(:encryption, :tls_options, :verify_mode, OpenSSL::SSL::VERIFY_NONE) if options[:host].ipaddress?

if mode == "ldaps"
options[:encryption] = {:method => :simple_tls}
Copy link
Member

Choose a reason for hiding this comment

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

Does options[:encryption] need to be a hash even when not using ldaps? I ask because it used to be an empty hash.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, options[:encryption] does not need to be a hash even when not using ldaps.

In fact this PR is fixing exactly that. options[:encryption] can not be an empty hash if it is not needed because net-ldap chokes on and empty hash for options[:encryption]

Making it an empty hash was the bug I introduced with #16850
that this PR is fixing.

Initially options[:encryption] was only set if mode == "ldaps" as follows:

options[:encryption] = {:method => :simple_tls} if mode == "ldaps"

Copy link
Member Author

Choose a reason for hiding this comment

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

@bdunne Thanks again!

options.store_path(:encryption, :tls_options, :verify_mode, OpenSSL::SSL::VERIFY_NONE) if options[:host].ipaddress?
end

# Make sure we do NOT log the clear-text password
log_options = Vmdb::Settings.mask_passwords!(options.deep_clone)
Expand Down
46 changes: 34 additions & 12 deletions spec/lib/miq_ldap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,41 @@
expect(MiqLdap.sid_to_s(data)).to eq("S-1-5-21-4106323499-3255682937-2389761597-1183")
end

it 'returns a hostname when a hostname is availble and does not set verify mode' do
allow(TCPSocket).to receive(:gethostbyname).and_return(["testhostname", "aliases", "type", "192.168.252.20"])
allow(TCPSocket).to receive(:new)
ldap = MiqLdap.new(:host => ["testhostname", "localhost", "dummy", @host])
expect(ldap.ldap.host).to eq("testhostname")
expect(ldap.ldap.instance_variable_get(:@encryption).try(:has_key_path?, :tls_options, :verify_mode)).to be_falsey
context 'when a hostname is available' do
before do
allow(TCPSocket).to receive(:gethostbyname).and_return(["testhostname", "aliases", "type", "192.168.252.20"])
allow(TCPSocket).to receive(:new)
end

it 'when mode is ldaps returns a hostname and does not set verify_mode' do
ldap = MiqLdap.new(:mode => "ldaps", :host => ["testhostname", "localhost", "dummy", @host])
expect(ldap.ldap.host).to eq("testhostname")
expect(ldap.ldap.instance_variable_get(:@encryption).try(:has_key_path?, :tls_options, :verify_mode)).to be_falsey
end

it 'when mode is ldap returns a hostname and does not set encryption options' do
ldap = MiqLdap.new(:mode => "ldap", :host => ["testhostname", "localhost", "dummy", @host])
expect(ldap.ldap.host).to eq("testhostname")
expect(ldap.ldap.instance_variable_get(:@encryption)).to be_nil
end
end

it 'returns an IPAddress and disables verify mode when only an IPAddress is availble' do
expect(TCPSocket).not_to receive(:gethostbyname)
allow(TCPSocket).to receive(:new)
ldap = MiqLdap.new(:host => ["192.168.254.15", "localhost", "dummy", @host])
expect(ldap.ldap.host).to eq("192.168.254.15")
expect(ldap.ldap.instance_variable_get(:@encryption).fetch_path(:tls_options, :verify_mode)).to eq(OpenSSL::SSL::VERIFY_NONE)
context 'when only an IPAddress is available' do
before do
expect(TCPSocket).not_to receive(:gethostbyname)
allow(TCPSocket).to receive(:new)
end

it 'when mode is ldaps returns an IPAddress and disables verify_mode' do
ldap = MiqLdap.new(:mode => "ldaps", :host => ["192.168.254.15", "localhost", "dummy", @host])
expect(ldap.ldap.host).to eq("192.168.254.15")
expect(ldap.ldap.instance_variable_get(:@encryption).fetch_path(:tls_options, :verify_mode)).to eq(OpenSSL::SSL::VERIFY_NONE)
end

it 'when mode is ldap returns an IPAddress and does not set encryption options' do
ldap = MiqLdap.new(:mode => "ldap", :host => ["192.168.254.15", "localhost", "dummy", @host])
expect(ldap.ldap.host).to eq("192.168.254.15")
expect(ldap.ldap.instance_variable_get(:@encryption)).to be_nil
end
end
end