Skip to content

Commit ee9e536

Browse files
sstoltzecamelpunchStrech
authored
Add new arbitrary SSL configuration option (#134)
Co-authored-by: Andrew Bruce <andrew.bruce@maersk.com> Co-authored-by: Sergey Fedorov <oni.strech@gmail.com>
1 parent 343f48e commit ee9e536

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

lib/avrora/client.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ defmodule Avrora.Client do
106106
def registry_url, do: get(@opts, :registry_url, nil)
107107
def registry_auth, do: get(@opts, :registry_auth, nil)
108108
def registry_user_agent, do: get(@opts, :registry_user_agent, "Avrora/#{version()} Elixir")
109+
def registry_ssl_opts, do: get(@opts, :registry_ssl_opts, nil)
109110
def registry_ssl_cacerts, do: get(@opts, :registry_ssl_cacerts, nil)
110111
def registry_schemas_autoreg, do: get(@opts, :registry_schemas_autoreg, true)
111112
def convert_null_values, do: get(@opts, :convert_null_values, true)

lib/avrora/config.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Avrora.Config do
99
* `registry_url` URL for Schema Registry, default `nil`
1010
* `registry_auth` authentication settings for Schema Registry, default `nil`
1111
* `registry_user_agent` HTTP `User-Agent` header for Schema Registry requests, default `Avrora/<version> Elixir`
12+
* `registry_ssl_opts` Erlang SSL client options for connecting to the Schema Registry (takes precedence over other SSL options) (see https://www.erlang.org/docs/26/man/ssl#type-client_option), default `nil`
1213
* `registry_ssl_cacerts` DER-encoded trusted certificate (not combined) (see https://www.erlang.org/docs/26/man/ssl#type-client_cacerts), default `nil`
1314
* `registry_ssl_cacert_path` path to a file containing PEM-encoded CA certificates, default `nil`
1415
* `registry_schemas_autoreg` automatically register schemas in Schema Registry, default `true`
@@ -30,6 +31,7 @@ defmodule Avrora.Config do
3031
@callback registry_url :: String.t() | nil
3132
@callback registry_auth :: tuple() | nil
3233
@callback registry_user_agent :: String.t() | nil
34+
@callback registry_ssl_opts :: [:ssl.tls_option()] | nil
3335
@callback registry_ssl_cacerts :: binary() | nil
3436
@callback registry_ssl_cacert_path :: String.t() | nil
3537
@callback registry_schemas_autoreg :: boolean()

lib/avrora/storage/registry.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ defmodule Avrora.Storage.Registry do
135135
defp options do
136136
ssl_options =
137137
cond do
138+
!is_nil(registry_ssl_opts()) -> registry_ssl_opts()
138139
!is_nil(registry_ssl_cacerts()) -> [verify: :verify_peer, cacerts: [registry_ssl_cacerts()]]
139140
!is_nil(registry_ssl_cacert_path()) -> [verify: :verify_peer, cacertfile: registry_ssl_cacert_path()]
140141
true -> [verify: :verify_none]
@@ -183,6 +184,7 @@ defmodule Avrora.Storage.Registry do
183184
defp registry_url, do: Config.self().registry_url()
184185
defp registry_auth, do: Config.self().registry_auth()
185186
defp registry_user_agent, do: Config.self().registry_user_agent()
187+
defp registry_ssl_opts, do: Config.self().registry_ssl_opts()
186188
defp registry_ssl_cacerts, do: Config.self().registry_ssl_cacerts()
187189
defp registry_ssl_cacert_path, do: Config.self().registry_ssl_cacert_path()
188190
end

test/avrora/storage/registry_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ defmodule Avrora.Storage.RegistryTest do
273273
assert :ok == Registry.get(1) |> elem(0)
274274
end
275275

276+
test "when request should perform SSL verification based on given arbitrary SSL options" do
277+
stub(Avrora.ConfigMock, :registry_ssl_cacert_path, fn -> "path/to/other/file" end)
278+
stub(Avrora.ConfigMock, :registry_ssl_opts, fn -> [verify: :verify_peer, cacertfile: "path/to/file"] end)
279+
280+
Avrora.HTTPClientMock
281+
|> expect(:get, fn url, options ->
282+
assert url == "http://reg.loc/schemas/ids/1"
283+
assert Keyword.fetch!(options, :ssl_options) == [verify: :verify_peer, cacertfile: "path/to/file"]
284+
285+
{:ok, %{"schema" => json_schema()}}
286+
end)
287+
288+
assert :ok == Registry.get(1) |> elem(0)
289+
end
290+
276291
test "when registry url is unconfigured" do
277292
stub(Avrora.ConfigMock, :registry_url, fn -> nil end)
278293

test/support/config.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ defmodule Support.Config do
4242
@impl true
4343
def registry_user_agent, do: nil
4444
@impl true
45+
def registry_ssl_opts, do: nil
46+
@impl true
4547
def registry_ssl_cacerts, do: nil
4648
@impl true
4749
def registry_ssl_cacert_path, do: nil

0 commit comments

Comments
 (0)