Class: Gitlab::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/shell.rb

Overview

This class is an artifact of a time when common repository operations were performed by calling out to scripts in the gitlab-shell project. Now, these operations are all performed by Gitaly, and are mostly accessible through the Repository class. Prefer using a Repository to functionality here.

Legacy code relating to namespaces still relies on Gitlab::Shell; it can be converted to a module once gitlab.com/groups/gitlab-org/-/epics/2320 is completed. gitlab.com/gitlab-org/gitlab/-/issues/25095 tracks it.

Constant Summary collapse

Error =
Class.new(StandardError)
API_HEADER =
'Gitlab-Shell-Api-Request'
JWT_ISSUER =
'gitlab-shell'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_secret_token!Object

Ensure gitlab shell has a secret token stored in the secret_file if that was never generated, generate a new one



44
45
46
47
48
# File 'lib/gitlab/shell.rb', line 44

def ensure_secret_token!
  return if File.exist?(File.join(Gitlab.config.gitlab_shell.path, '.gitlab_shell_secret'))

  generate_and_link_secret_token
end

.header_set?(headers) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/gitlab/shell.rb', line 31

def header_set?(headers)
  headers[API_HEADER].present?
end

.secret_tokenString

Retrieve GitLab Shell secret token

Returns:

  • (String)

    secret token



38
39
40
# File 'lib/gitlab/shell.rb', line 38

def secret_token
  @secret_token ||= File.read(Gitlab.config.gitlab_shell.secret_file).chomp
end

.verify_api_request(headers) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/shell.rb', line 21

def verify_api_request(headers)
  payload, header = JSONWebToken::HMACToken.decode(headers[API_HEADER], secret_token)
  return unless payload['iss'] == JWT_ISSUER

  [payload, header]
rescue JWT::DecodeError, JWT::ExpiredSignature, JWT::ImmatureSignature => ex
  Gitlab::ErrorTracking.track_exception(ex)
  nil
end

.versionString

Return GitLab shell version

Returns:

  • (String)

    version



61
62
63
# File 'lib/gitlab/shell.rb', line 61

def version
  @version ||= File.read(gitlab_shell_version_file).chomp if File.readable?(gitlab_shell_version_file)
end

.version_requiredString

Returns required GitLab shell version

Returns:

  • (String)

    version from the manifest file



53
54
55
56
# File 'lib/gitlab/shell.rb', line 53

def version_required
  @version_required ||= File.read(Rails.root
                                  .join('GITLAB_SHELL_VERSION')).strip
end

Instance Method Details

#repository_exists?(storage, dir_name) ⇒ Boolean

Deprecated.

Check if repository exists on disk

Examples:

Check if repository exists

repository_exists?('default', 'gitlab-org/gitlab.git')

Parameters:

  • storage (String)

    project’s storage path

  • dir_name (Object)

    repository dir name

Returns:

  • (Boolean)

    whether repository exists or not



105
106
107
108
109
# File 'lib/gitlab/shell.rb', line 105

def repository_exists?(storage, dir_name)
  Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists?
rescue GRPC::Internal
  false
end