diff options
| author | Luke Kanies <luke@madstop.com> | 2008-04-16 12:34:53 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2008-04-16 12:34:53 -0500 |
| commit | c19c9d436d4d8ca0a5773065b53348242d237651 (patch) | |
| tree | 8d676822ba0d372963f14cb3e33b3ddf36cc4316 | |
| parent | 054e4e431a145737c42d767249f1b94685c9a6d7 (diff) | |
| download | puppet-c19c9d436d4d8ca0a5773065b53348242d237651.tar.gz puppet-c19c9d436d4d8ca0a5773065b53348242d237651.tar.xz puppet-c19c9d436d4d8ca0a5773065b53348242d237651.zip | |
Removing all the cases where the ssl host specifies
a terminus. Also, getting rid of some metaprogramming
that wasn't really helping.
| -rw-r--r-- | lib/puppet/ssl/host.rb | 88 | ||||
| -rwxr-xr-x | spec/unit/ssl/host.rb | 134 |
2 files changed, 24 insertions, 198 deletions
diff --git a/lib/puppet/ssl/host.rb b/lib/puppet/ssl/host.rb index 373ee5003..6f49175aa 100644 --- a/lib/puppet/ssl/host.rb +++ b/lib/puppet/ssl/host.rb @@ -19,6 +19,8 @@ class Puppet::SSL::Host # Search for more than one host, optionally only specifying # an interest in hosts with a given file type. + # This just allows our non-indirected class to have one of + # indirection methods. def self.search(options = {}) classes = [Key, CertificateRequest, Certificate] if klass = options[:for] @@ -26,62 +28,35 @@ class Puppet::SSL::Host 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| + classlist.collect { |klass| klass.search }.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) - var = "@%s" % name - - maker = "generate_%s" % name - reader = "read_%s" % name - - classname = file2constant(name.to_s) - - begin - klass = const_get(classname) - rescue - raise Puppet::DevError, "Cannot map %s to a valid constant" % name - end - - # Define the method that creates it. - define_method(maker, &block) - - # Define the reading method. - define_method(reader) do - klass.find(self.name) - end - - # Define the overall method, which just calls the reader and maker - # as appropriate. - define_method(name) do - unless cert = instance_variable_get(var) - return nil unless cert = send(reader) - instance_variable_set(var, cert) - end - cert.content - end + def key + return nil unless (defined?(@key) and @key) or @key = Key.find(name) + @key.content end # This is the private key; we can create it from scratch # with no inputs. - manage_file :key do + def generate_key @key = Key.new(name) @key.generate @key.save :in => :file true end + def certificate_request + return nil unless (defined?(@certificate_request) and @certificate_request) or @certificate_request = CertificateRequest.find(name) + @certificate_request.content + end + # Our certificate request requires the key but that's all. - manage_file :certificate_request do + def generate_certificate_request generate_key unless key @certificate_request = CertificateRequest.new(name) @certificate_request.generate(key) @@ -89,19 +64,11 @@ class Puppet::SSL::Host return true end - # Our certificate itself might not successfully "generate", since - # that generation is actually accomplished by a CA signing the - # stored CSR. - manage_file :certificate do - generate_certificate_request unless certificate_request - - @certificate = Certificate.new(name) - if @certificate.generate(certificate_request) - @certificate.save :in => :file - return true - else - return false - end + # There's no ability to generate a certificate -- if we don't have it, then we should be + # automatically looking in the ca, and if the ca doesn't have it, we don't have one. + def certificate + return nil unless (defined?(@certificate) and @certificate) or @certificate = Certificate.find(name) + @certificate.content end # Is this a ca host, meaning that all of its files go in the CA collections? @@ -126,23 +93,4 @@ class Puppet::SSL::Host def public_key key.public_key end - - # Try to get our signed certificate. - def retrieve_signed_certificate(source = :ca_file) - if cert = Puppet::SSL::Certificate.find(name, :in => source) - @certificate = cert - @certificate.save :in => :file - return true - else - return false - end - end - - # Send our CSR to the server, defaulting to the - # local CA. - def send_certificate_request(dest = :ca_file) - raise ArgumentError, "Must generate CSR before sending to server" unless certificate_request - - @certificate_request.save :in => dest - end end diff --git a/spec/unit/ssl/host.rb b/spec/unit/ssl/host.rb index 9588722f4..e82971683 100755 --- a/spec/unit/ssl/host.rb +++ b/spec/unit/ssl/host.rb @@ -53,7 +53,7 @@ describe Puppet::SSL::Host do Puppet::SSL::Key.expects(:new).with("myname").returns(@key) @key.expects(:generate) - @key.expects(:save).with(:in => :file) + @key.expects(:save) @host.generate_key.should be_true @host.key.should equal(@realkey) @@ -87,10 +87,12 @@ describe Puppet::SSL::Host do Puppet::SSL::CertificateRequest.expects(:new).with("myname").returns @request key = stub 'key', :public_key => mock("public_key") + + @host.expects(:key).times(2).returns(nil).then.returns(key) @host.expects(:generate_key).returns(key) @request.stubs(:generate) - @request.stubs(:save).with(:in => :file) + @request.stubs(:save) @host.generate_certificate_request end @@ -101,7 +103,7 @@ describe Puppet::SSL::Host do key = stub 'key', :public_key => mock("public_key") @host.stubs(:key).returns(key) @request.expects(:generate).with(key) - @request.expects(:save).with(:in => :file) + @request.expects(:save) @host.generate_certificate_request.should be_true @host.certificate_request.should equal(@realrequest) @@ -120,46 +122,13 @@ describe Puppet::SSL::Host do @realcert = mock 'certificate' @cert = stub 'cert', :content => @realcert end + it "should find the certificate in the Certificate class and return the SSL certificate, not the wrapper" do Puppet::SSL::Certificate.expects(:find).with("myname").returns @cert @host.certificate.should equal(@realcert) end - it "should generate a new certificate request when generating the cert if no request exists" do - Puppet::SSL::Certificate.expects(:new).with("myname").returns @cert - - request = stub 'request' - @host.expects(:generate_certificate_request) - - @cert.stubs(:generate) - @cert.stubs(:save).with(:in => :file) - - @host.generate_certificate - end - - it "should be able to generate and save a new certificate using the certificate request" do - Puppet::SSL::Certificate.expects(:new).with("myname").returns @cert - - request = stub 'request' - @host.stubs(:certificate_request).returns(request) - @cert.expects(:generate).with(request).returns(true) - @cert.expects(:save).with(:in => :file) - - @host.generate_certificate.should be_true - @host.certificate.should equal(@realcert) - end - - it "should return false if no certificate could be generated" do - Puppet::SSL::Certificate.expects(:new).with("myname").returns @cert - - request = stub 'request' - @host.stubs(:certificate_request).returns(request) - @cert.expects(:generate).with(request).returns(false) - - @host.generate_certificate.should be_false - end - it "should return any previously found certificate" do Puppet::SSL::Certificate.expects(:find).with("myname").returns(@cert).once @@ -184,83 +153,6 @@ describe Puppet::SSL::Host do end end - describe "when sending its CSR to the CA" do - before do - @realrequest = "real request" - @request = stub 'request', :content => @realrequest - - @host.instance_variable_set("@certificate_request", @request) - end - - it "should be able to send its CSR" do - @request.expects(:save) - - @host.send_certificate_request - end - - it "should default to sending its CSR to the :ca_file" do - @request.expects(:save).with(:in => :ca_file) - - @host.send_certificate_request - end - - it "should allow specification of another CA terminus" do - @request.expects(:save).with(:in => :rest) - - @host.send_certificate_request :rest - end - end - - describe "when retrieving its signed certificate from the CA" do - before do - @realcert = "real cert" - @cert = stub 'cert', :content => @realcert - end - - it "should be able to send its CSR" do - Puppet::SSL::Certificate.expects(:find).with { |*args| args[0] == @host.name } - - @host.retrieve_signed_certificate - end - - it "should default to searching for its certificate in the :ca_file" do - Puppet::SSL::Certificate.expects(:find).with { |*args| args[1] == {:in => :ca_file} } - - @host.retrieve_signed_certificate - end - - it "should allow specification of another CA terminus" do - Puppet::SSL::Certificate.expects(:find).with { |*args| args[1] == {:in => :rest} } - - @host.retrieve_signed_certificate :rest - end - - it "should return true and set its certificate if retrieval was successful" do - cert = stub 'cert', :content => "mycert", :save => nil - Puppet::SSL::Certificate.stubs(:find).returns cert - - @host.retrieve_signed_certificate.should be_true - @host.certificate.should == "mycert" - end - - it "should save the retrieved certificate to the local disk" do - cert = stub 'cert', :content => "mycert" - Puppet::SSL::Certificate.stubs(:find).returns cert - - cert.expects(:save).with :in => :file - - @host.retrieve_signed_certificate - @host.certificate - end - - it "should return false and not set its certificate if retrieval was unsuccessful" do - Puppet::SSL::Certificate.stubs(:find).returns nil - - @host.retrieve_signed_certificate.should be_false - @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 @@ -294,20 +186,6 @@ describe Puppet::SSL::Host do 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" |
