Skip to content

Commit

Permalink
support ssl (amberframework#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
c910335 authored and eliasjpr committed Aug 1, 2017
1 parent 26fba1c commit ad1a9da
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
31 changes: 31 additions & 0 deletions spec/amber/ssl/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIFYDCCA0igAwIBAgIJAPazl2ttuX/bMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTcwNzMxMDYyOTE0WhcNMTcwODMwMDYyOTE0WjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC
CgKCAgEArFPWbNeEjazL79G8YkbBUxgsItoIEBOjvER/PcTdNNnvwIoKV9LBbyxT
c3HdZDmI/ceZAsoHe7ZYHJX1AlakAOQUpoO0U9W+lYqJ5J91TzY0PwEb6Lfg+3U2
xXswWmu9Ume/oKsXb87AliXTHS2HAZ7mEu1irmRNnfzsTVak92/rWYDZ0hTEnqFb
IaTVuiFkdftywk41IxK4PPwmB+L7bLFxnVtXjAd4fckkFv8QCf+1FjrVawUYySNU
YuWnHWD9RR66LxHm7pt/CuouAjfZvaXuob+0hG5/YjhzPzmN8NIhHA2q/K8Orv0j
r628/+RjkMz9uixK1u3+Smvb3PApautM1Pz1J02+r6dKJOxmlCbDjVGiCbMQCz6p
o+RlwFK3ljZb9FKY2LnKcPzngx0RxeiT+Jf1uyQ76bLHv4XpF8T35MTzY48czbgj
JYsHo2PNpuSVf83iv65ubbi4N+/bNlfArErNF9GsWF74HNapgh1v+S5DVuxXzn7u
p9ca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.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQCsU9Zs14SNrMvv
0bxiRsFTGCwi2ggQE6O8RH89xN002e/AigpX0sFvLFNzcd1kOYj9x5kCygd7tlgc
lfUCVqQA5BSmg7RT1b6Vionkn3VPNjQ/ARvot+D7dTbFezBaa71SZ7+gqxdvzsCW
JdMdLYcBnuYS7WKuZE2d/OxNVqT3b+tZgNnSFMSeoVshpNW6IWR1+3LCTjUjErg8
/CYH4vtssXGdW1eMB3h9ySQW/xAJ/7UWOtVrBRjJI1Ri5acdYP1FHrovEebum38K
6i4CN9m9pe6hv7SEbn9iOHM/OY3w0iEcDar8rw6u/SOvrbz/5GOQzP26LErW7f5K
a9vc8Clq60zU/PUnTb6vp0ok7GaUJsONUaIJsxALPqmj5GXAUreWNlv0UpjYucpw
/OeDHRHF6JP4l/W7JDvpsse/hekXxPfkxPNjjxzNuCMliwejY82m5JV/zeK/rm5t
uLg379s2V8CsSs0X0axYXvgc1qmCHW/5LkNW7FfOfu6n1xoPVhHg/m2Gfn42P2b5
ZycQwZM63ytDjJtdjaqWm/HQ48r//skKY8kzEVTAQBRwaG0q/IQoLrely61TxQ7t
MG8XmZVJD1IHTdY3+B9yMu0C6mZFNsaOju9OQcu8FBDD2cO3R5AYRp52bkFpdg4U
m9GK0/YS7AB0Ztlpa/0GszdUQzslcQIDAQABAoICAAtZ9mzzAWNHt6NTgIJEwOVV
WUrua8uLT6TOKkeKWTvrAzT+otHIKz/EfrbDEF9Svuhgpn6dikKm70Tqx9nglSsN
2yJg2JcRJF27Y3vw2QxfuK9RxmHdAu/td68ofzXllPIBrYjK3F9cEimR0kdAwXzk
16j1hCOSrd4zU+mARJub/rfFmhORlKYSYOtlbUK2gmSXLKcJ+cwWppY10deBJyiw
w5gm9SxfLZuzjTIKmSbfdjFBrN9A1bIiJqPhFtIDmotcC82f80NScNr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+f
LnFRVMde5NTOxTbRRQ/6t7fvcnspz+4=
-----END PRIVATE KEY-----
10 changes: 10 additions & 0 deletions spec/amber/ssl/ssl_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "../../../spec_helper"

module Amber
describe SSL do
it "generates tls" do
tls = SSL.new(File.join(__DIR__, "key.pem"), File.join(__DIR__, "cert.pem")).generate_tls
tls.should be_a(OpenSSL::SSL::Context::Server)
end
end
end
7 changes: 6 additions & 1 deletion src/amber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module Amber
property pubsub_adapter : WebSockets::Adapters::RedisAdapter.class | WebSockets::Adapters::MemoryAdapter.class
property redis_url : String
property session : Hash(Symbol, Symbol | Int32 | String)
property ssl_key_file : String?
property ssl_cert_file : String?

def initialize
@app_path = __FILE__
Expand Down Expand Up @@ -89,14 +91,17 @@ module Amber

def start
time = Time.now
str_host = "http://#{host}:#{port}".colorize(:light_cyan).underline
ssl_enabled = ssl_key_file && ssl_cert_file
scheme = ssl_enabled ? "https" : "http"
str_host = "#{scheme}://#{host}:#{port}".colorize(:light_cyan).underline
version = "[Amber #{Amber::VERSION}]".colorize(:light_cyan).to_s
log.info "#{version} serving application \"#{name}\" at #{str_host}".to_s

# prepare pipelines for processing and memoize them to gain a little performance
handler.prepare_pipelines

server = HTTP::Server.new(host, port, handler)
server.tls = Amber::SSL.new(ssl_key_file.not_nil!, ssl_cert_file.not_nil!).generate_tls if ssl_enabled

Signal::INT.trap do
puts "Shutting down Amber"
Expand Down
15 changes: 15 additions & 0 deletions src/amber/ssl.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "openssl"

module Amber
class SSL
def initialize(@key_file : String, @cert_file : String)
end

def generate_tls
tls = OpenSSL::SSL::Context::Server.new
tls.private_key = @key_file
tls.certificate_chain = @cert_file
tls
end
end
end

0 comments on commit ad1a9da

Please sign in to comment.