1 # frozen_string_literal: true
5 describe Puppet::Type.type(:apt_key).provider(:apt_key) do
6 describe 'instances' do
7 it 'has an instance method' do
8 expect(described_class).to respond_to :instances
12 describe 'prefetch' do
13 it 'has a prefetch method' do
14 expect(described_class).to respond_to :prefetch
18 context 'self.instances no key' do
20 # Unable to remove `master` from below terminology as it relies on outside code
21 allow(described_class).to receive(:apt_key).with(
22 ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
23 ).and_return('uid:-::::1284991450::07BEBE04F4AE4A8E885A761325717D8509D9C1DC::Ubuntu Extras Archive Automatic Signing Key <ftpmaster@ubuntu.com>::::::::::0:')
26 it 'returns no resources' do
27 expect(described_class.instances.size).to eq(0)
31 context 'self.instances multiple keys' do
33 command_output = <<~OUTPUT
34 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
35 tru:t:1:1549900774:0:3:1:5
36 pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:
37 fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:
38 uid:-::::1095016255::B84AE656F4F5A826C273A458512EF8E282754CE1::Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>:
39 sub:-:2048:16:251BEFF479164387:1095016263::::::e:
40 pub:-:1024:17:46181433FBB75451:1104433784:::-:::scSC:
41 fpr:::::::::C5986B4F1257FFA86632CBA746181433FBB75451:
43 allow(described_class).to receive(:apt_key).with(
44 ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
45 ).and_return(command_output)
48 it 'returns 2 resources' do # rubocop:disable RSpec/MultipleExpectations
49 expect(described_class.instances.size).to eq(2)
50 expect(described_class.instances[0].name).to eq('630239CC130E1A7FD81A27B140976EAF437D05B5')
51 expect(described_class.instances[0].id).to eq('40976EAF437D05B5')
52 expect(described_class.instances[1].name).to eq('C5986B4F1257FFA86632CBA746181433FBB75451')
53 expect(described_class.instances[1].id).to eq('46181433FBB75451')
57 context 'create apt_key resource' do
58 it 'apt_key with content set and source nil' do
59 expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
61 :"keyserver.ubuntu.com",
64 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
68 provider = described_class.new(resource)
69 expect(provider).not_to be_exist
71 expect(provider).to be_exist
74 it 'apt_key content and source nil, options set' do
75 expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
77 :"keyserver.ubuntu.com",
78 '--keyserver-options',
82 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
87 provider = described_class.new(resource)
88 expect(provider).not_to be_exist
90 expect(provider).to be_exist
93 it 'apt_key with content set' do # rubocop:disable RSpec/MultipleExpectations
94 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
95 resource = Puppet::Type::Apt_key.new(name: 'gsd',
100 provider = described_class.new(resource)
101 expect(provider).not_to be_exist
102 expect(provider).to receive(:tempfile).and_return(Tempfile.new('foo'))
104 expect(provider).to be_exist
107 it 'apt_key with source set' do # rubocop:disable RSpec/MultipleExpectations
108 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
109 resource = Puppet::Type::Apt_key.new(name: 'gsd',
111 source: 'ftp://bla/herpderp.gpg',
114 provider = described_class.new(resource)
115 expect(provider).not_to be_exist
116 expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
118 expect(provider).to be_exist
121 it 'apt_key with source and weak ssl verify set' do # rubocop:disable RSpec/MultipleExpectations
122 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
123 resource = Puppet::Type::Apt_key.new(name: 'gsd',
125 source: 'https://bla/herpderp.gpg',
129 provider = described_class.new(resource)
130 expect(provider).not_to be_exist
131 expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
133 expect(provider).to be_exist
136 describe 'different valid id keys' do
138 '32bit key id' => 'EF8D349F',
139 '64bit key id' => '7F438280EF8D349F',
140 '160bit key fingerprint' => '6F6B15509CF8E59E6E469F327F438280EF8D349F',
141 '32bit key id lowercase' => 'EF8D349F'.downcase,
142 '64bit key id lowercase' => '7F438280EF8D349F'.downcase,
143 '160bit key fingerprint lowercase' => '6F6B15509CF8E59E6E469F327F438280EF8D349F'.downcase,
144 '32bit key id 0x formatted' => '0xEF8D349F',
145 '64bit key id 0x formatted' => '0x7F438280EF8D349F',
146 '160bit key fingerprint 0x formatted' => '0x6F6B15509CF8E59E6E469F327F438280EF8D349F',
148 hash_of_keys.each do |key_type, value|
149 it "#{key_type} #{value} is valid" do
150 expect(described_class).to receive(:apt_key).with(array_including('adv', '--no-tty',
152 :"keyserver.ubuntu.com",
154 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
158 provider = described_class.new(resource)
159 expect(provider).not_to be_exist
161 expect(provider).to be_exist
166 it 'apt_key with invalid key length' do
168 Puppet::Type::Apt_key.new(name: 'source and content nil',
171 }.to raise_error(Puppet::ResourceError, %r{Parameter id failed on Apt_key})
175 context 'key_line_hash function' do
177 expect(described_class.key_line_hash('pub:-:1024:1:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
179 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
180 key_long: '40976EAF437D05B5',
181 key_short: '437D05B5',
188 expect(described_class.key_line_hash('pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
190 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
191 key_long: '40976EAF437D05B5',
192 key_short: '437D05B5',
199 expect(described_class.key_line_hash('pub:-:1024:18:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
201 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
202 key_long: '40976EAF437D05B5',
203 key_short: '437D05B5',
209 it 'matches ecdsa' do
210 expect(described_class.key_line_hash('pub:-:1024:19:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
212 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
213 key_long: '40976EAF437D05B5',
214 key_short: '437D05B5',