Skip to content

Commit d3aea9c

Browse files
Merge pull request #1007 from maturnbull/proxy_per_host
(MODULES-11173) Add per-host overrides for apt::proxy
2 parents bb7eb5c + c3c611f commit d3aea9c

File tree

6 files changed

+165
-3
lines changed

6 files changed

+165
-3
lines changed

REFERENCE.md

+48
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ be manipulated through the `apt-key` command.
4141

4242
* [`Apt::Auth_conf_entry`](#aptauth_conf_entry): Login configuration settings that are recorded in the file `/etc/apt/auth.conf`.
4343
* [`Apt::Proxy`](#aptproxy): Configures Apt to connect to a proxy server.
44+
* [`Apt::Proxy_Per_Host`](#aptproxy_per_host): Adds per-host overrides to the system default APT proxy configuration
4445

4546
### Tasks
4647

@@ -1099,6 +1100,7 @@ Struct[{
10991100
https => Optional[Boolean],
11001101
https_acng => Optional[Boolean],
11011102
direct => Optional[Boolean],
1103+
perhost => Optional[Array[Apt::Proxy_Per_Host]],
11021104
}]
11031105
```
11041106

@@ -1132,6 +1134,52 @@ Specifies whether to enable https proxies.
11321134

11331135
Specifies whether or not to use a `DIRECT` https proxy if http proxy is used but https is not.
11341136

1137+
### <a name="aptproxy_per_host"></a>`Apt::Proxy_Per_Host`
1138+
1139+
Adds per-host overrides to the system default APT proxy configuration
1140+
1141+
Alias of
1142+
1143+
```puppet
1144+
Struct[{
1145+
scope => String,
1146+
host => Optional[String],
1147+
port => Optional[Integer[1, 65535]],
1148+
https => Optional[Boolean],
1149+
direct => Optional[Boolean],
1150+
}]
1151+
```
1152+
1153+
#### Parameters
1154+
1155+
The following parameters are available in the `Apt::Proxy_Per_Host` data type:
1156+
1157+
* [`scope`](#scope)
1158+
* [`host`](#host)
1159+
* [`port`](#port)
1160+
* [`https`](#https)
1161+
* [`direct`](#direct)
1162+
1163+
##### <a name="scope"></a>`scope`
1164+
1165+
Specifies the scope of the override. Valid options: a string containing a hostname.
1166+
1167+
##### <a name="host"></a>`host`
1168+
1169+
Specifies a proxy host to be stored in `/etc/apt/apt.conf.d/01proxy`. Valid options: a string containing a hostname.
1170+
1171+
##### <a name="port"></a>`port`
1172+
1173+
Specifies a proxy port to be stored in `/etc/apt/apt.conf.d/01proxy`. Valid options: an integer containing a port number.
1174+
1175+
##### <a name="https"></a>`https`
1176+
1177+
Specifies whether to enable https for this override.
1178+
1179+
##### <a name="direct"></a>`direct`
1180+
1181+
Specifies whether or not to use a `DIRECT` target to bypass the system default proxy.
1182+
11351183
## Tasks
11361184

11371185
### <a name="init"></a>`init`

manifests/init.pp

+23-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,29 @@
204204
}
205205

206206
$_purge = merge($::apt::purge_defaults, $purge)
207-
$_proxy = merge($apt::proxy_defaults, $proxy)
207+
208+
if $proxy['perhost'] {
209+
$_perhost = $proxy['perhost'].map |$item| {
210+
$_item = merge($apt::proxy_defaults, $item)
211+
$_scheme = $_item['https'] ? {
212+
true => 'https',
213+
default => 'http' }
214+
$_port = $_item['port'] ? {
215+
Integer => ":${_item['port']}",
216+
default => ''
217+
}
218+
$_target = $_item['direct'] ? {
219+
true => 'DIRECT',
220+
default => "${_scheme}://${_item['host']}${_port}/" }
221+
merge($item, {
222+
'scheme' => $_scheme,
223+
'target' => $_target })
224+
}
225+
} else {
226+
$_perhost = {}
227+
}
228+
229+
$_proxy = merge($apt::proxy_defaults, $proxy, { 'perhost' => $_perhost } )
208230

209231
$confheadertmp = epp('apt/_conf_header.epp')
210232
$proxytmp = epp('apt/proxy.epp', {'proxies' => $_proxy})

spec/classes/apt_spec.rb

+64-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,69 @@
101101
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
102102
%r{Acquire::http::proxy "http://localhost:8080/";},
103103
).without_content(
104-
%r{Acquire::https::proxy},
104+
%r{Acquire::https::proxy },
105+
)
106+
}
107+
end
108+
109+
context 'when host=localhost and per-host[proxyscope]=proxyhost' do
110+
let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost' }] } } }
111+
112+
it {
113+
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
114+
%r{Acquire::http::proxy::proxyscope "http://proxyhost:8080/";},
115+
)
116+
}
117+
end
118+
119+
context 'when host=localhost and per-host[proxyscope]=proxyhost:8081' do
120+
let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost', 'port' => 8081 }] } } }
121+
122+
it {
123+
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
124+
%r{Acquire::http::proxy::proxyscope "http://proxyhost:8081/";},
125+
)
126+
}
127+
end
128+
129+
context 'when host=localhost and per-host[proxyscope]=[https]proxyhost' do
130+
let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost', 'https' => true }] } } }
131+
132+
it {
133+
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
134+
%r{Acquire::https::proxy::proxyscope "https://proxyhost:8080/";},
135+
)
136+
}
137+
end
138+
139+
context 'when host=localhost and per-host[proxyscope]=[direct]' do
140+
let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'direct' => true }] } } }
141+
142+
it {
143+
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
144+
%r{Acquire::http::proxy::proxyscope "DIRECT";},
145+
)
146+
}
147+
end
148+
149+
context 'when host=localhost and per-host[proxyscope]=[https][direct]' do
150+
let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'https' => true, 'direct' => true }] } } }
151+
152+
it {
153+
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
154+
%r{Acquire::https::proxy::proxyscope "DIRECT";},
155+
)
156+
}
157+
end
158+
159+
context 'when host=localhost and per-host[proxyscope]=proxyhost and per-host[proxyscope2]=proxyhost2' do
160+
let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost' }, { 'scope' => 'proxyscope2', 'host' => 'proxyhost2' }] } } }
161+
162+
it {
163+
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
164+
%r{Acquire::http::proxy::proxyscope "http://proxyhost:8080/";},
165+
).with_content(
166+
%r{Acquire::http::proxy::proxyscope2 "http://proxyhost2:8080/";},
105167
)
106168
}
107169
end
@@ -113,7 +175,7 @@
113175
is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
114176
%r{Acquire::http::proxy "http://localhost:8180/";},
115177
).without_content(
116-
%r{Acquire::https::proxy},
178+
%r{Acquire::https::proxy },
117179
)
118180
}
119181
end

templates/proxy.epp

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<%- | Hash $proxies | -%>
2+
<% $proxies['perhost'].each |$proxy| { -%>
3+
Acquire::<%= $proxy['scheme'] %>::proxy::<%= $proxy['scope'] %> "<%= $proxy['target'] %>";
4+
<% } -%>
25
Acquire::http::proxy "http://<%= $proxies['host'] %>:<%= $proxies['port'] %>/";
36
<%- if $proxies['https'] { %>
47
Acquire::https::proxy "https://<%= $proxies['host'] %>:<%= $proxies['port'] %>/";

types/proxy.pp

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
https => Optional[Boolean],
2424
https_acng => Optional[Boolean],
2525
direct => Optional[Boolean],
26+
perhost => Optional[Array[Apt::Proxy_Per_Host]],
2627
}
2728
]

types/proxy_per_host.pp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# @summary Adds per-host overrides to the system default APT proxy configuration
2+
#
3+
# @param scope
4+
# Specifies the scope of the override. Valid options: a string containing a hostname.
5+
#
6+
# @param host
7+
# Specifies a proxy host to be stored in `/etc/apt/apt.conf.d/01proxy`. Valid options: a string containing a hostname.
8+
#
9+
# @param port
10+
# Specifies a proxy port to be stored in `/etc/apt/apt.conf.d/01proxy`. Valid options: an integer containing a port number.
11+
#
12+
# @param https
13+
# Specifies whether to enable https for this override.
14+
#
15+
# @param direct
16+
# Specifies whether or not to use a `DIRECT` target to bypass the system default proxy.
17+
#
18+
type Apt::Proxy_Per_Host = Struct[
19+
{
20+
scope => String,
21+
host => Optional[String],
22+
port => Optional[Integer[1, 65535]],
23+
https => Optional[Boolean],
24+
direct => Optional[Boolean],
25+
}
26+
]

0 commit comments

Comments
 (0)