Require valid type for available_themses

Currently, if a users gives invalid input type for available_themses,
the logic ignores the input and reflect nothing in the config file,
which can cause unexpected results. This ensures the given input is
a hash value.

This also adds validations to ensure only required values are included
by the hash.

Change-Id: I61aefe65e6218a79dce7a0633cb0ceb13b0021b4
This commit is contained in:
Takashi Kajinami 2023-10-22 22:00:20 +09:00
parent d94647073a
commit 5a3245f9d4
4 changed files with 73 additions and 9 deletions

View File

@ -0,0 +1,15 @@
Puppet::Functions.create_function(:validate_available_themes) do
def validate_available_themes(themes)
req_keys = Set.new(['name', 'label', 'path'])
themes.each do |theme|
if theme.keys.to_set != req_keys
if theme.keys.to_set.subset?(req_keys)
raise Puppet::Error, "Some of the required keys (name, label and path) are missing"
else
raise Puppet::Error, "Unsupported keys are detected"
end
end
end
end
end

View File

@ -352,7 +352,7 @@
# [*available_themes*]
# (optional) An array of hashes detailing available themes. Each hash must
# have the followings keys for themes to be made available; name, label,
# path. Defaults to false
# path. Defaults to undef
#
# { 'name' => 'theme_name', 'label' => 'theme_label', 'path' => 'theme_path' }
#
@ -364,14 +364,15 @@
# ]
# }
#
# Or in Hiera:
# horizon::available_themes:
# - { name: 'default', label: 'Default', path: 'themes/default' }
# - { name: 'material', label: 'Material', path: 'themes/material' }
# Or in Hiera:
# horizon::available_themes:
# - { name: 'default', label: 'Default', path: 'themes/default' }
# - { name: 'material', label: 'Material', path: 'themes/material' }
#
# [*default_theme*]
# (optional) The default theme to use from list of available themes. Value should be theme_name.
# Defaults to false
# (optional) The default theme to use from list of available themes. Value
# should be theme_name.
# Defaults to undef
#
# [*authentication_plugins*]
# (optional) List of authentication plugins to be used.
@ -610,8 +611,8 @@ class horizon(
Boolean $secure_cookies = false,
$django_session_engine = undef,
$vhost_extra_params = undef,
$available_themes = false,
$default_theme = false,
Horizon::AvailableThemes $available_themes = undef,
Optional[String[1]] $default_theme = undef,
Array[String[1]] $authentication_plugins = [],
Enum['on', 'off'] $password_autocomplete = 'off',
$create_image_defaults = undef,
@ -657,6 +658,10 @@ class horizon(
fail('websso_initial_choice is required when websso_choices_hide_keystone is true')
}
if $available_themes {
validate_available_themes($available_themes)
}
Service <| title == 'memcached' |> -> Class['horizon']
if $policy_files_path != undef {

View File

@ -0,0 +1,38 @@
require 'spec_helper'
describe 'Horizon::AvailableThemes' do
describe 'valid types' do
context 'with valid types' do
[
[
{'name' => 'default'}
],
[
{'name' => 'default'},
{'name' => 'custom'}
],
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end
end
describe 'invalid types' do
context 'with garbage inputs' do
[
[],
['name'],
[{'name' => 1}],
[{1 => 'default'}],
[{'name' => ''}],
[{'' => 'default'}],
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end

6
types/availablethemes.pp Normal file
View File

@ -0,0 +1,6 @@
type Horizon::AvailableThemes = Optional[
Array[
Hash[String[1], String[1]],
1
]
]