1 # An entry in pg_hba and the corresponding firewall rule if necessary
3 # This currently only supports a limited number of entry types. Only
4 # what we need at the moment.
6 # See the upstream documentation at https://www.postgresql.org/docs/11/auth-pg-hba-conf.html
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', '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',
29 Boolean $firewall = true,
31 $address_methods = ['md5', 'trust']
32 if $method in $address_methods {
34 fail("Authentication method ${method} needs an address")
37 if !($method in $address_methods) {
38 fail("Authentication method ${method} needs no address")
42 # get remaining cluster info and verify consistency
44 $clusters = $facts['postgresql_clusters']
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}")
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}")
56 $cluster = $filtered[0]
58 fail('postgres::cluster::hba_entry needs either the port of both a pg version and cluster name')
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}")
66 if $pg_cluster and $pg_cluster != $real_cluster {
67 fail("Inconsisten cluster name information: ${pg_cluster} != ${real_cluster}")
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}",
75 chain => "pg-${real_port}",
79 $real_database = Array($database, true).sort().join(',')
80 $real_user = Array($user, true).sort().join(',')
81 $real_address = $address ? {
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}") }
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",
96 content => inline_template( @(EOF) ),
99 <% @real_address.each do |addr| -%>
100 <%= [@connection_type, @real_database, @real_user, addr, @method].join(' ') %>