Skip to content

Commit 4aaf37f

Browse files
author
Ciaran McCrisken
committed
(IAC-1186) Add new $use_servername_for_filenames parameter
Prior to this commit, the `$filename` variable, which is used to construct the filename of Apache's various config and log files, obtained it's default value from the `$name` parameter. As #2064 highlights, it is possible for `$name` to contain spaces in it and this can cause cumbersome log file names, albeit POSIX compliant. Also related is #2068, which changes the `$filename` variable source from `$name` to `$servername`. This arguably seems more appropriate, especially given that `$servername` defaults to `$name` if undefined. This commit attempts to create a satisfactory solution to both #2064 and #2068 by introducing the `$use_servername_for_filenames` param. When set to `true`, a sanitized `$servername` parameter value will be used to construct `$filename`. When undefined or set to `$false`, it will retain the existing behaviour and use the `$name` parameter value. This will default to `false` until the next major release (v6.0.0), after which it will default to `true`. Then, in the subsequent major release (v7.0.0) it will be deprecated altogether and the default behaviour will be to use the sanitized value of `$servername` for the `$filename` var. Warning messages are output to the console to alert users of this change in behaviour.
1 parent 935acd4 commit 4aaf37f

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

manifests/vhost.pp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,12 @@
17171717
# client request exceeds that limit, the server will return an error response
17181718
# instead of servicing the request.
17191719
#
1720+
# @param $use_servername_for_filenames
1721+
# When set to true, default log / config file names will be derived from the sanitized
1722+
# value of the $servername parameter.
1723+
# When set to false (default), the existing behaviour of using the $name parameter
1724+
# will remain.
1725+
17201726
define apache::vhost (
17211727
Variant[Boolean,String] $docroot,
17221728
$manage_docroot = true,
@@ -1782,6 +1788,7 @@
17821788
$access_log_format = false,
17831789
$access_log_env_var = false,
17841790
Optional[Array] $access_logs = undef,
1791+
Optional[Boolean] $use_servername_for_filenames = false,
17851792
$aliases = undef,
17861793
Optional[Variant[Hash, Array[Variant[Array,Hash]]]] $directories = undef,
17871794
Boolean $error_log = true,
@@ -2036,8 +2043,39 @@
20362043
$priority_real = '25-'
20372044
}
20382045

2039-
## Apache include does not always work with spaces in the filename
2040-
$filename = regsubst($name, ' ', '_', 'G')
2046+
# IAC-1186: A number of configuration and log file names are generated using the $name parameter. It is possile for
2047+
# the $name parameter to contain spaces, which could then be transferred to the log / config filenames. Although
2048+
# POSIX compliant, this can be cumbersome.
2049+
#
2050+
# It seems more appropriate to use the $servername parameter to derive default log / config filenames from. We should
2051+
# also perform some sanitiation on the $servername parameter to strip spaces from it, as it defaults to the value of
2052+
# $name, should $servername NOT be defined.
2053+
#
2054+
# We will retain the default behaviour for filenames but allow the use of a sanitized version of $servername to be
2055+
# used, using the new $use_servername_for_filenames parameter.
2056+
#
2057+
# This will default to false until the next major release (v6.0.0), at which point, we will default this to true and
2058+
# warn about it's imminent deprecation in the subsequent major release (v7.0.0)
2059+
#
2060+
# In v7.0.0, we will deprecate the $use_servername_for_filenames parameter altogether and use the sanitized value of
2061+
# $servername for default log / config filenames.
2062+
$filename = $use_servername_for_filenames ? {
2063+
true => regsubst($servername, ' ', '_', 'G'),
2064+
false => $name,
2065+
}
2066+
2067+
if ! $use_servername_for_filenames {
2068+
$use_servername_for_filenames_warn_msg = '
2069+
It is possible for the $name parameter to be defined with spaces in it. Although supported on POSIX systems, this
2070+
can lead to cumbersome file names. The $servername attribute has stricter conditions from Apache (i.e. no spaces)
2071+
When $use_servername_for_filenames = true, the $servername parameter, sanitized, is used to construct log and config
2072+
file names.
2073+
2074+
From version v6.0.0 of the puppetlabs-apache module, this parameter will default to true. From version v7.0.0 of the
2075+
module, the $use_servername_for_filenames will be removed and log/config file names will be dervied from the
2076+
sanitized $servername parameter when not explicitly defined.'
2077+
warning($use_servername_for_filenames_warn_msg)
2078+
}
20412079

20422080
# This ensures that the docroot exists
20432081
# But enables it to be specified across multiple vhost resources
@@ -2096,9 +2134,9 @@
20962134
$error_log_destination = $error_log_syslog
20972135
} else {
20982136
if $ssl {
2099-
$error_log_destination = "${logroot}/${name}_error_ssl.log"
2137+
$error_log_destination = "${logroot}/${filename}_error_ssl.log"
21002138
} else {
2101-
$error_log_destination = "${logroot}/${name}_error.log"
2139+
$error_log_destination = "${logroot}/${filename}_error.log"
21022140
}
21032141
}
21042142

@@ -2117,9 +2155,9 @@
21172155
$modsec_audit_log_destination = $modsec_audit_log_pipe
21182156
} elsif $modsec_audit_log {
21192157
if $ssl {
2120-
$modsec_audit_log_destination = "${logroot}/${name}_security_ssl.log"
2158+
$modsec_audit_log_destination = "${logroot}/${filename}_security_ssl.log"
21212159
} else {
2122-
$modsec_audit_log_destination = "${logroot}/${name}_security.log"
2160+
$modsec_audit_log_destination = "${logroot}/${filename}_security.log"
21232161
}
21242162
} else {
21252163
$modsec_audit_log_destination = undef

spec/acceptance/vhost_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,58 @@ class { 'apache': }
948948
end
949949
end
950950

951+
context 'when a manifest defines $servername' do
952+
describe 'when the $use_servername_for_filenames parameter is set to true' do
953+
pp = <<-MANIFEST
954+
class { 'apache': }
955+
host { 'test.server': ip => '127.0.0.1' }
956+
apache::vhost { 'test.server':
957+
use_servername_for_filenames => true,
958+
servername => 'test.servername',
959+
docroot => '/tmp',
960+
logroot => '/tmp',
961+
}
962+
MANIFEST
963+
it 'applies cleanly and NOT print warning about $use_servername_for_filenames usage for test.server vhost' do
964+
result = apply_manifest(pp, catch_failures: true)
965+
expect(result.stderr).not_to contain %r{
966+
.*Warning\:\sScope\(Apache::Vhost\[test\.server\]\)\:.*
967+
It\sis\spossible\sfor\sthe\s\$name\sparameter.*
968+
sanitized\s\$servername\sparameter\swhen\snot\sexplicitly\sdefined\.
969+
}xm
970+
end
971+
describe file("#{apache_hash['vhost_dir']}/25-test.servername.conf") do
972+
it { is_expected.to be_file }
973+
it { is_expected.to contain ' ErrorLog "/tmp/test.servername_error.log' }
974+
it { is_expected.to contain ' CustomLog "/tmp/test.servername_access.log' }
975+
end
976+
end
977+
describe 'when the $use_servername_for_filenames parameter is NOT defined' do
978+
pp = <<-MANIFEST
979+
class { 'apache': }
980+
host { 'test.server': ip => '127.0.0.1' }
981+
apache::vhost { 'test.server':
982+
servername => 'test.servername',
983+
docroot => '/tmp',
984+
logroot => '/tmp',
985+
}
986+
MANIFEST
987+
it 'applies cleanly and print warning about $use_servername_for_filenames usage for test.server vhost' do
988+
result = apply_manifest(pp, catch_failures: true)
989+
expect(result.stderr).to contain %r{
990+
.*Warning\:\sScope\(Apache::Vhost\[test\.server\]\)\:.*
991+
It\sis\spossible\sfor\sthe\s\$name\sparameter.*
992+
sanitized\s\$servername\sparameter\swhen\snot\sexplicitly\sdefined\.
993+
}xm
994+
end
995+
describe file("#{apache_hash['vhost_dir']}/25-test.server.conf") do
996+
it { is_expected.to be_file }
997+
it { is_expected.to contain ' ErrorLog "/tmp/test.server_error.log' }
998+
it { is_expected.to contain ' CustomLog "/tmp/test.server_access.log' }
999+
end
1000+
end
1001+
end
1002+
9511003
['access', 'error'].each do |logtype|
9521004
case logtype
9531005
when 'access'

templates/vhost/_access_log.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<% elsif log['pipe'] -%>
1515
<% destination = log['pipe'] -%>
1616
<% else -%>
17-
<% destination ||= "#{@logroot}/#{@name}_access_ssl.log" if @ssl -%>
18-
<% destination ||= "#{@logroot}/#{@name}_access.log" -%>
17+
<% destination ||= "#{@logroot}/#{@filename}_access_ssl.log" if @ssl -%>
18+
<% destination ||= "#{@logroot}/#{@filename}_access.log" -%>
1919
<% end -%>
2020
CustomLog "<%= destination %>" <%= format %> <%= env %>
2121
<% end -%>

0 commit comments

Comments
 (0)