Skip to content

Commit 9c1a2b7

Browse files
(FM-8971) WIP-allow deferred function for pwd
1 parent 821b5c5 commit 9c1a2b7

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

lib/puppet/functions/postgresql/postgresql_password.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
end
2929

3030
def default_impl(username, password, sensitive = false, hash = 'md5', salt = nil)
31+
if password.is_a?(String) && password.match?(%r{^(md5|SCRAM-SHA-256).+})
32+
return password
33+
end
3134
password = password.unwrap if password.respond_to?(:unwrap)
3235
pass = if hash == 'md5'
3336
'md5' + Digest::MD5.hexdigest(password.to_s + username.to_s)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Puppet::Functions.create_function(:'postgresql::prepend_sql_password') do
2+
dispatch :default_impl do
3+
required_param 'String', :password
4+
return_type 'String'
5+
end
6+
def default_impl(password)
7+
"ENCRYPTED PASSWORD #{password}"
8+
end
9+
end

manifests/server/role.pp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,25 @@
8484
$createdb_sql = $createdb ? { true => 'CREATEDB', default => 'NOCREATEDB' }
8585
$superuser_sql = $superuser ? { true => 'SUPERUSER', default => 'NOSUPERUSER' }
8686
$replication_sql = $replication ? { true => 'REPLICATION', default => '' }
87-
if ($password_hash_unsensitive != false) {
88-
$password_sql = "ENCRYPTED PASSWORD '${password_hash_unsensitive}'"
87+
88+
if (type($password_hash_unsensitive) =~ Type[Deferred]) {
89+
$password_sql = Deferred('postgresql::prepend_sql_password', [$password_hash_unsensitive])
90+
} elsif ($password_hash_unsensitive != false) {
91+
$password_sql = postgresql::prepend_sql_password($password_hash_unsensitive)
8992
} else {
9093
$password_sql = ''
9194
}
9295

96+
if type($password_sql) =~ Type[Deferred] {
97+
$command = Deferred('sprintf', ["CREATE ROLE \"%s\" %s %s %s %s %s %s CONNECTION LIMIT %s",
98+
$username, $password_sql, $login_sql, $createrole_sql, $createdb_sql,
99+
$superuser_sql, $replication_sql, $connection_limit])
100+
} else {
101+
$command = "CREATE ROLE \"${username}\" ${password_sql} ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql} ${replication_sql} CONNECTION LIMIT ${connection_limit}"
102+
}
103+
93104
postgresql_psql { "CREATE ROLE ${username} ENCRYPTED PASSWORD ****":
94-
command => Sensitive("CREATE ROLE \"${username}\" ${password_sql} ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql} ${replication_sql} CONNECTION LIMIT ${connection_limit}"),
105+
command => Sensitive($command),
95106
unless => "SELECT 1 FROM pg_roles WHERE rolname = '${username}'",
96107
require => undef,
97108
sensitive => true,
@@ -134,9 +145,14 @@
134145
}
135146

136147
if $password_hash_unsensitive and $update_password {
137-
if($password_hash_unsensitive =~ /^(md5|SCRAM-SHA-256).+/) {
138-
$pwd_hash_sql = $password_hash_unsensitive
139-
} else {
148+
if type($password_hash_unsensitive) =~ Type[Deferred] {
149+
$pwd_hash_sql = Deferred('postgresql::postgresql_password',[$username,
150+
$password_hash,
151+
$password_hash =~ Sensitive[String],
152+
$hash,
153+
$salt])
154+
}
155+
else {
140156
$pwd_hash_sql = postgresql::postgresql_password(
141157
$username,
142158
$password_hash,
@@ -145,9 +161,18 @@
145161
$salt,
146162
)
147163
}
164+
if type($pwd_hash_sql) =~ Type[Deferred] {
165+
$pw_command = Deferred('sprintf', ["ALTER ROLE \"%s\" ENCRYPTED PASSWORD '%s'", $username, $pwd_hash_sql])
166+
$unless_pw_command = Deferred('sprintf', ["SELECT 1 FROM pg_shadow WHERE usename = '%s' AND passwd = '%s'",
167+
$username,
168+
$pwd_hash_sql])
169+
} else {
170+
$pw_command = "ALTER ROLE \"${username}\" ENCRYPTED PASSWORD '${pwd_hash_sql}'"
171+
$unless_pw_command = "SELECT 1 FROM pg_shadow WHERE usename = '${username}' AND passwd = '${pwd_hash_sql}'"
172+
}
148173
postgresql_psql { "ALTER ROLE ${username} ENCRYPTED PASSWORD ****":
149-
command => Sensitive("ALTER ROLE \"${username}\" ENCRYPTED PASSWORD '${pwd_hash_sql}'"),
150-
unless => Sensitive("SELECT 1 FROM pg_shadow WHERE usename = '${username}' AND passwd = '${pwd_hash_sql}'"),
174+
command => Sensitive($pw_command),
175+
unless => Sensitive($unless_pw_command),
151176
sensitive => true,
152177
}
153178
}

0 commit comments

Comments
 (0)