staticsync requires a pty
[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   String $restrict = 'restrict',
10   Array[Stdlib::IP::Address] $from_hosts = $base::public_addresses,
11 ) {
12   $from = $from_hosts.join(',')
13
14   if (size(split($key, "\n")) > 1) {
15     fail('More than one line in key for ssh::authorized_key')
16   }
17   if (size(split($command, '"')) > 1) {
18     fail('command must not contain double quotes')
19   }
20   if (size(split($from, '"')) > 1) {
21     fail('from_hosts must not contain double quotes')
22   }
23
24   $from_space = $from_hosts.join(' ')
25
26   if $key {
27     @@concat::fragment { "ssh::authorized_key::${name} ${target_user} from ${::hostname}":
28       tag     => "ssh::authorized_key::fragment::${collect_tag}::${target_user}",
29       target  => "/etc/ssh/userkeys/${target_user}",
30       order   => '200',
31       content => @("EOF"),
32                  # from ${::fqdn}
33                  command="${command}",from="${from}",${restrict} ${key}
34                  | EOF
35     }
36   } else {
37     notify{ "Warning, ssh key for ${name}, ${target_user} not defined (yet?).": }
38   }
39
40   @@ferm::rule { "ssh-${collect_tag}_${target_user}-${name}_from_${::hostname}":
41     tag         => "ssh::authorized_key::ferm::${collect_tag}::${target_user}",
42     description => "allow ssh for ssh to ${target_user}",
43     domain      => '(ip ip6)',
44     chain       => 'ssh',
45     rule        => "saddr (${from_space}) ACCEPT",
46   }
47 }