Sha256: eec85d504c17f396f87503521e44d61880c91b331f4acd1bc034cf702bb9b618

Contents?: true

Size: 1.22 KB

Versions: 137

Compression:

Stored size: 1.22 KB

Contents

require 'openssl'

module Kontena
  module Machine
    module CertHelper

      def certificate_public_key(cert)
        cert[/(-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----)/m, 1]
      end

      def generate_self_signed_cert
        key = OpenSSL::PKey::RSA.new(2048)
        public_key = key.public_key

        subject = "/C=FI/O=Test/OU=Test/CN=Test"

        cert = OpenSSL::X509::Certificate.new
        cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject)
        cert.not_before = Time.now
        cert.not_after = Time.now + (10 * 365 * 24 * 60 * 60)
        cert.public_key = public_key
        cert.serial = 0x0
        cert.version = 2

        ef = OpenSSL::X509::ExtensionFactory.new
        ef.subject_certificate = cert
        ef.issuer_certificate = cert
        cert.extensions = [
          ef.create_extension("basicConstraints","CA:TRUE", true),
          ef.create_extension("subjectKeyIdentifier", "hash")
        ]
        cert.add_extension ef.create_extension("authorityKeyIdentifier",
                                               "keyid:always,issuer:always")

        cert.sign key, OpenSSL::Digest::SHA1.new

        pem = cert.to_pem
        pem << key.to_s
        pem
      end
    end
  end
end

Version data entries

137 entries across 137 versions & 2 rubygems

Version Path
kontena-cli-1.2.2.rc1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.1.rc1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.rc3 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.rc2 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.rc1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.pre3 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.pre2 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.6 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.pre1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.5 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.5.rc3 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.5.rc2 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.5.rc1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.4 lib/kontena/machine/cert_helper.rb
kontena-cli-1.2.0.dev1 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.3 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.2 lib/kontena/machine/cert_helper.rb
kontena-cli-1.1.2.rc2 lib/kontena/machine/cert_helper.rb