2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
2019-02-15 15:39:39 +05:30
|
|
|
module Config
|
|
|
|
module Loader
|
|
|
|
class Yaml
|
2019-07-07 11:18:12 +05:30
|
|
|
DataTooLargeError = Class.new(Loader::FormatError)
|
2020-07-28 23:09:34 +05:30
|
|
|
NotHashError = Class.new(Loader::FormatError)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(config, additional_permitted_classes: [])
|
|
|
|
@config = YAML.safe_load(config,
|
|
|
|
permitted_classes: [Symbol, *additional_permitted_classes],
|
|
|
|
permitted_symbols: [],
|
|
|
|
aliases: true
|
|
|
|
)
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue Psych::Exception => e
|
2019-02-15 15:39:39 +05:30
|
|
|
raise Loader::FormatError, e.message
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2019-07-07 11:18:12 +05:30
|
|
|
hash? && !too_big?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def load_raw!
|
2019-07-07 11:18:12 +05:30
|
|
|
raise DataTooLargeError, 'The parsed YAML is too big' if too_big?
|
2020-07-28 23:09:34 +05:30
|
|
|
raise NotHashError, 'Invalid configuration format' unless hash?
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
@config
|
|
|
|
end
|
|
|
|
|
|
|
|
def load!
|
|
|
|
@symbolized_config ||= load_raw!.deep_symbolize_keys
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def hash?
|
|
|
|
@config.is_a?(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def too_big?
|
|
|
|
!deep_size.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deep_size
|
|
|
|
strong_memoize(:deep_size) do
|
|
|
|
Gitlab::Utils::DeepSize.new(@config,
|
2021-11-11 11:23:49 +05:30
|
|
|
max_size: Gitlab::CurrentSettings.current_application_settings.max_yaml_size_bytes,
|
|
|
|
max_depth: Gitlab::CurrentSettings.current_application_settings.max_yaml_depth)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|