Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic tls #37

Merged
merged 6 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: elixir
otp_release:
- 18.3
- 19.2
- 19.3
elixir:
- 1.4.2
branches:
Expand All @@ -14,6 +15,7 @@ before_install:

before_script:
- ./gnatsd-v0.9.6-linux-amd64/gnatsd -p 4223 --user bob --pass alice &
- ./gnatsd-v0.9.6-linux-amd64/gnatsd -p 4224 --tls --tlscert test/fixtures/server.pem --tlskey test/fixtures/key.pem &

script:
- mix test --include multi_server
47 changes: 32 additions & 15 deletions lib/gnat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,48 +113,51 @@ defmodule Gnat do
connection_settings = Map.merge(@default_connection_settings, connection_settings)
{:ok, tcp} = :gen_tcp.connect(connection_settings.host, connection_settings.port, connection_settings.tcp_opts)
case perform_handshake(tcp, connection_settings) do
:ok ->
{:ok, socket} ->
parser = Parser.new
{:ok, %{tcp: tcp, connection_settings: connection_settings, next_sid: 1, receivers: %{}, parser: parser}}
{:ok, %{socket: socket, connection_settings: connection_settings, next_sid: 1, receivers: %{}, parser: parser}}
{:error, reason} ->
:gen_tcp.close(tcp)
{:error, reason}
end
end

def handle_info({:tcp, tcp, data}, %{tcp: tcp, parser: parser}=state) do
def handle_info({:tcp, socket, data}, %{socket: socket, parser: parser}=state) do
Logger.debug "#{__MODULE__} received #{inspect data}"
{new_parser, messages} = Parser.parse(parser, data)
new_state = %{state | parser: new_parser}
new_state = Enum.reduce(messages, new_state, &process_message/2)
{:noreply, new_state}
end
def handle_info({:ssl, socket, data}, state) do
handle_info({:tcp, socket, data}, state)
end
def handle_info(other, state) do
Logger.error "#{__MODULE__} received unexpected message: #{inspect other}"
{:noreply, state}
end


def handle_call(:stop, _from, state) do
:gen_tcp.close(state.tcp)
socket_close(state.connection_settings, state.socket)
{:stop, :normal, :ok, state}
end
def handle_call({:sub, receiver, topic, opts}, _from, %{next_sid: sid}=state) do
sub = Command.build(:sub, topic, sid, opts)
:ok = :gen_tcp.send(state.tcp, sub)
:ok = socket_write(state.connection_settings, state.socket, sub)
next_state = add_subscription_to_state(state, sid, receiver) |> Map.put(:next_sid, sid + 1)
{:reply, {:ok, sid}, next_state}
end
def handle_call({:pub, topic, message, opts}, _from, state) do
command = Command.build(:pub, topic, message, opts)
:ok = :gen_tcp.send(state.tcp, command)
:ok = socket_write(state.connection_settings, state.socket, command)
{:reply, :ok, state}
end
def handle_call({:request, request}, _from, %{next_sid: sid}=state) do
sub = Command.build(:sub, request.inbox, sid, [])
unsub = Command.build(:unsub, sid, [max_messages: 1])
pub = Command.build(:pub, request.topic, request.body, reply_to: request.inbox)
:ok = :gen_tcp.send(state.tcp, [sub, unsub, pub])
:ok = socket_write(state.connection_settings, state.socket, [sub, unsub, pub])
state = add_subscription_to_state(state, sid, request.recipient) |> cleanup_subscription_from_state(sid, max_messages: 1)
next_sid = sid + 1
{:reply, {:ok, sid}, %{state | next_sid: next_sid}}
Expand All @@ -164,32 +167,46 @@ defmodule Gnat do
false -> {:reply, :ok, state}
true ->
command = Command.build(:unsub, sid, opts)
:ok = :gen_tcp.send(state.tcp, command)
:ok = socket_write(state.connection_settings, state.socket, command)
{:reply, :ok, state}
end
end
def handle_call({:ping, pinger}, _from, state) do
:ok = :gen_tcp.send(state.tcp, "PING\r\n")
:ok = socket_write(state.connection_settings, state.socket, "PING\r\n")
{:reply, :ok, Map.put(state, :pinger, pinger)}
end

defp perform_handshake(tcp, connection_settings) do
receive do
{:tcp, ^tcp, operation} ->
{_, [{:info, options}]} = Parser.parse(Parser.new, operation)
connect(tcp, options, connection_settings)
{:ok, socket} = upgrade_connection(tcp, options, connection_settings)
connect(socket, options, connection_settings)
{:ok, socket}
after 1000 ->
{:error, "timed out waiting for info"}
end
end

defp connect(tcp, %{auth_required: true}=_options, %{username: username, password: password}=_connection_settings) do
defp connect(socket, %{auth_required: true}=_options, %{username: username, password: password}=connection_settings) do
opts = Poison.Encoder.encode(%{user: username, pass: password, verbose: false}, strict_keys: true)
:gen_tcp.send(tcp, "CONNECT #{opts}\r\n")
socket_write(connection_settings, socket, "CONNECT #{opts}\r\n")
end
defp connect(socket, _options, connection_settings) do
socket_write(connection_settings, socket, "CONNECT {\"verbose\": false}\r\n")
end
defp connect(tcp, _options, _connection_settings) do
:gen_tcp.send(tcp, "CONNECT {\"verbose\": false}\r\n")

defp upgrade_connection(tcp, %{tls_required: true}, %{tls: true}) do
:ok = :inet.setopts(tcp, [active: true])
:ssl.connect(tcp, [], 1_000)
end
defp upgrade_connection(tcp, _server_settings, _connection_settions), do: {:ok, tcp}

defp socket_close(%{tls: true}, socket), do: :ssl.close(socket)
defp socket_close(_, socket), do: :gen_tcp.close(socket)

defp socket_write(%{tls: true}, socket, iodata), do: :ssl.send(socket, iodata)
defp socket_write(_, socket, iodata), do: :gen_tcp.send(socket, iodata)

defp add_subscription_to_state(%{receivers: receivers}=state, sid, pid) do
receivers = Map.put(receivers, sid, %{recipient: pid, unsub_after: :infinity})
Expand All @@ -210,7 +227,7 @@ defmodule Gnat do
update_subscriptions_after_delivering_message(state, sid)
end
defp process_message(:ping, state) do
:gen_tcp.send(state.tcp, "PONG\r\n")
socket_write(state.connection_settings, state.socket, "PONG\r\n")
state
end
defp process_message(:pong, state) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Gnat.Mixfile do
end

def application do
[extra_applications: [:logger]]
[extra_applications: [:logger, :ssl]]
end

defp deps do
Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-----BEGIN CERTIFICATE-----
MIIGjzCCBHegAwIBAgIJAKT2W9SKY7o4MA0GCSqGSIb3DQEBCwUAMIGLMQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xEzAR
BgNVBAoTCkFwY2VyYSBJbmMxEDAOBgNVBAsTB25hdHMuaW8xEjAQBgNVBAMTCWxv
Y2FsaG9zdDEcMBoGCSqGSIb3DQEJARYNZGVyZWtAbmF0cy5pbzAeFw0xNTExMDUy
MzA2MTdaFw0xOTExMDQyMzA2MTdaMIGLMQswCQYDVQQGEwJVUzELMAkGA1UECBMC
Q0ExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xEzARBgNVBAoTCkFwY2VyYSBJbmMx
EDAOBgNVBAsTB25hdHMuaW8xEjAQBgNVBAMTCWxvY2FsaG9zdDEcMBoGCSqGSIb3
DQEJARYNZGVyZWtAbmF0cy5pbzCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC
ggIBAJOyBvFaREbmO/yaw8UD8u5vSk+Qrwdkfa0iHMo11nkcVtynHNKcgRUTkZBC
xEZILVsuPa+WSUcUc0ej0TmuimrtOjXGn+LD0TrDVz6dd6lBufLXjo1fbUnKUjml
TBYB2h7StDksrBPFnbEOVKN+qb1No4YxfvbJ6EK3xfnsm3dvamnetJugrmQ2EUlu
glPNZDIShu9Fcsiq2hjw+dJ2Erl8kx2/PE8nOdcDG9I4wAM71pw9L1dHGmMOnTsq
opLDVkMNjeIgMPxj5aIhvS8Tcnj16ZNi4h10587vld8fIdz+OgTDFMNi91PgZQmX
9puXraBGi5UEn0ly57IIY+aFkx74jPWgnVYz8w8G+W2GTFYQEVgHcPTJ4aIPjyRd
m/cLelV34TMNCoTXmpIKVBkJY01t2awUYN0AcauhmD1L+ihY2lVk330lxQR11ZQ/
rjSRpG6jzb6diVK5wpNjsRR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J59yo7KUn1nX7HsFvBVh9D8147J5BxtPztc0GtCQTXFT73nQapJjAd5J+AC5AB4t
ShE+MRD+XIlPB/aMgtzz9Th8UCktVKoPOpFMC0SvFbbINWL/JO1QGhuZLMTKLjQN
QBzjrETAOA9PICpI5hcPtTXz172X+I8/tIEFrZfew0Fdt/oAVcnb659zKiR8EuAq
+Svp
-----END CERTIFICATE-----
30 changes: 30 additions & 0 deletions test/fixtures/client-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN CERTIFICATE-----
MIIFPDCCAySgAwIBAgIJAO+k4G7bNTypMA0GCSqGSIb3DQEBCwUAMIGLMQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xEzAR
BgNVBAoTCkFwY2VyYSBJbmMxEDAOBgNVBAsTB25hdHMuaW8xEjAQBgNVBAMTCWxv
Y2FsaG9zdDEcMBoGCSqGSIb3DQEJARYNZGVyZWtAbmF0cy5pbzAeFw0xNTExMDUy
MzEwNDdaFw0xOTExMDQyMzEwNDdaMBYxFDASBgNVBAMTC25hdHMtY2xpZW50MIIC
IjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArgLxszD5/vDrDUwwIEgQx9I0
J/H6MXPO0Tj9D2BnR+nwjCe9M03fsq4Il96BVzoaAiAQD1r4NyAX2adKydlnE3/m
bUFiSVHErJceEi9aSs+WlLdmKEgU2qrsIal9KzthlI786qtjb7OFSCxP14R4xYA5
dlZXhJ9oUuFhVTdaVmRMzWuWj8RbBx8VptSZ0f7Q+Uv8GuB0kyiVkv6GYcH/IWuI
7jnM0QcVWBmxJfWmqd0yx/FLlX/LRXqdiyoFSIlMaP0VOwto3uEhAoBk83Z+/zrZ
Brymx1Nnz3qzTCf8/mdMjPuWibXDTLbo0/Kf6neHs6wxx8irb1ZfIwhn8grXTcgd
rg9bfcyyUOBey7QXiedpU0xFqoH26E+Aq+CV4R56i1sJKsSYEGu8O69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 deletions test/fixtures/client-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEArgLxszD5/vDrDUwwIEgQx9I0J/H6MXPO0Tj9D2BnR+nwjCe9
M03fsq4Il96BVzoaAiAQD1r4NyAX2adKydlnE3/mbUFiSVHErJceEi9aSs+WlLdm
KEgU2qrsIal9KzthlI786qtjb7OFSCxP14R4xYA5dlZXhJ9oUuFhVTdaVmRMzWuW
j8RbBx8VptSZ0f7Q+Uv8GuB0kyiVkv6GYcH/IWuI7jnM0QcVWBmxJfWmqd0yx/FL
lX/LRXqdiyoFSIlMaP0VOwto3uEhAoBk83Z+/zrZBrymx1Nnz3qzTCf8/mdMjPuW
ibXDTLbo0/Kf6neHs6wxx8irb1ZfIwhn8grXTcgdrg9bfcyyUOBey7QXiedpU0xF
qoH26E+Aq+CV4R56i1sJKsSYEGu8O69H8zu5dganLZRhcCHcZhMe7Nbiu5BcuOW4
r3rGDMTLXSugEX91iy5jJaYmRjtPN5imQIJtf+GKVq7YLv4MQV6R3xRiZXaocCae
1qzIMc4kxCKvZTmxuJsvIUPjNnGumwbjV/a2fLFX9tMqUKyEmiPtFtqNH/kmkHCQ
5FGYIIj3wGuD5yWfK5Tr3iHOdNJoNNPgPBg9tMRwj3+W8+uyBxc+FUEb8a9m3R4V
mAYyiqgzCA0DWZBF1fOYLWfRnwS5OBKiP4OUlUEbYZUEzfvDbLOwQrb123cCAwEA
AQKCAgAQUkBfYVGhgvFZDvNYo8nHJEU2FfE0oDsezqyVu6IUUbH5Q2TwofZAaShv
LjSNfOqhlmZLOmobqYvzI0jVg+myH4X6a26Pl/bNhWMRq5VZfP0Pt+ACGTizheKe
Caqu2mP9rie0zxyFhp4Ste1LNqapR6ycF98flmAPngomFwoHHmNBxTybAXzUPysl
ub0vwCnTqDfeQX1NrDnTTsJF+w82EEMIrS0z0elDmS1PdSoLtq6jqFNBk3n6a1TJ
j8htFEuxcUODhT9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OFmnMjfgMwmit3tfm7zkmkzNfmASugDPWjA=
-----END RSA PRIVATE KEY-----
51 changes: 51 additions & 0 deletions test/fixtures/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJKgIBAAKCAgEAtgHLcgRjeSqV/mHa8S2T0IHhWe0AP55pVzdj3G4UcniTRJyy
PCtgfdhzBBbR8Ok5AIjTXTZihBPu08IFP6sLTDWYzzbRlIpL/LZIgr1wzosdaRRt
BxZ95ov67PYcHeNMSby2YQQVMsEkUxsylSy+MDkYuoZRGzCw2NgSXwz3BLUERPDZ
754IVrjDGr2gYen8OCHS9mCUfNAvmiwSlFy3VppCjo6NbNlzUKDHhLGYw6gxYXwF
DOU7tqKRtkQnGTTdMgU2mH9rMm3ua+Iyx5bvaY/5tf2yb/xuwg2JiAkwzYcDKMiA
VUxdfwBh8QULjCjNiWguqfTLL1N2OHIZuxSODTJN3iUD0uQYqugF1jV2s9J6Tk2P
1uvbtQYYZ9TZ10APnFgEh54Vj7eepJPzryghcH+bU/vWny2mSC6PH9Goqvee86oE
eLOahBpZmw8Ldf8lzg29UeKGm43M3+7UPmbEaHGzH5GqesiSFLQio2uiSCA9lrO6
CYee133keBNvcmmNjdEYRhcBA2v6ZkZQJz4JW7SaEVfEAxlx9WnmcODiEoeJpG/Q
pxqoGaefwAHnDkWJOmnNRtE/TPPsaTCt26XBHpzYRvnvn7/TbZNuALHwH1IfjMlF
OPma2srnp4WBNye5cH5idZo/v/uqYohnPGt3dQO+fNpuGcyKIgru8vyqI5MCAwEA
AQKCAgEAl6zBNUAxAW2a2AYGZgx8bTt/Z+hY16uUz8jqIG1f/tE6sOgApKHlZJp3
pwW5aRGCnk5oDfrfeH///Fpo81kALj9QHAbr+uSRVIU3wjRLCOTn2oTaIxj8TJ+E
ueqTHdko3x4zwn+bhtNsCRHWQnip+hfq4q5Ccu1Nwze1f56XUEXly+oHRGenPVX1
yZgTSuWqecC+RPHRbH413T4z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E0SIn6PKd7uXZnXTTxTf6AR1RNzFcStIL5lC06V6Savpa
tSX2voU3DgUIDYrYUhDweukR8i+0nrkR8wRUUjxaAeegUIRHN5ffpk57lQNaNg==
-----END RSA PRIVATE KEY-----
31 changes: 31 additions & 0 deletions test/fixtures/server.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIFQTCCAymgAwIBAgIJAO+k4G7bNTyoMA0GCSqGSIb3DQEBCwUAMIGLMQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xEzAR
BgNVBAoTCkFwY2VyYSBJbmMxEDAOBgNVBAsTB25hdHMuaW8xEjAQBgNVBAMTCWxv
Y2FsaG9zdDEcMBoGCSqGSIb3DQEJARYNZGVyZWtAbmF0cy5pbzAeFw0xNTExMDUy
MzA2MzRaFw0xOTExMDQyMzA2MzRaMBQxEjAQBgNVBAMTCWxvY2FsaG9zdDCCAiIw
DQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBALYBy3IEY3kqlf5h2vEtk9CB4Vnt
AD+eaVc3Y9xuFHJ4k0ScsjwrYH3YcwQW0fDpOQCI0102YoQT7tPCBT+rC0w1mM82
0ZSKS/y2SIK9cM6LHWkUbQcWfeaL+uz2HB3jTEm8tmEEFTLBJFMbMpUsvjA5GLqG
URswsNjYEl8M9wS1BETw2e+eCFa4wxq9oGHp/Dgh0vZglHzQL5osEpRct1aaQo6O
jWzZc1Cgx4SxmMOoMWF8BQzlO7aikbZEJxk03TIFNph/azJt7mviMseW72mP+bX9
sm/8bsINiYgJMM2HAyjIgFVMXX8AYfEFC4wozYloLqn0yy9TdjhyGbsUjg0yTd4l
A9LkGKroBdY1drPSek5Nj9br27UGGGfU2ddAD5xYBIeeFY+3nqST868oIXB/m1P7
1p8tpkgujx/RqKr3nvOqBHizmoQaWZsPC3X/Jc4NvVHihpuNzN/u1D5mxGhxsx+R
qnrIkhS0IqNrokggPZazugmHntd95HgTb3JpjY3RGEYXAQNr+mZGUCc+CVu0mhFX
xAMZcfVp5nDg4hKHiaRv0KcaqBmnn8AB5w5FiTppzUbRP0zz7GkwrdulwR6c2Eb5
75+/022TbgCx8B9SH4zJRTj5mtrK56eFgTcnuXB+YnWaP7/7qmKIZzxrd3UDvnza
bhnMiiIK7vL8qiOTAgMBAAGjHjAcMBoGA1UdEQQTMBGCCWxvY2FsaG9zdIcEfwAA
ATANBgkqhkiG9w0BAQsFAAOCAgEAOrh8XfW6quwBAcCxHf6/uvu/iNq4yHCg2qH6
VtWs/x38I2t3BRSNsLsJGieh6yLlZDzOus+XYui4uDE50XmcwaIsY0VcXnvdyZVZ
w9+lMyfp00kRF1o3B6eVxq0pRE5VB0cai7XI7tyfpRwGzA+oNLF4vBvxAHm9Ony5
Q57DC/HFzyUogdkMYciO/kd9oa4HosDEXwaE8UvZUL8OVl/dptMXLL/GGwzZsUAE
1sLAbgm044YChLUDzgBAtDTkB/HNkcPzSKwULuskhe7ndoaEQNXVZuP7quGiZ/W1
1lE59gnmnyG8ySFCL05jHrKLtFAJe88gQjgDK65ZJv4W/k7ocmT+HhCxWyQWcX6v
abJ0EssqeSQuzRMuZebMJJ8s46d6RcYuMdIX3RDXq+1moJDFopE7lgNrlRhWgaky
Og8f/u8s1j75tk1YaYcY9uBKjKk7f681R9wMumkd6IEmEvkUwHNFsctxi4fGI7h1
PRdKL0DlhVmnpHlKs6Kvm2sJ3twSAGSrC4u0LuxACeR3XbiBfyhFV/291LSuw/y1
JtWOW5koh0g1k9xtkiu3/ePVdG/CLp796IyRhdB1jP/vD7W5RLLG/VAlomfjsPsB
AnwFYbVZ8KrmMKYUpTJOH31CRzFdOB6nWqXu5tk3nOtLKo1nIOuVtmp9XLz3VtHe
NiZPnqA=
-----END CERTIFICATE-----
23 changes: 9 additions & 14 deletions test/gnat_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@ defmodule GnatTest do
doctest Gnat

setup context do
if context[:multi_server] do
case :gen_tcp.connect('localhost', 4223, [:binary]) do
{:ok, socket} ->
:gen_tcp.close(socket)
{:error, reason} ->
Mix.raise "Cannot connect to gnatsd" <>
" (http://localhost:4223):" <>
" #{:inet.format_error(reason)}\n" <>
"You probably need to start a gnatsd " <>
"server that requires authentication with " <>
"the following command `gnatsd -p 4223 " <>
"--user bob --pass alice`."
end
end
CheckForExpectedNatsServers.check(Map.keys(context))
:ok
end

Expand All @@ -41,6 +28,14 @@ defmodule GnatTest do
:ok = Gnat.stop(pid)
end

@tag :multi_server
test "connet to a server which requires TLS" do
connection_settings = %{port: 4224, tls: true}
{:ok, gnat} = Gnat.start_link(connection_settings)
assert Gnat.ping(gnat) == :ok
assert Gnat.stop(gnat) == :ok
end

test "subscribe to topic and receive a message" do
{:ok, pid} = Gnat.start_link()
{:ok, _ref} = Gnat.sub(pid, self(), "test")
Expand Down
Loading