Class: Fluent::CaGenerate
- Inherits:
-
Object
- Object
- Fluent::CaGenerate
- Defined in:
- lib/fluent/command/ca_generate.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ private_key_length: 2048, cert_country: 'US', cert_state: 'CA', cert_locality: 'Mountain View', cert_common_name: 'Fluentd Forward CA', }
- HELP_TEXT =
<<HELP Usage: fluent-ca-generate DIR_PATH PRIVATE_KEY_PASSPHRASE [--country COUNTRY] [--state STATE] [--locality LOCALITY] [--common-name COMMON_NAME] HELP
Class Method Summary collapse
- .certificates_from_file(path) ⇒ Object
- .generate_ca_pair(opts = {}) ⇒ Object
- .generate_self_signed_server_pair(opts = {}) ⇒ Object
- .generate_server_pair(opts = {}) ⇒ Object
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(argv = ARGV) ⇒ CaGenerate
constructor
A new instance of CaGenerate.
- #usage(msg = nil) ⇒ Object
Constructor Details
#initialize(argv = ARGV) ⇒ CaGenerate
Returns a new instance of CaGenerate.
19 20 21 22 23 24 25 26 |
# File 'lib/fluent/command/ca_generate.rb', line 19 def initialize(argv = ARGV) @argv = argv @options = {} @opt_parser = OptionParser.new configure_option_parser @options.merge!(DEFAULT_OPTIONS) end |
Class Method Details
.certificates_from_file(path) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/fluent/command/ca_generate.rb', line 59 def self.certificates_from_file(path) data = File.read(path) pattern = Regexp.compile('-+BEGIN CERTIFICATE-+\n(?:[^-]*\n)+-+END CERTIFICATE-+\n', Regexp::MULTILINE) list = [] data.scan(pattern){|match| list << OpenSSL::X509::Certificate.new(match)} list end |
.generate_ca_pair(opts = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fluent/command/ca_generate.rb', line 67 def self.generate_ca_pair(opts={}) key = OpenSSL::PKey::RSA.generate(opts[:private_key_length]) issuer = subject = OpenSSL::X509::Name.new subject.add_entry('C', opts[:cert_country]) subject.add_entry('ST', opts[:cert_state]) subject.add_entry('L', opts[:cert_locality]) subject.add_entry('CN', opts[:cert_common_name]) digest = OpenSSL::Digest::SHA256.new factory = OpenSSL::X509::ExtensionFactory.new cert = OpenSSL::X509::Certificate.new cert.not_before = Time.at(0) cert.not_after = Time.now + 5 * 365 * 86400 # 5 years after cert.public_key = key cert.serial = 1 cert.issuer = issuer cert.subject = subject cert.add_extension(factory.create_extension('basicConstraints', 'CA:TRUE')) cert.sign(key, digest) return cert, key end |
.generate_self_signed_server_pair(opts = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/fluent/command/ca_generate.rb', line 125 def self.generate_self_signed_server_pair(opts={}) key = OpenSSL::PKey::RSA.generate(opts[:private_key_length]) issuer = subject = OpenSSL::X509::Name.new subject.add_entry('C', opts[:country]) subject.add_entry('ST', opts[:state]) subject.add_entry('L', opts[:locality]) subject.add_entry('CN', opts[:common_name]) digest = OpenSSL::Digest::SHA256.new cert = OpenSSL::X509::Certificate.new cert.not_before = Time.at(0) cert.not_after = Time.now + 5 * 365 * 86400 # 5 years after cert.public_key = key cert.serial = 1 cert.issuer = issuer cert.subject = subject cert.sign(key, digest) return cert, key end |
.generate_server_pair(opts = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/fluent/command/ca_generate.rb', line 93 def self.generate_server_pair(opts={}) key = OpenSSL::PKey::RSA.generate(opts[:private_key_length]) ca_key = OpenSSL::PKey::read(File.read(opts[:ca_key_path]), opts[:ca_key_passphrase]) ca_cert = OpenSSL::X509::Certificate.new(File.read(opts[:ca_cert_path])) issuer = ca_cert.issuer subject = OpenSSL::X509::Name.new subject.add_entry('C', opts[:country]) subject.add_entry('ST', opts[:state]) subject.add_entry('L', opts[:locality]) subject.add_entry('CN', opts[:common_name]) digest = OpenSSL::Digest::SHA256.new cert = OpenSSL::X509::Certificate.new cert.not_before = Time.at(0) cert.not_after = Time.now + 5 * 365 * 86400 # 5 years after cert.public_key = key cert.serial = 2 cert.issuer = issuer cert.subject = subject factory = OpenSSL::X509::ExtensionFactory.new server_cert.add_extension(factory.create_extension('basicConstraints', 'CA:FALSE')) server_cert.add_extension(factory.create_extension('nsCertType', 'server')) cert.sign ca_key, digest return cert, key end |
Instance Method Details
#call ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fluent/command/ca_generate.rb', line 34 def call ca_dir, passphrase, = @argv[0..1] unless ca_dir && passphrase puts "#{HELP_TEXT}" puts '' exit 1 end FileUtils.mkdir_p(ca_dir) cert, key = Fluent::CaGenerate.generate_ca_pair(@options) key_data = key.export(OpenSSL::Cipher.new('aes256'), passphrase) File.open(File.join(ca_dir, 'ca_key.pem'), 'w') do |file| file.write key_data end File.open(File.join(ca_dir, 'ca_cert.pem'), 'w') do |file| file.write cert.to_pem end puts "successfully generated: ca_key.pem, ca_cert.pem" puts "copy and use ca_cert.pem to client(out_forward)" end |
#usage(msg = nil) ⇒ Object
28 29 30 31 32 |
# File 'lib/fluent/command/ca_generate.rb', line 28 def usage(msg = nil) puts HELP_TEXT puts "Error: #{msg}" if msg exit 1 end |