From 69edb0d6344a7c628c538219aa7b7b01ae131f6f Mon Sep 17 00:00:00 2001 From: Will Meek Date: Mon, 6 Nov 2017 15:46:57 +0000 Subject: [PATCH] Add a 'direct' option to proxy settings This commit adds a 'direct' boolean option to proxy settings. When set to true, if https is not true, the https proxy is set to 'DIRECT'. --- README.md | 2 ++ manifests/init.pp | 3 +++ manifests/params.pp | 1 + spec/classes/apt_spec.rb | 51 ++++++++++++++++++++++++++++++++-------- templates/proxy.epp | 4 ++-- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7c7a7b3..a93d7bd 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,8 @@ Main class, includes all other classes. * 'https': Specifies whether to enable https proxies. Valid options: 'true' and 'false'. Default: 'false'. * 'ensure': Optional parameter. Valid options: 'file', 'present', and 'absent'. Default: 'undef'. Prefer 'file' over 'present'. + + * 'direct': Specifies whether or not to use a 'DIRECT' https proxy if http proxy is used but https is not. Valid options: 'true' and 'false'. Default: 'false'. * `purge`: Specifies whether to purge any existing settings that aren't managed by Puppet. Valid options: a hash made up from the following keys: diff --git a/manifests/init.pp b/manifests/init.pp index 581ce2d..aa61986 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -78,6 +78,9 @@ class apt ( if $proxy['https']{ assert_type(Boolean, $proxy['https']) } + if $proxy['direct']{ + assert_type(Boolean, $proxy['direct']) + } $_proxy = merge($apt::proxy_defaults, $proxy) diff --git a/manifests/params.pp b/manifests/params.pp index 8126c02..f85b9d1 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -49,6 +49,7 @@ class apt::params { 'host' => undef, 'port' => 8080, 'https' => false, + 'direct' => false, } $purge_defaults = { diff --git a/spec/classes/apt_spec.rb b/spec/classes/apt_spec.rb index e46ad05..cbbe746 100644 --- a/spec/classes/apt_spec.rb +++ b/spec/classes/apt_spec.rb @@ -54,7 +54,7 @@ describe 'apt' do it 'lays down /etc/apt/apt.conf.d/15update-stamp' do is_expected.to contain_file('/etc/apt/apt.conf.d/15update-stamp').with(group: 'root', mode: '0644', - owner: 'root').with_content(/APT::Update::Post-Invoke-Success \{"touch \/var\/lib\/apt\/periodic\/update-success-stamp 2>\/dev\/null \|\| true";\};/) # rubocop:disable Metrics/LineLength + owner: 'root').with_content(%r{APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};}) # rubocop:disable Metrics/LineLength end it { @@ -70,9 +70,9 @@ describe 'apt' do it { is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content( - /Acquire::http::proxy "http:\/\/localhost:8080\/";/, + %r{Acquire::http::proxy "http://localhost:8080/";}, ).without_content( - %r{Acquire::https::proxy "DIRECT"}, + %r{Acquire::https::proxy}, ) } end @@ -82,9 +82,9 @@ describe 'apt' do it { is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content( - /Acquire::http::proxy "http:\/\/localhost:8180\/";/, + %r{Acquire::http::proxy "http://localhost:8180/";}, ).without_content( - %r{Acquire::https::proxy "DIRECT"}, + %r{Acquire::https::proxy}, ) } end @@ -94,9 +94,40 @@ describe 'apt' do it { is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content( - /Acquire::http::proxy "http:\/\/localhost:8080\/";/, + %r{Acquire::http::proxy "http://localhost:8080/";}, ).with_content( - /Acquire::https::proxy "https:\/\/localhost:8080\/";/, + %r{Acquire::https::proxy "https://localhost:8080/";}, + ) + } + end + + context 'host=localhost and direct=true' do + let(:params) { { proxy: { 'host' => 'localhost', 'direct' => true } } } + + it { + is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content( + %r{Acquire::http::proxy "http://localhost:8080/";}, + ).with_content( + %r{Acquire::https::proxy "DIRECT";}, + ) + } + end + + context 'host=localhost and https=true and direct=true' do + let(:params) { { proxy: { 'host' => 'localhost', 'https' => true, 'direct' => true } } } + + it { + is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content( + %r{Acquire::http::proxy "http://localhost:8080/";}, + ).with_content( + %r{Acquire::https::proxy "https://localhost:8080/";}, + ) + } + it { + is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content( + %r{Acquire::http::proxy "http://localhost:8080/";}, + ).without_content( + %r{Acquire::https::proxy "DIRECT";}, ) } end @@ -175,14 +206,14 @@ describe 'apt' do is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present') } - it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(/^deb http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) } - it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(/^deb-src http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) } + it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(%r{^deb http://debian.mirror.iweb.ca/debian/ unstable main contrib non-free$}) } + it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(%r{^deb-src http://debian.mirror.iweb.ca/debian/ unstable main contrib non-free$}) } it { is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present') } - it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) } + it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com precise main$}) } end context 'with confs defined on valid osfamily' do diff --git a/templates/proxy.epp b/templates/proxy.epp index 94e41af..ee663cb 100644 --- a/templates/proxy.epp +++ b/templates/proxy.epp @@ -2,6 +2,6 @@ Acquire::http::proxy "http://<%= $proxies['host'] %>:<%= $proxies['port'] %>/"; <%- if $proxies['https'] { %> Acquire::https::proxy "https://<%= $proxies['host'] %>:<%= $proxies['port'] %>/"; -<%- } else { -%> - Acquire::https::proxy "DIRECT"; +<%- } elsif $proxies['direct'] { -%> +Acquire::https::proxy "DIRECT"; <%- } -%> -- 2.45.2