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:')
25 it 'returns no resources' do
26 expect(described_class.instances.size).to eq(0)
30 context 'self.instances multiple keys' do
32 command_output = <<-OUTPUT
33 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
34 tru:t:1:1549900774:0:3:1:5
35 pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:
36 fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:
37 uid:-::::1095016255::B84AE656F4F5A826C273A458512EF8E282754CE1::Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>:
38 sub:-:2048:16:251BEFF479164387:1095016263::::::e:
39 pub:-:1024:17:46181433FBB75451:1104433784:::-:::scSC:
40 fpr:::::::::C5986B4F1257FFA86632CBA746181433FBB75451:
42 allow(described_class).to receive(:apt_key).with(
43 ['adv', '--no-tty', '--list-keys', '--with-colons', '--fingerprint', '--fixed-list-mode'],
44 ).and_return(command_output)
46 it 'returns 2 resources' do
47 expect(described_class.instances.size).to eq(2)
48 expect(described_class.instances[0].name).to eq('630239CC130E1A7FD81A27B140976EAF437D05B5')
49 expect(described_class.instances[0].id).to eq('40976EAF437D05B5')
50 expect(described_class.instances[1].name).to eq('C5986B4F1257FFA86632CBA746181433FBB75451')
51 expect(described_class.instances[1].id).to eq('46181433FBB75451')
55 context 'create apt_key resource' do
56 it 'apt_key with content set and source nil' do
57 expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
59 :"keyserver.ubuntu.com",
62 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
66 provider = described_class.new(resource)
67 expect(provider).not_to be_exist
69 expect(provider).to be_exist
72 it 'apt_key content and source nil, options set' do
73 expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
75 :"keyserver.ubuntu.com",
76 '--keyserver-options',
80 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
85 provider = described_class.new(resource)
86 expect(provider).not_to be_exist
88 expect(provider).to be_exist
91 it 'apt_key with content set' do
92 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
93 resource = Puppet::Type::Apt_key.new(name: 'gsd',
98 provider = described_class.new(resource)
99 expect(provider).not_to be_exist
100 expect(provider).to receive(:tempfile).and_return(Tempfile.new('foo'))
102 expect(provider).to be_exist
105 it 'apt_key with source set' do
106 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
107 resource = Puppet::Type::Apt_key.new(name: 'gsd',
109 source: 'ftp://bla/herpderp.gpg',
112 provider = described_class.new(resource)
113 expect(provider).not_to be_exist
114 expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
116 expect(provider).to be_exist
119 it 'apt_key with source and weak ssl verify set' do
120 expect(described_class).to receive(:apt_key).with(array_including('add', kind_of(String)))
121 resource = Puppet::Type::Apt_key.new(name: 'gsd',
123 source: 'https://bla/herpderp.gpg',
127 provider = described_class.new(resource)
128 expect(provider).not_to be_exist
129 expect(provider).to receive(:source_to_file).and_return(Tempfile.new('foo'))
131 expect(provider).to be_exist
134 describe 'different valid id keys' do
136 '32bit key id' => 'EF8D349F',
137 '64bit key id' => '7F438280EF8D349F',
138 '160bit key fingerprint' => '6F6B15509CF8E59E6E469F327F438280EF8D349F',
139 '32bit key id lowercase' => 'EF8D349F'.downcase,
140 '64bit key id lowercase' => '7F438280EF8D349F'.downcase,
141 '160bit key fingerprint lowercase' => '6F6B15509CF8E59E6E469F327F438280EF8D349F'.downcase,
142 '32bit key id 0x formatted' => '0xEF8D349F',
143 '64bit key id 0x formatted' => '0x7F438280EF8D349F',
144 '160bit key fingerprint 0x formatted' => '0x6F6B15509CF8E59E6E469F327F438280EF8D349F',
146 hash_of_keys.each do |key_type, value|
147 it "#{key_type} #{value} is valid" do
148 expect(described_class).to receive(:apt_key).with(array_including('adv', '--no-tty',
150 :"keyserver.ubuntu.com",
152 resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
156 provider = described_class.new(resource)
157 expect(provider).not_to be_exist
159 expect(provider).to be_exist
164 it 'apt_key with invalid key length' do
166 Puppet::Type::Apt_key.new(name: 'source and content nil',
169 }.to raise_error(Puppet::ResourceError, %r{Parameter id failed on Apt_key})
173 context 'key_line_hash function' do
175 expect(described_class.key_line_hash('pub:-:1024:1:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
177 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
178 key_long: '40976EAF437D05B5',
179 key_short: '437D05B5',
186 expect(described_class.key_line_hash('pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
188 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
189 key_long: '40976EAF437D05B5',
190 key_short: '437D05B5',
197 expect(described_class.key_line_hash('pub:-:1024:18:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
199 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
200 key_long: '40976EAF437D05B5',
201 key_short: '437D05B5',
207 it 'matches ecdsa' do
208 expect(described_class.key_line_hash('pub:-:1024:19:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
210 key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
211 key_long: '40976EAF437D05B5',
212 key_short: '437D05B5',