Class: Puppet::SSL::Base

Inherits:
Object show all
Defined in:
lib/vendor/puppet/ssl/base.rb

Overview

The base class for wrapping SSL instances.

Constant Summary collapse

SEPARATOR =

For now, use the YAML separator.

"\n---\n"
VALID_CERTNAME =

Only allow printing ascii characters, excluding /

/\A[ -.0-~]+\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Base

Returns a new instance of Base.



44
45
46
47
# File 'lib/vendor/puppet/ssl/base.rb', line 44

def initialize(name)
  @name = name.to_s.downcase
  self.class.validate_certname(@name)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



33
34
35
# File 'lib/vendor/puppet/ssl/base.rb', line 33

def content
  @content
end

#nameObject

Returns the value of attribute name.



33
34
35
# File 'lib/vendor/puppet/ssl/base.rb', line 33

def name
  @name
end

Class Method Details

.from_multiple_s(text) ⇒ Object



12
13
14
# File 'lib/vendor/puppet/ssl/base.rb', line 12

def self.from_multiple_s(text)
  text.split(SEPARATOR).collect { |inst| from_s(inst) }
end

.to_multiple_s(instances) ⇒ Object



16
17
18
# File 'lib/vendor/puppet/ssl/base.rb', line 16

def self.to_multiple_s(instances)
  instances.collect { |inst| inst.to_s }.join(SEPARATOR)
end

.validate_certname(name) ⇒ Object



29
30
31
# File 'lib/vendor/puppet/ssl/base.rb', line 29

def self.validate_certname(name)
  raise "Certname #{name.inspect} must not contain unprintable or non-ASCII characters" unless name =~ VALID_CERTNAME
end

.wrapped_classObject

Raises:



24
25
26
27
# File 'lib/vendor/puppet/ssl/base.rb', line 24

def self.wrapped_class
  raise(Puppet::DevError, "#{self} has not declared what class it wraps") unless defined?(@wrapped_class)
  @wrapped_class
end

.wraps(klass) ⇒ Object



20
21
22
# File 'lib/vendor/puppet/ssl/base.rb', line 20

def self.wraps(klass)
  @wrapped_class = klass
end

Instance Method Details

#ca?Boolean

Is this file for the CA?

Returns:

  • (Boolean)


36
37
38
# File 'lib/vendor/puppet/ssl/base.rb', line 36

def ca?
  name == Puppet::SSL::Host.ca_name
end

#fingerprint(md = :MD5) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vendor/puppet/ssl/base.rb', line 66

def fingerprint(md = :MD5)
  # ruby 1.8.x openssl digest constants are string
  # but in 1.9.x they are symbols
  mds = md.to_s.upcase
  if OpenSSL::Digest.constants.include?(mds)
    md = mds
  elsif OpenSSL::Digest.constants.include?(mds.to_sym)
    md = mds.to_sym
  else
    raise ArgumentError, "#{md} is not a valid digest algorithm for fingerprinting certificate #{name}"
  end

  OpenSSL::Digest.const_get(md).hexdigest(content.to_der).scan(/../).join(':').upcase
end

#generateObject

Raises:



40
41
42
# File 'lib/vendor/puppet/ssl/base.rb', line 40

def generate
  raise Puppet::DevError, "#{self.class} did not override 'generate'"
end

#read(path) ⇒ Object

Read content from disk appropriately.



50
51
52
# File 'lib/vendor/puppet/ssl/base.rb', line 50

def read(path)
  @content = wrapped_class.new(File.read(path))
end

#to_sObject

Convert our thing to pem.



55
56
57
58
# File 'lib/vendor/puppet/ssl/base.rb', line 55

def to_s
  return "" unless content
  content.to_pem
end

#to_textObject

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



61
62
63
64
# File 'lib/vendor/puppet/ssl/base.rb', line 61

def to_text
  return "" unless content
  content.to_text
end