Skip to content

Use a stricter data type on apache::vhost::aliases #2253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@
Optional[Array[Hash]] $access_logs = undef,
Boolean $use_servername_for_filenames = false,
Boolean $use_port_for_filenames = false,
Optional[Variant[Array[Hash],Hash,String]] $aliases = undef,
Array[Hash[String[1], String[1]]] $aliases = [],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be even better if we use a Struct to describe the possible keys and enforce that path is present, but this is good enough for now.

Optional[Variant[Hash, Array[Variant[Array,Hash]]]] $directories = undef,
Boolean $error_log = true,
Optional[String] $error_log_file = undef,
Expand Down Expand Up @@ -2222,7 +2222,6 @@

# Load mod_alias if needed and not yet loaded
if ($scriptalias or $scriptaliases != [])
or ($aliases and $aliases != [])
or ($redirect_source and $redirect_dest)
or ($redirectmatch_regexp or $redirectmatch_status or $redirectmatch_dest) {
if ! defined(Class['apache::mod::alias']) and ($ensure == 'present') {
Expand Down Expand Up @@ -2376,7 +2375,9 @@

# Template uses:
# - $aliases
if $aliases and ! empty($aliases) {
if ! empty($aliases) and $ensure == 'present' {
include apache::mod::alias

concat::fragment { "${name}-aliases":
target => "${priority_real}${filename}.conf",
order => 20,
Expand Down
20 changes: 12 additions & 8 deletions spec/acceptance/default_mods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ class { 'apache':
}
apache::vhost { 'defaults.example.com':
docroot => '#{apache_hash['doc_root']}/defaults',
aliases => {
alias => '/css',
path => '#{apache_hash['doc_root']}/css',
},
aliases => [
{
alias => '/css',
path => '#{apache_hash['doc_root']}/css',
},
],
directories => [
{
'path' => "#{apache_hash['doc_root']}/admin",
Expand Down Expand Up @@ -76,10 +78,12 @@ class { 'apache':
}
apache::vhost { 'defaults.example.com':
docroot => '#{apache_hash['doc_root']}/defaults',
aliases => {
alias => '/css',
path => '#{apache_hash['doc_root']}/css',
},
aliases => [
{
alias => '/css',
path => '#{apache_hash['doc_root']}/css',
},
],
setenv => 'TEST1 one',
}
MANIFEST
Expand Down
13 changes: 11 additions & 2 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@
'logroot_owner' => 'root',
'logroot_group' => 'root',
'log_level' => 'crit',
'aliases' => [
{
'alias' => '/image',
'path' => '/rspec/image',
},
],
'access_log' => false,
'access_log_file' => 'httpd_access_log',
'access_log_syslog' => true,
'access_log_format' => '%h %l %u %t \"%r\" %>s %b',
'access_log_env_var' => '',
'aliases' => '/image',
'directories' => [
{
'path' => '/var/www/files',
Expand Down Expand Up @@ -619,7 +624,11 @@
)
}
it { is_expected.to contain_concat__fragment('rspec.example.com-docroot') }
it { is_expected.to contain_concat__fragment('rspec.example.com-aliases') }
it {
is_expected.to contain_concat__fragment('rspec.example.com-aliases').with(
content: %r{^\s+Alias /image "/rspec/image"$},
)
}
it { is_expected.to contain_concat__fragment('rspec.example.com-itk') }
it { is_expected.to contain_concat__fragment('rspec.example.com-fallbackresource') }
it { is_expected.to contain_concat__fragment('rspec.example.com-directories') }
Expand Down
14 changes: 6 additions & 8 deletions templates/vhost/_aliases.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<% if @aliases and ! @aliases.empty? -%>
## Alias declarations for resources outside the DocumentRoot
<%- [@aliases].flatten.compact.each do |alias_statement| -%>
<%- if alias_statement["path"] != '' -%>
<%- if alias_statement["alias"] and alias_statement["alias"] != '' -%>
<%- @aliases.each do |alias_statement| -%>
<%- if alias_statement["path"] -%>
<%- if alias_statement["alias"] -%>
Alias <%= alias_statement["alias"] %> "<%= alias_statement["path"] %>"
<%- elsif alias_statement["aliasmatch"] and alias_statement["aliasmatch"] != '' -%>
<%- elsif alias_statement["aliasmatch"] -%>
AliasMatch <%= alias_statement["aliasmatch"] %> "<%= alias_statement["path"] %>"
<%- elsif alias_statement["scriptalias"] and alias_statement["scriptalias"] != '' -%>
<%- elsif alias_statement["scriptalias"] -%>
ScriptAlias <%= alias_statement["scriptalias"] %> "<%= alias_statement["path"] %>"
<%- elsif alias_statement["scriptaliasmatch"] and alias_statement["scriptaliasmatch"] != '' -%>
<%- elsif alias_statement["scriptaliasmatch"] -%>
ScriptAliasMatch <%= alias_statement["scriptaliasmatch"] %> "<%= alias_statement["path"] %>"
<%- end -%>
<%- end -%>
<%- end -%>
<% end -%>