Skip to content

Instantly share code, notes, and snippets.

@codemilan
Last active December 2, 2019 09:15
Show Gist options
  • Save codemilan/b1ed38c8037209b2f13b30962dcd6ce6 to your computer and use it in GitHub Desktop.
Save codemilan/b1ed38c8037209b2f13b30962dcd6ce6 to your computer and use it in GitHub Desktop.
Running puma in ssl mode in development environment.
# Ref:- 'https://gist.github.com/tadast/9932075'
# Inside your rails app root directory.
# run "mkdir config/certs && touch config/certs/.keep"
# Add below two lines in .gitignore
# /config/certs/*
# !/config/certs/.keep
# Add below code in config/puma.rb
if Rails.env.development?
unless File.exist?(Rails.root.join('config', 'certs', 'localhost.key'))
def generate_root_cert(root_key)
root_ca = OpenSSL::X509::Certificate.new
root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
root_ca.serial = 0x0
root_ca.subject = OpenSSL::X509::Name.parse "/C=BE/O=A1/OU=A/CN=localhost"
root_ca.issuer = root_ca.subject # root CA's are "self-signed"
root_ca.public_key = root_key.public_key
root_ca.not_before = Time.now
root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
root_ca
end
root_key = OpenSSL::PKey::RSA.new(2048)
file = File.new( Rails.root.join('config', 'certs', 'localhost.key'), "wb")
file.write(root_key)
file.close
root_cert = generate_root_cert(root_key)
file = File.new( Rails.root.join('config','certs', 'localhost.cert'), "wb")
file.write(root_cert)
file.close
end
ssl_bind '0.0.0.0', '8443', {
key: Rails.root.join('config','certs', 'localhost.key'),
cert: Rails.root.join('config','certs', 'localhost.cert')
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment