diff --git a/REFERENCE.md b/REFERENCE.md index f2d0e3315..fb8fc6c30 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -60,7 +60,7 @@ _Private Resource types_ then returns the resulting hash. * [`mysql::password`](#mysqlpassword): Hash a string as mysql's "PASSWORD()" function would do it * [`mysql::strip_hash`](#mysqlstrip_hash): When given a hash this function strips out all blank entries. -* [`mysql_password`](#mysql_password): Hash a string as mysql's "PASSWORD()" function would do it +* [`mysql_password`](#mysql_password): DEPRECATED. Use the namespaced function [`mysql::password`](#mysqlpassword) instead. **Tasks** @@ -1176,7 +1176,7 @@ Default value: present Valid values: %r{\w*} -The password hash of the user. Use mysql_password() for creating such a hash. +The password hash of the user. Use mysql::password() for creating such a hash. ##### `plugin` @@ -1310,15 +1310,15 @@ Hash to be stripped ### mysql_password -Type: Ruby 3.x API +Type: Ruby 4.x API -Hash a string as mysql's "PASSWORD()" function would do it +DEPRECATED. Use the namespaced function [`mysql::password`](#mysqlpassword) instead. #### `mysql_password(String $password)` The mysql_password function. -Returns: `String` the mysql password hash from the clear text password. +Returns: `String` The mysql password hash from the 4.x function mysql::password. ##### `password` diff --git a/lib/puppet/functions/mysql_password.rb b/lib/puppet/functions/mysql_password.rb new file mode 100644 index 000000000..d2ac76d38 --- /dev/null +++ b/lib/puppet/functions/mysql_password.rb @@ -0,0 +1,17 @@ +# @summary DEPRECATED. Use the namespaced function [`mysql::password`](#mysqlpassword) instead. +Puppet::Functions.create_function(:mysql_password) do + # @param password + # Plain text password. + # + # @return + # The mysql password hash from the 4.x function mysql::password. + dispatch :mysql_password do + required_param 'String', :password + return_type 'String' + end + + def mysql_password(password) + call_function('deprecation', 'mysql_password', "This method has been deprecated, please use the namespaced version 'mysql::password' instead.") + call_function('mysql::password', password) + end +end diff --git a/lib/puppet/parser/functions/mysql_password.rb b/lib/puppet/parser/functions/mysql_password.rb deleted file mode 100644 index 53ba580b2..000000000 --- a/lib/puppet/parser/functions/mysql_password.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'digest/sha1' -module Puppet::Parser::Functions - newfunction(:mysql_password, type: :rvalue, doc: <<-EOS - @summary - Hash a string as mysql's "PASSWORD()" function would do it - - @param [String] password Plain text password. - - @return [String] the mysql password hash from the clear text password. - EOS - ) do |args| - - if args.size != 1 - raise Puppet::ParseError, _('mysql_password(): Wrong number of arguments given (%{args_length} for 1)') % { args_length: args.length } - end - - return '' if args[0].empty? - return args[0] if args[0] =~ %r{\*[A-F0-9]{40}$} - '*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase - end -end diff --git a/lib/puppet/type/mysql_user.rb b/lib/puppet/type/mysql_user.rb index a2d3c7856..e008375a4 100644 --- a/lib/puppet/type/mysql_user.rb +++ b/lib/puppet/type/mysql_user.rb @@ -47,7 +47,7 @@ end newproperty(:password_hash) do - desc 'The password hash of the user. Use mysql_password() for creating such a hash.' + desc 'The password hash of the user. Use mysql::password() for creating such a hash.' newvalue(%r{\w*}) def change_to_s(currentvalue, _newvalue) diff --git a/spec/functions/mysql_password_spec.rb b/spec/functions/mysql_password_spec.rb index ef90399d4..a1dfffcb4 100644 --- a/spec/functions/mysql_password_spec.rb +++ b/spec/functions/mysql_password_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'mysql::password' do +shared_examples 'mysql::password function' do it 'exists' do is_expected.not_to eq(nil) end @@ -29,3 +29,13 @@ is_expected.to run.with_params('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19').and_return('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19') end end + +describe 'mysql::password' do + it_behaves_like 'mysql::password function' + + describe 'non-namespaced shim' do + describe 'mysql_password', type: :puppet_function do + it_behaves_like 'mysql::password function' + end + end +end diff --git a/spec/unit/puppet/functions/mysql_password_spec.rb b/spec/unit/puppet/functions/mysql_password_spec.rb deleted file mode 100644 index 85c4d44a2..000000000 --- a/spec/unit/puppet/functions/mysql_password_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'spec_helper' - -describe 'the mysql_password function' do - before :all do # rubocop:disable RSpec/BeforeAfterAll - Puppet::Parser::Functions.autoloader.loadall - end - - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } - - it 'exists' do - expect(Puppet::Parser::Functions.function('mysql_password')).to eq('function_mysql_password') - end - - it 'raises a ParseError if there is less than 1 arguments' do - expect { scope.function_mysql_password([]) }.to(raise_error(Puppet::ParseError)) - end - - it 'raises a ParseError if there is more than 1 arguments' do - expect { scope.function_mysql_password(['foo', 'bar']) }.to(raise_error(Puppet::ParseError)) - end - - it 'converts password into a hash' do - result = scope.function_mysql_password(['password']) - expect(result).to(eq('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19')) - end - - it 'converts an empty password into a empty string' do - result = scope.function_mysql_password(['']) - expect(result).to(eq('')) - end - - it 'does not convert a password that is already a hash' do - result = scope.function_mysql_password(['*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19']) - expect(result).to(eq('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19')) - end -end