class Puppet::SSL::Base

The base class for wrapping SSL instances.

Constants

SEPARATOR

For now, use the YAML separator.

VALID_CERTNAME

Only allow printing ascii characters, excluding /

Attributes

content[RW]
name[RW]

Public Class Methods

from_instance(instance, name = nil) click to toggle source

Create an instance of our Puppet::SSL::* class using a given instance of the wrapped class

# File lib/puppet/ssl/base.rb, line 56
def self.from_instance(instance, name = nil)
  raise ArgumentError, "Object must be an instance of #{wrapped_class}, #{instance.class} given" unless instance.is_a? wrapped_class
  raise ArgumentError, "Name must be supplied if it cannot be determined from the instance" if name.nil? and !instance.respond_to?(:subject)

  name ||= name_from_subject(instance.subject)
  result = new(name)
  result.content = instance
  result
end
from_multiple_s(text) click to toggle source
# File lib/puppet/ssl/base.rb, line 13
def self.from_multiple_s(text)
  text.split(SEPARATOR).collect { |inst| from_s(inst) }
end
from_s(string, name = nil) click to toggle source

Convert a string into an instance

# File lib/puppet/ssl/base.rb, line 67
def self.from_s(string, name = nil)
  instance = wrapped_class.new(string)
  from_instance(instance, name)
end
name_from_subject(subject) click to toggle source

Method to extract a ‘name’ from the subject of a certificate

# File lib/puppet/ssl/base.rb, line 51
def self.name_from_subject(subject)
  subject.to_s.sub(/\/CN=/, '')
end
new(name) click to toggle source
# File lib/puppet/ssl/base.rb, line 45
def initialize(name)
  @name = name.to_s.downcase
  self.class.validate_certname(@name)
end
to_multiple_s(instances) click to toggle source
# File lib/puppet/ssl/base.rb, line 17
def self.to_multiple_s(instances)
  instances.collect { |inst| inst.to_s }.join(SEPARATOR)
end
validate_certname(name) click to toggle source
# File lib/puppet/ssl/base.rb, line 30
def self.validate_certname(name)
  raise "Certname #{name.inspect} must not contain unprintable or non-ASCII characters" unless name =~ VALID_CERTNAME
end
wrapped_class() click to toggle source
# File lib/puppet/ssl/base.rb, line 25
def self.wrapped_class
  raise(Puppet::DevError, "#{self} has not declared what class it wraps") unless defined?(@wrapped_class)
  @wrapped_class
end
wraps(klass) click to toggle source
# File lib/puppet/ssl/base.rb, line 21
def self.wraps(klass)
  @wrapped_class = klass
end

Public Instance Methods

ca?() click to toggle source

Is this file for the CA?

# File lib/puppet/ssl/base.rb, line 37
def ca?
  name == Puppet::SSL::Host.ca_name
end
digest(algorithm=nil) click to toggle source
# File lib/puppet/ssl/base.rb, line 94
def digest(algorithm=nil)
  unless algorithm
    algorithm = digest_algorithm
  end

  Puppet::SSL::Digest.new(algorithm, content.to_der)
end
digest_algorithm() click to toggle source
# File lib/puppet/ssl/base.rb, line 102
def digest_algorithm
  # The signature_algorithm on the X509 cert is a combination of the digest
  # algorithm and the encryption algorithm
  # e.g. md5WithRSAEncryption, sha256WithRSAEncryption
  # Unfortunately there isn't a consistent pattern
  # See RFCs 3279, 5758
  digest_re = Regexp.union(
    /ripemd160/,
    /md[245]/,
    /sha\d*/
  )
  ln = content.signature_algorithm
  if match = digest_re.match(ln)
    match[0].downcase
  else
    raise Puppet::Error, "Unknown signature algorithm '#{ln}'"
  end
end
fingerprint(md = :SHA256) click to toggle source
# File lib/puppet/ssl/base.rb, line 89
def fingerprint(md = :SHA256)
  mds = md.to_s.upcase
  digest(mds).to_hex
end
generate() click to toggle source
# File lib/puppet/ssl/base.rb, line 41
def generate
  raise Puppet::DevError, "#{self.class} did not override 'generate'"
end
read(path) click to toggle source

Read content from disk appropriately.

# File lib/puppet/ssl/base.rb, line 73
def read(path)
  @content = wrapped_class.new(File.read(path))
end
to_s() click to toggle source

Convert our thing to pem.

# File lib/puppet/ssl/base.rb, line 78
def to_s
  return "" unless content
  content.to_pem
end
to_text() click to toggle source

Provide the full text of the thing we’re dealing with.

# File lib/puppet/ssl/base.rb, line 84
def to_text
  return "" unless content
  content.to_text
end