7b87017810ede129f731ac161cb6ae8dc53829fd
[puppet-modules/puppetlabs-apt.git] / spec / unit / puppet / provider / apt_key_spec.rb
1 require 'spec_helper'
2
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
7     end
8   end
9
10   describe 'prefetch' do
11     it 'has a prefetch method' do
12       expect(described_class).to respond_to :prefetch
13     end
14   end
15
16   context 'self.instances no key' do
17     before :each 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:')
22     end
23     it 'returns no resources' do
24       expect(described_class.instances.size).to eq(0)
25     end
26   end
27
28   context 'self.instances multiple keys' do
29     before :each 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:
39 OUTPUT
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)
43     end
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')
50     end
51   end
52
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',
56                                                          '--keyserver',
57                                                          :"keyserver.ubuntu.com",
58                                                          '--recv-keys',
59                                                          'C105B9DE'])
60       resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
61                                            id: 'C105B9DE',
62                                            ensure: 'present')
63
64       provider = described_class.new(resource)
65       expect(provider).not_to be_exist
66       provider.create
67       expect(provider).to be_exist
68     end
69
70     it 'apt_key content and source nil, options set' do
71       expect(described_class).to receive(:apt_key).with(['adv', '--no-tty',
72                                                          '--keyserver',
73                                                          :"keyserver.ubuntu.com",
74                                                          '--keyserver-options',
75                                                          'jimno',
76                                                          '--recv-keys',
77                                                          'C105B9DE'])
78       resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
79                                            id: 'C105B9DE',
80                                            options: 'jimno',
81                                            ensure: 'present')
82
83       provider = described_class.new(resource)
84       expect(provider).not_to be_exist
85       provider.create
86       expect(provider).to be_exist
87     end
88
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',
92                                            id: 'C105B9DE',
93                                            content: 'asad',
94                                            ensure: 'present')
95
96       provider = described_class.new(resource)
97       expect(provider).not_to be_exist
98       expect(provider).to receive(:tempfile).and_return(Tempfile.new('foo'))
99       provider.create
100       expect(provider).to be_exist
101     end
102
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',
106                                            id: 'C105B9DE',
107                                            source: 'ftp://bla/herpderp.gpg',
108                                            ensure: 'present')
109
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'))
113       provider.create
114       expect(provider).to be_exist
115     end
116
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',
120                                            id: 'C105B9DE',
121                                            source: 'https://bla/herpderp.gpg',
122                                            ensure: 'present',
123                                            weak_ssl: true)
124
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'))
128       provider.create
129       expect(provider).to be_exist
130     end
131
132     describe 'different valid id keys' do
133       hash_of_keys = {
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',
143       }
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',
147                                                                             '--keyserver',
148                                                                             :"keyserver.ubuntu.com",
149                                                                             '--recv-keys'))
150           resource = Puppet::Type::Apt_key.new(name: 'source and content nil',
151                                                id: value,
152                                                ensure: 'present')
153
154           provider = described_class.new(resource)
155           expect(provider).not_to be_exist
156           provider.create
157           expect(provider).to be_exist
158         end
159       end
160     end
161
162     it 'apt_key with invalid key length' do
163       expect {
164         Puppet::Type::Apt_key.new(name: 'source and content nil',
165                                   id: '1',
166                                   ensure: 'present')
167       }.to raise_error(Puppet::ResourceError, %r{Parameter id failed on Apt_key})
168     end
169   end
170
171   context 'key_line_hash function' do
172     it 'matches rsa' do
173       expect(described_class.key_line_hash('pub:-:1024:1:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
174         key_expiry: nil,
175         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
176         key_long: '40976EAF437D05B5',
177         key_short: '437D05B5',
178         key_size: '1024',
179         key_type: :rsa,
180       )
181     end
182
183     it 'matches dsa' do
184       expect(described_class.key_line_hash('pub:-:1024:17:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
185         key_expiry: nil,
186         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
187         key_long: '40976EAF437D05B5',
188         key_short: '437D05B5',
189         key_size: '1024',
190         key_type: :dsa,
191       )
192     end
193
194     it 'matches ecc' do
195       expect(described_class.key_line_hash('pub:-:1024:18:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
196         key_expiry: nil,
197         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
198         key_long: '40976EAF437D05B5',
199         key_short: '437D05B5',
200         key_size: '1024',
201         key_type: :ecc,
202       )
203     end
204
205     it 'matches ecdsa' do
206       expect(described_class.key_line_hash('pub:-:1024:19:40976EAF437D05B5:1095016255:::-:::scESC:', 'fpr:::::::::630239CC130E1A7FD81A27B140976EAF437D05B5:')).to include(
207         key_expiry: nil,
208         key_fingerprint: '630239CC130E1A7FD81A27B140976EAF437D05B5',
209         key_long: '40976EAF437D05B5',
210         key_short: '437D05B5',
211         key_size: '1024',
212         key_type: :ecdsa,
213       )
214     end
215   end
216 end