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
|
|
|
|
|
2016-11-15 10:02:44 -05:00
|
|
|
def wiki?
|
2017-11-22 02:50:36 -05:00
|
|
|
set_project unless defined?(@wiki) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@wiki # 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
|
|
|
|
2017-11-14 10:14:35 -05:00
|
|
|
def fix_git_env_repository_paths(env, repository_path)
|
|
|
|
if obj_dir_relative = env['GIT_OBJECT_DIRECTORY_RELATIVE'].presence
|
|
|
|
env['GIT_OBJECT_DIRECTORY'] = File.join(repository_path, obj_dir_relative)
|
|
|
|
end
|
|
|
|
|
|
|
|
if alt_obj_dirs_relative = env['GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE'].presence
|
|
|
|
env['GIT_ALTERNATE_OBJECT_DIRECTORIES'] = alt_obj_dirs_relative.map { |dir| File.join(repository_path, dir) }
|
|
|
|
end
|
|
|
|
|
|
|
|
env
|
|
|
|
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
|
|
|
|
|
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]
|
|
|
|
@project, @wiki = 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
|
2017-06-15 20:03:54 -04:00
|
|
|
@project, @wiki, @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
|
|
|
|
Gitlab::GlRepository.gl_repository(project, wiki?)
|
|
|
|
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
|
2017-05-23 14:34:58 -04:00
|
|
|
if 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 repository full path so that gitlab-shell has it when
|
|
|
|
# handling ssh commands
|
|
|
|
def repository_path
|
|
|
|
repository.path_to_repo
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the Gitaly Address if it is enabled
|
|
|
|
def gitaly_payload(action)
|
2018-01-22 11:51:47 -05:00
|
|
|
return unless %w[git-receive-pack git-upload-pack].include?(action)
|
|
|
|
|
|
|
|
if action == 'git-receive-pack'
|
|
|
|
return unless Gitlab::GitalyClient.feature_enabled?(
|
|
|
|
:ssh_receive_pack,
|
|
|
|
status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT
|
|
|
|
)
|
|
|
|
end
|
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
|