2012-09-12 02:23:16 -04:00
|
|
|
module Gitlab
|
2016-06-03 11:07:40 -04:00
|
|
|
module Auth
|
2016-09-15 05:57:09 -04:00
|
|
|
Result = Struct.new(:user, :project, :type, :capabilities) do
|
|
|
|
def succeeded?
|
|
|
|
user.present? || [:ci].include?(type)
|
2016-09-14 11:28:24 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-03 08:57:34 -04:00
|
|
|
|
2016-04-29 12:58:55 -04:00
|
|
|
class << self
|
2016-06-10 08:51:16 -04:00
|
|
|
def find_for_git_client(login, password, project:, ip:)
|
2016-04-29 12:58:55 -04:00
|
|
|
raise "Must provide an IP for rate limiting" if ip.nil?
|
|
|
|
|
2016-09-14 11:28:24 -04:00
|
|
|
result = service_access_token_check(login, password, project) ||
|
|
|
|
build_access_token_check(login, password) ||
|
|
|
|
user_with_password_for_git(login, password) ||
|
|
|
|
oauth_access_token_check(login, password) ||
|
|
|
|
personal_access_token_check(login, password) ||
|
|
|
|
Result.new
|
2016-04-29 12:58:55 -04:00
|
|
|
|
2016-09-15 05:57:09 -04:00
|
|
|
rate_limit!(ip, success: result.succeeded?, login: login)
|
2016-06-03 08:57:34 -04:00
|
|
|
result
|
2016-04-29 12:58:55 -04:00
|
|
|
end
|
|
|
|
|
2016-06-10 08:51:16 -04:00
|
|
|
def find_with_user_password(login, password)
|
2016-04-29 12:58:55 -04:00
|
|
|
user = User.by_login(login)
|
|
|
|
|
|
|
|
# If no user is found, or it's an LDAP server, try LDAP.
|
|
|
|
# LDAP users are only authenticated via LDAP
|
|
|
|
if user.nil? || user.ldap_user?
|
|
|
|
# Second chance - try LDAP authentication
|
|
|
|
return nil unless Gitlab::LDAP::Config.enabled?
|
|
|
|
|
|
|
|
Gitlab::LDAP::Authentication.login(login, password)
|
|
|
|
else
|
|
|
|
user if user.valid_password?(password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-06 11:40:26 -04:00
|
|
|
def rate_limit!(ip, success:, login:)
|
|
|
|
rate_limiter = Gitlab::Auth::IpRateLimiter.new(ip)
|
|
|
|
return unless rate_limiter.enabled?
|
|
|
|
|
|
|
|
if success
|
|
|
|
# Repeated login 'failures' are normal behavior for some Git clients so
|
|
|
|
# it is important to reset the ban counter once the client has proven
|
|
|
|
# they are not a 'bad guy'.
|
|
|
|
rate_limiter.reset!
|
|
|
|
else
|
|
|
|
# Register a login failure so that Rack::Attack can block the next
|
|
|
|
# request from this IP if needed.
|
|
|
|
rate_limiter.register_fail!
|
|
|
|
|
|
|
|
if rate_limiter.banned?
|
|
|
|
Rails.logger.info "IP #{ip} failed to login " \
|
|
|
|
"as #{login} but has been temporarily banned from Git auth"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-29 12:58:55 -04:00
|
|
|
private
|
|
|
|
|
2016-09-14 11:28:24 -04:00
|
|
|
def service_access_token_check(login, password, project)
|
2016-04-29 12:58:55 -04:00
|
|
|
matched_login = /(?<service>^[a-zA-Z]*-ci)-token$/.match(login)
|
|
|
|
|
2016-09-14 11:28:24 -04:00
|
|
|
return unless project && matched_login.present?
|
2016-04-29 12:58:55 -04:00
|
|
|
|
|
|
|
underscored_service = matched_login['service'].underscore
|
|
|
|
|
2016-08-08 06:01:25 -04:00
|
|
|
if Service.available_services_names.include?(underscored_service)
|
2016-04-29 12:58:55 -04:00
|
|
|
# We treat underscored_service as a trusted input because it is included
|
|
|
|
# in the Service.available_services_names whitelist.
|
2016-06-03 11:23:34 -04:00
|
|
|
service = project.public_send("#{underscored_service}_service")
|
2016-04-29 12:58:55 -04:00
|
|
|
|
2016-09-14 11:28:24 -04:00
|
|
|
if service && service.activated? && service.valid_token?(password)
|
2016-09-15 04:34:53 -04:00
|
|
|
Result.new(nil, project, :ci, build_capabilities)
|
2016-08-17 18:59:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_with_password_for_git(login, password)
|
|
|
|
user = find_with_user_password(login, password)
|
2016-09-14 11:28:24 -04:00
|
|
|
return unless user
|
|
|
|
|
|
|
|
type =
|
|
|
|
if user.two_factor_enabled?
|
|
|
|
:missing_personal_token
|
|
|
|
else
|
|
|
|
:gitlab_or_ldap
|
|
|
|
end
|
|
|
|
|
2016-09-15 05:57:09 -04:00
|
|
|
Result.new(user, nil, type, full_capabilities)
|
2016-08-17 18:59:25 -04:00
|
|
|
end
|
|
|
|
|
2016-04-29 12:58:55 -04:00
|
|
|
def oauth_access_token_check(login, password)
|
|
|
|
if login == "oauth2" && password.present?
|
|
|
|
token = Doorkeeper::AccessToken.by_token(password)
|
2016-08-17 18:21:18 -04:00
|
|
|
if token && token.accessible?
|
|
|
|
user = User.find_by(id: token.resource_owner_id)
|
2016-09-14 11:28:24 -04:00
|
|
|
Result.new(user, nil, :oauth, read_capabilities)
|
2016-08-17 18:21:18 -04:00
|
|
|
end
|
2016-04-29 12:58:55 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-10 20:03:32 -04:00
|
|
|
|
|
|
|
def personal_access_token_check(login, password)
|
|
|
|
if login && password
|
|
|
|
user = User.find_by_personal_access_token(password)
|
2016-08-17 18:21:18 -04:00
|
|
|
validation = User.by_login(login)
|
2016-09-15 05:57:09 -04:00
|
|
|
if user && user == validation
|
|
|
|
Result.new(user, nil, :personal_token, full_capabilities)
|
|
|
|
end
|
2016-08-08 06:01:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_access_token_check(login, password)
|
|
|
|
return unless login == 'gitlab-ci-token'
|
|
|
|
return unless password
|
|
|
|
|
|
|
|
build = Ci::Build.running.find_by_token(password)
|
|
|
|
return unless build
|
|
|
|
|
|
|
|
if build.user
|
|
|
|
# If user is assigned to build, use restricted credentials of user
|
2016-09-15 04:34:53 -04:00
|
|
|
Result.new(build.user, build.project, :build, build_capabilities)
|
2016-08-08 06:01:25 -04:00
|
|
|
else
|
|
|
|
# Otherwise use generic CI credentials (backward compatibility)
|
2016-09-15 04:34:53 -04:00
|
|
|
Result.new(nil, build.project, :ci, build_capabilities)
|
2016-08-17 18:21:18 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-13 09:27:05 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-09-15 04:34:53 -04:00
|
|
|
def build_capabilities
|
2016-09-13 09:27:05 -04:00
|
|
|
[
|
|
|
|
:read_project,
|
2016-09-15 04:34:53 -04:00
|
|
|
:build_download_code,
|
|
|
|
:build_read_container_image,
|
|
|
|
:build_create_container_image
|
2016-09-13 09:27:05 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-09-14 11:28:24 -04:00
|
|
|
def read_capabilities
|
2016-09-15 04:34:53 -04:00
|
|
|
[
|
|
|
|
:read_project,
|
2016-09-13 09:27:05 -04:00
|
|
|
:download_code,
|
2016-09-14 11:28:24 -04:00
|
|
|
:read_container_image
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_capabilities
|
|
|
|
read_capabilities + [
|
2016-09-13 09:27:05 -04:00
|
|
|
:push_code,
|
|
|
|
:update_container_image
|
|
|
|
]
|
|
|
|
end
|
2013-07-16 04:28:19 -04:00
|
|
|
end
|
2012-09-12 02:23:16 -04:00
|
|
|
end
|
|
|
|
end
|