Skip to content

Commit e185638

Browse files
committed
Add ability to use hex hash with caching_sha2_password plugin
1 parent 49ebfc3 commit e185638

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/puppet/functions/mysql/password.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
return_type 'Variant[String, Sensitive[String]]'
2020
end
2121

22-
def password(password, sensitive = false)
22+
def password(password, sensitive = false) # rubocop:disable Style/OptionalBooleanParameter
2323
password = password.unwrap if password.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
2424

25-
result_string = if %r{\*[A-F0-9]{40}$}.match?(password)
25+
result_string = if %r{\*[A-F0-9]{40}$}.match?(password) or %r{0x[A-F0-9]+$}.match?(password)
2626
password
2727
elsif password.empty?
2828
''

lib/puppet/provider/mysql_user/mysql.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ def self.instances
2323
# rubocop:enable Layout/LineLength
2424
@max_user_connections, @max_connections_per_hour, @max_queries_per_hour, @max_updates_per_hour, ssl_type, ssl_cipher,
2525
x509_issuer, x509_subject, @password, @plugin, @authentication_string = mysql_caller(query, 'regular').chomp.split(%r{\t})
26+
27+
if @plugin == 'caching_sha2_password'
28+
@password = mysql_caller("SELECT CONCAT('0x',HEX('#{@password}'))", 'regular').chomp
29+
end
30+
2631
@tls_options = parse_tls_options(ssl_type, ssl_cipher, x509_issuer, x509_subject)
2732
if (newer_than('mariadb' => '10.1.21') && (@plugin == 'ed25519' || @plugin == 'mysql_native_password')) ||
2833
(newer_than('mariadb' => '10.2.16') && older_than('mariadb' => '10.2.19')) ||
@@ -76,6 +81,8 @@ def create
7681
if !plugin.nil?
7782
if password_hash.nil?
7883
self.class.mysql_caller("CREATE USER '#{merged_name}' IDENTIFIED WITH '#{plugin}'", 'system')
84+
elsif plugin.eql? "caching_sha2_password"
85+
self.class.mysql_caller("CREATE USER '#{merged_name}' IDENTIFIED WITH '#{plugin}' AS X'#{password_hash[2..-1]}'", 'system')
7986
else
8087
self.class.mysql_caller("CREATE USER '#{merged_name}' IDENTIFIED WITH '#{plugin}' AS '#{password_hash}'", 'system')
8188
end
@@ -159,9 +166,11 @@ def password_hash=(string)
159166
end
160167
self.class.mysql_caller(sql, 'system')
161168
elsif !mysqld_version.nil? && newer_than('mysql' => '5.7.6', 'percona' => '5.7.6', 'mariadb' => '10.2.0')
162-
raise ArgumentError, _('Only mysql_native_password (*ABCD...XXX) hashes are supported.') unless %r{^\*|^$}.match?(string)
169+
raise ArgumentError, _('Only mysql_native_password (*ABCD...XXX) or caching_sha2_password (0x1234ABC...XXX) hashes are supported.') unless %r{^\*|^$}.match?(string) || %r{0x[A-F0-9]+$}.match?(string)
163170

164-
self.class.mysql_caller("ALTER USER #{merged_name} IDENTIFIED WITH mysql_native_password AS '#{string}'", 'system')
171+
sql = "ALTER USER #{merged_name} IDENTIFIED WITH"
172+
plugin == 'caching_sha2_password' ? sql += " '#{plugin}' AS X'#{@resource[:password_hash][2..-1]}'" : sql += " 'mysql_native_password' AS '#{@resource[:password_hash]}'"
173+
self.class.mysql_caller(sql, 'system')
165174
else
166175
# default ... if mysqld_version does not work
167176
self.class.mysql_caller("SET PASSWORD FOR #{merged_name} = '#{string}'", 'system')

0 commit comments

Comments
 (0)