Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ssh_key_groups #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,34 @@ accounts::users:
- 'command="/path/to/script.sh arg1 $SSH_ORIGINAL_COMMAND"'
```

### SSH key groups

Alternatively to specify the ssh keys for every user, they can be specified in groups in a seperate structure. Those groups can then be included for individual users.

```yaml
accounts::ssh_key_groups:
mykey_group1:
mykey1:
type: 'ssh-rsa'
key: 'AAAA....'
mykey2:
type: 'ssh-rsa'
key: 'AAAA....'
mykey_group2:
mykey3:
type: 'ssh-rsa'
key: 'AAAA....'

accounts::users:
foo:
ssh_key_groups: ['mykey_group1', 'mykey_group2']
ssh_keys:
'mykey4':
type: 'ssh-rsa'
key: 'AAAA....'
```


### Password Management

You can either provide an already hashed password or you can let the module take
Expand Down
15 changes: 8 additions & 7 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Puppet accounts management
#
class accounts(
Boolean $manage_users = true,
Boolean $manage_groups = true,
Hash $users = {},
Hash $groups = {},
Hash $user_defaults = {},
Hash $options = {},
Boolean $use_lookup = true,
Boolean $manage_users = true,
Boolean $manage_groups = true,
Hash $users = {},
Hash $groups = {},
Hash $user_defaults = {},
Hash $options = {},
Hash $ssh_key_groups = {},
Boolean $use_lookup = true,
) inherits ::accounts::params {

# currently used mainly in tests to turn-off hiera backends
Expand Down
11 changes: 10 additions & 1 deletion manifests/user.pp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
Array $groups = [],
Optional[Stdlib::Absolutepath] $ssh_key_source = undef,
Hash $ssh_keys = {},
Array $ssh_key_groups = [],
Boolean $purge_ssh_keys = false,
String $shell ='/bin/bash',
String $pwhash = '',
Expand Down Expand Up @@ -247,8 +248,16 @@
}
}

$mapped_ssh_keys = $ssh_key_groups.reduce({}) |$memo, $key_group| {
if ($key_group in $accounts::ssh_key_groups) {
$memo + $accounts::ssh_key_groups[$key_group]
} else {
fail("Accounts:user ${username}: ssh_key_group ${key_group} does not exist!")
}
}

accounts::authorized_keys { $username:
ssh_keys => $ssh_keys,
ssh_keys => $mapped_ssh_keys + $ssh_keys,
ssh_key_source => $ssh_key_source,
authorized_keys_file => $authorized_keys_file,
home_dir => $home_dir,
Expand Down
66 changes: 66 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -672,4 +672,70 @@

it_behaves_like 'having_user_account', 'foo'
end

context 'ssh_key_groups are defined' do
let(:ssh_key_groups) do
{
'key_group1' => {
'[email protected]' => { 'type' => 'ssh-rsa', 'key' => 'user1-key' },
},
'key_group2' => {
'[email protected]' => { 'type' => 'ssh-rsa', 'key' => 'user2-key' },
'[email protected]' => { 'type' => 'ssh-rsa', 'key' => 'user3-key' },
},
}
end

describe 'user includes only one ssh key group' do
let(:params) do
{
'ssh_key_groups' => ssh_key_groups,
'users' => { 'testuser' => {
'managehome' => true,
'ssh_key_groups' => ['key_group1']
}}
}
end

it { is_expected.to contain_ssh_authorized_key('[email protected]') }
it { is_expected.to_not contain_ssh_authorized_key('[email protected]') }
it { is_expected.to_not contain_ssh_authorized_key('[email protected]') }
end

describe 'user includes multiple ssh key groups' do
let(:params) do
{
'ssh_key_groups' => ssh_key_groups,
'users' => { 'testuser' => {
'managehome' => true,
'ssh_key_groups' => ['key_group1', 'key_group2']
}}
}
end

it { is_expected.to contain_ssh_authorized_key('[email protected]') }
it { is_expected.to contain_ssh_authorized_key('[email protected]') }
it { is_expected.to contain_ssh_authorized_key('[email protected]') }
end

describe 'user includes an ssh key group and an individual ssh key' do
let(:params) do
{
'ssh_key_groups' => ssh_key_groups,
'users' => { 'testuser' => {
'managehome' => true,
'ssh_key_groups' => ['key_group1'],
'ssh_keys' => {
'[email protected]' => { 'type' => 'ssh-rsa', 'key' => 'user4-key' }
}
}}
}
end

it { is_expected.to contain_ssh_authorized_key('[email protected]') }
it { is_expected.to_not contain_ssh_authorized_key('[email protected]') }
it { is_expected.to_not contain_ssh_authorized_key('[email protected]') }
it { is_expected.to contain_ssh_authorized_key('[email protected]') }
end
end
end