diff options
-rw-r--r-- | lib/puppet/type/ssh_authorized_key.rb | 20 | ||||
-rwxr-xr-x | spec/unit/type/ssh_authorized_key.rb | 9 |
2 files changed, 23 insertions, 6 deletions
diff --git a/lib/puppet/type/ssh_authorized_key.rb b/lib/puppet/type/ssh_authorized_key.rb index 997afb81e..33ed1d62b 100644 --- a/lib/puppet/type/ssh_authorized_key.rb +++ b/lib/puppet/type/ssh_authorized_key.rb @@ -39,11 +39,14 @@ module Puppet return super end - if user = resource[:user] + return nil unless user = resource[:user] + + begin return File.expand_path("~%s/.ssh/authorized_keys" % user) + rescue + Puppet.debug "The required user is not yet present on the system" + return nil end - - return nil end end @@ -77,9 +80,14 @@ module Puppet end validate do - unless should(:target) or should(:user) - raise Puppet::Error, "Attribute 'user' or 'target' is mandatory" - end + # Go ahead if target attribute is defined + return if @parameters[:target].shouldorig[0] != :absent + + # Go ahead if user attribute is defined + return if @parameters.include?(:user) + + # If neither target nor user is defined, this is an error + raise Puppet::Error, "Attribute 'user' or 'target' is mandatory" end end end diff --git a/spec/unit/type/ssh_authorized_key.rb b/spec/unit/type/ssh_authorized_key.rb index db389864e..f1e9963e9 100755 --- a/spec/unit/type/ssh_authorized_key.rb +++ b/spec/unit/type/ssh_authorized_key.rb @@ -121,4 +121,13 @@ describe ssh_authorized_key do resource.should(:target).should == target end end + + describe "when calling validate" do + it "should not crash on a non-existant user" do + resource = @class.create( + :name => "Test", + :user => "ihopesuchuserdoesnotexist") + proc { resource.validate }.should_not raise_error + end + end end |