From 52a181347c4f85b9c313e400c20fd21ecef7b9b2 Mon Sep 17 00:00:00 2001 From: Adam Cammack Date: Wed, 27 Feb 2013 23:55:05 -0600 Subject: Add an SSL example --- examples/README.md | 3 +++ examples/ssl_hello_world/README.md | 31 ++++++++++++++++++++++ examples/ssl_hello_world/priv/ssl/cowboy-ca.crt | 16 +++++++++++ examples/ssl_hello_world/priv/ssl/server.crt | 17 ++++++++++++ examples/ssl_hello_world/priv/ssl/server.key | 15 +++++++++++ examples/ssl_hello_world/rebar.config | 4 +++ .../ssl_hello_world/src/ssl_hello_world.app.src | 15 +++++++++++ examples/ssl_hello_world/src/ssl_hello_world.erl | 14 ++++++++++ .../ssl_hello_world/src/ssl_hello_world_app.erl | 28 +++++++++++++++++++ .../ssl_hello_world/src/ssl_hello_world_sup.erl | 23 ++++++++++++++++ examples/ssl_hello_world/src/toppage_handler.erl | 18 +++++++++++++ examples/ssl_hello_world/start.sh | 3 +++ 12 files changed, 187 insertions(+) create mode 100644 examples/ssl_hello_world/README.md create mode 100644 examples/ssl_hello_world/priv/ssl/cowboy-ca.crt create mode 100644 examples/ssl_hello_world/priv/ssl/server.crt create mode 100644 examples/ssl_hello_world/priv/ssl/server.key create mode 100644 examples/ssl_hello_world/rebar.config create mode 100644 examples/ssl_hello_world/src/ssl_hello_world.app.src create mode 100644 examples/ssl_hello_world/src/ssl_hello_world.erl create mode 100644 examples/ssl_hello_world/src/ssl_hello_world_app.erl create mode 100644 examples/ssl_hello_world/src/ssl_hello_world_sup.erl create mode 100644 examples/ssl_hello_world/src/toppage_handler.erl create mode 100755 examples/ssl_hello_world/start.sh diff --git a/examples/README.md b/examples/README.md index ef5bfaf..bda8e46 100644 --- a/examples/README.md +++ b/examples/README.md @@ -40,6 +40,9 @@ Cowboy Examples * [rest_stream_response](./rest_stream_response): stream results from a data store + * [ssl_hello_world](./ssl_hello_world): + simplest SSL application + * [static_world](./static_world): static file handler diff --git a/examples/ssl_hello_world/README.md b/examples/ssl_hello_world/README.md new file mode 100644 index 0000000..766ff59 --- /dev/null +++ b/examples/ssl_hello_world/README.md @@ -0,0 +1,31 @@ +Cowboy Hello World +================== + +To compile this example you need rebar in your PATH. + +Type the following command: +``` +$ rebar get-deps compile +``` + +You can then start the Erlang node with the following command: +``` +./start.sh +``` + +Then point your browser to the indicated URL. You will need to temporarily +trust the root certificate authority in `priv/ssl/cowboy-ca.crt`. + +Example +------- + +``` bash +$ curl --cacert priv/ssl/cowboy-ca.crt -i https://localhost:8443 +HTTP/1.1 200 OK +connection: keep-alive +server: Cowboy +date: Fri, 28 Sep 2012 04:10:25 GMT +content-length: 12 + +Hello world! +``` diff --git a/examples/ssl_hello_world/priv/ssl/cowboy-ca.crt b/examples/ssl_hello_world/priv/ssl/cowboy-ca.crt new file mode 100644 index 0000000..a35ac39 --- /dev/null +++ b/examples/ssl_hello_world/priv/ssl/cowboy-ca.crt @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE----- +MIICeDCCAeGgAwIBAgIJAOvpU0y2e5J4MA0GCSqGSIb3DQEBBQUAMFUxCzAJBgNV +BAYTAlVTMQ4wDAYDVQQIDAVUZXhhczETMBEGA1UECgwKTmluZSBOaW5lczEPMA0G +A1UECwwGQ293Ym95MRAwDgYDVQQDDAdST09UIENBMB4XDTEzMDIyODA1MTAwMVoX +DTMzMDIyMzA1MTAwMVowVTELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMRMw +EQYDVQQKDApOaW5lIE5pbmVzMQ8wDQYDVQQLDAZDb3dib3kxEDAOBgNVBAMMB1JP +T1QgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMzmY7Us06yjyUbpqwPx +Iv+xh/g3V7we07ClC9GEYnvr3OQvdA1jFEHccMBUUjRoQ8DPd6uSyK5UkixABs08 +Tt5B3VsnGKr0DIN+IO4SN2PkmBqIU/BN3KdcwN65YNr3iM0KsKWeFtAZdYx4CakX +7REbO0wjK20AH3xSBn3uFGiBAgMBAAGjUDBOMB0GA1UdDgQWBBRKfZ8KF2jlLBDm +NL6IuEuGY0pdbzAfBgNVHSMEGDAWgBRKfZ8KF2jlLBDmNL6IuEuGY0pdbzAMBgNV +HRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAG1I0kBxXiLkM1b7rl2zPLizREYg +1m+ajb6rWzPOBg6TXjv58Be+H4tqoHIL/M/crixew5emftBkuAGjiKMhbIokjvan +aPTCV8U6HHvNvz9c68HpESWbd+56cHqfsS5XCKp1OpW5tbL2UQYpFKMP4qmbv3Ea +pBfPPmSFMBb1i2AI +-----END CERTIFICATE----- diff --git a/examples/ssl_hello_world/priv/ssl/server.crt b/examples/ssl_hello_world/priv/ssl/server.crt new file mode 100644 index 0000000..0bdfaed --- /dev/null +++ b/examples/ssl_hello_world/priv/ssl/server.crt @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICpTCCAg6gAwIBAgIJAOvpU0y2e5J5MA0GCSqGSIb3DQEBBQUAMFUxCzAJBgNV +BAYTAlVTMQ4wDAYDVQQIDAVUZXhhczETMBEGA1UECgwKTmluZSBOaW5lczEPMA0G +A1UECwwGQ293Ym95MRAwDgYDVQQDDAdST09UIENBMB4XDTEzMDIyODA1MjMzNFoX +DTMzMDIyMzA1MjMzNFowVzELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMRMw +EQYDVQQKDApOaW5lIE5pbmVzMQ8wDQYDVQQLDAZDb3dib3kxEjAQBgNVBAMMCWxv +Y2FsaG9zdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAzbW1GjECzHUc/WST +qLiAGqjCNccR5saVS+yoz2SPRhpoyf0/qBrX5BY0tzmgozoTiRfE4wCiVD99Cc+D +rp/FM49r4EpZdocIovprmOmv/gwkoj95zaA6PKNn1OdmDp2hwJsX2Zm3kpbGUZTx +jDkkccmgUb4EjL7qNHq7saQtivUCAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgB +hvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYE +FB6jTEIWI8T1ckORA4GezbyYxtbvMB8GA1UdIwQYMBaAFEp9nwoXaOUsEOY0voi4 +S4ZjSl1vMA0GCSqGSIb3DQEBBQUAA4GBACMboVQjrx8u/fk3gl/sR0tbA0Wf/NcS +2Dzsy2czndgVUAG4Sqb+hfgn0dqAyUKghRrj3JDcYxYksGPIklDfPzZb7yJ39l16 +6x5ZiIzhp8CAVdPvRxRznw5rZwaXesryXu1jVSZxTr3MYZdkG6KaAM0t90+YlGLZ +UG8fAicx0Bf+ +-----END CERTIFICATE----- diff --git a/examples/ssl_hello_world/priv/ssl/server.key b/examples/ssl_hello_world/priv/ssl/server.key new file mode 100644 index 0000000..b6f7374 --- /dev/null +++ b/examples/ssl_hello_world/priv/ssl/server.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQDNtbUaMQLMdRz9ZJOouIAaqMI1xxHmxpVL7KjPZI9GGmjJ/T+o +GtfkFjS3OaCjOhOJF8TjAKJUP30Jz4Oun8Uzj2vgSll2hwii+muY6a/+DCSiP3nN +oDo8o2fU52YOnaHAmxfZmbeSlsZRlPGMOSRxyaBRvgSMvuo0eruxpC2K9QIDAQAB +AoGAaD85c/h6bpq7Aj7CBbLaWKhFI3OqwsTITB22vsM7SE+B4zsP02UnG1OVi3UM +zytTUxpUkKV1njQ+bYZYOVqGWF4Up8tTqUglHn0FTPok1AIemELWtz3sXvdSHC1T +lqvFBAZ9kibn13qGyVOiyCFaMwfOM/05RvV7p3jfUMTWnNECQQDs7yCJZ8Ol8MyH +TGZzvkjoN2zg1KwmTbSD1hkP6QAJtPdRuqFbjlEru0/PefgOXsWLRIa3/3v0qw2G +xGkV6AXTAkEA3kNbFisqUydjPnZIYv/P6SvPdUimHJEjXbAbfNfzS9dzszrOVJd2 +XqGH7z5yzjoH3IyaIMW8GnubVzGDSjrHFwJAKSU5vELlygpwKkrNO+pelN0TLlQg +dSJnZ8GlZorq88SWcn37iX/EftivenNO7YftvEqxLoDSkOGnnrC7Iw/A+wJBAIEe +L/QY72WPJCBNJpAce/PA96vyoE1II3txqwZDjZspdpVQPDz4IFOpEwbxCFC1dYuy +Qnd3Z2cbF4r3wIWGz9ECQQCJGNhUNtY+Om1ELdqPcquxE2VRV/pucnvJSTKwyo2C +Rvm6H7kFDwPDuN23YnTOlTiho0zzCkclcIukhIVJ+dKz +-----END RSA PRIVATE KEY----- diff --git a/examples/ssl_hello_world/rebar.config b/examples/ssl_hello_world/rebar.config new file mode 100644 index 0000000..6ad3062 --- /dev/null +++ b/examples/ssl_hello_world/rebar.config @@ -0,0 +1,4 @@ +{deps, [ + {cowboy, ".*", + {git, "git://github.com/extend/cowboy.git", "master"}} +]}. diff --git a/examples/ssl_hello_world/src/ssl_hello_world.app.src b/examples/ssl_hello_world/src/ssl_hello_world.app.src new file mode 100644 index 0000000..514da1c --- /dev/null +++ b/examples/ssl_hello_world/src/ssl_hello_world.app.src @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +{application, ssl_hello_world, [ + {description, "Cowboy Hello World example with SSL."}, + {vsn, "1"}, + {modules, []}, + {registered, []}, + {applications, [ + kernel, + stdlib, + cowboy + ]}, + {mod, {ssl_hello_world_app, []}}, + {env, []} +]}. diff --git a/examples/ssl_hello_world/src/ssl_hello_world.erl b/examples/ssl_hello_world/src/ssl_hello_world.erl new file mode 100644 index 0000000..1413b7b --- /dev/null +++ b/examples/ssl_hello_world/src/ssl_hello_world.erl @@ -0,0 +1,14 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(ssl_hello_world). + +%% API. +-export([start/0]). + +%% API. + +start() -> + ok = application:start(crypto), + ok = application:start(ranch), + ok = application:start(cowboy), + ok = application:start(ssl_hello_world). diff --git a/examples/ssl_hello_world/src/ssl_hello_world_app.erl b/examples/ssl_hello_world/src/ssl_hello_world_app.erl new file mode 100644 index 0000000..ae8f0cf --- /dev/null +++ b/examples/ssl_hello_world/src/ssl_hello_world_app.erl @@ -0,0 +1,28 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @private +-module(ssl_hello_world_app). +-behaviour(application). + +%% API. +-export([start/2]). +-export([stop/1]). + +%% API. + +start(_Type, _Args) -> + Dispatch = cowboy_router:compile([ + {'_', [ + {"/", toppage_handler, []} + ]} + ]), + {ok, _} = cowboy:start_https(https, 100, [ + {port, 8443}, + {cacertfile, "priv/ssl/cowboy-ca.crt"}, + {certfile, "priv/ssl/server.crt"}, + {keyfile, "priv/ssl/server.key"} + ], [{env, [{dispatch, Dispatch}]}]), + ssl_hello_world_sup:start_link(). + +stop(_State) -> + ok. diff --git a/examples/ssl_hello_world/src/ssl_hello_world_sup.erl b/examples/ssl_hello_world/src/ssl_hello_world_sup.erl new file mode 100644 index 0000000..27b63f8 --- /dev/null +++ b/examples/ssl_hello_world/src/ssl_hello_world_sup.erl @@ -0,0 +1,23 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @private +-module(ssl_hello_world_sup). +-behaviour(supervisor). + +%% API. +-export([start_link/0]). + +%% supervisor. +-export([init/1]). + +%% API. + +-spec start_link() -> {ok, pid()}. +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +%% supervisor. + +init([]) -> + Procs = [], + {ok, {{one_for_one, 10, 10}, Procs}}. diff --git a/examples/ssl_hello_world/src/toppage_handler.erl b/examples/ssl_hello_world/src/toppage_handler.erl new file mode 100644 index 0000000..4124b5a --- /dev/null +++ b/examples/ssl_hello_world/src/toppage_handler.erl @@ -0,0 +1,18 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Hello world handler. +-module(toppage_handler). + +-export([init/3]). +-export([handle/2]). +-export([terminate/3]). + +init(_Transport, Req, []) -> + {ok, Req, undefined}. + +handle(Req, State) -> + {ok, Req2} = cowboy_req:reply(200, [], <<"Hello world!">>, Req), + {ok, Req2, State}. + +terminate(_Reason, _Req, _State) -> + ok. diff --git a/examples/ssl_hello_world/start.sh b/examples/ssl_hello_world/start.sh new file mode 100755 index 0000000..c8c7969 --- /dev/null +++ b/examples/ssl_hello_world/start.sh @@ -0,0 +1,3 @@ +#!/bin/sh +erl -pa ebin deps/*/ebin -s ssl_hello_world \ + -eval "io:format(\"Point your browser at https://localhost:8443~n\")." -- cgit v1.2.3