Class: Puppet::SSL::Base
Overview
The base class for wrapping SSL instances.
Direct Known Subclasses
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
-
#content ⇒ Object
Returns the value of attribute content.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.from_instance(instance, name = nil) ⇒ Object
Create an instance of our Puppet::SSL::* class using a given instance of the wrapped class.
- .from_multiple_s(text) ⇒ Object
-
.from_s(string, name = nil) ⇒ Object
Convert a string into an instance.
-
.name_from_subject(subject) ⇒ String
private
name_from_subject extracts the common name attribute from the subject of an x.509 certificate certificate.
- .to_multiple_s(instances) ⇒ Object
- .validate_certname(name) ⇒ Object
- .wrapped_class ⇒ Object
- .wraps(klass) ⇒ Object
Instance Method Summary collapse
- #digest(algorithm = nil) ⇒ Object
- #digest_algorithm ⇒ Object
- #fingerprint(md = :SHA256) ⇒ Object
- #generate ⇒ Object
-
#initialize(name) ⇒ Base
constructor
A new instance of Base.
-
#read(path) ⇒ Object
Read content from disk appropriately.
- #to_data_hash ⇒ Object
-
#to_s ⇒ Object
Convert our thing to pem.
-
#to_text ⇒ Object
Provide the full text of the thing we’re dealing with.
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
#content ⇒ Object
Returns the value of attribute content.
37 38 39 |
# File 'lib/puppet/ssl/base.rb', line 37 def content @content end |
#name ⇒ Object
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
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_class ⇒ Object
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_algorithm ⇒ Object
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 |
#generate ⇒ Object
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_hash ⇒ Object
105 106 107 |
# File 'lib/puppet/ssl/base.rb', line 105 def to_data_hash to_s end |
#to_s ⇒ Object
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_text ⇒ Object
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 |