2018-10-11 16:12:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
# Load a specific server configuration
|
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
module LDAP
|
|
|
|
class Config
|
|
|
|
NET_LDAP_ENCRYPTION_METHOD = {
|
|
|
|
simple_tls: :simple_tls,
|
|
|
|
start_tls: :start_tls,
|
|
|
|
plain: nil
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
attr_accessor :provider, :options
|
|
|
|
|
2018-05-18 10:27:12 -04:00
|
|
|
InvalidProvider = Class.new(StandardError)
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
def self.enabled?
|
|
|
|
Gitlab.config.ldap.enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.servers
|
|
|
|
Gitlab.config.ldap['servers']&.values || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.available_servers
|
|
|
|
return [] unless enabled?
|
|
|
|
|
2018-05-18 10:27:12 -04:00
|
|
|
_available_servers
|
|
|
|
end
|
|
|
|
|
|
|
|
def self._available_servers
|
2018-02-23 07:10:39 -05:00
|
|
|
Array.wrap(servers.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.providers
|
|
|
|
servers.map { |server| server['provider_name'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_provider?(provider)
|
|
|
|
providers.include?(provider)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.invalid_provider(provider)
|
2018-05-18 10:27:12 -04:00
|
|
|
raise InvalidProvider.new("Unknown provider (#{provider}). Available providers: #{providers}")
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(provider)
|
|
|
|
if self.class.valid_provider?(provider)
|
|
|
|
@provider = provider
|
|
|
|
else
|
|
|
|
self.class.invalid_provider(provider)
|
|
|
|
end
|
|
|
|
|
|
|
|
@options = config_for(@provider) # Use @provider, not provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
base_config.enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def adapter_options
|
|
|
|
opts = base_options.merge(
|
|
|
|
encryption: encryption_options
|
|
|
|
)
|
|
|
|
|
|
|
|
opts.merge!(auth_options) if has_auth?
|
|
|
|
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def omniauth_options
|
|
|
|
opts = base_options.merge(
|
|
|
|
base: base,
|
|
|
|
encryption: options['encryption'],
|
|
|
|
filter: omniauth_user_filter,
|
|
|
|
name_proc: name_proc,
|
2018-05-29 16:33:13 -04:00
|
|
|
disable_verify_certificates: !options['verify_certificates'],
|
|
|
|
tls_options: tls_options
|
2018-02-23 07:10:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if has_auth?
|
|
|
|
opts.merge!(
|
|
|
|
bind_dn: options['bind_dn'],
|
|
|
|
password: options['password']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def base
|
2018-05-18 10:27:12 -04:00
|
|
|
@base ||= Person.normalize_dn(options['base'])
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def uid
|
|
|
|
options['uid']
|
|
|
|
end
|
|
|
|
|
2018-05-18 10:27:12 -04:00
|
|
|
def label
|
|
|
|
options['label']
|
|
|
|
end
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
def sync_ssh_keys?
|
|
|
|
sync_ssh_keys.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# The LDAP attribute in which the ssh keys are stored
|
|
|
|
def sync_ssh_keys
|
|
|
|
options['sync_ssh_keys']
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_filter
|
|
|
|
options['user_filter']
|
|
|
|
end
|
|
|
|
|
|
|
|
def constructed_user_filter
|
|
|
|
@constructed_user_filter ||= Net::LDAP::Filter.construct(user_filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_base
|
|
|
|
options['group_base']
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin_group
|
|
|
|
options['admin_group']
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_directory
|
|
|
|
options['active_directory']
|
|
|
|
end
|
|
|
|
|
|
|
|
def block_auto_created_users
|
|
|
|
options['block_auto_created_users']
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
default_attributes.merge(options['attributes'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def timeout
|
|
|
|
options['timeout'].to_i
|
|
|
|
end
|
|
|
|
|
2018-05-18 10:27:12 -04:00
|
|
|
def external_groups
|
|
|
|
options['external_groups'] || []
|
|
|
|
end
|
|
|
|
|
2018-02-23 07:10:39 -05:00
|
|
|
def has_auth?
|
|
|
|
options['password'] || options['bind_dn']
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_username_or_email_login
|
|
|
|
options['allow_username_or_email_login']
|
|
|
|
end
|
|
|
|
|
|
|
|
def lowercase_usernames
|
|
|
|
options['lowercase_usernames']
|
|
|
|
end
|
|
|
|
|
|
|
|
def name_proc
|
|
|
|
if allow_username_or_email_login
|
|
|
|
proc { |name| name.gsub(/@.*\z/, '') }
|
|
|
|
else
|
|
|
|
proc { |name| name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_attributes
|
|
|
|
{
|
|
|
|
'username' => %w(uid sAMAccountName userid),
|
|
|
|
'email' => %w(mail email userPrincipalName),
|
|
|
|
'name' => 'cn',
|
|
|
|
'first_name' => 'givenName',
|
|
|
|
'last_name' => 'sn'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def base_options
|
|
|
|
{
|
|
|
|
host: options['host'],
|
|
|
|
port: options['port']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_config
|
|
|
|
Gitlab.config.ldap
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_for(provider)
|
|
|
|
base_config.servers.values.find { |server| server['provider_name'] == provider }
|
|
|
|
end
|
|
|
|
|
|
|
|
def encryption_options
|
2018-05-29 16:33:13 -04:00
|
|
|
method = translate_method
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless method
|
2018-02-23 07:10:39 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
method: method,
|
2018-05-29 16:33:13 -04:00
|
|
|
tls_options: tls_options
|
2018-02-23 07:10:39 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-05-29 16:33:13 -04:00
|
|
|
def translate_method
|
|
|
|
NET_LDAP_ENCRYPTION_METHOD[options['encryption']&.to_sym]
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
2018-05-29 16:33:13 -04:00
|
|
|
def tls_options
|
|
|
|
return @tls_options if defined?(@tls_options)
|
|
|
|
|
|
|
|
method = translate_method
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless method
|
2018-02-23 07:10:39 -05:00
|
|
|
|
2018-05-29 16:33:13 -04:00
|
|
|
opts = if options['verify_certificates'] && method != 'plain'
|
|
|
|
# Dup so we don't accidentally overwrite the constant
|
|
|
|
OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.dup
|
2018-02-23 07:10:39 -05:00
|
|
|
else
|
|
|
|
# It is important to explicitly set verify_mode for two reasons:
|
|
|
|
# 1. The behavior of OpenSSL is undefined when verify_mode is not set.
|
|
|
|
# 2. The net-ldap gem implementation verifies the certificate hostname
|
|
|
|
# unless verify_mode is set to VERIFY_NONE.
|
|
|
|
{ verify_mode: OpenSSL::SSL::VERIFY_NONE }
|
|
|
|
end
|
|
|
|
|
2018-05-29 16:33:13 -04:00
|
|
|
opts.merge!(custom_tls_options)
|
2018-02-23 07:10:39 -05:00
|
|
|
|
2018-05-29 16:33:13 -04:00
|
|
|
@tls_options = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_tls_options
|
|
|
|
return {} unless options['tls_options']
|
|
|
|
|
|
|
|
# Dup so we don't overwrite the original value
|
|
|
|
custom_options = options['tls_options'].dup.delete_if { |_, value| value.nil? || value.blank? }
|
|
|
|
custom_options.symbolize_keys!
|
|
|
|
|
|
|
|
if custom_options[:cert]
|
|
|
|
begin
|
|
|
|
custom_options[:cert] = OpenSSL::X509::Certificate.new(custom_options[:cert])
|
|
|
|
rescue OpenSSL::X509::CertificateError => e
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.error "LDAP TLS Options 'cert' is invalid for provider #{provider}: #{e.message}" # rubocop:disable Gitlab/RailsLogger
|
2018-05-29 16:33:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if custom_options[:key]
|
|
|
|
begin
|
|
|
|
custom_options[:key] = OpenSSL::PKey.read(custom_options[:key])
|
|
|
|
rescue OpenSSL::PKey::PKeyError => e
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.error "LDAP TLS Options 'key' is invalid for provider #{provider}: #{e.message}" # rubocop:disable Gitlab/RailsLogger
|
2018-05-29 16:33:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
custom_options
|
2018-02-23 07:10:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def auth_options
|
|
|
|
{
|
|
|
|
auth: {
|
|
|
|
method: :simple,
|
|
|
|
username: options['bind_dn'],
|
|
|
|
password: options['password']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def omniauth_user_filter
|
|
|
|
uid_filter = Net::LDAP::Filter.eq(uid, '%{username}')
|
|
|
|
|
|
|
|
if user_filter.present?
|
|
|
|
Net::LDAP::Filter.join(uid_filter, constructed_user_filter).to_s
|
|
|
|
else
|
|
|
|
uid_filter.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|