Class: Gitlab::GitalyClient::StorageSettings

Inherits:
Object
  • Object
show all
Extended by:
TemporarilyAllow
Defined in:
lib/gitlab/gitaly_client/storage_settings.rb

Overview

This is a chokepoint that is meant to help us stop remove all places where production code (app, config, db, lib) touches Git repositories directly.

Constant Summary collapse

DirectPathAccessError =
Class.new(StandardError)
InvalidConfigurationError =
Class.new(StandardError)
INVALID_STORAGE_MESSAGE =
<<~MSG
  Storage is invalid because it has no `path` key.

  For source installations, update your config/gitlab.yml Refer to gitlab.yml.example for an updated example.
  If you're using the GitLab Development Kit, you can update your configuration running `gdk reconfigure`.
MSG
Deprecated =

This class will give easily recognizable NoMethodErrors

Class.new
MUTEX =
Mutex.new
ALLOW_KEY =
:allow_disk_access

Constants included from TemporarilyAllow

TemporarilyAllow::TEMPORARILY_ALLOW_MUTEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TemporarilyAllow

temporarily_allow, temporarily_allowed?

Constructor Details

#initialize(storage) ⇒ StorageSettings

Returns a new instance of StorageSettings.



47
48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 47

def initialize(storage)
  raise InvalidConfigurationError, "expected a Hash, got a #{storage.class.name}" unless storage.is_a?(Hash)
  raise InvalidConfigurationError, INVALID_STORAGE_MESSAGE unless storage.has_key?('path')

  # Support a nil 'path' field because some of the circuit breaker tests use it.
  @legacy_disk_path = File.expand_path(storage['path'], Rails.root) if storage['path']

  storage['path'] = Deprecated
  @hash = ActiveSupport::HashWithIndifferentAccess.new(storage)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args, &block) ⇒ Object (private)



73
74
75
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 73

def method_missing(msg, *args, &block)
  @hash.public_send(msg, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
end

Class Method Details

.allow_disk_accessObject

If your code needs this method then your code needs to be fixed.



29
30
31
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 29

def self.allow_disk_access
  temporarily_allow(ALLOW_KEY) { yield }
end

.disk_access_denied?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 33

def self.disk_access_denied?
  return false if rugged_enabled?

  !temporarily_allowed?(ALLOW_KEY)
rescue StandardError
  false # Err on the side of caution, don't break gitlab for people
end

.rugged_enabled?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 41

def self.rugged_enabled?
  Gitlab::Git::RuggedImpl::Repository::FEATURE_FLAGS.any? do |flag|
    Feature.enabled?(flag)
  end
end

Instance Method Details

#gitaly_addressObject



58
59
60
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 58

def gitaly_address
  @hash.fetch(:gitaly_address)
end

#legacy_disk_pathObject



62
63
64
65
66
67
68
69
# File 'lib/gitlab/gitaly_client/storage_settings.rb', line 62

def legacy_disk_path
  # Do not use self.class due to Spring reloading issues
  if Gitlab::GitalyClient::StorageSettings.disk_access_denied?
    raise DirectPathAccessError, "git disk access denied"
  end

  @legacy_disk_path
end