| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'openssl' |
| 4 |
require 'base64' |
| 5 |
|
| 6 |
# @summary This function returns the postgresql password hash from the clear text username / password |
| 7 |
Puppet::Functions.create_function(:'postgresql::postgresql_password') do |
| 8 |
# @param username |
| 9 |
# The clear text `username` |
| 10 |
# @param password |
| 11 |
# The clear text `password` |
| 12 |
# @param sensitive |
| 13 |
# If the Postgresql-Passwordhash should be of Datatype Sensitive[String] |
| 14 |
# @param hash |
| 15 |
# Set type for password hash |
| 16 |
# |
| 17 |
# Default value comes from `postgresql::params::password_encryption` and changes based on the `postgresql::globals::version`. |
| 18 |
# @param salt |
| 19 |
# Use a specific salt value for scram-sha-256, default is username |
| 20 |
# |
| 21 |
# @return |
| 22 |
# The postgresql password hash from the clear text username / password. |
| 23 |
dispatch :default_impl do |
| 24 |
required_param 'Variant[String[1], Integer]', :username |
| 25 |
required_param 'Variant[String[1], Sensitive[String[1]], Integer]', :password |
| 26 |
optional_param 'Boolean', :sensitive |
| 27 |
optional_param 'Optional[Postgresql::Pg_password_encryption]', :hash |
| 28 |
optional_param 'Optional[Variant[String[1], Integer]]', :salt |
| 29 |
return_type 'Variant[String, Sensitive[String]]' |
| 30 |
end |
| 31 |
|
| 32 |
def default_impl(username, password, sensitive = false, hash = nil, salt = nil) |
| 33 |
hash = call_function('postgresql::default', 'password_encryption') if hash.nil? |
| 34 |
password = password.unwrap if password.respond_to?(:unwrap) |
| 35 |
if password.is_a?(String) && password.match?(%r{^(md5[0-9a-f]{32}$|SCRAM-SHA-256\$)}) |
| 36 |
return Puppet::Pops::Types::PSensitiveType::Sensitive.new(password) if sensitive |
| 37 |
|
| 38 |
return password |
| 39 |
end |
| 40 |
pass = case hash |
| 41 |
when 'md5', nil # ensure default value when definded with nil |
| 42 |
"md5#{Digest::MD5.hexdigest(password.to_s + username.to_s)}" |
| 43 |
when 'scram-sha-256' |
| 44 |
pg_sha256(password, (salt || username)) |
| 45 |
else |
| 46 |
raise(Puppet::ParseError, "postgresql::postgresql_password(): got unkown hash type '#{hash}'") |
| 47 |
end |
| 48 |
if sensitive |
| 49 |
Puppet::Pops::Types::PSensitiveType::Sensitive.new(pass) |
| 50 |
else |
| 51 |
pass |
| 52 |
end |
| 53 |
end |
| 54 |
|
| 55 |
def pg_sha256(password, salt) |
| 56 |
digest = digest_key(password, salt) |
| 57 |
'SCRAM-SHA-256$%{iterations}:%{salt}$%{client_key}:%{server_key}' % { |
| 58 |
iterations: '4096', |
| 59 |
salt: Base64.strict_encode64(salt), |
| 60 |
client_key: Base64.strict_encode64(client_key(digest)), |
| 61 |
server_key: Base64.strict_encode64(server_key(digest)) |
| 62 |
} |
| 63 |
end |
| 64 |
|
| 65 |
def digest_key(password, salt) |
| 66 |
OpenSSL::KDF.pbkdf2_hmac( |
| 67 |
password, |
| 68 |
salt: salt, |
| 69 |
iterations: 4096, |
| 70 |
length: 32, |
| 71 |
hash: OpenSSL::Digest.new('SHA256'), |
| 72 |
) |
| 73 |
end |
| 74 |
|
| 75 |
def client_key(digest_key) |
| 76 |
hmac = OpenSSL::HMAC.new(digest_key, OpenSSL::Digest.new('SHA256')) |
| 77 |
hmac << 'Client Key' |
| 78 |
hmac.digest |
| 79 |
OpenSSL::Digest.new('SHA256').digest hmac.digest |
| 80 |
end |
| 81 |
|
| 82 |
def server_key(digest_key) |
| 83 |
hmac = OpenSSL::HMAC.new(digest_key, OpenSSL::Digest.new('SHA256')) |
| 84 |
hmac << 'Server Key' |
| 85 |
hmac.digest |
| 86 |
end |
| 87 |
end |