From 7e0412f9d2bdbcc8133f84dc60b52b8570cda8fc Mon Sep 17 00:00:00 2001 From: Sliim Date: Sun, 23 Apr 2017 23:01:21 +0200 Subject: [PATCH] Fix authorized_users attribute when multiple users Duplicate resource attributes before processing them. Fix #4 --- providers/key.rb | 4 +-- spec/unit/default_spec.rb | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/providers/key.rb b/providers/key.rb index 2c3e650..675d11d 100644 --- a/providers/key.rb +++ b/providers/key.rb @@ -6,8 +6,8 @@ user = PMSIpilot::SshKeys::User.normalize!( username, { - :authorized_keys => new_resource.authorized_keys, - :authorized_users => new_resource.authorized_users + :authorized_keys => new_resource.authorized_keys.dup, + :authorized_users => new_resource.authorized_users.dup }, Proc.new do |item| data_bag_item(new_resource.data_bag, item) diff --git a/spec/unit/default_spec.rb b/spec/unit/default_spec.rb index 00e4450..3669776 100644 --- a/spec/unit/default_spec.rb +++ b/spec/unit/default_spec.rb @@ -298,5 +298,66 @@ expect(chef_run.converge(described_recipe)).to render_file('/home/bob/.ssh/authorized_keys').with_content("joe_public_key\njoe_other_public_key") end end + + describe 'With multiple users' do + it 'Should install correct public keys' do + allow(Dir).to receive(:home) { '/home/bob' } + stub_command('test -e /home/bob/.ssh').and_return(false) + allow(Dir).to receive(:home) { '/home/bob' } + stub_command('test -e /home/bob/.ssh').and_return(false) + + stub_data_bag_item(:ssh_keys, 'bob').and_return({ + :id => 'bob', + :keys => [ + { + :id => 'bob_key', + :pub => 'bob_public_key', + :priv => 'bob_private_key' + } + ] + }) + stub_data_bag_item(:ssh_keys, 'joe').and_return({ + :id => 'joe', + :keys => [ + { + :id => 'joe_key', + :pub => 'joe_public_key', + :priv => 'joe_private_key' + } + ] + }) + stub_data_bag_item(:ssh_keys, 'foo').and_return({ + :id => 'foo', + :keys => [ + { + :id => 'foo_key', + :pub => 'foo_public_key', + :priv => 'foo_private_key' + } + ] + }) + + chef_run = ChefSpec::SoloRunner.new(step_into: ['user_ssh_keys_key']) do |node| + node.set['user_ssh_keys'] = { + :users => { + :joe => { + :authorized_users => [ + 'foo' + ] + }, + :bob => { + :authorized_users => [ + 'joe', + ] + } + } + } + end + + subject = chef_run.converge(described_recipe) + expect(subject).to render_file('/home/bob/.ssh/authorized_keys').with_content("joe_public_key") + expect(subject).to_not render_file('/home/bob/.ssh/authorized_keys').with_content("foo_public_key") + end + end end end