From: Francois Deppierraz <[EMAIL PROTECTED]>

This avoid exceptions during type instanciation when a user does not yet exist.
The drawback is that we cannot use generated resources anymore and have to
mkdir, chown and chmod directly in the provided which is somewhat hackish.

Signed-off-by: Francois Deppierraz <[EMAIL PROTECTED]>
---
 lib/puppet/provider/ssh_authorized_key/parsed.rb |   36 +++++++++++++++++++
 lib/puppet/type/ssh_authorized_key.rb            |   26 +-------------
 spec/unit/provider/ssh_authorized_key/parsed.rb  |   21 +++++++++++
 spec/unit/type/ssh_authorized_key.rb             |   41 ----------------------
 4 files changed, 58 insertions(+), 66 deletions(-)

diff --git a/lib/puppet/provider/ssh_authorized_key/parsed.rb 
b/lib/puppet/provider/ssh_authorized_key/parsed.rb
index 351ebcd..3bd22c0 100644
--- a/lib/puppet/provider/ssh_authorized_key/parsed.rb
+++ b/lib/puppet/provider/ssh_authorized_key/parsed.rb
@@ -29,5 +29,41 @@ Puppet::Type.type(:ssh_authorized_key).provide(:parsed,
                 record[:options] = record[:options].join(',')
             end
         }
+
+    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.
+        if user = @resource.should(:user)
+            target = File.expand_path("~%s/.ssh/authorized_keys" % user)
+            @property_hash[:target] = target
+            @resource[:target] = target
+        end
+
+        super
+    end
+
+    def flush
+        # As path expansion had to be moved in the provider, we cannot 
generate new file
+        # resources and thus have to chown and chmod here. It smells hackish.
+        
+        # Create target's parent directory if nonexistant
+        if target = @property_hash[:target]
+            dir = File.dirname(@property_hash[:target])
+            if not File.exist? dir
+                Puppet.debug("Creating directory %s which did not exist" % dir)
+                Dir.mkdir(dir, 0700)
+            end
+        end
+
+        # Generate the file
+        super
+
+        # Ensure correct permissions
+        if target and user = @property_hash[:user]
+            File.chown(Puppet::Util.uid(user), nil, dir)
+            File.chown(Puppet::Util.uid(user), nil, @property_hash[:target])
+        end
+    end
 end
 
diff --git a/lib/puppet/type/ssh_authorized_key.rb 
b/lib/puppet/type/ssh_authorized_key.rb
index 3a12e95..1db4a0a 100644
--- a/lib/puppet/type/ssh_authorized_key.rb
+++ b/lib/puppet/type/ssh_authorized_key.rb
@@ -27,11 +27,6 @@ module Puppet
 
         newproperty(:user) do
             desc "The user account in which the SSH key should be installed."
-
-            def value=(value)
-                @resource[:target] = 
File.expand_path("~%s/.ssh/authorized_keys" % value)
-                super
-            end
         end
 
         newproperty(:target) do
@@ -45,25 +40,6 @@ module Puppet
             defaultto do :absent end
         end
 
-        def generate
-            atype = Puppet::Type.type(:file)
-            target = self.should(:target)
-            dir =  File.dirname(target)
-            user = should(:user) ? should(:user) : "root"
-
-            rels = []
-
-            unless catalog.resource(:file, dir)
-                rels << atype.create(:name => dir, :ensure => :directory, 
:mode => 0700, :owner => user)
-            end
-
-            unless catalog.resource(:file, target)
-                rels << atype.create(:name => target, :ensure => :present, 
:mode => 0600, :owner => user)
-            end
-
-            rels
-        end
-
         autorequire(:user) do
             if should(:user)
                 should(:user)
@@ -71,7 +47,7 @@ module Puppet
         end
 
         validate do
-            unless should(:target)
+            unless should(:target) or should(:user)
                 raise Puppet::Error, "Attribute 'user' or 'target' is 
mandatory"
             end
         end
diff --git a/spec/unit/provider/ssh_authorized_key/parsed.rb 
b/spec/unit/provider/ssh_authorized_key/parsed.rb
index c35ddc5..16efc5b 100755
--- a/spec/unit/provider/ssh_authorized_key/parsed.rb
+++ b/spec/unit/provider/ssh_authorized_key/parsed.rb
@@ -71,4 +71,25 @@ describe provider_class do
 
         genkey(key).should == "from=\"192.168.1.1\",no-pty,no-X11-forwarding 
ssh-rsa AAAAfsfddsjldjgksdflgkjsfdlgkj [EMAIL PROTECTED]"
     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
 end
diff --git a/spec/unit/type/ssh_authorized_key.rb 
b/spec/unit/type/ssh_authorized_key.rb
index 0b41af4..e214784 100755
--- a/spec/unit/type/ssh_authorized_key.rb
+++ b/spec/unit/type/ssh_authorized_key.rb
@@ -77,47 +77,6 @@ describe ssh_authorized_key do
         @class.attrtype(:target).should == :property
     end
 
-    it "should autorequire parent directories when user is given" do
-        @catalog.add_resource @class.create(
-          :name   => "Test",
-          :key    => "AAA",
-          :type   => "ssh-rsa",
-          :ensure => :present,
-          :user   => "root")
-        @catalog.apply
-
-        target = File.expand_path("~root/.ssh")
-        @catalog.resource(:file, target).should 
be_an_instance_of(Puppet::Type.type(:file))
-    end
-
-    it "should set target when user is given" do
-        @catalog.add_resource @class.create(
-          :name   => "Test",
-          :key    => "AAA",
-          :type   => "ssh-rsa",
-          :ensure => :present,
-          :user   => "root")
-        @catalog.apply
-
-        target = File.expand_path("~root/.ssh/authorized_keys")
-        @catalog.resource(:file, target).should 
be_an_instance_of(Puppet::Type.type(:file))
-    end
-
-
-    it "should autorequire parent directories when target is given" do
-        target = "/tmp/home/foo/bar/.ssh/authorized_keys"
-
-        @catalog.add_resource @class.create(
-          :name   => "Test",
-          :key    => "AAA",
-          :type   => "ssh-rsa",
-          :ensure => :present,
-          :target => target)
-        @catalog.apply
-
-        @catalog.resource(:file, target).should 
be_an_instance_of(Puppet::Type.type(:file))
-    end
-
     it "should raise an error when neither user nor target is given" do
         proc do
             @class.create(
-- 
1.5.4.3


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to