diff options
author | Luke Kanies <luke@madstop.com> | 2008-04-01 18:24:46 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-04-15 21:34:06 -0500 |
commit | c98ad25403dbb27289048513af48d0b3e1723b18 (patch) | |
tree | 15a544c0666d276fc645ccd9ff45d3d76f2f3b8a | |
parent | d184b3539db2e857b8df424171a1beed4560a035 (diff) | |
download | puppet-c98ad25403dbb27289048513af48d0b3e1723b18.tar.gz puppet-c98ad25403dbb27289048513af48d0b3e1723b18.tar.xz puppet-c98ad25403dbb27289048513af48d0b3e1723b18.zip |
Adding a :search method to the ssl_file terminus type
and the SSL::Host class.
-rw-r--r-- | lib/puppet/indirector/ssl_file.rb | 12 | ||||
-rw-r--r-- | lib/puppet/ssl/host.rb | 19 | ||||
-rwxr-xr-x | spec/unit/indirector/ssl_file.rb | 39 | ||||
-rwxr-xr-x | spec/unit/ssl/host.rb | 70 |
4 files changed, 140 insertions, 0 deletions
diff --git a/lib/puppet/indirector/ssl_file.rb b/lib/puppet/indirector/ssl_file.rb index 7a1501dbf..17cb0a144 100644 --- a/lib/puppet/indirector/ssl_file.rb +++ b/lib/puppet/indirector/ssl_file.rb @@ -62,6 +62,18 @@ class Puppet::Indirector::SslFile < Puppet::Indirector::Terminus end end + # Search for more than one file. At this point, it just returns + # an instance for every file in the directory. + def search(options = {}) + dir = collection_directory + Dir.entries(dir).reject { |file| file !~ /\.pem$/ }.collect do |file| + name = file.sub(/\.pem$/, '') + result = model.new(name) + result.read(File.join(dir, file)) + result + end + end + private # A demeterish pointer to the collection directory. diff --git a/lib/puppet/ssl/host.rb b/lib/puppet/ssl/host.rb index 0e65d30b1..373ee5003 100644 --- a/lib/puppet/ssl/host.rb +++ b/lib/puppet/ssl/host.rb @@ -17,6 +17,25 @@ class Puppet::SSL::Host attr_reader :name attr_accessor :ca + # Search for more than one host, optionally only specifying + # an interest in hosts with a given file type. + def self.search(options = {}) + classes = [Key, CertificateRequest, Certificate] + if klass = options[:for] + classlist = [klass].flatten + else + classlist = [Key, CertificateRequest, Certificate] + end + args = {} + args[:in] = options[:in] if options[:in] + + # Collect the results from each class, flatten them, collect all of the names, make the name list unique, + # then create a Host instance for each one. + classlist.collect { |klass| klass.search(args) }.flatten.collect { |r| r.name }.uniq.collect do |name| + new(name) + end + end + # A bit of metaprogramming that we use to define all of # the methods for managing our ssl-related files. def self.manage_file(name, &block) diff --git a/spec/unit/indirector/ssl_file.rb b/spec/unit/indirector/ssl_file.rb index 5c148a967..90faf1533 100755 --- a/spec/unit/indirector/ssl_file.rb +++ b/spec/unit/indirector/ssl_file.rb @@ -126,5 +126,44 @@ describe Puppet::Indirector::SslFile do end end end + + describe "when searching for certificates" do + before do + @model = mock 'model' + @file_class.stubs(:model).returns @model + end + it "should return a certificate instance for all files that exist" do + Dir.expects(:entries).with(@path).returns %w{one.pem two.pem} + + one = stub 'one', :read => nil + two = stub 'two', :read => nil + + @model.expects(:new).with("one").returns one + @model.expects(:new).with("two").returns two + + @searcher.search.should == [one, two] + end + + it "should read each certificate in using the model's :read method" do + Dir.expects(:entries).with(@path).returns %w{one.pem} + + one = stub 'one' + one.expects(:read).with(File.join(@path, "one.pem")) + + @model.expects(:new).with("one").returns one + + @searcher.search + end + + it "should skip any files that do not match /\.pem$/" do + Dir.expects(:entries).with(@path).returns %w{. .. one.pem} + + one = stub 'one', :read => nil + + @model.expects(:new).with("one").returns one + + @searcher.search + end + end end end diff --git a/spec/unit/ssl/host.rb b/spec/unit/ssl/host.rb index 4a3fbbb83..9588722f4 100755 --- a/spec/unit/ssl/host.rb +++ b/spec/unit/ssl/host.rb @@ -260,4 +260,74 @@ describe Puppet::SSL::Host do @host.certificate.should be_nil end end + + it "should have a method for listing certificate hosts" do + Puppet::SSL::Host.should respond_to(:search) + end + + describe "when listing certificate hosts" do + it "should default to listing all clients with any file types" do + Puppet::SSL::Key.expects(:search).returns [] + Puppet::SSL::Certificate.expects(:search).returns [] + Puppet::SSL::CertificateRequest.expects(:search).returns [] + Puppet::SSL::Host.search + end + + it "should be able to list only clients with a key" do + Puppet::SSL::Key.expects(:search).returns [] + Puppet::SSL::Certificate.expects(:search).never + Puppet::SSL::CertificateRequest.expects(:search).never + Puppet::SSL::Host.search :for => Puppet::SSL::Key + end + + it "should be able to list only clients with a certificate" do + Puppet::SSL::Key.expects(:search).never + Puppet::SSL::Certificate.expects(:search).returns [] + Puppet::SSL::CertificateRequest.expects(:search).never + Puppet::SSL::Host.search :for => Puppet::SSL::Certificate + end + + it "should be able to list only clients with a certificate request" do + Puppet::SSL::Key.expects(:search).never + Puppet::SSL::Certificate.expects(:search).never + Puppet::SSL::CertificateRequest.expects(:search).returns [] + Puppet::SSL::Host.search :for => Puppet::SSL::CertificateRequest + end + + it "should default to not specifying a search terminus" do + Puppet::SSL::Key.expects(:search).with({}).returns [] + Puppet::SSL::Certificate.expects(:search).with({}).returns [] + Puppet::SSL::CertificateRequest.expects(:search).with({}).returns [] + Puppet::SSL::Host.search + end + + it "should use any specified search terminus" do + Puppet::SSL::Key.expects(:search).with(:in => :ca_file).returns [] + Puppet::SSL::Certificate.expects(:search).with(:in => :ca_file).returns [] + Puppet::SSL::CertificateRequest.expects(:search).with(:in => :ca_file).returns [] + Puppet::SSL::Host.search :in => :ca_file + end + + it "should return a Host instance created with the name of each found instance" do + key = stub 'key', :name => "key" + cert = stub 'cert', :name => "cert" + csr = stub 'csr', :name => "csr" + + Puppet::SSL::Key.expects(:search).returns [key] + Puppet::SSL::Certificate.expects(:search).returns [cert] + Puppet::SSL::CertificateRequest.expects(:search).returns [csr] + + returned = [] + %w{key cert csr}.each do |name| + result = mock(name) + returned << result + Puppet::SSL::Host.expects(:new).with(name).returns result + end + + result = Puppet::SSL::Host.search + returned.each do |r| + result.should be_include(r) + end + end + end end |