diff --git a/manifests/default_mods.pp b/manifests/default_mods.pp index b03b2660b5..474b53748b 100644 --- a/manifests/default_mods.pp +++ b/manifests/default_mods.pp @@ -69,8 +69,6 @@ 'FreeBSD': { include apache::mod::actions include apache::mod::authn_core - include apache::mod::cache - include apache::mod::disk_cache include apache::mod::filter include apache::mod::headers include apache::mod::info diff --git a/manifests/mod/cache.pp b/manifests/mod/cache.pp index a822ae9aa4..d31cebd3f0 100644 --- a/manifests/mod/cache.pp +++ b/manifests/mod/cache.pp @@ -1,8 +1,58 @@ # @summary # Installs `mod_cache` -# +# +# @param cache_ignore_headers +# Specifies HTTP header(s) that should not be stored in the cache. +# +# @param cache_default_expire +# The default duration to cache a document when no expiry date is specified. +# +# @param cache_max_expire +# The maximum time in seconds to cache a document +# +# @param cache_ignore_no_lastmod +# Ignore the fact that a response has no Last Modified header. +# +# @param cache_header +# Add an X-Cache header to the response. +# +# @param cache_lock +# Enable the thundering herd lock. +# +# @param cache_ignore_cache_control +# Ignore request to not serve cached content to client +# # @see https://httpd.apache.org/docs/current/mod/mod_cache.html for additional documentation. # -class apache::mod::cache { - ::apache::mod { 'cache': } +class apache::mod::cache ( + Array[String[1]] $cache_ignore_headers = [], + Optional[Integer] $cache_default_expire = undef, + Optional[Integer] $cache_max_expire = undef, + Optional[Apache::OnOff] $cache_ignore_no_lastmod = undef, + Optional[Apache::OnOff] $cache_header = undef, + Optional[Apache::OnOff] $cache_lock = undef, + Optional[Apache::OnOff] $cache_ignore_cache_control = undef, +) { + include apache + apache::mod { 'cache': } + + $_configuration_file_name = 'cache.conf' + + file { $_configuration_file_name: + ensure => file, + path => "${apache::mod_dir}/${_configuration_file_name}", + mode => $apache::file_mode, + content => epp('apache/mod/cache.conf.epp', { + cache_ignore_headers => $cache_ignore_headers, + cache_default_expire => $cache_default_expire, + cache_max_expire => $cache_max_expire, + cache_ignore_no_lastmod => $cache_ignore_no_lastmod, + cache_header => $cache_header, + cache_lock => $cache_lock, + cache_ignore_cache_control => $cache_ignore_cache_control, + }), + require => Exec["mkdir ${apache::mod_dir}"], + before => File[$apache::mod_dir], + notify => Class['apache::service'], + } } diff --git a/manifests/mod/cache_disk.pp b/manifests/mod/cache_disk.pp new file mode 100644 index 0000000000..c8752fc4f3 --- /dev/null +++ b/manifests/mod/cache_disk.pp @@ -0,0 +1,85 @@ +# @summary +# Installs and configures `mod_cache_disk`. +# +# @description +# This will install an configure the proper module depending on the used apache version, so +# - mod_cache_disk for apache version >= 2.4 +# - mod_disk_cache for older apache versions +# +# @param cache_root +# Defines the name of the directory on the disk to contain cache files. +# Default depends on the Apache version and operating system: +# - Debian: /var/cache/apache2/mod_cache_disk +# - FreeBSD: /var/cache/mod_cache_disk +# - Red Hat: /var/cache/httpd/proxy +# +# @param cache_enable +# Defines an array of directories to cache, the default is none +# +# @param cache_dir_length +# The number of characters in subdirectory names +# +# @param cache_dir_levels +# The number of levels of subdirectories in the cache. +# +# @param cache_max_filesize +# The maximum size (in bytes) of a document to be placed in the cache +# +# @param cache_ignore_headers +# DEPRECATED Ignore request to not serve cached content to client (included for compatibility reasons to support disk_cache) +# +# @param configuration_file_name +# DEPRECATED Name of module configuration file (used for the compatibility layer for disk_cache) +# +# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html +# +class apache::mod::cache_disk ( + Optional[Stdlib::Absolutepath] $cache_root = undef, + Array[String] $cache_enable = [], + Optional[Integer] $cache_dir_length = undef, + Optional[Integer] $cache_dir_levels = undef, + Optional[Integer] $cache_max_filesize = undef, + Optional[String] $cache_ignore_headers = undef, + Optional[String] $configuration_file_name = undef, +) { + include apache + + if $cache_ignore_headers { + deprecation( + 'apache::mod::cache_disk', + 'The parameter cache_ignore_headers is deprecated. Please use apache::mod::cache::cache_ignore_headers instead.' + ) + } + + $_cache_root = $cache_root ? { + undef => $facts['os']['family'] ? { + 'debian' => '/var/cache/apache2/mod_cache_disk', + 'redhat' => '/var/cache/httpd/proxy', + 'freebsd' => '/var/cache/mod_cache_disk', + }, + default => $cache_root, + } + $_configuration_file_name = pick($configuration_file_name, 'cache_disk.conf') + $_class_name = 'apache::mod::cache_disk' + + apache::mod { 'cache_disk': } + + Class['apache::mod::cache'] -> Class[$_class_name] + + file { $_configuration_file_name: + ensure => file, + path => "${apache::mod_dir}/${_configuration_file_name}", + mode => $apache::file_mode, + content => epp('apache/mod/cache_disk.conf.epp', { + cache_root => $_cache_root, + cache_enable => $cache_enable, + cache_dir_length => $cache_dir_length, + cache_dir_levels => $cache_dir_levels, + cache_max_filesize => $cache_max_filesize, + cache_ignore_headers => $cache_ignore_headers, + }), + require => Exec["mkdir ${apache::mod_dir}"], + before => File[$apache::mod_dir], + notify => Class['apache::service'], + } +} diff --git a/manifests/mod/disk_cache.pp b/manifests/mod/disk_cache.pp index 4a3dc35883..92e56e23f3 100644 --- a/manifests/mod/disk_cache.pp +++ b/manifests/mod/disk_cache.pp @@ -1,12 +1,11 @@ # @summary # Installs and configures `mod_disk_cache`. -# +# # @param cache_root # Defines the name of the directory on the disk to contain cache files. # Default depends on the Apache version and operating system: # - Debian: /var/cache/apache2/mod_cache_disk # - FreeBSD: /var/cache/mod_cache_disk -# - Red Hat: /var/cache/httpd/proxy # # @param cache_ignore_headers # Specifies HTTP header(s) that should not be stored in the cache. @@ -17,44 +16,24 @@ # You can then control this behaviour in individual vhosts by explicitly defining CacheEnable. # # @note -# On Apache 2.4, mod_cache_disk installed. +# Apache 2.2, mod_disk_cache installed. On Apache 2.4, mod_cache_disk installed. +# This class is deprecated, use mode_cache_disk instead # -# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation. +# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation on version 2.4. # class apache::mod::disk_cache ( Optional[Stdlib::Absolutepath] $cache_root = undef, Optional[String] $cache_ignore_headers = undef, Boolean $default_cache_enable = true, ) { - include apache - if $cache_root { - $_cache_root = $cache_root - } else { - $_cache_root = $facts['os']['family'] ? { - 'Debian' => '/var/cache/apache2/mod_cache_disk', - 'RedHat' => '/var/cache/httpd/proxy', - 'FreeBSD' => '/var/cache/mod_cache_disk', - } - } - - apache::mod { 'cache_disk': } - - Class['apache::mod::cache'] -> Class['apache::mod::disk_cache'] - - $parameters = { - 'default_cache_enable' => $default_cache_enable, - '_cache_root' => $_cache_root, - 'cache_ignore_headers' => $cache_ignore_headers, - } + deprecation('apache::mod::disk_cache', 'This class is deprecated; please use apache::mod::cache_disk') - # Template uses $_cache_root - file { 'disk_cache.conf': - ensure => file, - path => "${apache::mod_dir}/disk_cache.conf", - mode => $apache::file_mode, - content => epp('apache/mod/disk_cache.conf.epp', $parameters), - require => Exec["mkdir ${apache::mod_dir}"], - before => File[$apache::mod_dir], - notify => Class['apache::service'], + class { 'apache::mod::cache_disk': + cache_root => $cache_root, + cache_enable => ['/'], + cache_ignore_headers => $cache_ignore_headers, + cache_dir_length => 1, + cache_dir_levels => 2, + configuration_file_name => 'cache_disk.conf' } } diff --git a/spec/classes/mod/cache_disk_spec.rb b/spec/classes/mod/cache_disk_spec.rb new file mode 100644 index 0000000000..fdd55fa8d9 --- /dev/null +++ b/spec/classes/mod/cache_disk_spec.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'apache::mod::cache_disk', type: :class do + context 'on a Debian OS' do + include_examples 'Debian 11' + + let(:params) do + { + cache_enable: ['/'], + } + end + + let :pre_condition do + 'class{ "apache": + default_mods => ["cache"], + mod_dir => "/tmp/junk", + }' + end + + it { is_expected.to compile } + it { is_expected.to contain_class('apache::mod::cache_disk') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } + it { is_expected.to contain_apache__mod('cache_disk') } + + default_config = %r{CacheEnable disk /\nCacheRoot "/var/cache/apache2/mod_cache_disk"} + + it { is_expected.to contain_file('cache_disk.conf').with(content: default_config) } + + describe 'with multiple cache_enable parameters' do + let(:params) do + { + cache_enable: ['/', '/something'], + } + end + + it { + expect(subject).to contain_file('cache_disk.conf') + .with(content: %r{CacheEnable disk /\nCacheEnable disk /something\nCacheRoot "/var/cache/apache2/mod_cache_disk"}) + } + end + + describe 'with cache_dir_length' do + let(:params) do + { + cache_dir_length: 2, + cache_enable: ['/'], + } + end + + it { + expect(subject).to contain_file('cache_disk.conf') + .with(content: %r{#{default_config}\nCacheDirLength 2}) + } + end + + describe 'with cache_dir_levels' do + let(:params) do + { + cache_dir_levels: 2, + cache_enable: ['/'], + } + end + + it { + expect(subject).to contain_file('cache_disk.conf') + .with(content: %r{#{default_config}\nCacheDirLevels 2}) + } + end + end + + context 'on a RedHat 8-based OS' do + include_examples 'RedHat 8' + + let(:params) do + { + cache_enable: ['/'], + } + end + + let :pre_condition do + 'class{ "apache": + default_mods => ["cache"], + mod_dir => "/tmp/junk", + }' + end + + it { is_expected.to compile } + + it { + expect(subject).to contain_file('cache_disk.conf') + .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/httpd/proxy"}) + } + end + + context 'on a FreeBSD OS' do + include_examples 'FreeBSD 10' + + let(:params) do + { + cache_enable: ['/'], + } + end + + let :pre_condition do + 'class{ "apache": + default_mods => ["cache"], + mod_dir => "/tmp/junk", + }' + end + + it { is_expected.to compile } + + it { + expect(subject).to contain_file('cache_disk.conf') + .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/mod_cache_disk"}) + } + end +end diff --git a/spec/classes/mod/cache_spec.rb b/spec/classes/mod/cache_spec.rb new file mode 100644 index 0000000000..3e98a3e679 --- /dev/null +++ b/spec/classes/mod/cache_spec.rb @@ -0,0 +1,122 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'apache::mod::cache', type: :class do + context 'on a Debian OS' do + include_examples 'Debian 11' + + it { is_expected.to compile } + it { is_expected.to contain_class('apache::mod::cache') } + it { is_expected.to contain_apache__mod('cache') } + + it { + expect(subject).to contain_file('cache.conf') + .with(content: '') + } + + describe 'with cache_ignore_headers' do + let(:params) do + { + cache_ignore_headers: ['Set-Cookie'], + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheIgnoreHeaders Set-Cookie}) + } + end + + describe 'with cache_ignore_headers' do + let(:params) do + { + cache_ignore_headers: ['Set-Cookie', 'X-Forwarded-For', 'Cross-Origin-Embedder-Policy', 'Expires', 'Access-Control-Allow-Headers'], + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheIgnoreHeaders Access-Control-Allow-Headers Cross-Origin-Embedder-Policy Expires Set-Cookie X-Forwarded-For}) + } + end + + describe 'with cache_default_expire' do + let(:params) do + { + cache_default_expire: 2000, + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheDefaultExpire 2000}) + } + end + + describe 'with cache_max_expire' do + let(:params) do + { + cache_max_expire: 2000, + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheMaxExpire 2000}) + } + end + + describe 'with cache_ignore_no_lastmod' do + let(:params) do + { + cache_ignore_no_lastmod: 'On', + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheIgnoreNoLastMod On}) + } + end + + describe 'with cache_header' do + let(:params) do + { + cache_header: 'On', + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheHeader On}) + } + end + + describe 'with cache_lock' do + let(:params) do + { + cache_lock: 'On', + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheLock On}) + } + end + + describe 'with cache_ignore_cache_control' do + let(:params) do + { + cache_ignore_cache_control: 'On', + } + end + + it { + expect(subject).to contain_file('cache.conf') + .with(content: %r{CacheIgnoreCacheControl On}) + } + end + end +end diff --git a/spec/classes/mod/disk_cache_spec.rb b/spec/classes/mod/disk_cache_spec.rb index ce840bdcf6..9438d0886d 100644 --- a/spec/classes/mod/disk_cache_spec.rb +++ b/spec/classes/mod/disk_cache_spec.rb @@ -14,18 +14,18 @@ let :pre_condition do 'class{ "apache": - default_mods => ["cache"], + default_mods => ["cache", "disk_cache"], mod_dir => "/tmp/junk", }' end it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/apache2/mod_cache_disk"\nCacheDirLevels 2\nCacheDirLength 1\nCacheIgnoreHeaders Set-Cookie}) } @@ -34,11 +34,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheRoot "/var/cache/apache2/mod_cache_disk"\nCacheDirLevels 2\nCacheDirLength 1\n}) } end @@ -48,11 +48,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/apache2/mod_cache_disk"\nCacheDirLevels 2\nCacheDirLength 1\n}) } end @@ -83,7 +83,7 @@ it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/httpd/proxy"\nCacheDirLevels 2\nCacheDirLength 1\nCacheIgnoreHeaders Set-Cookie}) } @@ -92,11 +92,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheRoot "/var/cache/httpd/proxy"\nCacheDirLevels 2\nCacheDirLength 1\n}) } end @@ -106,11 +106,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/httpd/proxy"\nCacheDirLevels 2\nCacheDirLength 1\n}) } end @@ -140,11 +140,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/mod_cache_disk"\nCacheDirLevels 2\nCacheDirLength 1\nCacheIgnoreHeaders Set-Cookie}) } @@ -153,11 +153,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheRoot "/var/cache/mod_cache_disk"\nCacheDirLevels 2\nCacheDirLength 1\n}) } end @@ -167,11 +167,11 @@ it { is_expected.to compile } it { is_expected.to contain_class('apache::mod::disk_cache') } - it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Disk_cache]') } + it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') } it { is_expected.to contain_apache__mod('cache_disk') } it { - expect(subject).to contain_file('disk_cache.conf') + expect(subject).to contain_file('cache_disk.conf') .with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/mod_cache_disk"\nCacheDirLevels 2\nCacheDirLength 1\n}) } end diff --git a/templates/mod/cache.conf.epp b/templates/mod/cache.conf.epp new file mode 100644 index 0000000000..354f3b52b8 --- /dev/null +++ b/templates/mod/cache.conf.epp @@ -0,0 +1,30 @@ +<% | + Optional[Array[String[1]]] $cache_ignore_headers = undef, + Optional[Integer] $cache_default_expire = undef, + Optional[Integer] $cache_max_expire = undef, + Optional[Apache::OnOff] $cache_ignore_no_lastmod = undef, + Optional[Apache::OnOff] $cache_header = undef, + Optional[Apache::OnOff] $cache_lock = undef, + Optional[Apache::OnOff] $cache_ignore_cache_control = undef, +| -%> +<%- if $cache_default_expire { -%> +CacheDefaultExpire <%= $cache_default_expire %> +<%- } -%> +<%- if $cache_max_expire { -%> +CacheMaxExpire <%= $cache_max_expire %> +<%- } -%> +<%- if $cache_ignore_no_lastmod { -%> +CacheIgnoreNoLastMod <%= $cache_ignore_no_lastmod %> +<%- } -%> +<%- if $cache_header { -%> +CacheHeader <%= $cache_header %> +<%- } -%> +<%- if $cache_lock { -%> +CacheLock <%= $cache_lock %> +<%- } -%> +<%- if $cache_ignore_cache_control { -%> +CacheIgnoreCacheControl <%= $cache_ignore_cache_control %> +<%- } -%> +<%- if ! empty($cache_ignore_headers) { -%> +CacheIgnoreHeaders <%= $cache_ignore_headers.sort.join(' ') %> +<%- } -%> diff --git a/templates/mod/cache_disk.conf.epp b/templates/mod/cache_disk.conf.epp new file mode 100644 index 0000000000..6ae5a7b130 --- /dev/null +++ b/templates/mod/cache_disk.conf.epp @@ -0,0 +1,26 @@ +<% | + Optional[String] $cache_root = undef, + Array[String] $cache_enable = [], + Optional[Integer] $cache_dir_length = undef, + Optional[Integer] $cache_dir_levels = undef, + Optional[Integer] $cache_max_filesize = undef, + Optional[String] $cache_ignore_headers = undef, +| -%> +<%- if $cache_enable { -%> + <%- $cache_enable.each |$enable| { -%> +CacheEnable disk <%= $enable %> + <%- } -%> +<%- } -%> +CacheRoot "<%= $cache_root %>" +<%- if $cache_dir_levels { -%> +CacheDirLevels <%= $cache_dir_levels %> +<%- } -%> +<%- if $cache_dir_length { -%> +CacheDirLength <%= $cache_dir_length %> +<%- } -%> +<%- if $cache_max_filesize { -%> +CacheMaxFileSize <%= $cache_max_filesize %> +<%- } -%> +<%- if $cache_ignore_headers { -%> +CacheIgnoreHeaders <%= $cache_ignore_headers -%> +<%- } -%> diff --git a/templates/mod/disk_cache.conf.epp b/templates/mod/disk_cache.conf.epp deleted file mode 100644 index 2cfc0e647a..0000000000 --- a/templates/mod/disk_cache.conf.epp +++ /dev/null @@ -1,9 +0,0 @@ -<% if $default_cache_enable { -%> -CacheEnable disk / -<% } -%> -CacheRoot "<%= $_cache_root %>" -CacheDirLevels 2 -CacheDirLength 1 -<% if $cache_ignore_headers { -%> -CacheIgnoreHeaders <%= $cache_ignore_headers -%> -<% } -%> \ No newline at end of file