pub_line = nil
fpr_line = nil
- result = @apt_key_cmd.start_read(context, %w[adv --list-keys --with-colons --fingerprint --fixed-list-mode]) do |handle|
- handle.stdout.each_line.map { |line|
- line = line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
+ result = @apt_key_cmd.start_read(context, *%w[adv --list-keys --with-colons --fingerprint --fixed-list-mode]) do |process|
+ process.io.stdout.each_line.map { |line|
+ line = line.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').strip
if line.start_with?('pub')
pub_line = line
elsif line.start_with?('fpr')
# puts "debug: key_line_to_hash"
- hash = key_line_to_hash(pub_line, fpr_line)
+ hash = self.class.key_line_to_hash(pub_line, fpr_line)
# reset scanning
pub_line = nil
hash
}.compact!
-
- result
end
end
short: fingerprint[-8..-1], # last 8 characters of fingerprint
size: pub_split[2].to_i,
type: key_type,
- created: Time.at(pub_split[5].to_i),
- expiry: expiry,
+ created: Time.at(pub_split[5].to_i).to_s,
+ expiry: expiry.to_s,
expired: !!(expiry && Time.now >= expiry),
}
end
RSpec.describe Puppet::Provider::AptKey2::AptKey2 do
subject(:provider) { described_class.new }
- let(:context) { instance_double('context') }
+
+ let(:context) { instance_double('context') }
describe '#canonicalize(resources)' do
it('works with empty inputs') { expect(provider.canonicalize([])).to eq [] }
end
end
+ describe '.key_line_to_hash(pub, fpr)' do
+ subject(:result) { described_class.key_line_to_hash(pub, fpr) }
+
+ let(:pub) { "pub:-:4096:#{key_type}:7638D0442B90D010:1416603673:1668891673::-:::scSC:::::::" }
+ let(:fpr) { "fpr:::::::::#{id}:" }
+
+ let(:key_type) { :foo }
+
+ let(:short) { 'a' * 8 }
+ let(:long) { ('1' * 8) + short }
+ let(:id) { 'f' * (40 - 16) + long }
+
+ it('returns the id') { expect(result[:id]).to eq id }
+ it('returns the long id') { expect(result[:long]).to eq long }
+ it('returns the short id') { expect(result[:short]).to eq short }
+
+ [[1, :rsa], [17, :dsa], [18, :ecc], [19, :ecdsa], [:foo, :unrecognized]].each do |key_type, value|
+ context "with a key type of #{key_type}" do
+ let(:key_type) { key_type }
+
+ it("returns #{value.inspect} as key type") { expect(result[:type]).to eq value }
+ end
+ end
+ end
+
describe '#get' do
let(:apt_key_cmd) { instance_double('Puppet::ResourceApi::Command') }
- let(:handle) { instance_double('handle') }
+ let(:process) { instance_double('ChildProcess::AbstractProcess') }
+ let(:io) { instance_double('ChildProcess::AbstractIO') }
let(:stdout) do
- StringIO.new(<<EOS
+ StringIO.new <<EOS
Executing: /tmp/apt-key-gpghome.4VkaIao1Ca/gpg.1.sh --list-keys --with-colons --fingerprint --fixed-list-mode
tru:t:1:1505150630:0:3:1:5
pub:-:4096:1:7638D0442B90D010:1416603673:1668891673::-:::scSC:::::::
fpr:::::::::D21169141CECD440F2EB8DDA9D6D8F6BC857C906:
uid:-::::1416604417::088FA6B00E33BCC6F6EB4DFEFAC591F9940E06F0::Debian Security Archive Automatic Signing Key (8/jessie) <ftpmaster@debian.org>:
EOS
- )
end
- it 'processes input' do
+ before(:each) do
allow(Puppet::ResourceApi::Command).to receive(:new).and_return(apt_key_cmd)
- expect(apt_key_cmd).to receive(:start_read).with(context, any_args).and_yield(handle)
- expect(handle).to receive(:stdout).and_yield(stdout)
+ allow(process).to receive(:io).and_return(io)
+ end
- expect { provider.get(context) }.not_to raise_error
+ it 'processes input' do # rubocop:ignore RSpec/ExampleLength
+ expect(apt_key_cmd).to receive(:start_read).with(context, any_args).and_yield(process)
+ expect(io).to receive(:stdout).and_return(stdout)
+ expect(provider.get(context)).to eq [
+ { ensure: 'present',
+ id: '126C0D24BD8A2942CC7DF8AC7638D0442B90D010',
+ fingerprint: '126C0D24BD8A2942CC7DF8AC7638D0442B90D010',
+ long: '7638D0442B90D010',
+ short: '2B90D010',
+ size: 4096,
+ type: :rsa,
+ created: '2014-11-21 21:01:13 +0000',
+ expiry: '2022-11-19 21:01:13 +0000',
+ expired: false },
+ { ensure: 'present',
+ id: 'D21169141CECD440F2EB8DDA9D6D8F6BC857C906',
+ fingerprint: 'D21169141CECD440F2EB8DDA9D6D8F6BC857C906',
+ long: '9D6D8F6BC857C906',
+ short: 'C857C906',
+ size: 4096,
+ type: :rsa,
+ created: '2014-11-21 21:13:37 +0000',
+ expiry: '2022-11-19 21:13:37 +0000',
+ expired: false },
+ ]
end
end
end