44cdc8025d20c86680552deba2eb91091e9e5271
[mirror/dsa-puppet.git] / modules / ssh / manifests / authorized_key_add.pp
1 # store ssh authorized_keys snippets that roles on different hosts can then
2 # collect using ssh::authorized_key_collect
3
4 define ssh::authorized_key_add(
5   String $target_user,
6   String $command,
7   String $key,
8   String $collect_tag,
9   Array[Stdlib::IP::Address] $from_hosts = $base::public_addresses,
10 ) {
11   $from = $from_hosts.join(',')
12
13   if (size(split($key, "\n")) > 1) {
14     fail('More than one line in key for ssh::authorized_key')
15   }
16   if (size(split($command, '"')) > 1) {
17     fail('command must not contain double quotes')
18   }
19   if (size(split($from, '"')) > 1) {
20     fail('from_hosts must not contain double quotes')
21   }
22
23   $from_space = $from_hosts.join(' ')
24
25   @@concat::fragment { "ssh::authorized_key::${name} ${target_user} ${from}":
26     tag     => "ssh::authorized_key::fragment::${collect_tag}::${target_user}",
27     target  => "/etc/ssh/userkeys/${target_user}",
28     order   => '200',
29     content => @("EOF"),
30                command="${command}",from="${from}",restrict ${key}
31                | EOF
32   }
33
34   @@ferm::rule { "ssh-${target_user}-${name}":
35     tag         => "ssh::authorized_key::ferm::${collect_tag}::${target_user}",
36     description => "allow ssh for ssh to ${target_user}",
37     domain      => '(ip ip6)',
38     chain       => 'ssh',
39     rule        => "saddr (${from_space}) ACCEPT",
40   }
41 }