# end
end
- def create(global_context, title, should, noop = false)
- global_context.creating(title) do |context|
+ def create(context, name, should, noop = false)
+ context.creating(name) do
if should[:source].nil? && should[:content].nil?
# Breaking up the command like this is needed because it blows up
# if --recv-keys isn't the last argument.
args.push('--recv-keys', should[:name])
@apt_key_cmd.run(context, *args, noop: noop)
elsif should[:content]
- temp_key_file(context, title, should[:content]) do |key_file|
+ temp_key_file(context, name, should[:content]) do |key_file|
# @apt_key_cmd.run(context, 'add', key_file, noop: noop)
# require'pry';binding.pry
# puts key_file
apt_key('add', key_file.path, noop: noop)
# In case we really screwed up, better safe than sorry.
else
- context.fail("an unexpected condition occurred while trying to add the key: #{title} (content: #{should[:content].inspect}, source: #{should[:source].inspect})")
+ context.fail("an unexpected condition occurred while trying to add the key: #{name} (content: #{should[:content].inspect}, source: #{should[:source].inspect})")
end
end
end
- def delete(global_context, title, noop = false)
- global_context.deleting(title) do |context|
- @apt_key_cmd.run(context, 'del', title, noop: noop)
+ def delete(context, name, noop = false)
+ context.deleting(name) do
+ @apt_key_cmd.run(context, 'del', name, noop: noop)
end
end
# This method writes out the specified contents to a temporary file and
# confirms that the fingerprint from the file, matches the long key that is in the manifest
- def temp_key_file(context, title, content)
+ def temp_key_file(context, name, content)
file = Tempfile.new('apt_key')
begin
file.write content
file.close
if File.executable? '/usr/bin/gpg'
- extracted_keys = `/usr/bin/gpg --with-fingerprint --with-colons #{file.path}`.each_line.select { |line| line =~ %r{^fpr:} }.map { |fpr| fpr.split(':')[9] }
+ extracted_keys = `/usr/bin/gpg --with-fingerprint --with-colons #{file.path}`.each_line.select { |line| line =~ %r{^fpr:} }.map { |fpr| fpr.strip.split(':')[9] }
- if extracted_keys.include? title
+ if extracted_keys.include? name
context.debug('Fingerprint verified against extracted key')
- elsif extracted_keys.any? { |k| k =~ %r{#{title}$} }
+ elsif extracted_keys.any? { |k| k =~ %r{#{name}$} }
context.debug('Fingerprint matches the extracted key')
else
- raise ArgumentError, "The fingerprint in your manifest (#{title}) and the fingerprint from content/source (#{extracted_keys.inspect}) do not match. "\
+ raise ArgumentError, "The fingerprint in your manifest (#{name}) and the fingerprint from content/source (#{extracted_keys.inspect}) do not match. "\
' Please check there is not an error in the name or check the content/source is legitimate.'
end
else
RSpec.describe Puppet::Provider::AptKey2::AptKey2 do
subject(:provider) { described_class.new }
- let(:context) { instance_double('Puppet::ResourceApi::BaseContext') }
+ let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') }
let(:key_list) do
<<EOS
Executing: /tmp/apt-key-gpghome.4VkaIao1Ca/gpg.1.sh --list-keys --with-colons --fingerprint --fixed-list-mode
end
context 'when fetching a key from the keyserver' do
- let(:creating_ctx) { instance_double('Puppet::ResourceApi::BaseContext', 'creating_ctx') }
-
it 'updates the system' do
- expect(context).to receive(:creating).with(fingerprint).and_yield(creating_ctx)
- expect(apt_key_cmd).to receive(:run).with(creating_ctx, 'adv', '--keyserver', 'keyserver.example.com', '--recv-keys', fingerprint, noop: false).and_return 0
+ expect(context).to receive(:creating).with(fingerprint).and_yield
+ expect(apt_key_cmd).to receive(:run).with(context, 'adv', '--keyserver', 'keyserver.example.com', '--recv-keys', fingerprint, noop: false).and_return 0
provider.set(context, fingerprint =>
{
is: nil,
end
context 'when adding a key from a string' do
- let(:creating_ctx) { instance_double('Puppet::ResourceApi::BaseContext', 'creating_ctx') }
let(:key_tempfile) { instance_double('Tempfile') }
it 'updates the system' do
- expect(context).to receive(:creating).with(fingerprint).and_yield(creating_ctx)
+ expect(context).to receive(:creating).with(fingerprint).and_yield
expect(Tempfile).to receive(:new).with('apt_key').and_return(key_tempfile)
expect(key_tempfile).to receive(:write).with('public gpg key block')
allow(key_tempfile).to receive(:path).with(no_args).and_return('tempfilename')
expect(key_tempfile).to receive(:unlink)
expect(File).to receive(:executable?).with('/usr/bin/gpg').and_return(true)
expect(provider).to receive(:`).with('/usr/bin/gpg --with-fingerprint --with-colons tempfilename').and_return("\nfpr:::::::::#{fingerprint}:\n") # rubocop:disable RSpec/SubjectStub
- expect(creating_ctx).to receive(:debug).with('Fingerprint verified against extracted key')
+ expect(context).to receive(:debug).with('Fingerprint verified against extracted key')
- # expect(apt_key_cmd).to receive(:run).with(creating_ctx, 'add', 'tempfilename', noop: false).and_return 0
+ # expect(apt_key_cmd).to receive(:run).with(context, 'add', 'tempfilename', noop: false).and_return 0
expect(provider).to receive(:system).with('apt-key add tempfilename') # rubocop:disable RSpec/SubjectStub
provider.set(context, fingerprint =>
{
end
context 'when deleting a key' do
- let(:deleting_ctx) { instance_double('Puppet::ResourceApi::BaseContext', 'deleting_ctx') }
-
it 'updates the system' do
- expect(context).to receive(:deleting).with(fingerprint).and_yield(deleting_ctx)
- expect(apt_key_cmd).to receive(:run).with(deleting_ctx, 'del', fingerprint, noop: false).and_return 0
+ expect(context).to receive(:deleting).with(fingerprint).and_yield
+ expect(apt_key_cmd).to receive(:run).with(context, 'del', fingerprint, noop: false).and_return 0
provider.set(context, fingerprint =>
{
is: {