Skip to content

Commit 06207c3

Browse files
authored
Merge pull request #1051 from puppetlabs/maint-move_apt_mark_to_provider
Harden apt-mark defined type
2 parents 4b12e7b + 79bec3d commit 06207c3

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

examples/mark.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apt::mark { 'vim':
2+
setting => 'auto',
3+
}

manifests/mark.pp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,31 @@
88
define apt::mark (
99
Enum['auto','manual','hold','unhold'] $setting,
1010
) {
11-
case $setting {
12-
'unhold': {
13-
$unless_cmd = undef
14-
}
15-
default: {
16-
$unless_cmd = "/usr/bin/apt-mark show${setting} ${title} | /bin/fgrep -qs ${title}"
17-
}
11+
if $title !~ /^[a-zA-Z0-9\-_]+$/ {
12+
fail("Invalid package name: ${title}")
1813
}
19-
exec { "/usr/bin/apt-mark ${setting} ${title}":
20-
onlyif => "/usr/bin/dpkg -l ${title}",
21-
unless => $unless_cmd,
14+
15+
if $setting == 'unhold' {
16+
$unless_cmd = undef
17+
} else {
18+
$action = "show${setting}"
19+
20+
# It would be ideal if we could break out this command in to an array of args, similar
21+
# to $onlyif_cmd and $command. However, in this case it wouldn't work as expected due
22+
# to the inclusion of a pipe character.
23+
# When passed to the exec function, the posix provider will strip everything to the right of the pipe,
24+
# causing the command to return a full list of packages for the given action.
25+
# The trade off is to use an interpolated string knowing that action is built from an enum value and
26+
# title is pre-validated.
27+
$unless_cmd = ["/usr/bin/apt-mark ${action} ${title} | grep ${title} -q"]
28+
}
29+
30+
$onlyif_cmd = [['/usr/bin/dpkg', '-l', $title]]
31+
$command = ['/usr/bin/apt-mark', $setting, $title]
32+
33+
exec { "apt-mark ${setting} ${title}":
34+
command => $command,
35+
onlyif => $onlyif_cmd,
36+
unless => $unless_cmd,
2237
}
2338
}

spec/defines/mark_spec.rb

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
end
3333

3434
it {
35-
is_expected.to contain_exec('/usr/bin/apt-mark manual my_source')
35+
is_expected.to contain_exec('apt-mark manual my_source')
3636
}
3737
end
3838

@@ -47,4 +47,50 @@
4747
is_expected.to raise_error(Puppet::PreformattedError, %r{expects a match for Enum\['auto', 'hold', 'manual', 'unhold'\], got 'foobar'})
4848
end
4949
end
50+
51+
[
52+
'package',
53+
'package1',
54+
'package_name',
55+
'package-name',
56+
].each do |value|
57+
describe 'with a valid resource title' do
58+
let :title do
59+
value
60+
end
61+
62+
let :params do
63+
{
64+
'setting' => 'manual',
65+
}
66+
end
67+
68+
it do
69+
is_expected.to contain_exec("apt-mark manual #{title}")
70+
end
71+
end
72+
end
73+
74+
[
75+
'|| ls -la ||',
76+
'packakge with space',
77+
'package<>|',
78+
'|| touch /tmp/foo.txt ||',
79+
].each do |value|
80+
describe 'with an invalid resource title' do
81+
let :title do
82+
value
83+
end
84+
85+
let :params do
86+
{
87+
'setting' => 'manual',
88+
}
89+
end
90+
91+
it do
92+
is_expected.to raise_error(Puppet::PreformattedError, %r{Invalid package name: #{title}})
93+
end
94+
end
95+
end
5096
end

0 commit comments

Comments
 (0)