Do not require ssl on localhost
[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 # @param firewall         also add a firewall rule
19 define postgres::cluster::hba_entry (
20   Optional[Integer] $pg_port = undef,
21   Optional[String] $pg_cluster = undef,
22   Optional[String] $pg_version = undef,
23   Enum['local', 'host', 'hostssl'] $connection_type = 'hostssl',
24   Variant[String,Array[String]] $database = 'sameuser',
25   Variant[String,Array[String]] $user = 'all',
26   Optional[Variant[Stdlib::IP::Address, Array[Stdlib::IP::Address]]] $address = undef,
27   Enum['md5', 'trust'] $method = 'md5',
28   String $order = '50',
29   Boolean $firewall = true,
30 ) {
31   $address_methods = ['md5', 'trust']
32   if $method in $address_methods {
33     if !$address {
34       fail("Authentication method ${method} needs an address")
35     }
36   } else {
37     if !($method in $address_methods) {
38       fail("Authentication method ${method} needs no address")
39     }
40   }
41
42   # get remaining cluster info and verify consistency
43   ###
44   $clusters = $facts['postgresql_clusters']
45   if $pg_port {
46     $filtered = $clusters.filter |$cluster| { $cluster['port'] == $pg_port }
47     if $filtered.length != 1 {
48       fail("Did not find exactly one cluster with port ${pg_port}")
49     }
50     $cluster = $filtered[0]
51   } elsif $pg_cluster and $pg_version {
52     $filtered = $clusters.filter |$cluster| { $cluster['version'] == $pg_version and $cluster['cluster'] == $pg_cluster}
53     if $filtered.length != 1 {
54       fail("Did not find exactly one cluster ${pg_version}/${pg_cluster}")
55     }
56     $cluster = $filtered[0]
57   } else {
58     fail('postgres::cluster::hba_entry needs either the port of both a pg version and cluster name')
59   }
60   $real_port    = $cluster['port']
61   $real_version = $cluster['version']
62   $real_cluster = $cluster['cluster']
63   if $pg_version and $pg_version != $real_version {
64     fail("Inconsisten cluster version information: ${pg_version} != ${real_version}")
65   }
66   if $pg_cluster and $pg_cluster != $real_cluster {
67     fail("Inconsisten cluster name information: ${pg_cluster} != ${real_cluster}")
68   }
69   ###
70
71   if ($address and $firewall) {
72     ferm::rule::simple { "postgres::cluster::hba_entry::${name}":
73       description => "allow access to pg${real_version}/${real_cluster}: ${name}",
74       saddr       => $address,
75       chain       => "pg-${real_port}",
76     }
77   }
78
79   $real_database = Array($database, true).sort().join(',')
80   $real_user     = Array($user, true).sort().join(',')
81   $real_address  = $address ? {
82     undef   => [''],
83     default => Array($address, true).map |$a| {
84       if    $a =~ Stdlib::IP::Address::V4::CIDR     { $a }
85       elsif $a =~ Stdlib::IP::Address::V4::Nosubnet { "${a}/32" }
86       elsif $a =~ Stdlib::IP::Address::V6::CIDR     { $a }
87       elsif $a =~ Stdlib::IP::Address::V6::Nosubnet { "${a}/128" }
88       else { fail("Do not know address type for ${a}") }
89     }
90   }
91
92   @concat::fragment { "postgres::cluster::pg_hba::${name}":
93     tag     => "postgres::cluster::${real_version}::${real_cluster}::hba",
94     target  => "postgres::cluster::${real_version}::${real_cluster}::hba",
95     order   => $order,
96     content => inline_template( @(EOF) ),
97                   #
98                   # rule <%= @name %>
99                   <% @real_address.each do |addr| -%>
100                   <%= [@connection_type, @real_database, @real_user, addr, @method].join(' ') %>
101                   <% end -%>
102                   #
103                   | EOF
104   }
105 }