Skip to content

Commit b46be2d

Browse files
committed
Improve documentation and fail on Windows
1 parent 3906d21 commit b46be2d

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ Calling the class or definition from outside the current module will fail. For e
389389
|5 |SHA-256 |
390390
|6 |SHA-512 (recommended)|
391391

392-
The third argument to this function is the sale to use.
392+
The third argument to this function is the salt to use.
393393

394394
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.
395395

lib/puppet/parser/functions/pw_hash.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@
3232
password = args[0].to_s
3333
return nil if password.empty?
3434

35-
# work around JRuby bug in String#crypt for JRuby < 1.7.17
36-
if RUBY_PLATFORM == 'java' and 'test'.crypt('$1$1') != '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
37-
def password.crypt(salt)
38-
# puppetserver bundles Apache Commons Codec
39-
org.apache.commons.codec.digest.Crypt.crypt(self.to_java_bytes, salt)
35+
# handle weak implementations of String#crypt
36+
if 'test'.crypt('$1$1') != '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
37+
# JRuby < 1.7.17
38+
if RUBY_PLATFORM == 'java'
39+
# override String#crypt for password variable
40+
def password.crypt(salt)
41+
# puppetserver bundles Apache Commons Codec
42+
org.apache.commons.codec.digest.Crypt.crypt(self.to_java_bytes, salt)
43+
end
44+
else
45+
# MS Windows and other systems that don't support enhanced salts
46+
raise Puppet::ParseError, 'system does not support enhanced salts'
4047
end
4148
end
4249
password.crypt("$#{args[1]}$#{args[2]}")

spec/functions/pw_hash_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
expect { scope.function_pw_hash(['password', 6, '%']) }.to( raise_error(ArgumentError, /characters in salt must be in the set/) )
4545
end
4646

47+
it "should fail on platforms with weak implementations of String#crypt" do
48+
String.any_instance.expects(:crypt).with('$1$1').returns('$1SoNol0Ye6Xk')
49+
expect { scope.function_pw_hash(['password', 6, 'salt']) }.to( raise_error(Puppet::ParseError, /system does not support enhanced salts/) )
50+
end
51+
4752
it "should return a hashed password" do
4853
result = scope.function_pw_hash(['password', 6, 'salt'])
4954
expect(result).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.')

0 commit comments

Comments
 (0)