summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/ssl/certificate_factory.rb19
-rwxr-xr-xspec/unit/ssl/certificate_factory.rb107
2 files changed, 121 insertions, 5 deletions
diff --git a/lib/puppet/ssl/certificate_factory.rb b/lib/puppet/ssl/certificate_factory.rb
index abdeb8a2c..47b9f74d7 100644
--- a/lib/puppet/ssl/certificate_factory.rb
+++ b/lib/puppet/ssl/certificate_factory.rb
@@ -1,3 +1,5 @@
+require 'puppet/ssl'
+
# The tedious class that does all the manipulations to the
# certificate to correctly sign it. Yay.
class Puppet::SSL::CertificateFactory
@@ -8,23 +10,30 @@ class Puppet::SSL::CertificateFactory
"h" => 60 * 60,
"s" => 1
}
+
+ attr_reader :name, :cert_type, :csr, :issuer, :serial
def initialize(cert_type, csr, issuer, serial)
- @cert_type, @csr, @issuer = cert_type, csr, issuer
+ @cert_type, @csr, @issuer, @serial = cert_type, csr, issuer, serial
@name = @csr.subject
+ end
+ # Actually generate our certificate.
+ def result
@cert = OpenSSL::X509::Certificate.new
@cert.version = 2 # X509v3
- @cert.subject = csr.subject
- @cert.issuer = issuer.subject
- @cert.public_key = csr.public_key
- @cert.serial = serial
+ @cert.subject = @csr.subject
+ @cert.issuer = @issuer.subject
+ @cert.public_key = @csr.public_key
+ @cert.serial = @serial
build_extensions()
set_ttl
+
+ @cert
end
private
diff --git a/spec/unit/ssl/certificate_factory.rb b/spec/unit/ssl/certificate_factory.rb
new file mode 100755
index 000000000..822b330f2
--- /dev/null
+++ b/spec/unit/ssl/certificate_factory.rb
@@ -0,0 +1,107 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/ssl/certificate_factory'
+
+describe Puppet::SSL::CertificateFactory do
+ before do
+ @cert_type = mock 'cert_type'
+ @name = mock 'name'
+ @csr = stub 'csr', :subject => @name
+ @issuer = mock 'issuer'
+ @serial = mock 'serial'
+
+ @factory = Puppet::SSL::CertificateFactory.new(@cert_type, @csr, @issuer, @serial)
+ end
+
+ describe "when initializing" do
+ it "should set its :cert_type to its first argument" do
+ @factory.cert_type.should equal(@cert_type)
+ end
+
+ it "should set its :csr to its second argument" do
+ @factory.csr.should equal(@csr)
+ end
+
+ it "should set its :issuer to its third argument" do
+ @factory.issuer.should equal(@issuer)
+ end
+
+ it "should set its :serial to its fourth argument" do
+ @factory.serial.should equal(@serial)
+ end
+
+ it "should set its name to the subject of the csr" do
+ @factory.name.should equal(@name)
+ end
+ end
+
+ describe "when generating the certificate" do
+ before do
+ @cert = mock 'cert'
+
+ @cert.stub_everything
+
+ @factory.stubs :build_extensions
+
+ @factory.stubs :set_ttl
+
+ @issuer_name = mock 'issuer_name'
+ @issuer.stubs(:subject).returns @issuer_name
+
+ @public_key = mock 'public_key'
+ @csr.stubs(:public_key).returns @public_key
+
+ OpenSSL::X509::Certificate.stubs(:new).returns @cert
+ end
+
+ it "should return a new X509 certificate" do
+ OpenSSL::X509::Certificate.expects(:new).returns @cert
+ @factory.result.should equal(@cert)
+ end
+
+ it "should set the certificate's version to 2" do
+ @cert.expects(:version=).with 2
+ @factory.result
+ end
+
+ it "should set the certificate's subject to the CSR's subject" do
+ @cert.expects(:subject=).with @name
+ @factory.result
+ end
+
+ it "should set the certificate's issuer to the Issuer's subject" do
+ @cert.expects(:issuer=).with @issuer_name
+ @factory.result
+ end
+
+ it "should set the certificate's public key to the CSR's public key" do
+ @cert.expects(:public_key=).with @public_key
+ @factory.result
+ end
+
+ it "should set the certificate's serial number to the provided serial number" do
+ @cert.expects(:serial=).with @serial
+ @factory.result
+ end
+
+ it "should build extensions for the certificate" do
+ @factory.expects(:build_extensions)
+ @factory.result
+ end
+
+ it "should set the ttl of the certificate" do
+ @factory.expects(:set_ttl)
+ @factory.result
+ end
+ end
+
+ describe "when building extensions" do
+ it "should have tests"
+ end
+
+ describe "when setting the ttl" do
+ it "should have tests"
+ end
+end