debian-mirror-gitlab/lib/gitlab/shell.rb

410 lines
13 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
# Gitaly note: SSH key operations are not part of Gitaly so will never be migrated.
2017-09-10 17:25:29 +05:30
2016-08-24 12:49:21 +05:30
require 'securerandom'
2014-09-02 18:07:02 +05:30
module Gitlab
class Shell
2017-09-10 17:25:29 +05:30
GITLAB_SHELL_ENV_VARS = %w(GIT_TERMINAL_PROMPT).freeze
2017-08-17 22:00:37 +05:30
Error = Class.new(StandardError)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
class << self
2016-11-03 12:29:30 +05:30
def secret_token
@secret_token ||= begin
File.read(Gitlab.config.gitlab_shell.secret_file).chomp
end
end
def ensure_secret_token!
return if File.exist?(File.join(Gitlab.config.gitlab_shell.path, '.gitlab_shell_secret'))
generate_and_link_secret_token
end
2015-04-26 12:48:37 +05:30
def version_required
2017-09-10 17:25:29 +05:30
@version_required ||= File.read(Rails.root
.join('GITLAB_SHELL_VERSION')).strip
2015-04-26 12:48:37 +05:30
end
2016-09-29 09:46:39 +05:30
2016-11-03 12:29:30 +05:30
private
# Create (if necessary) and link the secret token file
def generate_and_link_secret_token
secret_file = Gitlab.config.gitlab_shell.secret_file
shell_path = Gitlab.config.gitlab_shell.path
unless File.size?(secret_file)
# Generate a new token of 16 random hexadecimal characters and store it in secret_file.
@secret_token = SecureRandom.hex(16)
File.write(secret_file, @secret_token)
end
link_path = File.join(shell_path, '.gitlab_shell_secret')
if File.exist?(shell_path) && !File.exist?(link_path)
FileUtils.symlink(secret_file, link_path)
end
end
2015-04-26 12:48:37 +05:30
end
2019-03-02 22:35:43 +05:30
# Convenience methods for initializing a new repository with a Project model.
def create_project_repository(project)
create_repository(project.repository_storage, project.disk_path, project.full_path)
end
def create_wiki_repository(project)
create_repository(project.repository_storage, project.wiki.disk_path, project.wiki.full_path)
end
2014-09-02 18:07:02 +05:30
# Init new repository
#
2018-10-15 14:42:47 +05:30
# storage - the shard key
2019-03-02 22:35:43 +05:30
# disk_path - project disk path
# gl_project_path - project name
2014-09-02 18:07:02 +05:30
#
# Ex.
2019-03-02 22:35:43 +05:30
# create_repository("default", "path/to/gitlab-ci", "gitlab/gitlab-ci")
2014-09-02 18:07:02 +05:30
#
2019-03-02 22:35:43 +05:30
def create_repository(storage, disk_path, gl_project_path)
relative_path = disk_path.dup
2018-03-17 18:26:18 +05:30
relative_path << '.git' unless relative_path.end_with?('.git')
2019-03-02 22:35:43 +05:30
# During creation of a repository, gl_repository may not be known
# because that depends on a yet-to-be assigned project ID in the
# database (e.g. project-1234), so for now it is blank.
repository = Gitlab::Git::Repository.new(storage, relative_path, '', gl_project_path)
2018-11-18 11:00:15 +05:30
wrapped_gitaly_errors { repository.gitaly_repository_client.create_repository }
true
2018-05-09 12:01:36 +05:30
rescue => err # Once the Rugged codes gets removes this can be improved
2019-09-30 21:07:59 +05:30
Rails.logger.error("Failed to add repository #{storage}/#{disk_path}: #{err}") # rubocop:disable Gitlab/RailsLogger
2018-03-17 18:26:18 +05:30
false
2014-09-02 18:07:02 +05:30
end
2019-03-02 22:35:43 +05:30
def import_wiki_repository(project, wiki_formatter)
import_repository(project.repository_storage, wiki_formatter.disk_path, wiki_formatter.import_url, project.wiki.full_path)
end
def import_project_repository(project)
import_repository(project.repository_storage, project.disk_path, project.import_url, project.full_path)
end
2014-09-02 18:07:02 +05:30
# Import repository
#
2018-05-09 12:01:36 +05:30
# storage - project's storage name
2018-03-17 18:26:18 +05:30
# name - project disk path
# url - URL to import from
2014-09-02 18:07:02 +05:30
#
# Ex.
2018-05-09 12:01:36 +05:30
# import_repository("nfs-file06", "gitlab/gitlab-ci", "https://gitlab.com/gitlab-org/gitlab-test.git")
2014-09-02 18:07:02 +05:30
#
2019-03-02 22:35:43 +05:30
def import_repository(storage, name, url, gl_project_path)
2018-03-17 18:26:18 +05:30
if url.start_with?('.', '/')
raise Error.new("don't use disk paths with import_repository: #{url.inspect}")
end
2018-11-08 19:23:39 +05:30
relative_path = "#{name}.git"
2019-03-02 22:35:43 +05:30
cmd = GitalyGitlabProjects.new(storage, relative_path, gl_project_path)
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
success = cmd.import_project(url, git_timeout)
2018-03-17 18:26:18 +05:30
raise Error, cmd.output unless success
success
2017-08-17 22:00:37 +05:30
end
2016-08-24 12:49:21 +05:30
# storage - project's storage path
2018-03-17 18:26:18 +05:30
# path - project disk path
# new_path - new project disk path
2014-09-02 18:07:02 +05:30
#
# Ex.
2016-08-24 12:49:21 +05:30
# mv_repository("/path/to/storage", "gitlab/gitlab-ci", "randx/gitlab-ci-new")
def mv_repository(storage, path, new_path)
2018-03-17 18:26:18 +05:30
return false if path.empty? || new_path.empty?
2019-12-21 20:55:43 +05:30
Gitlab::Git::Repository.new(storage, "#{path}.git", nil, nil).rename("#{new_path}.git")
true
rescue => e
Gitlab::Sentry.track_acceptable_exception(e, extra: { path: path, new_path: new_path, storage: storage })
false
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
# Fork repository to new path
2019-03-02 22:35:43 +05:30
# source_project - forked-from Project
# target_project - forked-to Project
def fork_repository(source_project, target_project)
forked_from_relative_path = "#{source_project.disk_path}.git"
fork_args = [target_project.repository_storage, "#{target_project.disk_path}.git", target_project.full_path]
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
GitalyGitlabProjects.new(source_project.repository_storage, forked_from_relative_path, source_project.full_path).fork_repository(*fork_args)
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
# Removes a repository from file system, using rm_diretory which is an alias
# for rm_namespace. Given the underlying implementation removes the name
# passed as second argument on the passed storage.
2014-09-02 18:07:02 +05:30
#
2016-08-24 12:49:21 +05:30
# storage - project's storage path
2018-03-17 18:26:18 +05:30
# name - project disk path
2014-09-02 18:07:02 +05:30
#
# Ex.
2016-08-24 12:49:21 +05:30
# remove_repository("/path/to/storage", "gitlab/gitlab-ci")
def remove_repository(storage, name)
2018-03-17 18:26:18 +05:30
return false if name.empty?
2019-12-21 20:55:43 +05:30
Gitlab::Git::Repository.new(storage, "#{name}.git", nil, nil).remove
true
rescue => e
2019-09-30 21:07:59 +05:30
Rails.logger.warn("Repository does not exist: #{e} at: #{name}.git") # rubocop:disable Gitlab/RailsLogger
2019-12-21 20:55:43 +05:30
Gitlab::Sentry.track_acceptable_exception(e, extra: { path: name, storage: storage })
2018-03-17 18:26:18 +05:30
false
2014-09-02 18:07:02 +05:30
end
2019-07-07 11:18:12 +05:30
# Add new key to authorized_keys
2014-09-02 18:07:02 +05:30
#
# Ex.
# add_key("key-42", "sha-rsa ...")
#
def add_key(key_id, key_content)
2018-03-17 18:26:18 +05:30
return unless self.authorized_keys_enabled?
2019-12-04 20:38:33 +05:30
gitlab_authorized_keys.add_key(key_id, key_content)
2014-09-02 18:07:02 +05:30
end
# Batch-add keys to authorized_keys
#
# Ex.
2019-07-07 11:18:12 +05:30
# batch_add_keys(Key.all)
def batch_add_keys(keys)
2018-03-17 18:26:18 +05:30
return unless self.authorized_keys_enabled?
2019-12-04 20:38:33 +05:30
gitlab_authorized_keys.batch_add_keys(keys)
2014-09-02 18:07:02 +05:30
end
2019-07-07 11:18:12 +05:30
# Remove ssh key from authorized_keys
2014-09-02 18:07:02 +05:30
#
# Ex.
2019-07-07 11:18:12 +05:30
# remove_key("key-342")
2014-09-02 18:07:02 +05:30
#
2019-07-07 11:18:12 +05:30
def remove_key(id, _ = nil)
2018-03-17 18:26:18 +05:30
return unless self.authorized_keys_enabled?
2019-12-04 20:38:33 +05:30
gitlab_authorized_keys.rm_key(id)
2014-09-02 18:07:02 +05:30
end
# Remove all ssh keys from gitlab shell
#
# Ex.
# remove_all_keys
#
def remove_all_keys
2018-03-17 18:26:18 +05:30
return unless self.authorized_keys_enabled?
2019-12-04 20:38:33 +05:30
gitlab_authorized_keys.clear
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
# Remove ssh keys from gitlab shell that are not in the DB
#
# Ex.
# remove_keys_not_found_in_db
#
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def remove_keys_not_found_in_db
return unless self.authorized_keys_enabled?
2019-09-30 21:07:59 +05:30
Rails.logger.info("Removing keys not found in DB") # rubocop:disable Gitlab/RailsLogger
2018-03-17 18:26:18 +05:30
batch_read_key_ids do |ids_in_file|
ids_in_file.uniq!
keys_in_db = Key.where(id: ids_in_file)
next unless ids_in_file.size > keys_in_db.count # optimization
ids_to_remove = ids_in_file - keys_in_db.pluck(:id)
ids_to_remove.each do |id|
2019-09-30 21:07:59 +05:30
Rails.logger.info("Removing key-#{id} not found in DB") # rubocop:disable Gitlab/RailsLogger
2018-03-17 18:26:18 +05:30
remove_key("key-#{id}")
end
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
2014-09-02 18:07:02 +05:30
# Add empty directory for storing repositories
#
# Ex.
2018-10-15 14:42:47 +05:30
# add_namespace("default", "gitlab")
2014-09-02 18:07:02 +05:30
#
2016-08-24 12:49:21 +05:30
def add_namespace(storage, name)
2019-12-04 20:38:33 +05:30
# https://gitlab.com/gitlab-org/gitlab-foss/issues/58012
2019-07-07 11:18:12 +05:30
Gitlab::GitalyClient.allow_n_plus_1_calls do
Gitlab::GitalyClient::NamespaceService.new(storage).add(name)
end
2018-03-17 18:26:18 +05:30
rescue GRPC::InvalidArgument => e
raise ArgumentError, e.message
2014-09-02 18:07:02 +05:30
end
# Remove directory from repositories storage
# Every repository inside this directory will be removed too
#
# Ex.
2018-10-15 14:42:47 +05:30
# rm_namespace("default", "gitlab")
2014-09-02 18:07:02 +05:30
#
2016-08-24 12:49:21 +05:30
def rm_namespace(storage, name)
2018-10-15 14:42:47 +05:30
Gitlab::GitalyClient::NamespaceService.new(storage).remove(name)
2018-03-17 18:26:18 +05:30
rescue GRPC::InvalidArgument => e
raise ArgumentError, e.message
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
alias_method :rm_directory, :rm_namespace
2014-09-02 18:07:02 +05:30
# Move namespace directory inside repositories storage
#
# Ex.
2016-08-24 12:49:21 +05:30
# mv_namespace("/path/to/storage", "gitlab", "gitlabhq")
2014-09-02 18:07:02 +05:30
#
2016-08-24 12:49:21 +05:30
def mv_namespace(storage, old_name, new_name)
2018-10-15 14:42:47 +05:30
Gitlab::GitalyClient::NamespaceService.new(storage).rename(old_name, new_name)
2019-02-15 15:39:39 +05:30
rescue GRPC::InvalidArgument => e
Gitlab::Sentry.track_acceptable_exception(e, extra: { old_name: old_name, new_name: new_name, storage: storage })
2018-03-17 18:26:18 +05:30
false
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
def url_to_repo(path)
2014-09-02 18:07:02 +05:30
Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git"
end
# Return GitLab shell version
def version
gitlab_shell_version_file = "#{gitlab_shell_path}/VERSION"
if File.readable?(gitlab_shell_version_file)
2015-04-26 12:48:37 +05:30
File.read(gitlab_shell_version_file).chomp
2014-09-02 18:07:02 +05:30
end
end
2015-09-11 14:41:01 +05:30
# Check if such directory exists in repositories.
#
# Usage:
2016-08-24 12:49:21 +05:30
# exists?(storage, 'gitlab')
# exists?(storage, 'gitlab/cookies.git')
2015-09-11 14:41:01 +05:30
#
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-08-24 12:49:21 +05:30
def exists?(storage, dir_name)
2018-10-15 14:42:47 +05:30
Gitlab::GitalyClient::NamespaceService.new(storage).exists?(dir_name)
2016-08-24 12:49:21 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-08-24 12:49:21 +05:30
2019-12-21 20:55:43 +05:30
def repository_exists?(storage, dir_name)
Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists?
rescue GRPC::Internal
false
end
2019-07-07 11:18:12 +05:30
def hooks_path
File.join(gitlab_shell_path, 'hooks')
end
2014-09-02 18:07:02 +05:30
protected
def gitlab_shell_path
2018-03-17 18:26:18 +05:30
File.expand_path(Gitlab.config.gitlab_shell.path)
end
2014-09-02 18:07:02 +05:30
def gitlab_shell_user_home
File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
end
2016-08-24 12:49:21 +05:30
def full_path(storage, dir_name)
2014-09-02 18:07:02 +05:30
raise ArgumentError.new("Directory name can't be blank") if dir_name.blank?
2018-10-15 14:42:47 +05:30
File.join(Gitlab.config.repositories.storages[storage].legacy_disk_path, dir_name)
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
def authorized_keys_enabled?
# Return true if nil to ensure the authorized_keys methods work while
# fixing the authorized_keys file during migration.
return true if Gitlab::CurrentSettings.current_application_settings.authorized_keys_enabled.nil?
Gitlab::CurrentSettings.current_application_settings.authorized_keys_enabled
end
2017-09-10 17:25:29 +05:30
private
2018-03-17 18:26:18 +05:30
def git_timeout
Gitlab.config.gitlab_shell.git_timeout
end
2018-11-18 11:00:15 +05:30
def wrapped_gitaly_errors
yield
2018-03-17 18:26:18 +05:30
rescue GRPC::NotFound, GRPC::BadStatus => e
# Old Popen code returns [Error, output] to the caller, so we
# need to do the same here...
raise Error, e
end
2018-11-08 19:23:39 +05:30
2019-07-07 11:18:12 +05:30
def gitlab_authorized_keys
@gitlab_authorized_keys ||= Gitlab::AuthorizedKeys.new
end
def batch_read_key_ids(batch_size: 100, &block)
return unless self.authorized_keys_enabled?
2019-12-04 20:38:33 +05:30
gitlab_authorized_keys.list_key_ids.lazy.each_slice(batch_size) do |key_ids|
yield(key_ids)
2019-07-07 11:18:12 +05:30
end
end
def strip_key(key)
key.split(/[ ]+/)[0, 2].join(' ')
end
def add_keys_to_io(keys, io)
keys.each do |k|
key = strip_key(k.key)
raise Error.new("Invalid key: #{key.inspect}") if key.include?("\t") || key.include?("\n")
io.puts("#{k.shell_id}\t#{key}")
end
end
2018-11-08 19:23:39 +05:30
class GitalyGitlabProjects
2019-03-02 22:35:43 +05:30
attr_reader :shard_name, :repository_relative_path, :output, :gl_project_path
2018-11-08 19:23:39 +05:30
2019-03-02 22:35:43 +05:30
def initialize(shard_name, repository_relative_path, gl_project_path)
2018-11-08 19:23:39 +05:30
@shard_name = shard_name
@repository_relative_path = repository_relative_path
@output = ''
2019-03-02 22:35:43 +05:30
@gl_project_path = gl_project_path
2018-11-08 19:23:39 +05:30
end
def import_project(source, _timeout)
2019-03-02 22:35:43 +05:30
raw_repository = Gitlab::Git::Repository.new(shard_name, repository_relative_path, nil, gl_project_path)
2018-11-08 19:23:39 +05:30
Gitlab::GitalyClient::RepositoryService.new(raw_repository).import_repository(source)
true
rescue GRPC::BadStatus => e
@output = e.message
false
end
2019-03-02 22:35:43 +05:30
def fork_repository(new_shard_name, new_repository_relative_path, new_project_name)
target_repository = Gitlab::Git::Repository.new(new_shard_name, new_repository_relative_path, nil, new_project_name)
raw_repository = Gitlab::Git::Repository.new(shard_name, repository_relative_path, nil, gl_project_path)
2018-11-08 19:23:39 +05:30
Gitlab::GitalyClient::RepositoryService.new(target_repository).fork_repository(raw_repository)
rescue GRPC::BadStatus => e
logger.error "fork-repository failed: #{e.message}"
false
end
def logger
2019-09-30 21:07:59 +05:30
Rails.logger # rubocop:disable Gitlab/RailsLogger
2018-11-08 19:23:39 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
end