3 describe Puppet::Type.type(:apt_key).provider(:apt_key) do
4 describe 'instances' do
5 it 'has an instance method' do
6 expect(described_class).to respond_to :instances
10 describe 'prefetch' do
11 it 'has a prefetch method' do
12 expect(described_class).to respond_to :prefetch
16 context 'self.instances no key' do
18 # Unable to remove `master` from below terminology as it relies on outside code
19 allow(described_class).to receive(:apt_key).with(
20 ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
21 ).and_return('uid:-::::1284991450::07BEBE04F4AE4A8E885A761325717D8509D9C1DC::Ubuntu Extras Archive Automatic Signing Key <ftpmaster@ubuntu.com>::::::::::0:')
23 it 'returns no resources' do
24 expect(described_class.instances.size).to eq(0)
28 context 'self.instances multiple keys' do
30 command_output = <<-OUTPUT
31 Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.DU0GdRxjmE --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyring /etc/apt/trusted.gpg.d/puppetlabs-pc1-keyring.gpg --no-tty --list-keys --with-colons --fingerprint --fixed-list-mode
32 tru:t:1:1549900774:0:3:1:5
33 pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:
34 fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:
35 uid:-::::1095016255::B84AE656F4F5A826C273A458512EF8E282754CE1::Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>:
36 sub:-:2048:16:251BEFF479164387:1095016263::::::e:
37 pub:-:1024:17:46181433FBB75451:1104433784:::-:::scSC:
38 fpr:::::::::C5986B4F1257FFA86632CBA746181433FBB75451:
40 allow(described_class).to receive(:apt_key).with(
41 ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
42 ).and_return(command_output)
44 it 'returns 2 resources' do
45 expect(described_class.instances.size).to eq(2)
46 expect(described_class.instances[0].name).to eq('630239CC130E1A7FD81A27B140976EAF437D05B5')
47 expect(described_class.instances[0].id).to eq('40976EAF437D05B5')
48 expect(described_class.instances[1].name).to eq('C5986B4F1257FFA86632CBA746181433FBB75451')
49 expect(described_class.instances[1].id).to eq('46181433FBB75451')
53 context 'create apt_key resource' do
54 it 'apt_key with content set and source nil' do
55 expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
57 :"keyserver.ubuntu.com",
60 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
64 provider = described_class.new(resource)
65 expect(provider).not_to be_exist
67 expect(provider).to be_exist
70 it 'apt_key content and source nil, options set' do
71 expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
73 :"keyserver.ubuntu.com",
74 '--keyserver-options',
78 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
83 provider = described_class.new(resource)
84 expect(provider).not_to be_exist
86 expect(provider).to be_exist
89 it 'apt_key with content set' do
90 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
91 resource = Puppet::Type::Apt_key.new(name: 'gsd',
96 provider = described_class.new(resource)
97 expect(provider).not_to be_exist
98 expect(provider).to receive(:tempfile).and_return(Tempfile.new('foo'))
100 expect(provider).to be_exist
103 it 'apt_key with source set' do
104 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
105 resource = Puppet::Type::Apt_key.new(name: 'gsd',
107 source: 'ftp://bla/herpderp.gpg',
110 provider = described_class.new(resource)
111 expect(provider).not_to be_exist
112 expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
114 expect(provider).to be_exist
117 it 'apt_key with source and weak ssl verify set' do
118 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
119 resource = Puppet::Type::Apt_key.new(name: 'gsd',
121 source: 'https://bla/herpderp.gpg',
125 provider = described_class.new(resource)
126 expect(provider).not_to be_exist
127 expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
129 expect(provider).to be_exist
132 describe 'different valid id keys' do
134 '32bit key id' => 'EF8D349F',
135 '64bit key id' => '7F438280EF8D349F',
136 '160bit key fingerprint' => '6F6B15509CF8E59E6E469F327F438280EF8D349F',
137 '32bit key id lowercase' => 'EF8D349F'.downcase,
138 '64bit key id lowercase' => '7F438280EF8D349F'.downcase,
139 '160bit key fingerprint lowercase' => '6F6B15509CF8E59E6E469F327F438280EF8D349F'.downcase,
140 '32bit key id 0x formatted' => '0xEF8D349F',
141 '64bit key id 0x formatted' => '0x7F438280EF8D349F',
142 '160bit key fingerprint 0x formatted' => '0x6F6B15509CF8E59E6E469F327F438280EF8D349F',
144 hash_of_keys.each do |key_type, value|
145 it "#{key_type} #{value} is valid" do
146 expect(described_class).to receive(:apt_key).with(array_including('adv', '--no-tty',
148 :"keyserver.ubuntu.com",
150 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
154 provider = described_class.new(resource)
155 expect(provider).not_to be_exist
157 expect(provider).to be_exist
162 it 'apt_key with invalid key length' do
164 Puppet::Type::Apt_key.new(name: 'source and content nil',
167 }.to raise_error(Puppet::ResourceError, %r{Parameter id failed on Apt_key})
171 context 'key_line_hash function' do
173 expect(described_class.key_line_hash('pub:-:1024:1:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
175 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
176 key_long: '40976EAF437D05B5',
177 key_short: '437D05B5',
184 expect(described_class.key_line_hash('pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
186 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
187 key_long: '40976EAF437D05B5',
188 key_short: '437D05B5',
195 expect(described_class.key_line_hash('pub:-:1024:18:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
197 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
198 key_long: '40976EAF437D05B5',
199 key_short: '437D05B5',
205 it 'matches ecdsa' do
206 expect(described_class.key_line_hash('pub:-:1024:19:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
208 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
209 key_long: '40976EAF437D05B5',
210 key_short: '437D05B5',