Skip to content

Commit 978c343

Browse files
committed
(CAT-1483) - Enhancement of validation for apt::source parameters
1 parent 0a23900 commit 978c343

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

REFERENCE.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,17 +970,18 @@ Default value: `present`
970970

971971
##### <a name="-apt--source--release"></a>`release`
972972

973-
Data type: `Optional[String]`
973+
Data type: `Optional[String[1]]`
974974

975975
Specifies a distribution of the Apt repository.
976976

977977
Default value: `undef`
978978

979979
##### <a name="-apt--source--repos"></a>`repos`
980980

981-
Data type: `String`
981+
Data type: `String[1]`
982982

983-
Specifies a component of the Apt repository.
983+
Specifies a component of the Apt repository, Valid options: main, contrib, non-free, universe, multiverse and restricted.
984+
Default 'main'.
984985

985986
Default value: `'main'`
986987

manifests/source.pp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# Specifies a distribution of the Apt repository.
2424
#
2525
# @param repos
26-
# Specifies a component of the Apt repository.
26+
# Specifies a component of the Apt repository, Valid options: main, contrib, non-free, universe, multiverse and restricted.
27+
# Default 'main'.
2728
#
2829
# @param include
2930
# Configures include options. Valid options: a hash of available keys.
@@ -68,8 +69,8 @@
6869
Optional[String] $location = undef,
6970
String $comment = $name,
7071
String $ensure = present,
71-
Optional[String] $release = undef,
72-
String $repos = 'main',
72+
Optional[String[1]] $release = undef,
73+
String[1] $repos = 'main',
7374
Variant[Hash] $include = {},
7475
Optional[Variant[String, Hash]] $key = undef,
7576
Optional[Stdlib::AbsolutePath] $keyring = undef,
@@ -90,6 +91,8 @@
9091
} else {
9192
fail('os.distro.codename fact not available: release parameter required')
9293
}
94+
} elsif $release !~ Regexp(/^[a-z-]+$/) {
95+
fail('release parameter must be set to a valid release name')
9396
} else {
9497
$_release = $release
9598
}
@@ -131,6 +134,12 @@
131134
}
132135
}
133136

137+
$repos.split(' ').each | $repo | {
138+
if $repo !~ Regexp(/^(main|contrib|non-free|universe|multiverse|restricted)$/) {
139+
fail('repos parameter must be set to a valid component name, or a comma-separated list of valid component names')
140+
}
141+
}
142+
134143
$header = epp('apt/_header.epp')
135144

136145
if $architecture {

spec/defines/source_compat_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'comment' => 'foo',
4242
'location' => 'http://debian.mirror.iweb.ca/debian/',
4343
'release' => 'sid',
44-
'repos' => 'testing',
44+
'repos' => 'main',
4545
'include' => { 'src' => false },
4646
'key' => id,
4747
'pin' => '10',
@@ -51,7 +51,7 @@
5151
end
5252

5353
it {
54-
expect(subject).to contain_apt__setting('list-my_source').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid testing\n})
54+
expect(subject).to contain_apt__setting('list-my_source').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid main\n})
5555
.without_content(%r{deb-src})
5656
}
5757

spec/defines/source_spec.rb

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,39 @@
6565
}
6666
end
6767

68+
context 'with invalid repos' do
69+
let :params do
70+
{
71+
comment: 'foo',
72+
location: 'http://debian.mirror.iweb.ca/debian/',
73+
release: 'sid',
74+
repos: 'test',
75+
}
76+
end
77+
78+
it { is_expected.to raise_error(Puppet::Error, %r{repos parameter must be set to a valid component name, or a comma-separated list of valid component names}) }
79+
end
80+
81+
context 'with empty string for repos' do
82+
let :params do
83+
{
84+
comment: 'foo',
85+
location: 'http://debian.mirror.iweb.ca/debian/',
86+
release: 'sid',
87+
repos: '',
88+
}
89+
end
90+
91+
it { is_expected.to raise_error(Puppet::Error, %r{parameter 'repos' expects a String\[1\] value, got String}) }
92+
end
93+
6894
context 'with simple key' do
6995
let :params do
7096
{
7197
comment: 'foo',
7298
location: 'http://debian.mirror.iweb.ca/debian/',
7399
release: 'sid',
74-
repos: 'testing',
100+
repos: 'main',
75101
key: id,
76102
pin: '10',
77103
architecture: 'x86_64',
@@ -80,7 +106,7 @@
80106
end
81107

82108
it {
83-
expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid testing\n})
109+
expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid main\n})
84110
.without_content(%r{deb-src})
85111
}
86112

@@ -102,7 +128,7 @@
102128
comment: 'foo',
103129
location: 'http://debian.mirror.iweb.ca/debian/',
104130
release: 'sid',
105-
repos: 'testing',
131+
repos: 'main',
106132
key: {
107133
'ensure' => 'refreshed',
108134
'id' => id,
@@ -118,7 +144,7 @@
118144
end
119145

120146
it {
121-
expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid testing\n})
147+
expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid main\n})
122148
.without_content(%r{deb-src})
123149
}
124150

@@ -421,10 +447,22 @@
421447
end
422448
end
423449

450+
context 'with release is not empty string' do
451+
let(:params) { { location: 'hello.there', release: 'test' } }
452+
453+
it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there test main}) }
454+
end
455+
456+
context 'with release invalid string' do
457+
let(:params) { { location: 'hello.there', release: '/' } }
458+
459+
it { is_expected.to raise_error(Puppet::Error, %r{release parameter must be set to a valid release name}) }
460+
end
461+
424462
context 'with release is empty string' do
425463
let(:params) { { location: 'hello.there', release: '' } }
426464

427-
it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there main}) }
465+
it { is_expected.to raise_error(Puppet::Error, %r{expects a value of type Undef or String\[1\], got String}) }
428466
end
429467

430468
context 'with invalid pin' do

0 commit comments

Comments
 (0)