Skip to content

Commit 4732c3f

Browse files
authored
Merge pull request #27 from ciprianbadescu/MODULES-7613/use_composite_namevars
(MODULES-7613) use name and type as composite namevar
2 parents 9f710d8 + 92a734a commit 4732c3f

File tree

9 files changed

+251
-21
lines changed

9 files changed

+251
-21
lines changed

lib/puppet/provider/sshkey/parsed.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ def self.default_target
4646
'/etc/ssh/ssh_known_hosts'
4747
end
4848
end
49+
50+
def self.resource_for_record(record, resources)
51+
name = "#{record[:name]}@#{record[:type]}"
52+
resources[name]
53+
end
4954
end

lib/puppet/type/sshkey.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,33 @@ module Puppet
88

99
ensurable
1010

11-
newproperty(:type) do
11+
def name
12+
"#{self[:name]}@#{self[:type]}"
13+
end
14+
15+
def self.title_patterns
16+
[
17+
[
18+
%r{^(.*)@(.*)$},
19+
[
20+
[:name],
21+
[:type],
22+
],
23+
],
24+
[
25+
%r{^([^@]+)$},
26+
[
27+
[:name],
28+
],
29+
],
30+
]
31+
end
32+
33+
newparam(:type) do
1234
desc 'The encryption type used. Probably ssh-dss or ssh-rsa.'
1335

36+
isnamevar
37+
1438
newvalues :'ssh-dss', :'ssh-ed25519', :'ssh-rsa', :'ecdsa-sha2-nistp256', :'ecdsa-sha2-nistp384', :'ecdsa-sha2-nistp521'
1539

1640
aliasvalue(:dsa, :'ssh-dss')

spec/acceptance/tests/resource/ssh_authorized_key/destroy_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper_acceptance'
22

3-
RSpec.context 'sshkeys: Destroy' do
3+
RSpec.context 'ssh_authorized_key: Destroy' do
44
confine :except, platform: ['windows']
55

66
let(:auth_keys) { '~/.ssh/authorized_keys' }

spec/acceptance/tests/resource/ssh_authorized_key/modify_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper_acceptance'
22

3-
RSpec.context 'sshkeys: Modify' do
3+
RSpec.context 'ssh_authorized_key: Modify' do
44
let(:auth_keys) { '~/.ssh/authorized_keys' }
55
let(:name) { "pl#{rand(999_999).to_i}" }
66
let(:custom_key_directory) { "/etc/ssh_authorized_keys_#{name}" }

spec/acceptance/tests/resource/sshkey/create_spec.rb

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44
let(:keyname) { "pl#{rand(999_999).to_i}" }
55

66
# FIXME: This is bletcherous
7-
let(:macos_version) { fact_on(agent, 'os.macosx.version.major') }
8-
let(:ssh_known_hosts) do
9-
if ['10.9', '10.10'].include? macos_version
10-
'/etc/ssh_known_hosts'
11-
else
12-
'/etc/ssh/ssh_known_hosts'
13-
end
14-
end
7+
let(:ssh_known_hosts) { '/etc/ssh/ssh_known_hosts' }
158

169
before(:each) do
17-
osx_agents.each do |agent|
10+
posix_agents.agents.each do |agent|
1811
# The 'cp' might fail because the source file doesn't exist
1912
on(
2013
agent,
@@ -25,7 +18,7 @@
2518
end
2619

2720
after(:each) do
28-
osx_agents.each do |agent|
21+
posix_agents.each do |agent|
2922
# Is it present?
3023
rc = on(
3124
agent,
@@ -51,8 +44,8 @@
5144
end
5245
end
5346

54-
osx_agents.each do |agent|
55-
it "#{agent} should add an SSH key to the correct ssh_known_hosts file on OS X/macOS (PUP-5508)" do
47+
posix_agents.each do |agent|
48+
it "#{agent} should add an SSH key to the correct ssh_known_hosts file (OS X/macOS - PUP-5508)" do
5649
# Is it even there?
5750
rc = on(
5851
agent,
@@ -78,4 +71,37 @@
7871
end
7972
end
8073
end
74+
75+
posix_agents.each do |agent|
76+
it "#{agent} should allow to add two different type keys for the same host" do
77+
# Is it even there?
78+
rc = on(
79+
agent,
80+
"[ ! -e #{ssh_known_hosts} ]",
81+
acceptable_exit_codes: [0, 1],
82+
)
83+
if rc.exit_code == 1
84+
# If it's there, it should be empty
85+
on(agent, "cat #{ssh_known_hosts}") do |_res|
86+
expect(stdout).to be_empty
87+
end
88+
end
89+
on agent, puppet('apply'), stdin: <<MANIFEST
90+
sshkey { '#{keyname}@ssh-rsa':
91+
ensure => 'present',
92+
key => 'how_about_the_rsa_key_of_c',
93+
}
94+
95+
sshkey { '#{keyname}@ssh-dss':
96+
ensure => 'present',
97+
key => 'how_about_the_dss_key_of_c',
98+
}
99+
MANIFEST
100+
101+
on(agent, "cat #{ssh_known_hosts}") do |_rc|
102+
expect(stdout).to include("#{keyname} ssh-rsa")
103+
expect(stdout).to include("#{keyname} ssh-dss")
104+
end
105+
end
106+
end
81107
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require 'spec_helper_acceptance'
2+
3+
RSpec.context 'sshkeys: Destroy' 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+
before(:each) do
10+
posix_agents.agents.each do |agent|
11+
# The 'cp' might fail because the source file doesn't exist
12+
on(
13+
agent,
14+
"cp -fv #{ssh_known_hosts} /tmp/ssh_known_hosts",
15+
acceptable_exit_codes: [0, 1],
16+
)
17+
cmd = <<-CMD
18+
echo '' > #{ssh_known_hosts}
19+
echo '#{keyname} ssh-rsa how_about_the_initial_rsa_key_of_c' >> #{ssh_known_hosts}
20+
echo '#{keyname} ssh-dss how_about_the_initial_dss_key_of_c' >> #{ssh_known_hosts}
21+
CMD
22+
on(agent, cmd)
23+
end
24+
end
25+
26+
after(:each) do
27+
posix_agents.each do |agent|
28+
# Is it present?
29+
rc = on(
30+
agent,
31+
'[ -e /tmp/ssh_known_hosts ]',
32+
accept_all_exit_codes: true,
33+
)
34+
if rc.exit_code == 0
35+
# It's present, so restore the original
36+
on(
37+
agent,
38+
"mv -fv /tmp/ssh_known_hosts #{ssh_known_hosts}",
39+
accept_all_exit_codes: true,
40+
)
41+
else
42+
# It's missing, which means there wasn't one to backup; just
43+
# delete the one we laid down
44+
on(
45+
agent,
46+
"rm -fv #{ssh_known_hosts}",
47+
accept_all_exit_codes: true,
48+
)
49+
end
50+
end
51+
end
52+
53+
posix_agents.each do |agent|
54+
it "#{agent} should delete an rsa entry for an SSH known host key" do
55+
args = ['ensure=absent',
56+
"type='rsa'"]
57+
on(agent, puppet_resource('sshkey', keyname.to_s, args))
58+
59+
on(agent, "cat #{ssh_known_hosts}") do |_res|
60+
expect(stdout).not_to include('how_about_the_initial_rsa_key_of_c')
61+
end
62+
end
63+
64+
it "#{agent} should delete an dss entry for an SSH known host key" do
65+
args = ['ensure=absent',
66+
"type='ssh-dss'"]
67+
on(agent, puppet_resource('sshkey', keyname.to_s, args))
68+
69+
on(agent, "cat #{ssh_known_hosts}") do |_res|
70+
expect(stdout).not_to include('how_about_the_initial_dss_key_of_c')
71+
end
72+
end
73+
end
74+
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'spec_helper_acceptance'
2+
3+
RSpec.context 'sshkeys: Modify' 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+
before(:each) do
10+
posix_agents.agents.each do |agent|
11+
# The 'cp' might fail because the source file doesn't exist
12+
on(
13+
agent,
14+
"cp -fv #{ssh_known_hosts} /tmp/ssh_known_hosts",
15+
acceptable_exit_codes: [0, 1],
16+
)
17+
cmd = <<-CMD
18+
echo '' > #{ssh_known_hosts}
19+
echo '#{keyname} ssh-rsa how_about_the_initial_rsa_key_of_c' >> #{ssh_known_hosts}
20+
echo '#{keyname} ssh-dss how_about_the_initial_dss_key_of_c' >> #{ssh_known_hosts}
21+
CMD
22+
on(agent, cmd)
23+
end
24+
end
25+
26+
after(:each) do
27+
posix_agents.each do |agent|
28+
# Is it present?
29+
rc = on(
30+
agent,
31+
'[ -e /tmp/ssh_known_hosts ]',
32+
accept_all_exit_codes: true,
33+
)
34+
if rc.exit_code == 0
35+
# It's present, so restore the original
36+
on(
37+
agent,
38+
"mv -fv /tmp/ssh_known_hosts #{ssh_known_hosts}",
39+
accept_all_exit_codes: true,
40+
)
41+
else
42+
# It's missing, which means there wasn't one to backup; just
43+
# delete the one we laid down
44+
on(
45+
agent,
46+
"rm -fv #{ssh_known_hosts}",
47+
accept_all_exit_codes: true,
48+
)
49+
end
50+
end
51+
end
52+
53+
posix_agents.each do |agent|
54+
it "#{agent} should update an rsa entry for an SSH known host key" do
55+
args = ['ensure=present',
56+
"type='rsa'",
57+
"key='how_about_the_updated_rsa_key_of_c'"]
58+
on(agent, puppet_resource('sshkey', keyname.to_s, args))
59+
60+
on(agent, "cat #{ssh_known_hosts}") do |_res|
61+
expect(stdout).to include('how_about_the_updated_rsa_key_of_c')
62+
expect(stdout).not_to include('how_about_the_initial_rsa_key_of_c')
63+
end
64+
end
65+
66+
it "#{agent} should update an dss entry for an SSH known host key" do
67+
args = ['ensure=present',
68+
"type='ssh-dss'",
69+
"key='how_about_the_updated_dss_key_of_c'"]
70+
on(agent, puppet_resource('sshkey', keyname.to_s, args))
71+
72+
on(agent, "cat #{ssh_known_hosts}") do |_res|
73+
expect(stdout).to include('how_about_the_updated_dss_key_of_c')
74+
expect(stdout).not_to include('how_about_the_initial_dss_key_of_c')
75+
end
76+
end
77+
end
78+
end

spec/integration/provider/sshkey_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,29 @@
5252
expect(File.read(sshkey_file)).to match(%r{#{super_unique}.*mykey})
5353
end
5454

55+
it 'creates two SSH host key entries with two keys (ensure present)' do
56+
manifest = "
57+
#{type_under_test} { '#{super_unique}_rsa':
58+
ensure => 'present',
59+
type => 'rsa',
60+
name => '#{super_unique}',
61+
key => 'myrsakey',
62+
target => '#{sshkey_file}', }
63+
#{type_under_test} { '#{super_unique}_dss':
64+
ensure => 'present',
65+
type => 'ssh-dss',
66+
name => '#{super_unique}',
67+
key => 'mydsskey',
68+
target => '#{sshkey_file}' }"
69+
apply_with_error_check(manifest)
70+
expect(File.read(sshkey_file)).to match(%r{#{super_unique}.*myrsakey})
71+
expect(File.read(sshkey_file)).to match(%r{#{super_unique}.*mydsskey})
72+
end
73+
5574
it 'deletes an entry for an SSH host key' do
5675
manifest = "#{type_under_test} { '#{sshkey_name}':
5776
ensure => 'absent',
77+
type => 'rsa',
5878
target => '#{sshkey_file}' }"
5979
apply_with_error_check(manifest)
6080
expect(File.read(sshkey_file)).not_to match(%r{#{sshkey_name}.*Yqk0=})
@@ -121,6 +141,7 @@
121141
it 'updates an entry with a single new host_alias' do
122142
manifest = "#{type_under_test} { '#{sshkey_name}':
123143
ensure => 'present',
144+
type => 'rsa',
124145
host_aliases => '#{host_alias}',
125146
target => '#{sshkey_file}' }"
126147
apply_with_error_check(manifest)
@@ -132,6 +153,7 @@
132153
it 'updates an entry with multiple new host_aliases' do
133154
manifest = "#{type_under_test} { '#{sshkey_name}':
134155
ensure => 'present',
156+
type => 'rsa',
135157
host_aliases => [ 'r0ckdata.com', 'erict.net' ],
136158
target => '#{sshkey_file}' }"
137159
apply_with_error_check(manifest)

spec/unit/type/sshkey_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
require 'spec_helper'
2+
require 'pry'
23

34
describe Puppet::Type.type(:sshkey) do
4-
it 'uses :name as its namevar' do
5-
expect(described_class.key_attributes).to eq [:name]
5+
it 'uses :name and :type as its namevar' do
6+
expect(described_class.key_attributes).to eq [:type, :name]
67
end
78

89
describe 'when validating attributes' do
9-
[:name, :provider].each do |param|
10+
[:name, :provider, :type].each do |param|
1011
it "has a #{param} parameter" do
1112
expect(described_class.attrtype(param)).to eq :param
1213
end
1314
end
1415

15-
[:host_aliases, :ensure, :key, :type].each do |property|
16+
[:host_aliases, :ensure, :key].each do |property|
1617
it "has a #{property} property" do
1718
expect(described_class.attrtype(property)).to eq :property
1819
end
@@ -35,12 +36,12 @@
3536

3637
it 'aliases :rsa to :ssh-rsa' do
3738
key = described_class.new(name: 'foo', type: :rsa)
38-
expect(key.should(:type)).to eq :'ssh-rsa'
39+
expect(key.parameter(:type).value).to eq :'ssh-rsa'
3940
end
4041

4142
it 'aliases :dsa to :ssh-dss' do
4243
key = described_class.new(name: 'foo', type: :dsa)
43-
expect(key.should(:type)).to eq :'ssh-dss'
44+
expect(key.parameter(:type).value).to eq :'ssh-dss'
4445
end
4546

4647
it "doesn't support values other than ssh-dss, ssh-rsa, dsa, rsa for type" do

0 commit comments

Comments
 (0)