b708fa9625cf77747231ec2cef4b9bec30fd033b
[mirror/dsa-puppet.git] / modules / postgres / manifests / cluster / hba_entry.pp
1 # An entry in pg_hba and the corresponding firewall rule if necessary
2 #
3 # This currently only supports a limited number of entry types.  Only
4 # what we need at the moment.
5 #
6 # See the upstream documentation at https://www.postgresql.org/docs/11/auth-pg-hba-conf.html
7 # for details.
8 #
9 # @param pg_port          port of the postgres cluster
10 # @param pg_cluster       cluster name
11 # @param pg_version       pg version of the cluster
12 # @param connection_type  connection type
13 # @param database         database (or all, sameuser, replication, etc.)
14 # @param user             user (or all, etc.)
15 # @param address          hosts that match
16 # @param method           auth method
17 # @param order            ordering of this entry in pg_hba.conf
18 define postgres::cluster::hba_entry (
19   Optional[Integer] $pg_port = undef,
20   Optional[String] $pg_cluster = undef,
21   Optional[String] $pg_version = undef,
22   Enum['local', 'hostssl'] $connection_type = 'hostssl',
23   Variant[String,Array[String]] $database = 'sameuser',
24   Variant[String,Array[String]] $user = 'all',
25   Optional[Variant[Stdlib::IP::Address, Array[Stdlib::IP::Address]]] $address = undef,
26   Enum['md5', 'trust'] $method = 'md5',
27   String $order = '50',
28 ) {
29   $address_methods = ['md5']
30   if $method in $address_methods {
31     if !$address {
32       fail("Authentication method ${method} needs an address")
33     }
34   } else {
35     if !($method in $address_methods) {
36       fail("Authentication method ${method} needs no address")
37     }
38   }
39
40   $clusters = $facts['postgresql_clusters']
41   if $pg_port {
42     $filtered = $clusters.filter |$cluster| { $cluster['port'] == $pg_port }
43     if $filtered.length != 1 {
44       fail("Did not find exactly one cluster with port ${pg_port}")
45     }
46     $cluster = $filtered[0]
47   } elsif $pg_cluster and $pg_version {
48     $filtered = $clusters.filter |$cluster| { $cluster['version'] == $pg_version and $cluster['cluster'] == $pg_cluster}
49     if $filtered.length != 1 {
50       fail("Did not find exactly one cluster ${pg_version}/${pg_cluster}")
51     }
52     $cluster = $filtered[0]
53   } else {
54     fail('postgres::cluster::hba_entry needs either the port of both a pg version and cluster name')
55   }
56   $real_port    = $cluster['port']
57   $real_version = $cluster['version']
58   $real_cluster = $cluster['cluster']
59   if $pg_version and $pg_version != $real_version {
60     fail("Inconsisten cluster version information: ${pg_version} != ${real_version}")
61   }
62   if $pg_cluster and $pg_cluster != $real_cluster {
63     fail("Inconsisten cluster name information: ${pg_cluster} != ${real_cluster}")
64   }
65
66   if ($address) {
67     ferm::rule::simple { "postgres::cluster::hba_entry::${name}":
68       description => "allow access to pg${real_version}/${real_cluster}: ${name}",
69       saddr       => $address,
70       chain       => "pg-${real_port}",
71     }
72   }
73
74   $real_database = Array($database, true).sort().join(',')
75   $real_user     = Array($user, true).sort().join(',')
76   $real_address  = $address ? {
77     undef   => [''],
78     default => Array($address, true).map |$a| {
79       if    $a =~ Stdlib::IP::Address::V4::CIDR     { $a }
80       elsif $a =~ Stdlib::IP::Address::V4::Nosubnet { "${a}/32" }
81       elsif $a =~ Stdlib::IP::Address::V6::CIDR     { $a }
82       elsif $a =~ Stdlib::IP::Address::V6::Nosubnet { "${a}/128" }
83       else { fail("Do not know address type for ${a}") }
84     }
85   }
86
87   @concat::fragment { "postgres::cluster::pg_hba::${name}":
88     tag     => "postgres::cluster::${real_version}::${real_cluster}::hba",
89     target  => "postgres::cluster::${real_version}::${real_cluster}::hba",
90     order   => $order,
91     content => inline_template( @(EOF) ),
92                   #
93                   # rule <%= @name %>
94                   <% @real_address.each do |addr| -%>
95                   <%= [@connection_type, @real_database, @real_user, addr, @method].join(' ') %>
96                   <% end -%>
97                   #
98                   | EOF
99   }
100 }