Skip to content

Commit 65ca3ae

Browse files
authored
Merge pull request #1512 from cruelsmith/flexible_password_encryption_in_pg_hba_conf
Flexible password encryption in pg hba conf
2 parents 889811f + 3e04518 commit 65ca3ae

File tree

8 files changed

+49
-11
lines changed

8 files changed

+49
-11
lines changed

REFERENCE.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ The following parameters are available in the `postgresql::server` class:
874874
* [`manage_logdir`](#-postgresql--server--manage_logdir)
875875
* [`manage_xlogdir`](#-postgresql--server--manage_xlogdir)
876876
* [`password_encryption`](#-postgresql--server--password_encryption)
877+
* [`pg_hba_auth_password_encryption`](#-postgresql--server--pg_hba_auth_password_encryption)
877878
* [`roles`](#-postgresql--server--roles)
878879
* [`config_entries`](#-postgresql--server--config_entries)
879880
* [`pg_hba_rules`](#-postgresql--server--pg_hba_rules)
@@ -1300,12 +1301,21 @@ Default value: `$postgresql::params::manage_xlogdir`
13001301

13011302
##### <a name="-postgresql--server--password_encryption"></a>`password_encryption`
13021303

1303-
Data type: `Optional[Postgresql::Pg_password_encryption]`
1304+
Data type: `Postgresql::Pg_password_encryption`
13041305

13051306
Specify the type of encryption set for the password.
13061307

13071308
Default value: `$postgresql::params::password_encryption`
13081309

1310+
##### <a name="-postgresql--server--pg_hba_auth_password_encryption"></a>`pg_hba_auth_password_encryption`
1311+
1312+
Data type: `Optional[Postgresql::Pg_password_encryption]`
1313+
1314+
Specify the type of encryption set for the password in pg_hba_conf,
1315+
this value is usefull if you want to start enforcing scram-sha-256, but give users transition time.
1316+
1317+
Default value: `undef`
1318+
13091319
##### <a name="-postgresql--server--roles"></a>`roles`
13101320

13111321
Data type: `Hash[String, Hash]`
@@ -2417,6 +2427,7 @@ The following parameters are available in the `postgresql::server::instance::con
24172427
* [`log_line_prefix`](#-postgresql--server--instance--config--log_line_prefix)
24182428
* [`timezone`](#-postgresql--server--instance--config--timezone)
24192429
* [`password_encryption`](#-postgresql--server--instance--config--password_encryption)
2430+
* [`pg_hba_auth_password_encryption`](#-postgresql--server--instance--config--pg_hba_auth_password_encryption)
24202431
* [`extra_systemd_config`](#-postgresql--server--instance--config--extra_systemd_config)
24212432

24222433
##### <a name="-postgresql--server--instance--config--ip_mask_deny_postgres_user"></a>`ip_mask_deny_postgres_user`
@@ -2633,12 +2644,21 @@ Default value: `$postgresql::server::timezone`
26332644

26342645
##### <a name="-postgresql--server--instance--config--password_encryption"></a>`password_encryption`
26352646

2636-
Data type: `Optional[Postgresql::Pg_password_encryption]`
2647+
Data type: `Postgresql::Pg_password_encryption`
26372648

26382649
Specify the type of encryption set for the password.
26392650

26402651
Default value: `$postgresql::server::password_encryption`
26412652

2653+
##### <a name="-postgresql--server--instance--config--pg_hba_auth_password_encryption"></a>`pg_hba_auth_password_encryption`
2654+
2655+
Data type: `Optional[Postgresql::Pg_password_encryption]`
2656+
2657+
Specify the type of encryption set for the password in pg_hba_conf,
2658+
this value is usefull if you want to start enforcing scram-sha-256, but give users transition time.
2659+
2660+
Default value: `$postgresql::server::pg_hba_auth_password_encryption`
2661+
26422662
##### <a name="-postgresql--server--instance--config--extra_systemd_config"></a>`extra_systemd_config`
26432663

26442664
Data type: `Optional[String]`
@@ -4409,6 +4429,8 @@ Data type: `Optional[Optional[Postgresql::Pg_password_encryption]]`
44094429

44104430
Set type for password hash
44114431

4432+
Default value comes from `postgresql::params::password_encryption` and changes based on the `postgresql::globals::version`.
4433+
44124434
##### `salt`
44134435

44144436
Data type: `Optional[Optional[Variant[String[1], Integer]]]`

lib/puppet/functions/postgresql/postgresql_password.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# If the Postgresql-Passwordhash should be of Datatype Sensitive[String]
1414
# @param hash
1515
# Set type for password hash
16+
#
17+
# Default value comes from `postgresql::params::password_encryption` and changes based on the `postgresql::globals::version`.
1618
# @param salt
1719
# Use a specific salt value for scram-sha-256, default is username
1820
#
@@ -27,7 +29,8 @@
2729
return_type 'Variant[String, Sensitive[String]]'
2830
end
2931

30-
def default_impl(username, password, sensitive = false, hash = 'md5', salt = nil)
32+
def default_impl(username, password, sensitive = false, hash = nil, salt = nil)
33+
hash = call_function(:'postgresql::default', 'password_encryption') if hash.nil?
3134
password = password.unwrap if password.respond_to?(:unwrap)
3235
if password.is_a?(String) && password.match?(%r{^(md5[0-9a-f]{32}$|SCRAM-SHA-256\$)})
3336
return Puppet::Pops::Types::PSensitiveType::Sensitive.new(password) if sensitive

manifests/params.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
$manage_selinux = pick($manage_selinux, false)
2626
$package_ensure = 'present'
2727
$module_workdir = pick($module_workdir,'/tmp')
28-
$password_encryption = if versioncmp($version, '14') >= 0 { 'scram-sha-256' } else { undef }
28+
$password_encryption = versioncmp($version, '14') ? { -1 => 'md5', default => 'scram-sha-256' }
2929
$extra_systemd_config = undef
3030
$manage_datadir = true
3131
$manage_logdir = true

manifests/server.pp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@
9696
# @param manage_logdir Set to false if you have file{ $logdir: } already defined
9797
# @param manage_xlogdir Set to false if you have file{ $xlogdir: } already defined
9898
# @param password_encryption Specify the type of encryption set for the password.
99-
#
99+
# @param pg_hba_auth_password_encryption
100+
# Specify the type of encryption set for the password in pg_hba_conf,
101+
# this value is usefull if you want to start enforcing scram-sha-256, but give users transition time.
100102
# @param roles Specifies a hash from which to generate postgresql::server::role resources.
101103
# @param config_entries Specifies a hash from which to generate postgresql::server::config_entry resources.
102104
# @param pg_hba_rules Specifies a hash from which to generate postgresql::server::pg_hba_rule resources.
@@ -178,7 +180,8 @@
178180
Boolean $manage_datadir = $postgresql::params::manage_datadir,
179181
Boolean $manage_logdir = $postgresql::params::manage_logdir,
180182
Boolean $manage_xlogdir = $postgresql::params::manage_xlogdir,
181-
Optional[Postgresql::Pg_password_encryption] $password_encryption = $postgresql::params::password_encryption,
183+
Postgresql::Pg_password_encryption $password_encryption = $postgresql::params::password_encryption,
184+
Optional[Postgresql::Pg_password_encryption] $pg_hba_auth_password_encryption = undef,
182185
Optional[String] $extra_systemd_config = $postgresql::params::extra_systemd_config,
183186

184187
Hash[String, Hash] $roles = {},

manifests/server/instance/config.pp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
# @param log_line_prefix PostgreSQL log line prefix
4343
# @param timezone Set timezone for the PostgreSQL instance
4444
# @param password_encryption Specify the type of encryption set for the password.
45+
# @param pg_hba_auth_password_encryption
46+
# Specify the type of encryption set for the password in pg_hba_conf,
47+
# this value is usefull if you want to start enforcing scram-sha-256, but give users transition time.
4548
# @param extra_systemd_config
4649
# Adds extra config to systemd config file, can for instance be used to add extra openfiles. This can be a multi line string
4750
define postgresql::server::instance::config (
@@ -70,9 +73,12 @@
7073
Boolean $service_enable = $postgresql::server::service_enable,
7174
Optional[String[1]] $log_line_prefix = $postgresql::server::log_line_prefix,
7275
Optional[String[1]] $timezone = $postgresql::server::timezone,
73-
Optional[Postgresql::Pg_password_encryption] $password_encryption = $postgresql::server::password_encryption,
76+
Postgresql::Pg_password_encryption $password_encryption = $postgresql::server::password_encryption,
77+
Optional[Postgresql::Pg_password_encryption] $pg_hba_auth_password_encryption = $postgresql::server::pg_hba_auth_password_encryption,
7478
Optional[String] $extra_systemd_config = $postgresql::server::extra_systemd_config,
7579
) {
80+
$_pg_hba_auth_password_encryption = pick($pg_hba_auth_password_encryption,$password_encryption)
81+
7682
if ($manage_pg_hba_conf == true) {
7783
# Prepare the main pg_hba file
7884
concat { $pg_hba_conf_path:
@@ -105,7 +111,7 @@
105111
type => 'host',
106112
user => $user,
107113
address => '127.0.0.1/32',
108-
auth_method => 'md5',
114+
auth_method => $_pg_hba_auth_password_encryption,
109115
order => 3;
110116

111117
"deny access to postgresql user for instance ${name}":
@@ -118,13 +124,13 @@
118124
"allow access to all users for instance ${name}":
119125
type => 'host',
120126
address => $ip_mask_allow_all_users,
121-
auth_method => 'md5',
127+
auth_method => $_pg_hba_auth_password_encryption,
122128
order => 100;
123129

124130
"allow access to ipv6 localhost for instance ${name}":
125131
type => 'host',
126132
address => '::1/128',
127-
auth_method => 'md5',
133+
auth_method => $_pg_hba_auth_password_encryption,
128134
order => 101;
129135
}
130136
}

spec/acceptance/overridden_settings_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class { 'postgresql::server':
2626
type => 'host',
2727
database => 'mydb',
2828
user => 'myuser',
29-
auth_method => 'md5',
29+
auth_method => postgresql::default('password_encryption'),
3030
address => '192.0.2.100/32',
3131
},
3232
},

spec/functions/postgresql_password_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
require 'spec_helper'
44

55
describe 'postgresql_password' do
6+
include_examples 'Ubuntu 18.04'
7+
68
it_behaves_like 'postgresql_password function'
79
end

spec/functions/postgresql_postgresql_password_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
require 'spec_helper'
44

55
describe 'postgresql::postgresql_password' do
6+
include_examples 'Ubuntu 18.04'
7+
68
it_behaves_like 'postgresql_password function'
79
end

0 commit comments

Comments
 (0)