X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fpw_hash.rb;fp=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fpw_hash.rb;h=eaf1d7478756ed934c02902ad8cb2e46a1ea7509;hb=131e09855e065be940e104d9ab0f18940cc76257;hp=0deeb3a6cf2527e9934f1887511d27271cc40081;hpb=407d322498f4fde815abf381007fbecfe5c10b2b;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/pw_hash.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/pw_hash.rb index 0deeb3a6c..eaf1d7478 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/pw_hash.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/pw_hash.rb @@ -1,7 +1,5 @@ - # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. - -Puppet::Parser::Functions::newfunction( +Puppet::Parser::Functions.newfunction( :pw_hash, :type => :rvalue, :arity => 3, @@ -25,42 +23,40 @@ Puppet::Parser::Functions::newfunction( Note: this uses the Puppet Master's implementation of crypt(3). If your environment contains several different operating systems, ensure that they - are compatible before using this function.") do |args| - raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 - args.map! do |arg| - if arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive - arg.unwrap - else - arg - end - end - raise ArgumentError, "pw_hash(): first argument must be a string" unless args[0].is_a? String or args[0].nil? - raise ArgumentError, "pw_hash(): second argument must be a string" unless args[1].is_a? String - hashes = { 'md5' => '1', - 'sha-256' => '5', - 'sha-512' => '6' } - hash_type = hashes[args[1].downcase] - raise ArgumentError, "pw_hash(): #{args[1]} is not a valid hash type" if hash_type.nil? - raise ArgumentError, "pw_hash(): third argument must be a string" unless args[2].is_a? String - raise ArgumentError, "pw_hash(): third argument must not be empty" if args[2].empty? - raise ArgumentError, "pw_hash(): characters in salt must be in the set [a-zA-Z0-9./]" unless args[2].match(/\A[a-zA-Z0-9.\/]+\z/) - - password = args[0] - return nil if password.nil? or password.empty? - - salt = "$#{hash_type}$#{args[2]}" - - # handle weak implementations of String#crypt - if 'test'.crypt('$1$1') != '$1$1$Bp8CU9Oujr9SSEw53WV6G.' - # JRuby < 1.7.17 - if RUBY_PLATFORM == 'java' - # puppetserver bundles Apache Commons Codec - org.apache.commons.codec.digest.Crypt.crypt(password.to_java_bytes, salt) - else - # MS Windows and other systems that don't support enhanced salts - raise Puppet::ParseError, 'system does not support enhanced salts' - end + are compatible before using this function.", +) do |args| + raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 + args.map! do |arg| + if (defined? Puppet::Pops::Types::PSensitiveType::Sensitive) && (arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive) + arg.unwrap else - password.crypt(salt) + arg end + end + raise ArgumentError, 'pw_hash(): first argument must be a string' unless args[0].is_a?(String) || args[0].nil? + raise ArgumentError, 'pw_hash(): second argument must be a string' unless args[1].is_a? String + hashes = { 'md5' => '1', + 'sha-256' => '5', + 'sha-512' => '6' } + hash_type = hashes[args[1].downcase] + raise ArgumentError, "pw_hash(): #{args[1]} is not a valid hash type" if hash_type.nil? + raise ArgumentError, 'pw_hash(): third argument must be a string' unless args[2].is_a? String + raise ArgumentError, 'pw_hash(): third argument must not be empty' if args[2].empty? + raise ArgumentError, 'pw_hash(): characters in salt must be in the set [a-zA-Z0-9./]' unless args[2] =~ %r{\A[a-zA-Z0-9./]+\z} + + password = args[0] + return nil if password.nil? || password.empty? + + salt = "$#{hash_type}$#{args[2]}" + + # handle weak implementations of String#crypt + if 'test'.crypt('$1$1') != '$1$1$Bp8CU9Oujr9SSEw53WV6G.' + # JRuby < 1.7.17 + # MS Windows and other systems that don't support enhanced salts + raise Puppet::ParseError, 'system does not support enhanced salts' unless RUBY_PLATFORM == 'java' + # puppetserver bundles Apache Commons Codec + org.apache.commons.codec.digest.Crypt.crypt(password.to_java_bytes, salt) + else + password.crypt(salt) + end end