Commit 09ebe6f4 authored by rpereira2's avatar rpereira2

Add new method Gitlab::Config::Loader::Yaml#load_raw!

- Create a separate method which returns stringified keys.

- Memoizes the return value of the load! method.
parent dfdd1f42
...@@ -21,11 +21,15 @@ module Gitlab ...@@ -21,11 +21,15 @@ module Gitlab
hash? && !too_big? hash? && !too_big?
end end
def load! def load_raw!
raise DataTooLargeError, 'The parsed YAML is too big' if too_big? raise DataTooLargeError, 'The parsed YAML is too big' if too_big?
raise Loader::FormatError, 'Invalid configuration format' unless hash? raise Loader::FormatError, 'Invalid configuration format' unless hash?
@config.deep_symbolize_keys @config
end
def load!
@symbolized_config ||= load_raw!.deep_symbolize_keys
end end
private private
......
...@@ -5,6 +5,16 @@ require 'spec_helper' ...@@ -5,6 +5,16 @@ require 'spec_helper'
describe Gitlab::Config::Loader::Yaml do describe Gitlab::Config::Loader::Yaml do
let(:loader) { described_class.new(yml) } let(:loader) { described_class.new(yml) }
let(:yml) do
<<~YAML
image: 'ruby:2.7'
texts:
nested_key: 'value1'
more_text:
more_nested_key: 'value2'
YAML
end
context 'when yaml syntax is correct' do context 'when yaml syntax is correct' do
let(:yml) { 'image: ruby:2.7' } let(:yml) { 'image: ruby:2.7' }
...@@ -61,6 +71,15 @@ describe Gitlab::Config::Loader::Yaml do ...@@ -61,6 +71,15 @@ describe Gitlab::Config::Loader::Yaml do
expect(loader).not_to be_valid expect(loader).not_to be_valid
end end
end end
describe '#load_raw!' do
it 'raises error' do
expect { loader.load_raw! }.to raise_error(
Gitlab::Config::Loader::FormatError,
'Invalid configuration format'
)
end
end
end end
# Prevent Billion Laughs attack: https://gitlab.com/gitlab-org/gitlab-foss/issues/56018 # Prevent Billion Laughs attack: https://gitlab.com/gitlab-org/gitlab-foss/issues/56018
...@@ -123,4 +142,32 @@ describe Gitlab::Config::Loader::Yaml do ...@@ -123,4 +142,32 @@ describe Gitlab::Config::Loader::Yaml do
end end
end end
end end
describe '#load_raw!' do
it 'loads keys as strings' do
expect(loader.load_raw!).to eq(
'image' => 'ruby:2.7',
'texts' => {
'nested_key' => 'value1',
'more_text' => {
'more_nested_key' => 'value2'
}
}
)
end
end
describe '#load!' do
it 'symbolizes keys' do
expect(loader.load!).to eq(
image: 'ruby:2.7',
texts: {
nested_key: 'value1',
more_text: {
more_nested_key: 'value2'
}
}
)
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment