2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-15 10:02:44 -05:00
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module InternalHelpers
|
2017-09-18 13:25:23 -04:00
|
|
|
attr_reader :redirected_path
|
|
|
|
|
2019-03-18 12:51:11 -04:00
|
|
|
delegate :wiki?, to: :repo_type
|
|
|
|
|
|
|
|
def repo_type
|
|
|
|
set_project unless defined?(@repo_type) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@repo_type # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-11-15 10:02:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
2017-11-22 02:50:36 -05:00
|
|
|
set_project unless defined?(@project) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@project # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-11-15 10:02:44 -05:00
|
|
|
end
|
2017-06-26 09:55:06 -04:00
|
|
|
|
2016-11-15 10:02:44 -05:00
|
|
|
def ssh_authentication_abilities
|
|
|
|
[
|
|
|
|
:read_project,
|
|
|
|
:download_code,
|
|
|
|
:push_code
|
|
|
|
]
|
|
|
|
end
|
Accept environment variables from the `pre-receive` script.
1. Starting version 2.11, git changed the way the pre-receive flow works.
- Previously, the new potential objects would be added to the main repo. If the
pre-receive passes, the new objects stay in the repo but are linked up. If
the pre-receive fails, the new objects stay orphaned in the repo, and are
cleaned up during the next `git gc`.
- In 2.11, the new potential objects are added to a temporary "alternate object
directory", that git creates for this purpose. If the pre-receive passes, the
objects from the alternate object directory are migrated to the main repo. If
the pre-receive fails the alternate object directory is simply deleted.
2. In our workflow, the pre-recieve script (in `gitlab-shell) calls the
`/allowed` endpoint, which calls out directly to git to perform
various checks. These direct calls to git do _not_ have the necessary
environment variables set which allow access to the "alternate object
directory" (explained above). Therefore these calls to git are not able to
access any of the new potential objects to be added during this push.
3. We fix this by accepting the relevant environment variables
(GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_OBJECT_DIRECTORY) on the
`/allowed` endpoint, and then include these environment variables while
calling out to git.
4. This commit includes (whitelisted) these environment variables while making
the "force push" check. A `Gitlab::Git::RevList` module is extracted to
prevent `ForcePush` from being littered with these checks.
2016-12-07 02:55:49 -05:00
|
|
|
|
2017-04-05 03:29:30 -04:00
|
|
|
def parse_env
|
|
|
|
return {} if params[:env].blank?
|
Accept environment variables from the `pre-receive` script.
1. Starting version 2.11, git changed the way the pre-receive flow works.
- Previously, the new potential objects would be added to the main repo. If the
pre-receive passes, the new objects stay in the repo but are linked up. If
the pre-receive fails, the new objects stay orphaned in the repo, and are
cleaned up during the next `git gc`.
- In 2.11, the new potential objects are added to a temporary "alternate object
directory", that git creates for this purpose. If the pre-receive passes, the
objects from the alternate object directory are migrated to the main repo. If
the pre-receive fails the alternate object directory is simply deleted.
2. In our workflow, the pre-recieve script (in `gitlab-shell) calls the
`/allowed` endpoint, which calls out directly to git to perform
various checks. These direct calls to git do _not_ have the necessary
environment variables set which allow access to the "alternate object
directory" (explained above). Therefore these calls to git are not able to
access any of the new potential objects to be added during this push.
3. We fix this by accepting the relevant environment variables
(GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_OBJECT_DIRECTORY) on the
`/allowed` endpoint, and then include these environment variables while
calling out to git.
4. This commit includes (whitelisted) these environment variables while making
the "force push" check. A `Gitlab::Git::RevList` module is extracted to
prevent `ForcePush` from being littered with these checks.
2016-12-07 02:55:49 -05:00
|
|
|
|
|
|
|
JSON.parse(params[:env])
|
|
|
|
rescue JSON::ParserError
|
2017-04-05 03:29:30 -04:00
|
|
|
{}
|
Accept environment variables from the `pre-receive` script.
1. Starting version 2.11, git changed the way the pre-receive flow works.
- Previously, the new potential objects would be added to the main repo. If the
pre-receive passes, the new objects stay in the repo but are linked up. If
the pre-receive fails, the new objects stay orphaned in the repo, and are
cleaned up during the next `git gc`.
- In 2.11, the new potential objects are added to a temporary "alternate object
directory", that git creates for this purpose. If the pre-receive passes, the
objects from the alternate object directory are migrated to the main repo. If
the pre-receive fails the alternate object directory is simply deleted.
2. In our workflow, the pre-recieve script (in `gitlab-shell) calls the
`/allowed` endpoint, which calls out directly to git to perform
various checks. These direct calls to git do _not_ have the necessary
environment variables set which allow access to the "alternate object
directory" (explained above). Therefore these calls to git are not able to
access any of the new potential objects to be added during this push.
3. We fix this by accepting the relevant environment variables
(GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_OBJECT_DIRECTORY) on the
`/allowed` endpoint, and then include these environment variables while
calling out to git.
4. This commit includes (whitelisted) these environment variables while making
the "force push" check. A `Gitlab::Git::RevList` module is extracted to
prevent `ForcePush` from being littered with these checks.
2016-12-07 02:55:49 -05:00
|
|
|
end
|
2016-11-25 11:10:25 -05:00
|
|
|
|
|
|
|
def log_user_activity(actor)
|
2017-04-12 11:13:24 -04:00
|
|
|
commands = Gitlab::GitAccess::DOWNLOAD_COMMANDS
|
2016-11-25 11:10:25 -05:00
|
|
|
|
|
|
|
::Users::ActivityService.new(actor, 'Git SSH').execute if commands.include?(params[:action])
|
|
|
|
end
|
2017-04-28 12:52:09 -04:00
|
|
|
|
2017-08-29 22:10:41 -04:00
|
|
|
def merge_request_urls
|
|
|
|
::MergeRequests::GetUrlsService.new(project).execute(params[:changes])
|
|
|
|
end
|
|
|
|
|
2019-03-28 21:07:03 -04:00
|
|
|
def push_options_warning(warning)
|
|
|
|
options = Array.wrap(params[:push_options]).map { |p| "'#{p}'" }.join(' ')
|
|
|
|
"Error encountered with push options #{options}: #{warning}"
|
|
|
|
end
|
|
|
|
|
2017-08-31 17:00:40 -04:00
|
|
|
def redis_ping
|
|
|
|
result = Gitlab::Redis::SharedState.with { |redis| redis.ping }
|
|
|
|
|
|
|
|
result == 'PONG'
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-02-02 10:27:30 -05:00
|
|
|
def project_path
|
|
|
|
project&.path || project_path_match[:project_path]
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace_path
|
|
|
|
project&.namespace&.full_path || project_path_match[:namespace_path]
|
2018-01-22 13:10:56 -05:00
|
|
|
end
|
|
|
|
|
2017-04-28 12:52:09 -04:00
|
|
|
private
|
|
|
|
|
2018-02-02 10:27:30 -05:00
|
|
|
def project_path_match
|
|
|
|
@project_path_match ||= params[:project].match(Gitlab::PathRegex.full_project_git_path_regex) || {}
|
2018-01-19 08:04:14 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-04-28 12:52:09 -04:00
|
|
|
def set_project
|
|
|
|
if params[:gl_repository]
|
2019-03-18 12:51:11 -04:00
|
|
|
@project, @repo_type = Gitlab::GlRepository.parse(params[:gl_repository])
|
2017-06-15 20:03:54 -04:00
|
|
|
@redirected_path = nil
|
2017-04-28 12:52:09 -04:00
|
|
|
else
|
2019-03-18 12:51:11 -04:00
|
|
|
@project, @repo_type, @redirected_path = Gitlab::RepoPath.parse(params[:project])
|
2017-04-28 12:52:09 -04:00
|
|
|
end
|
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-05-23 14:34:58 -04:00
|
|
|
|
|
|
|
# Project id to pass between components that don't share/don't have
|
|
|
|
# access to the same filesystem mounts
|
|
|
|
def gl_repository
|
2019-03-18 12:51:11 -04:00
|
|
|
repo_type.identifier_for_subject(project)
|
2017-05-23 14:34:58 -04:00
|
|
|
end
|
|
|
|
|
2019-02-16 09:54:32 -05:00
|
|
|
def gl_project_path
|
|
|
|
if wiki?
|
|
|
|
project.wiki.full_path
|
|
|
|
else
|
|
|
|
project.full_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-26 09:55:06 -04:00
|
|
|
# Return the repository depending on whether we want the wiki or the
|
|
|
|
# regular repository
|
|
|
|
def repository
|
2019-03-18 12:51:11 -04:00
|
|
|
if repo_type.wiki?
|
2017-06-26 09:55:06 -04:00
|
|
|
project.wiki.repository
|
2017-05-23 14:34:58 -04:00
|
|
|
else
|
2017-06-26 09:55:06 -04:00
|
|
|
project.repository
|
2017-05-23 14:34:58 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-26 09:55:06 -04:00
|
|
|
|
|
|
|
# Return the Gitaly Address if it is enabled
|
|
|
|
def gitaly_payload(action)
|
2018-03-21 09:57:06 -04:00
|
|
|
return unless %w[git-receive-pack git-upload-pack git-upload-archive].include?(action)
|
2018-01-22 11:51:47 -05:00
|
|
|
|
2017-06-26 09:55:06 -04:00
|
|
|
{
|
|
|
|
repository: repository.gitaly_repository,
|
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
}
|
|
|
|
end
|
2016-11-15 10:02:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|