Skip to content

Commit 2e059d4

Browse files
committed
(PUP-10510) Fix sshkeys not being correctly purged
After adding support for composite namevars in version 2.0.0, the module lost the ability to purge sshkeys. This happens due to Puppet being unable to correctly match the names and types of the sshkeys to be purged. Part of the fix was done in puppetlabs/puppet#8174, which changes how a resource is initialized if the provider implements a `title` method. Additionally, we add the key name and type to be included in the output shown by `puppet resource`.
1 parent 9b2d2aa commit 2e059d4

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lib/puppet/provider/sshkey/parsed.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def self.default_mode
2828
0o644
2929
end
3030

31+
def title
32+
"#{property_hash[:name]}@#{property_hash[:type]}"
33+
end
34+
3135
def self.default_target
3236
case Facter.value(:operatingsystem)
3337
when 'Darwin'

lib/puppet/type/sshkey.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def name
1212
"#{self[:name]}@#{self[:type]}"
1313
end
1414

15+
def self.parameters_to_include
16+
[:name, :type]
17+
end
18+
1519
def self.title_patterns
1620
[
1721
[
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'spec_helper_acceptance'
2+
3+
RSpec.context 'sshkeys: Purge' do
4+
let(:keyname) { "pl#{rand(999_999).to_i}" }
5+
6+
# FIXME: This is bletcherous
7+
let(:ssh_known_hosts) { '/etc/ssh/ssh_known_hosts' }
8+
9+
let(:purge_manifest) do
10+
<<-MANIFEST
11+
resources { 'sshkey':
12+
purge => true,
13+
}
14+
MANIFEST
15+
end
16+
17+
before(:each) do
18+
posix_agents.agents.each do |agent|
19+
# The 'cp' might fail because the source file doesn't exist
20+
on(
21+
agent,
22+
"cp -fv #{ssh_known_hosts} /tmp/ssh_known_hosts",
23+
acceptable_exit_codes: [0, 1],
24+
)
25+
cmd = <<-CMD
26+
echo '' > #{ssh_known_hosts}
27+
echo '#{keyname} ssh-rsa how_about_the_initial_rsa_key_of_c' >> #{ssh_known_hosts}
28+
echo '#{keyname} ssh-dss how_about_the_initial_dss_key_of_c' >> #{ssh_known_hosts}
29+
CMD
30+
on(agent, cmd)
31+
end
32+
end
33+
34+
after(:each) do
35+
posix_agents.each do |agent|
36+
# Is it present?
37+
rc = on(
38+
agent,
39+
'[ -e /tmp/ssh_known_hosts ]',
40+
accept_all_exit_codes: true,
41+
)
42+
if rc.exit_code == 0
43+
# It's present, so restore the original
44+
on(
45+
agent,
46+
"mv -fv /tmp/ssh_known_hosts #{ssh_known_hosts}",
47+
accept_all_exit_codes: true,
48+
)
49+
else
50+
# It's missing, which means there wasn't one to backup; just
51+
# delete the one we laid down
52+
on(
53+
agent,
54+
"rm -fv #{ssh_known_hosts}",
55+
accept_all_exit_codes: true,
56+
)
57+
end
58+
end
59+
end
60+
61+
posix_agents.each do |agent|
62+
it "#{agent} should be able to purge all SSH known host keys" do
63+
apply_manifest_on(agent, purge_manifest, catch_failures: true)
64+
65+
# expect purging to be idempotent
66+
apply_manifest_on(agent, purge_manifest, catch_changes: true)
67+
68+
on(agent, "cat #{ssh_known_hosts}") do |_res|
69+
expect(stdout).not_to include('how_about_the_initial')
70+
end
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)