summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/provider/ssh_authorized_key/parsed.rb16
-rw-r--r--lib/puppet/type/ssh_authorized_key.rb36
-rwxr-xr-xspec/unit/type/ssh_authorized_key.rb59
3 files changed, 88 insertions, 23 deletions
diff --git a/lib/puppet/provider/ssh_authorized_key/parsed.rb b/lib/puppet/provider/ssh_authorized_key/parsed.rb
index 7cb6626de..351ebcd1b 100644
--- a/lib/puppet/provider/ssh_authorized_key/parsed.rb
+++ b/lib/puppet/provider/ssh_authorized_key/parsed.rb
@@ -29,21 +29,5 @@ Puppet::Type.type(:ssh_authorized_key).provide(:parsed,
record[:options] = record[:options].join(',')
end
}
-
- def prefetch
- if not @resource.should(:target)
- #
- # Set default target when user is given
- if val = @resource.should(:user)
- target = File.expand_path("~%s/.ssh/authorized_keys" % val)
- Puppet::debug("Setting target to %s" % target)
- @resource[:target] = target
- else
- raise Puppet::Error, "Missing attribute 'user' or 'target'"
- end
- end
-
- super
- end
end
diff --git a/lib/puppet/type/ssh_authorized_key.rb b/lib/puppet/type/ssh_authorized_key.rb
index e28fb7cda..a95f316af 100644
--- a/lib/puppet/type/ssh_authorized_key.rb
+++ b/lib/puppet/type/ssh_authorized_key.rb
@@ -27,6 +27,11 @@ 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
@@ -39,6 +44,37 @@ 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)
+ end
+ end
+
+ validate do
+ unless should(:target)
+ raise Puppet::Error, "Attribute 'user' or 'target' is mandatory"
+ end
+ end
end
end
diff --git a/spec/unit/type/ssh_authorized_key.rb b/spec/unit/type/ssh_authorized_key.rb
index 0e869747d..3c87decf1 100755
--- a/spec/unit/type/ssh_authorized_key.rb
+++ b/spec/unit/type/ssh_authorized_key.rb
@@ -14,6 +14,7 @@ describe ssh_authorized_key do
@provider = stub 'provider', :class => @provider_class, :file_path => "/tmp/whatever", :clear => nil
@provider_class.stubs(:new).returns(@provider)
+ @catalog = Puppet::Node::Catalog.new
end
it "should have a name parameter" do
@@ -33,27 +34,27 @@ describe ssh_authorized_key do
end
it "should support :present as a value for :ensure" do
- proc { @class.create(:name => "whev", :ensure => :present) }.should_not raise_error
+ proc { @class.create(:name => "whev", :ensure => :present, :user => "nobody") }.should_not raise_error
end
it "should support :absent as a value for :ensure" do
- proc { @class.create(:name => "whev", :ensure => :absent) }.should_not raise_error
+ proc { @class.create(:name => "whev", :ensure => :absent, :user => "nobody") }.should_not raise_error
end
it "should have an type property" do
@class.attrtype(:type).should == :property
end
it "should support ssh-dss as an type value" do
- proc { @class.create(:name => "whev", :type => "ssh-dss") }.should_not raise_error
+ proc { @class.create(:name => "whev", :type => "ssh-dss", :user => "nobody") }.should_not raise_error
end
it "should support ssh-rsa as an type value" do
- proc { @class.create(:name => "whev", :type => "ssh-rsa") }.should_not raise_error
+ proc { @class.create(:name => "whev", :type => "ssh-rsa", :user => "nobody") }.should_not raise_error
end
it "should support :dsa as an type value" do
- proc { @class.create(:name => "whev", :type => :dsa) }.should_not raise_error
+ proc { @class.create(:name => "whev", :type => :dsa, :user => "nobody") }.should_not raise_error
end
it "should support :rsa as an type value" do
- proc { @class.create(:name => "whev", :type => :rsa) }.should_not raise_error
+ proc { @class.create(:name => "whev", :type => :rsa, :user => "nobody") }.should_not raise_error
end
it "should not support values other than ssh-dss, ssh-rsa, dsa, rsa in the ssh_authorized_key_type" do
@@ -76,5 +77,49 @@ describe ssh_authorized_key do
@class.attrtype(:target).should == :property
end
- after { @class.clear }
+ 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
+
+ after do
+ @class.clear
+ @catalog.clear
+ end
end