Class: Puppet::SSL::Base

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

Overview

The base class for wrapping SSL instances.

Direct Known Subclasses

Certificate, CertificateRequest

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.



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

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

Instance Attribute Details

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.from_instance(instance, name = nil) ⇒ Object

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



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

def self.from_instance(instance, name = nil)
  unless instance.is_a?(wrapped_class)
    raise ArgumentError, _("Object must be an instance of %{class_name}, %{actual_class} given") %
                         { class_name: wrapped_class, actual_class: instance.class }
  end
  if name.nil? and !instance.respond_to?(:subject)
    raise ArgumentError, _("Name must be supplied if it cannot be determined from the instance")
  end

  name ||= name_from_subject(instance.subject)
  result = new(name)
  result.content = instance
  result
end

.from_multiple_s(text) ⇒ Object



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

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

.from_s(string, name = nil) ⇒ Object

Convert a string into an instance



81
82
83
84
# File 'lib/puppet/ssl/base.rb', line 81

def self.from_s(string, name = nil)
  instance = wrapped_class.new(string)
  from_instance(instance, name)
end

.name_from_subject(subject) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

name_from_subject extracts the common name attribute from the subject of an x.509 certificate certificate

Parameters:

  • subject (OpenSSL::X509::Name)

    The full subject (distinguished name) of the x.509 certificate.

Returns:

  • (String)

    the name (CN) extracted from the subject.



58
59
60
61
62
# File 'lib/puppet/ssl/base.rb', line 58

def self.name_from_subject(subject)
  if subject.respond_to? :to_a
    (subject.to_a.assoc('CN') || [])[1]
  end
end

.to_multiple_s(instances) ⇒ Object



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

def self.to_multiple_s(instances)
  instances.collect(&:to_s).join(SEPARATOR)
end

.validate_certname(name) ⇒ Object



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

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

.wrapped_classObject

Raises:



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

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

  @wrapped_class
end

.wraps(klass) ⇒ Object



23
24
25
# File 'lib/puppet/ssl/base.rb', line 23

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

Instance Method Details

#digest(algorithm = nil) ⇒ Object



121
122
123
124
125
# File 'lib/puppet/ssl/base.rb', line 121

def digest(algorithm = nil)
  algorithm ||= digest_algorithm

  Puppet::SSL::Digest.new(algorithm, content.to_der)
end

#digest_algorithmObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puppet/ssl/base.rb', line 127

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/i,
    /md[245]/i,
    /sha\d*/i
  )
  ln = content.signature_algorithm
  match = digest_re.match(ln)
  if match
    match[0].downcase
  else
    raise Puppet::Error, _("Unknown signature algorithm '%{ln}'") % { ln: ln }
  end
end

#fingerprint(md = :SHA256) ⇒ Object



116
117
118
119
# File 'lib/puppet/ssl/base.rb', line 116

def fingerprint(md = :SHA256)
  mds = md.to_s.upcase
  digest(mds).to_hex
end

#generateObject

Raises:



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

def generate
  raise Puppet::DevError, _("%{class_name} did not override 'generate'") % { class_name: self.class }
end

#read(path) ⇒ Object

Read content from disk appropriately.



87
88
89
90
91
92
93
94
95
96
# File 'lib/puppet/ssl/base.rb', line 87

def read(path)
  # applies to Puppet::SSL::Certificate, Puppet::SSL::CertificateRequest
  # nothing derives from Puppet::SSL::Certificate, but it is called by a number of other SSL Indirectors:
  # Puppet::Indirector::CertificateStatus::File (.indirection.find)
  # Puppet::Network::HTTP::WEBrick (.indirection.find)
  # Puppet::Network::HTTP::RackREST (.from_instance)
  # Puppet::Network::HTTP::WEBrickREST (.from_instance)
  # Puppet::SSL::Inventory (.indirection.search, implements its own add / rebuild / serials with encoding UTF8)
  @content = wrapped_class.new(Puppet::FileSystem.read(path, :encoding => Encoding::ASCII))
end

#to_data_hashObject



105
106
107
# File 'lib/puppet/ssl/base.rb', line 105

def to_data_hash
  to_s
end

#to_sObject

Convert our thing to pem.



99
100
101
102
103
# File 'lib/puppet/ssl/base.rb', line 99

def to_s
  return "" unless content

  content.to_pem
end

#to_textObject

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



110
111
112
113
114
# File 'lib/puppet/ssl/base.rb', line 110

def to_text
  return "" unless content

  content.to_text
end