diff options
-rw-r--r-- | lib/puppet/provider/ssh_authorized_key/parsed.rb | 15 | ||||
-rw-r--r-- | lib/puppet/type/ssh_authorized_key.rb | 14 | ||||
-rwxr-xr-x | spec/unit/provider/ssh_authorized_key/parsed.rb | 24 | ||||
-rwxr-xr-x | spec/unit/type/ssh_authorized_key.rb | 39 |
4 files changed, 48 insertions, 44 deletions
diff --git a/lib/puppet/provider/ssh_authorized_key/parsed.rb b/lib/puppet/provider/ssh_authorized_key/parsed.rb index 5604ba32a..051fb63ef 100644 --- a/lib/puppet/provider/ssh_authorized_key/parsed.rb +++ b/lib/puppet/provider/ssh_authorized_key/parsed.rb @@ -36,21 +36,8 @@ Puppet::Type.type(:ssh_authorized_key).provide(:parsed, :rts => /^\s+/, :match => /^(?:(.+) )?(\d+) (\d+) (\d+)(?: (.+))?$/ - def prefetch - # This was done in the type class but path expansion was failing for - # not yet existing users, the only workaround I found was to move that - # in the provider. - @resource[:target] = target - - super - end - def target - if user - File.expand_path("~%s/.ssh/authorized_keys" % user) - elsif target = @resource.should(:target) - target - end + @resource.should(:target) end def user diff --git a/lib/puppet/type/ssh_authorized_key.rb b/lib/puppet/type/ssh_authorized_key.rb index 66cf3e733..997afb81e 100644 --- a/lib/puppet/type/ssh_authorized_key.rb +++ b/lib/puppet/type/ssh_authorized_key.rb @@ -31,6 +31,20 @@ module Puppet newproperty(:target) do desc "The file in which to store the SSH key." + + defaultto :absent + + def should + if defined? @should and @should[0] != :absent + return super + end + + if user = resource[:user] + return File.expand_path("~%s/.ssh/authorized_keys" % user) + end + + return nil + end end newproperty(:options, :array_matching => :all) do diff --git a/spec/unit/provider/ssh_authorized_key/parsed.rb b/spec/unit/provider/ssh_authorized_key/parsed.rb index 73d623573..1e5d6be48 100755 --- a/spec/unit/provider/ssh_authorized_key/parsed.rb +++ b/spec/unit/provider/ssh_authorized_key/parsed.rb @@ -72,27 +72,6 @@ describe provider_class do genkey(key).should == "from=\"192.168.1.1\",no-pty,no-X11-forwarding ssh-rsa AAAAfsfddsjldjgksdflgkjsfdlgkj root@localhost\n" end - it "should prefetch ~user/.ssh/authorized_keys when user is given" do - key = Puppet::Type.type(:ssh_authorized_key).create( - :name => "Test", - :key => "AA", - :type => "rsa", - :ensure => :present, - :user => "root") - prov = @provider.new key - - prov.prefetch - prov.target.should == File.expand_path("~root/.ssh/authorized_keys") - end - - it "should create destination dir" do - # No idea how to test the flush method - end - - it "should set correct default permissions" do - # No idea how to test the flush method - end - it "'s parse_options method should be able to parse options containing commas" do options = %w{from="host1.reductlivelabs.com,host.reductivelabs.com" command="/usr/local/bin/run" ssh-pty} optionstr = options.join(", ") @@ -119,7 +98,8 @@ describe provider_class do describe "and a user has been specified" do before :each do @resource.stubs(:should).with(:user).returns "nobody" - @resource.stubs(:should).with(:target).returns nil + target = File.expand_path("~nobody/.ssh/authorized_keys") + @resource.stubs(:should).with(:target).returns target end it "should create the directory" do diff --git a/spec/unit/type/ssh_authorized_key.rb b/spec/unit/type/ssh_authorized_key.rb index 2cd5171c9..6d60ac2ef 100755 --- a/spec/unit/type/ssh_authorized_key.rb +++ b/spec/unit/type/ssh_authorized_key.rb @@ -89,14 +89,37 @@ describe ssh_authorized_key do @class.attrtype(:target).should == :property end - it "should raise an error when neither user nor target is given" do - proc do - @class.create( - :name => "Test", - :key => "AAA", - :type => "ssh-rsa", - :ensure => :present) - end.should raise_error(Puppet::Error) + describe "when neither user nor target is specified" do + it "should raise an error" do + proc do + @class.create( + :name => "Test", + :key => "AAA", + :type => "ssh-rsa", + :ensure => :present) + end.should raise_error(Puppet::Error) + end + end + + describe "when both target and user are specified" do + it "should use target" do + resource = @class.create( + :name => "Test", + :user => "root", + :target => "/tmp/blah") + resource.should(:target).should == "/tmp/blah" + end + end + + + describe "when user is specified" do + it "should determine target" do + resource = @class.create( + :name => "Test", + :user => "root") + target = File.expand_path("~root/.ssh/authorized_keys") + resource.should(:target).should == target + end end after do |