Skip to content

add scram-sha-256 support #1313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 1, 2022
Merged

add scram-sha-256 support #1313

merged 5 commits into from
Feb 1, 2022

Conversation

fe80
Copy link
Contributor

@fe80 fe80 commented Nov 29, 2021

Hi,

Since pg 14, scram-sha-256 is the default encryption method. I've change the postgresql_password for support the new encoding methods and change in postgresql::server::role for use this function. The default methods of the function still md5, but that create a breaking (encode doesn't have default value on my code)

I've set the username as salt chain per default.

I've test and validate on postgresql14

This PR include #1132

Regards,

@fe80 fe80 requested a review from a team as a code owner November 29, 2021 12:36
@puppet-community-rangefinder
Copy link

postgresql::postgresql_password is a function

that may have no external impact to Forge modules.

postgresql::server::role is a type

that may have no external impact to Forge modules.

This module is declared in 70 of 578 indexed public Puppetfiles.


These results were generated with Rangefinder, a tool that helps predict the downstream impact of breaking changes to elements used in Puppet modules. You can run this on the command line to get a full report.

Exact matches are those that we can positively identify via namespace and the declaring modules' metadata. Non-namespaced items, such as Puppet 3.x functions, will always be reported as near matches only.

@CLAassistant
Copy link

CLAassistant commented Nov 29, 2021

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@ekohl ekohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit I'm not that familiar with SCRAM-SHA-256, but it looks ok to me.

Copy link
Collaborator

@ekohl ekohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there were various Rubocop violations and somehow it didn't fail on those. In #1311 I've tried to fix it, but I'm not sure about the different values that my system returns. Could it be that the hash is Ruby version dependent?

)
}
it {
is_expected.to run.with_params('foo', 'bar', nil, 'scram-sha-256').and_return(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nil parameter gives an error for me:

expected postgresql_password("foo", "bar", nil, "scram-sha-256", "salt") to have returned "SCRAM-SHA-256$4096:c2FsdA==$zOt2zFfUQMbpQf3/vRnYB33QDK/L7APOBHniLy39j/4=:DcW5Jp8Do7wYhVp1f9aT0cyhUfzIAozGcvzXZj+M3YI=" instead of raising ArgumentError('postgresql::postgresql_password' parameter 'sensitive' expects a Boolean value, got Undef)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

Effectively postgresql_password have a mandatory boolean on 3 positions, I don't understand why it's work on my ci (I'm a beginner with puppet functions),

That should be like you say here #1313 (comment)

irb(main):001:0> require 'openssl'
=> true
irb(main):002:0> require 'base64'
=> true
irb(main):003:0>   def pg_sha256(password, salt)
    digest = digest_key(password, salt)
    'SCRAM-SHA-256$%s:%s$%s:%s' % [
      '4096',
      Base64.strict_encode64(salt),
      Base64.strict_encode64(client_key(digest)),
      Base64.strict_encode64(server_key(digest))
    ]
  end

  def digest_key(password, salt)
    OpenSSL::KDF.pbkdf2_hmac(
      password,
      salt: salt,
      iterations: 4096,
      length: 32,
      hash: OpenSSL::Digest::SHA256.new
    )
  end

  def client_key(digest_key)
    hmac = OpenSSL::HMAC.new(digest_key, OpenSSL::Digest::SHA256.new)
    hmac << 'Client Key'
    hmac.digest
    OpenSSL::Digest.new('SHA256').digest hmac.digest
  end

  def server_key(digest_key)
    hmac = OpenSSL::HMAC.new(digest_key, OpenSSL::Digest::SHA256.new)
    hmac << 'Server Key'
    hmac.digest
  end
=> :pg_sha256
=> :digest_key
=> :client_key
=> :server_key
irb(main):036:0> pg_sha256('bar', 'foo')
=> "SCRAM-SHA-256$4096:Zm9v$ea66ynZ8cS9Ty4ZkEYicwC72StsKLSwjcXIXKMgepTk=:dJYmOU6BMCaWkQOB3lrXH9OAF3lW2n3NJ26NO7Srq7U="

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want I try to fix this on a new PR ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do. I just merged a PR which already fixed some Rubocop failures so be sure to base it on the latest commit.

}
it {
is_expected.to run.with_params('foo', 'bar', nil, 'scram-sha-256').and_return(
'SCRAM-SHA-256$4096:YmFy$y1VOaTvvs4V3OECvMzre9FtgCZClGuBLVE6sNPsTKbs=:HwFqmSKbihSyHMqkhufOy++cWCFIoTRSg8y6YgeALzE='
Copy link
Collaborator

@ekohl ekohl Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my local system I'm getting a different value:

expected postgresql_password("foo", "bar", false, "scram-sha-256") to have returned "SCRAM-SHA-256$4096:YmFy$y1VOaTvvs4V3OECvMzre9FtgCZClGuBLVE6sNPsTKbs=:HwFqmSKbihSyHMqkhufOy++cWCFIoTRSg8y6YgeALzE=" instead of "SCRAM-SHA-256$4096:Zm9v$ea66ynZ8cS9Ty4ZkEYicwC72StsKLSwjcXIXKMgepTk=:dJYmOU6BMCaWkQOB3lrXH9OAF3lW2n3NJ26NO7Srq7U="

@Neustradamus
Copy link

@fe80: Good job!

Linked to:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants