2014-10-14 03:31:38 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::LDAP::Config do
|
2016-09-29 15:08:27 -04:00
|
|
|
include LdapHelpers
|
|
|
|
|
2017-07-25 13:09:00 -04:00
|
|
|
let(:config) { described_class.new('ldapmain') }
|
2014-10-14 03:31:38 -04:00
|
|
|
|
2017-06-08 19:30:54 -04:00
|
|
|
describe '#initialize' do
|
2014-10-14 03:31:38 -04:00
|
|
|
it 'requires a provider' do
|
2017-07-25 13:09:00 -04:00
|
|
|
expect{ described_class.new }.to raise_error ArgumentError
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
2016-09-29 15:08:27 -04:00
|
|
|
it 'works' do
|
2014-10-14 03:31:38 -04:00
|
|
|
expect(config).to be_a described_class
|
|
|
|
end
|
|
|
|
|
2016-09-29 15:08:27 -04:00
|
|
|
it 'raises an error if a unknown provider is used' do
|
2017-07-25 13:09:00 -04:00
|
|
|
expect{ described_class.new 'unknown' }.to raise_error(RuntimeError)
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-29 15:08:27 -04:00
|
|
|
|
2016-11-11 15:44:08 -05:00
|
|
|
describe '#adapter_options' do
|
|
|
|
it 'constructs basic options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
2017-06-08 18:46:06 -04:00
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 386,
|
|
|
|
'encryption' => 'plain'
|
2016-11-11 15:44:08 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options).to eq(
|
|
|
|
host: 'ldap.example.com',
|
|
|
|
port: 386,
|
2017-06-13 17:01:28 -04:00
|
|
|
encryption: nil
|
2016-11-11 15:44:08 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes authentication options when auth is configured' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
2017-06-08 19:30:54 -04:00
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true,
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => 'super_secret'
|
2016-11-11 15:44:08 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-06-08 19:30:54 -04:00
|
|
|
expect(config.adapter_options).to include({
|
2016-11-11 15:44:08 -05:00
|
|
|
auth: {
|
|
|
|
method: :simple,
|
|
|
|
username: 'uid=admin,dc=example,dc=com',
|
|
|
|
password: 'super_secret'
|
|
|
|
}
|
2017-06-08 19:30:54 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets encryption method to simple_tls when configured as simple_tls' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption]).to include({ method: :simple_tls })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets encryption method to start_tls when configured as start_tls' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'start_tls'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption]).to include({ method: :start_tls })
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when verify_certificates is enabled' do
|
|
|
|
it 'sets tls_options to OpenSSL defaults' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption]).to include({ tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when verify_certificates is disabled' do
|
|
|
|
it 'sets verify_mode to OpenSSL VERIFY_NONE' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption]).to include({
|
|
|
|
tls_options: {
|
|
|
|
verify_mode: OpenSSL::SSL::VERIFY_NONE
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
2016-11-11 15:44:08 -05:00
|
|
|
end
|
2017-06-08 19:57:13 -04:00
|
|
|
|
|
|
|
context 'when ca_file is specified' do
|
|
|
|
it 'passes it through in tls_options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'ca_file' => '/etc/ca.pem'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption][:tls_options]).to include({ ca_file: '/etc/ca.pem' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ca_file is a blank string' do
|
|
|
|
it 'does not add the ca_file key to tls_options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'ca_file' => ' '
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption][:tls_options]).not_to have_key(:ca_file)
|
|
|
|
end
|
|
|
|
end
|
2017-06-08 20:03:57 -04:00
|
|
|
|
|
|
|
context 'when ssl_version is specified' do
|
|
|
|
it 'passes it through in tls_options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'ssl_version' => 'TLSv1_2'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption][:tls_options]).to include({ ssl_version: 'TLSv1_2' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ssl_version is a blank string' do
|
|
|
|
it 'does not add the ssl_version key to tls_options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'ssl_version' => ' '
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options[:encryption][:tls_options]).not_to have_key(:ssl_version)
|
|
|
|
end
|
|
|
|
end
|
2016-11-11 15:44:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#omniauth_options' do
|
|
|
|
it 'constructs basic options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
2017-06-08 18:46:06 -04:00
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 386,
|
|
|
|
'base' => 'ou=users,dc=example,dc=com',
|
|
|
|
'encryption' => 'plain',
|
|
|
|
'uid' => 'uid'
|
2016-11-11 15:44:08 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include(
|
|
|
|
host: 'ldap.example.com',
|
|
|
|
port: 386,
|
|
|
|
base: 'ou=users,dc=example,dc=com',
|
2017-06-08 18:46:06 -04:00
|
|
|
encryption: 'plain',
|
2016-11-11 15:44:08 -05:00
|
|
|
filter: '(uid=%{username})'
|
|
|
|
)
|
|
|
|
expect(config.omniauth_options.keys).not_to include(:bind_dn, :password)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes authentication options when auth is configured' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'uid' => 'sAMAccountName',
|
|
|
|
'user_filter' => '(memberOf=cn=group1,ou=groups,dc=example,dc=com)',
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => 'super_secret'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include(
|
|
|
|
filter: '(&(sAMAccountName=%{username})(memberOf=cn=group1,ou=groups,dc=example,dc=com))',
|
|
|
|
bind_dn: 'uid=admin,dc=example,dc=com',
|
|
|
|
password: 'super_secret'
|
|
|
|
)
|
|
|
|
end
|
2017-06-09 13:29:19 -04:00
|
|
|
|
|
|
|
context 'when verify_certificates is enabled' do
|
|
|
|
it 'specifies disable_verify_certificates as false' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include({ disable_verify_certificates: false })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when verify_certificates is disabled' do
|
|
|
|
it 'specifies disable_verify_certificates as true' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include({ disable_verify_certificates: true })
|
|
|
|
end
|
|
|
|
end
|
2017-06-09 13:30:17 -04:00
|
|
|
|
|
|
|
context 'when ca_file is present' do
|
|
|
|
it 'passes it through' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true,
|
|
|
|
'ca_file' => '/etc/ca.pem'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include({ ca_file: '/etc/ca.pem' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ca_file is blank' do
|
|
|
|
it 'does not include the ca_file option' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true,
|
|
|
|
'ca_file' => ' '
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).not_to have_key(:ca_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-09 13:30:38 -04:00
|
|
|
context 'when ssl_version is present' do
|
|
|
|
it 'passes it through' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true,
|
|
|
|
'ssl_version' => 'TLSv1_2'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include({ ssl_version: 'TLSv1_2' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ssl_version is blank' do
|
|
|
|
it 'does not include the ssl_version option' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'encryption' => 'simple_tls',
|
|
|
|
'verify_certificates' => true,
|
|
|
|
'ssl_version' => ' '
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).not_to have_key(:ssl_version)
|
|
|
|
end
|
|
|
|
end
|
2016-11-11 15:44:08 -05:00
|
|
|
end
|
|
|
|
|
2016-09-29 15:08:27 -04:00
|
|
|
describe '#has_auth?' do
|
|
|
|
it 'is true when password is set' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => 'super_secret'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.has_auth?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when bind_dn is set and password is empty' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => ''
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.has_auth?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when password and bind_dn are not set' do
|
|
|
|
stub_ldap_config(options: { 'bind_dn' => nil, 'password' => nil })
|
|
|
|
|
|
|
|
expect(config.has_auth?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2017-01-05 17:01:04 -05:00
|
|
|
|
|
|
|
describe '#attributes' do
|
|
|
|
it 'uses default attributes when no custom attributes are configured' do
|
|
|
|
expect(config.attributes).to eq(config.default_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'merges the configuration attributes with default attributes' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'attributes' => {
|
|
|
|
'username' => %w(sAMAccountName),
|
|
|
|
'email' => %w(userPrincipalName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.attributes).to include({
|
|
|
|
'username' => %w(sAMAccountName),
|
|
|
|
'email' => %w(userPrincipalName),
|
|
|
|
'name' => 'cn'
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2014-10-23 16:57:16 -04:00
|
|
|
end
|