Class: RightScale::Certificate
- Defined in:
- lib/right_agent/security/certificate.rb
Overview
X.509 Certificate management
Instance Attribute Summary collapse
-
#raw_cert ⇒ Object
Underlying OpenSSL cert.
Class Method Summary collapse
-
.from_data(data) ⇒ Object
Initialize with raw certificate.
-
.load(file) ⇒ Object
Load certificate from file.
Instance Method Summary collapse
-
#data ⇒ Object
(also: #to_s)
Certificate data in PEM format.
-
#initialize(key, issuer, subject, valid_for = 3600*24*365*10) ⇒ Certificate
constructor
Generate a signed X.509 certificate.
-
#save(file) ⇒ Object
Save certificate to file in PEM format.
Constructor Details
#initialize(key, issuer, subject, valid_for = 3600*24*365*10) ⇒ Certificate
Generate a signed X.509 certificate
Parameters
- key(RsaKeyPair)
-
Key pair used to sign certificate
- issuer(DistinguishedName)
-
Certificate issuer
- subject(DistinguishedName)
-
Certificate subject
- valid_for(Integer)
-
Time in seconds before certificate expires, defaults to 10 years
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/right_agent/security/certificate.rb', line 38 def initialize(key, issuer, subject, valid_for = 3600*24*365*10) @raw_cert = OpenSSL::X509::Certificate.new @raw_cert.version = 2 @raw_cert.serial = 1 @raw_cert.subject = subject.to_x509 @raw_cert.issuer = issuer.to_x509 @raw_cert.public_key = key.to_public.raw_key @raw_cert.not_before = Time.now @raw_cert.not_after = Time.now + valid_for @raw_cert.sign(key.raw_key, OpenSSL::Digest::SHA1.new) end |
Instance Attribute Details
#raw_cert ⇒ Object
Underlying OpenSSL cert
29 30 31 |
# File 'lib/right_agent/security/certificate.rb', line 29 def raw_cert @raw_cert end |
Class Method Details
.from_data(data) ⇒ Object
Initialize with raw certificate
Parameters
- data(String)
-
Raw certificate data
Return
- res(Certificate)
-
Certificate
70 71 72 73 74 75 |
# File 'lib/right_agent/security/certificate.rb', line 70 def self.from_data(data) cert = OpenSSL::X509::Certificate.new(data) res = Certificate.allocate res.instance_variable_set(:@raw_cert, cert) res end |
.load(file) ⇒ Object
Load certificate from file
Parameters
- file(String)
-
File path name
Return
- res(Certificate)
-
Certificate
57 58 59 60 61 |
# File 'lib/right_agent/security/certificate.rb', line 57 def self.load(file) res = nil File.open(file, 'r') { |f| res = from_data(f) } if file res end |
Instance Method Details
#data ⇒ Object Also known as: to_s
Certificate data in PEM format
Return
- (String)
-
Certificate data
95 96 97 |
# File 'lib/right_agent/security/certificate.rb', line 95 def data @raw_cert.to_pem end |
#save(file) ⇒ Object
Save certificate to file in PEM format
Parameters
- file(String)
-
File path name
Return
- true
-
Always return true
84 85 86 87 88 89 |
# File 'lib/right_agent/security/certificate.rb', line 84 def save(file) File.open(file, "w") do |f| f.write(@raw_cert.to_pem) end true end |