Skip to content

Commit 527d6fb

Browse files
committed
Add authorized_users
1 parent e87736c commit 527d6fb

File tree

4 files changed

+119
-7
lines changed

4 files changed

+119
-7
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ Deploys SSH keys
1616

1717
### Users
1818

19-
| Key | Type | Default | Description |
20-
| :------------------ |:---------- | :--------- | :------------------------------------------------------- |
21-
| `databag` | String | `ssh_keys` | Databag where to search for keys |
22-
| `authorized_keys` | Array | `[]` | Array of strings representing authorized SSH public keys |
19+
| Key | Type | Default | Description |
20+
| :------------------ |:---------- | :--------- | :-------------------------------------------------------------------- |
21+
| `databag` | String | `ssh_keys` | Databag where to search for keys |
22+
| `authorized_keys` | Array | `[]` | Array of strings representing authorized SSH public keys |
23+
| `authorized_users` | Array | `[]` | Array of strings representing authorized users (found in the databag) |
2324

2425
## Databag
2526

recipes/default.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,26 @@
3838
end
3939
end
4040

41-
unless config['authorized_keys'].nil? || config['authorized_keys'].empty?
41+
authorized_keys = config['authorized_keys']
42+
authorized_keys ||= []
43+
44+
unless config['authorized_users'].nil? || config['authorized_users'].empty?
45+
config['authorized_users'].each do |authorized_user|
46+
raise Chef::Exceptions::ConfigurationError, "User #{authorized_user} does not exist" if databag[authorized_user].nil?
47+
48+
databag[authorized_user].each do |authorized_user_key|
49+
authorized_keys << authorized_user_key['pub']
50+
end
51+
end
52+
end
53+
54+
unless authorized_keys.empty?
4255
template "#{home}/.ssh/authorized_keys" do
4356
source 'authorized_keys.erb'
4457
owner user
4558
group user
4659
mode '0600'
47-
variables keys: config['authorized_keys']
60+
variables keys: authorized_keys
4861
end
4962
end
5063
end

spec/unit/default_spec.rb

+99
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,103 @@
210210
end
211211
end
212212
end
213+
214+
describe 'Add authorized users' do
215+
describe 'With one user' do
216+
it 'Throws a ConfigurationError if user does not exist in databag' do
217+
chef_run = ChefSpec::SoloRunner.new do |node|
218+
node.set['ssh_keys'] = {
219+
:users => {
220+
:bob => {
221+
:authorized_users => [
222+
'joe'
223+
]
224+
}
225+
}
226+
}
227+
end
228+
229+
expect { chef_run.converge(described_recipe) }.to raise_error(Chef::Exceptions::ConfigurationError)
230+
end
231+
232+
it 'Should add authorized user\'s key' do
233+
allow(Dir).to receive(:home) { '/home/bob' }
234+
stub_command('test -e /home/bob/.ssh').and_return(false)
235+
allow(Dir).to receive(:home) { '/home/bob' }
236+
stub_command('test -e /home/bob/.ssh').and_return(false)
237+
stub_data_bag(:ssh_keys).and_return({
238+
:bob => [
239+
{
240+
:id => 'bob_key',
241+
:pub => 'bob_public_key',
242+
:priv => 'bob_private_key'
243+
}
244+
],
245+
:joe => [
246+
{
247+
:id => 'job_key',
248+
:pub => 'joe_public_key',
249+
:priv => 'joe_private_key'
250+
}
251+
]
252+
})
253+
254+
chef_run = ChefSpec::SoloRunner.new do |node|
255+
node.set['ssh_keys'] = {
256+
:users => {
257+
:bob => {
258+
:authorized_users => [
259+
'joe'
260+
]
261+
}
262+
}
263+
}
264+
end
265+
266+
expect(chef_run.converge(described_recipe)).to render_file('/home/bob/.ssh/authorized_keys').with_content('joe_public_key')
267+
end
268+
269+
it 'Should add authorized user\'s keys' do
270+
allow(Dir).to receive(:home) { '/home/bob' }
271+
stub_command('test -e /home/bob/.ssh').and_return(false)
272+
allow(Dir).to receive(:home) { '/home/bob' }
273+
stub_command('test -e /home/bob/.ssh').and_return(false)
274+
stub_data_bag(:ssh_keys).and_return({
275+
:bob => [
276+
{
277+
:id => 'bob_key',
278+
:pub => 'bob_public_key',
279+
:priv => 'bob_private_key'
280+
}
281+
],
282+
:joe => [
283+
{
284+
:id => 'joe_key',
285+
:pub => 'joe_public_key',
286+
:priv => 'joe_private_key'
287+
},
288+
{
289+
:id => 'joe_other_key',
290+
:pub => 'joe_other_public_key',
291+
:priv => 'joe_other_private_key'
292+
}
293+
]
294+
})
295+
296+
chef_run = ChefSpec::SoloRunner.new do |node|
297+
node.set['ssh_keys'] = {
298+
:users => {
299+
:bob => {
300+
:authorized_users => [
301+
'joe'
302+
]
303+
}
304+
}
305+
}
306+
end
307+
308+
expect(chef_run.converge(described_recipe)).to render_file('/home/bob/.ssh/authorized_keys').with_content("joe_public_key\njoe_other_public_key")
309+
end
310+
end
311+
end
213312
end

templates/default/authorized_keys.erb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<% @keys.each do |key| %>
22
<%= key %>
3-
43
<% end %>

0 commit comments

Comments
 (0)