policy: Quote single quotes in yaml format

In YAML, when a string is surrounded by single quotes('), any single
quote in that string should be prefixed by another single quote('), as
is described in the example below

GOOD:
  'foo': 'this is a ''good'' example'

BAD:
  'foo': 'this is a 'bad' example'

Conflicts:
	manifests/policy/base.pp
	spec/defines/openstacklib_policy_base_spec.rb

Closes-Bug: #1965338
Change-Id: I0216c2e4ecf75dbdd93d06eae2ebf8e7f2f4ac1a
(cherry picked from commit eb01261c06)
(cherry picked from commit 5cc3c2a179)
(cherry picked from commit 9072278b84)
This commit is contained in:
Takashi Kajinami 2022-03-18 09:07:58 +09:00
parent 670bc70308
commit ef542d7324
2 changed files with 35 additions and 1 deletions

View File

@ -83,9 +83,16 @@ define openstacklib::policy::base (
replace => false, # augeas will manage the content, we just need to make sure it exists
content => ''
})
# NOTE(tkajianm): Currently we use single quotes('') to quote the whole
# value, thus a single quote in value should be escaped
# by another single quote (which results in '')
# NOTE(tkajinam): Replace '' by ' first in case ' is already escaped
$value_real = regsubst(regsubst($value, '\'\'', '\'', 'G'), '\'', '\'\'', 'G')
file_line { "${file_path}-${key}" :
path => $file_path,
line => "'${key}': '${value}'",
line => "'${key}': '${value_real}'",
match => "^['\"]?${key}['\"]?\\s*:.+"
}
File<| title == $file_path |>

View File

@ -71,6 +71,33 @@ describe 'openstacklib::policy::base' do
:match => '^[\'"]?context_is_admin or owner[\'"]?\s*:.+'
) }
context 'with single-quotes in value' do
before do
params.merge!({
:value => 'foo:\'bar\''
})
end
it { should contain_file_line('/etc/nova/policy.yaml-context_is_admin or owner').with(
:path => '/etc/nova/policy.yaml',
:line => '\'context_is_admin or owner\': \'foo:\'\'bar\'\'\'',
:match => '^[\'"]?context_is_admin or owner[\'"]?\s*:.+'
) }
end
context 'with pre-formatted single-quotes in value' do
before do
params.merge!({
:value => 'foo:\'\'bar\'\''
})
end
it { should contain_file_line('/etc/nova/policy.yaml-context_is_admin or owner').with(
:path => '/etc/nova/policy.yaml',
:line => '\'context_is_admin or owner\': \'foo:\'\'bar\'\'\'',
:match => '^[\'"]?context_is_admin or owner[\'"]?\s*:.+'
) }
end
end
end