2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-14 09:42:49 -04:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
|
|
|
# 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.
|
|
|
|
class StorageSettings
|
2018-06-01 07:56:29 -04:00
|
|
|
extend Gitlab::TemporarilyAllow
|
|
|
|
|
2018-03-14 09:42:49 -04:00
|
|
|
DirectPathAccessError = Class.new(StandardError)
|
2018-05-08 07:13:56 -04:00
|
|
|
InvalidConfigurationError = Class.new(StandardError)
|
|
|
|
|
2021-03-25 23:09:21 -04:00
|
|
|
INVALID_STORAGE_MESSAGE = <<~MSG
|
2018-05-08 07:13:56 -04:00
|
|
|
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.
|
2018-09-21 08:05:37 -04:00
|
|
|
If you're using the GitLab Development Kit, you can update your configuration running `gdk reconfigure`.
|
2018-05-08 07:13:56 -04:00
|
|
|
MSG
|
2018-03-14 09:42:49 -04:00
|
|
|
|
|
|
|
# This class will give easily recognizable NoMethodErrors
|
|
|
|
Deprecated = Class.new
|
|
|
|
|
2018-06-01 07:56:29 -04:00
|
|
|
MUTEX = Mutex.new
|
|
|
|
|
|
|
|
ALLOW_KEY = :allow_disk_access
|
|
|
|
|
|
|
|
# If your code needs this method then your code needs to be fixed.
|
|
|
|
def self.allow_disk_access
|
|
|
|
temporarily_allow(ALLOW_KEY) { yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.disk_access_denied?
|
2019-02-21 16:07:26 -05:00
|
|
|
return false if rugged_enabled?
|
|
|
|
|
2021-02-16 10:09:50 -05:00
|
|
|
!temporarily_allowed?(ALLOW_KEY)
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2018-06-01 07:56:29 -04:00
|
|
|
false # Err on the side of caution, don't break gitlab for people
|
|
|
|
end
|
2018-03-14 09:42:49 -04:00
|
|
|
|
2019-02-21 16:07:26 -05:00
|
|
|
def self.rugged_enabled?
|
|
|
|
Gitlab::Git::RuggedImpl::Repository::FEATURE_FLAGS.any? do |flag|
|
|
|
|
Feature.enabled?(flag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-14 09:42:49 -04:00
|
|
|
def initialize(storage)
|
2018-05-08 07:13:56 -04:00
|
|
|
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')
|
2018-03-14 09:42:49 -04:00
|
|
|
|
|
|
|
# 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
|
2021-06-17 05:09:53 -04:00
|
|
|
@hash = ActiveSupport::HashWithIndifferentAccess.new(storage)
|
2018-03-14 09:42:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_address
|
|
|
|
@hash.fetch(:gitaly_address)
|
|
|
|
end
|
|
|
|
|
2018-06-01 07:56:29 -04:00
|
|
|
def legacy_disk_path
|
2021-02-17 22:09:22 -05:00
|
|
|
# Do not use self.class due to Spring reloading issues
|
|
|
|
if Gitlab::GitalyClient::StorageSettings.disk_access_denied?
|
2021-02-16 10:09:50 -05:00
|
|
|
raise DirectPathAccessError, "git disk access denied"
|
2018-06-01 07:56:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@legacy_disk_path
|
|
|
|
end
|
|
|
|
|
2018-03-14 09:42:49 -04:00
|
|
|
private
|
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
def method_missing(msg, *args, &block)
|
|
|
|
@hash.public_send(msg, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
2018-03-14 09:42:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|