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
|
2020-04-15 14:09:36 -04:00
|
|
|
attr_reader :redirected_path
|
2017-09-18 13:25:23 -04:00
|
|
|
|
2019-03-18 12:51:11 -04:00
|
|
|
delegate :wiki?, to: :repo_type
|
|
|
|
|
2019-12-03 19:06:15 -05:00
|
|
|
def actor
|
|
|
|
@actor ||= Support::GitAccessActor.from_params(params)
|
|
|
|
end
|
|
|
|
|
2020-04-15 14:09:36 -04:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2019-03-18 12:51:11 -04:00
|
|
|
def repo_type
|
2020-04-15 14:09:36 -04:00
|
|
|
parse_repo_path unless defined?(@repo_type)
|
|
|
|
@repo_type
|
2016-11-15 10:02:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
2020-04-15 14:09:36 -04:00
|
|
|
parse_repo_path unless defined?(@project)
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
|
|
|
def container
|
|
|
|
parse_repo_path unless defined?(@container)
|
|
|
|
@container
|
2016-11-15 10:02:44 -05:00
|
|
|
end
|
2020-04-15 14:09:36 -04:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-06-26 09:55:06 -04:00
|
|
|
|
2019-08-07 03:51:12 -04:00
|
|
|
def access_checker_for(actor, protocol)
|
2020-03-04 19:07:49 -05:00
|
|
|
access_checker_klass.new(actor.key_or_user, container, protocol,
|
2019-08-07 03:51:12 -04:00
|
|
|
authentication_abilities: ssh_authentication_abilities,
|
2020-12-01 07:09:17 -05:00
|
|
|
repository_path: repository_path,
|
2019-08-07 03:51:12 -04:00
|
|
|
redirected_path: redirected_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_checker_klass
|
|
|
|
repo_type.access_checker_class
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
Gitlab::Json.parse(params[:env])
|
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
|
|
|
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
|
|
|
|
2020-01-06 04:07:42 -05:00
|
|
|
::Users::ActivityService.new(actor).execute if commands.include?(params[:action])
|
2016-11-25 11:10:25 -05:00
|
|
|
end
|
2017-04-28 12:52:09 -04:00
|
|
|
|
2017-08-31 17:00:40 -04:00
|
|
|
def redis_ping
|
|
|
|
result = Gitlab::Redis::SharedState.with { |redis| redis.ping }
|
|
|
|
|
|
|
|
result == 'PONG'
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2020-09-09 11:08:47 -04:00
|
|
|
Gitlab::AppLogger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
|
2017-08-31 17:00:40 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-04-28 12:52:09 -04:00
|
|
|
private
|
|
|
|
|
2020-12-01 07:09:17 -05:00
|
|
|
def repository_path
|
|
|
|
if container
|
|
|
|
"#{container.full_path}.git"
|
|
|
|
elsif params[:project]
|
|
|
|
# When the project doesn't exist, we still need to pass on the path
|
|
|
|
# to support auto-creation in `GitAccessProject`.
|
|
|
|
#
|
|
|
|
# For consistency with the Git HTTP controllers, we normalize the path
|
|
|
|
# to remove a leading slash and ensure a trailing `.git`.
|
|
|
|
#
|
|
|
|
# NOTE: For GitLab Shell, `params[:project]` is the full repository path
|
|
|
|
# from the SSH command, with an optional trailing `.git`.
|
|
|
|
"#{params[:project].delete_prefix('/').delete_suffix('.git')}.git"
|
|
|
|
end
|
2018-01-19 08:04:14 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2020-04-15 14:09:36 -04:00
|
|
|
def parse_repo_path
|
2020-03-04 19:07:49 -05:00
|
|
|
@container, @project, @repo_type, @redirected_path =
|
2020-01-24 13:09:00 -05:00
|
|
|
if params[:gl_repository]
|
|
|
|
Gitlab::GlRepository.parse(params[:gl_repository])
|
|
|
|
elsif params[:project]
|
|
|
|
Gitlab::RepoPath.parse(params[:project])
|
|
|
|
end
|
2017-04-28 12:52:09 -04:00
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-05-23 14:34:58 -04:00
|
|
|
|
2020-12-01 07:09:17 -05:00
|
|
|
# Repository id to pass between components that don't share/don't have
|
2017-05-23 14:34:58 -04:00
|
|
|
# access to the same filesystem mounts
|
|
|
|
def gl_repository
|
2020-03-04 19:07:49 -05:00
|
|
|
repo_type.identifier_for_container(container)
|
2017-05-23 14:34:58 -04:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
def gl_repository_path
|
2019-12-27 10:08:16 -05:00
|
|
|
repository.full_path
|
2019-02-16 09:54:32 -05:00
|
|
|
end
|
|
|
|
|
2020-12-01 07:09:17 -05:00
|
|
|
# Return the repository for the detected type and container
|
|
|
|
#
|
|
|
|
# @returns [Repository]
|
2017-06-26 09:55:06 -04:00
|
|
|
def repository
|
2020-03-04 19:07:49 -05:00
|
|
|
@repository ||= repo_type.repository_for(container)
|
2017-05-23 14:34:58 -04:00
|
|
|
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
|
|
|
{
|
2020-08-05 14:09:49 -04:00
|
|
|
repository: repository.gitaly_repository.to_h,
|
2020-06-25 08:09:00 -04:00
|
|
|
address: Gitlab::GitalyClient.address(repository.shard),
|
|
|
|
token: Gitlab::GitalyClient.token(repository.shard),
|
2022-01-21 10:13:54 -05:00
|
|
|
features: Feature::Gitaly.server_feature_flags(repository.project),
|
|
|
|
use_sidechannel: Feature.enabled?(:gitlab_shell_upload_pack_sidechannel, repository.project, default_enabled: :yaml)
|
2017-06-26 09:55:06 -04:00
|
|
|
}
|
|
|
|
end
|
2016-11-15 10:02:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|