3e700cc64c63e2e286fac413880b068957ea4168
[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   Variant[Array[String], 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   if $collect_tag =~ String {
25     $raw_tags = [ $collect_tag ]
26   } else {
27     $raw_tags = $collect_tag
28   }
29   $ssh_tags = $raw_tags.map |$t| { "ssh::authorized_key::fragment::${t}::${target_user}" }
30   $ferm_tags = $raw_tags.map |$t| { "ssh::authorized_key::ferm::${t}::${target_user}" }
31
32   $from_space = $from_hosts.join(' ')
33
34   if $key {
35     @@concat::fragment { "ssh::authorized_key::${name} ${target_user} from ${::hostname}":
36       tag     => $ssh_tags,
37       target  => "/etc/ssh/userkeys/${target_user}",
38       order   => '200',
39       content => @("EOF"),
40                  # from ${::fqdn}
41                  command="${command}",from="${from}",${restrict} ${key}
42                  | EOF
43     }
44   } else {
45     notify{ "Warning, ssh key for ${name}, ${target_user} not defined (yet?).": }
46   }
47
48   @@ferm::rule { "ssh-${raw_tags[0]}_${target_user}-${name}_from_${::hostname}":
49     tag         => $ssh_tags,
50     description => "allow ssh for ssh to ${target_user}",
51     domain      => '(ip ip6)',
52     chain       => 'ssh',
53     rule        => "saddr (${from_space}) ACCEPT",
54   }
55 }