Defined Type: postgresql::server::default_privileges

Defined in:
manifests/server/default_privileges.pp

Summary

Manage a database defaults privileges. Only works with PostgreSQL version 9.6 and above.

Overview

Parameters:

  • ensure (Enum['present', 'absent' ]) (defaults to: 'present')

    Specifies whether to grant or revoke the privilege.

  • role (String)

    Specifies the role or user whom you are granting access to.

  • db (String)

    Specifies the database to which you are granting access.

  • object_type (Pattern[ /(?i:^FUNCTIONS$)/, /(?i:^ROUTINES$)/, /(?i:^SEQUENCES$)/, /(?i:^TABLES$)/, /(?i:^TYPES$)/ ])

    Specify target object type: ‘FUNCTIONS’, ‘ROUTINES’, ‘SEQUENCES’, ‘TABLES’, ‘TYPES’.

  • privilege (String)

    Specifies comma-separated list of privileges to grant. Valid options: depends on object type.

  • schema (String) (defaults to: 'public')

    Target schema. Defaults to ‘public’.

  • psql_db (String) (defaults to: $postgresql::server::default_database)

    Defines the database to execute the grant against. This should not ordinarily be changed from the default.

  • psql_user (String) (defaults to: $postgresql::server::user)

    Specifies the OS user for running psql. Default value: The default user for the module, usually ‘postgres’.

  • psql_path (String) (defaults to: $postgresql::server::psql_path)

    Specifies the OS user for running psql. Default value: The default user for the module, usually ‘postgres’.

  • port (Integer) (defaults to: $postgresql::server::port)

    Specifies the port to access the server. Default value: The default user for the module, usually ‘5432’.

  • connect_settings (Hash) (defaults to: $postgresql::server::default_connect_settings)

    Specifies a hash of environment variables used when connecting to a remote server.

  • psql_path (defaults to: $postgresql::server::psql_path)

    Specifies the path to the psql command.

  • group (String) (defaults to: $postgresql::server::group)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'manifests/server/default_privileges.pp', line 15

define postgresql::server::default_privileges (
  String $role,
  String $db,
  String $privilege,
  Pattern[
    /(?i:^FUNCTIONS$)/,
    /(?i:^ROUTINES$)/,
    /(?i:^SEQUENCES$)/,
    /(?i:^TABLES$)/,
    /(?i:^TYPES$)/
  ] $object_type,
  String $schema                   = 'public',
  String $psql_db                  = $postgresql::server::default_database,
  String $psql_user                = $postgresql::server::user,
  Integer $port                    = $postgresql::server::port,
  Hash $connect_settings           = $postgresql::server::default_connect_settings,
  Enum['present',
    'absent'
  ] $ensure                        = 'present',
  String $group                    = $postgresql::server::group,
  String $psql_path                = $postgresql::server::psql_path,
) {

  # If possible use the version of the remote database, otherwise
  # fallback to our local DB version
  if $connect_settings != undef and has_key( $connect_settings, 'DBVERSION') {
    $version = $connect_settings['DBVERSION']
  } else {
    $version = $postgresql::server::_version
  }

  if (versioncmp($version, '9.6') == -1) {
    fail 'Default_privileges is only useable with PostgreSQL >= 9.6'
  }

  case $ensure {
    default: {
      # default is 'present'
      $sql_command = 'ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT %s ON %s TO "%s"'
      $unless_is = true
    }
    'absent': {
      $sql_command = 'ALTER DEFAULT PRIVILEGES IN SCHEMA %s REVOKE %s ON %s FROM "%s"'
      $unless_is = false
    }
  }

  #
  # Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
  #
  if $port != undef {
    $port_override = $port
  } elsif $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
    $port_override = undef
  } else {
    $port_override = $postgresql::server::port
  }

  ## Munge the input values
  $_object_type = upcase($object_type)
  $_privilege   = upcase($privilege)

  case $_object_type {
    # Routines and functions ends up with the same definition
    Pattern[
      /^ROUTINES$/,
      /^FUNCTIONS$/,
    ]: {
      case $_privilege {
        Pattern[
          /^ALL$/,
          /^EXECUTE$/,
        ]: {
          $_check_privilege = 'X'
        }
        default: { fail('Illegal value for $privilege parameter') }
      }
      $_check_type = 'f'
    }
    'SEQUENCES': {
      case $_privilege {
        /^(ALL)$/: { $_check_privilege = 'rwU' }
        /^SELECT$/: { $_check_privilege = 'r'}
        /^UPDATE$/: { $_check_privilege = 'w'}
        /^USAGE$/: { $_check_privilege = 'U'}
        default: { fail('Illegal value for $privilege parameter') }
      }
      $_check_type = 'S'
    }
    'TABLES': {
      case $_privilege {
        /^ALL$/: { $_check_privilege = 'arwdDxt' }
        /^DELETE$/: { $_check_privilege = 'd' }
        /^INSERT$/: { $_check_privilege = 'a' }
        /^REFERENCES$/: { $_check_privilege = 'x' }
        /^SELECT$/: { $_check_privilege = 'r' }
        /^TRIGGER$/: { $_check_privilege = 'd' }
        /^TRUNCATE$/: { $_check_privilege = 'D' }
        /^UPDATE$/: { $_check_privilege = 'w' }
        default: { fail('Illegal value for $privilege parameter') }
      }
      $_check_type = 'r'
    }
    'TYPES': {
      case $_privilege {
        /^(ALL|USAGE)$/: { $_check_privilege = 'U'}
        default: { fail('Illegal value for $privilege parameter') }
      }
      $_check_type = 'T'
    }
    default: {
      fail("Missing privilege validation for object type ${_object_type}")
    }
  }

  $_unless = $ensure ? {
    'absent' => "SELECT 1 WHERE NOT EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '%s=%s' = ANY (defaclacl) AND nspname = '%s' and defaclobjtype = '%s')",
    default  => "SELECT 1 WHERE EXISTS (SELECT * FROM pg_default_acl AS da JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '%s=%s' = ANY (defaclacl) AND nspname = '%s' and defaclobjtype = '%s')"
  }

  $unless_cmd = sprintf($_unless, $role, $_check_privilege, $schema, $_check_type)
  $grant_cmd = sprintf($sql_command, $schema, $_privilege, $_object_type, $role)

  postgresql_psql { "default_privileges:${name}":
    command          => $grant_cmd,
    db               => $db,
    port             => $port_override,
    connect_settings => $connect_settings,
    psql_user        => $psql_user,
    psql_group       => $group,
    psql_path        => $psql_path,
    unless           => $unless_cmd,
    environment      => 'PGOPTIONS=--client-min-messages=error'
  }

  if($role != undef and defined(Postgresql::Server::Role[$role])) {
    Postgresql::Server::Role[$role]->Postgresql_psql["default_privileges:${name}"]
  }

  if($db != undef and defined(Postgresql::Server::Database[$db])) {
    Postgresql::Server::Database[$db]->Postgresql_psql["default_privileges:${name}"]
  }
}