Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / pw_hash.rb
index ad3e393..0deeb3a 100644 (file)
@@ -1,3 +1,6 @@
+
+#  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(
   :pw_hash,
   :type => :rvalue,
@@ -24,6 +27,13 @@ Puppet::Parser::Functions::newfunction(
   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',
@@ -38,19 +48,19 @@ Puppet::Parser::Functions::newfunction(
     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'
-        # override String#crypt for password variable
-        def password.crypt(salt)
-          # puppetserver bundles Apache Commons Codec
-          org.apache.commons.codec.digest.Crypt.crypt(self.to_java_bytes, salt)
-        end
+        # 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
+    else
+      password.crypt(salt)
     end
-    password.crypt("$#{hash_type}$#{args[2]}")
 end