Skip to content

Commit

Permalink
Merge branch 'release/0.14.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Apr 12, 2024
2 parents 1088d31 + b3fea72 commit 19e5d15
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Config

config :tesla, Formation.Client, adapter: Tesla.Mock

config :exvcr,
vcr_cassette_library_dir: "test/fixture/vcr_cassettes"
3 changes: 2 additions & 1 deletion lib/formation/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule Formation.Application do

def start(_type, _args) do
children = [
{Finch, name: AWS.Finch}
{Finch, name: AWS.Finch},
{Finch, name: Formation.Finch}
]

opts = [strategy: :one_for_one, name: Formation.Supervisor]
Expand Down
5 changes: 5 additions & 0 deletions lib/formation/client.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Formation.Client do
use Tesla

adapter(Tesla.Adapter.Finch, name: Formation.Finch)
end
3 changes: 3 additions & 0 deletions lib/formation/lxd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ defmodule Formation.Lxd do
{:ok, %{body: %{"status_code" => status_code} = error}} when status_code >= 400 ->
{:error, error}

{:ok, _} ->
{:error, "failed to retrieve log output"}

{:error, error} ->
{:error, error}
end
Expand Down
19 changes: 19 additions & 0 deletions lib/formation/postgresql/credential.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule Formation.Postgresql.Credential do
|> maybe_set_hostname()
|> maybe_set_database()
|> maybe_set_ssl()
|> maybe_parse_certificate()
|> validate_required([:hostname, :port, :username, :database])
end

Expand All @@ -56,6 +57,14 @@ defmodule Formation.Postgresql.Credential do
|> to_string()
end

defp maybe_parse_certificate(changeset) do
if certificate = get_change(changeset, :certificate) do
put_change(changeset, :certificate, parse_certificate(certificate))
else
changeset
end
end

defp maybe_set_ssl(changeset) do
if secure = get_change(changeset, :secure) do
put_change(changeset, :ssl, secure)
Expand All @@ -79,4 +88,14 @@ defmodule Formation.Postgresql.Credential do
changeset
end
end

defp parse_certificate(certificate) when is_binary(certificate) do
with {:ok, uri} <- URI.new(certificate),
{:ok, %{body: body}} <- Formation.Client.get(to_string(uri)) do
body
else
{:error, _} ->
certificate
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Formation.MixProject do
def project do
[
app: :formation,
version: "0.14.1",
version: "0.14.3",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
description: description(),
Expand Down Expand Up @@ -55,6 +55,7 @@ defmodule Formation.MixProject do
{:lexdee, "~> 2.3"},
{:aws, "~> 0.13.0"},
{:finch, "~> 0.16.0"},
{:tesla, "~> 1.7.0"},
{:postgrex, "~> 0.17.1"},
{:ecto, "~> 3.10"},
# {:dep_from_hexpm, "~> 0.3.0"},
Expand Down
36 changes: 34 additions & 2 deletions test/formation/postgresql/credential_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,37 @@ defmodule Formation.Postgresql.CredentialTest do
end

describe "create credential with certificate" do
test "can create credential with certificate", %{
setup do
Tesla.Mock.mock(fn %{method: :get} ->
%Tesla.Env{status: 200, body: "hello"}
end)

:ok
end

test "can create credential with url certificate", %{
host: host,
port: port,
username: username,
password: password
} do
assert {:ok, credential} =
Credential.create(%{
host: host,
port: port,
username: username,
password: password,
certificate:
"https://truststore.pki.rds.amazonaws.com/us-east-1/us-east-1-bundle.pem",
ssl: false
})

refute is_nil(credential.certificate)

assert credential.certificate == "hello"
end

test "can create credential with binary certificate", %{
host: host,
port: port,
username: username,
Expand All @@ -25,11 +55,13 @@ defmodule Formation.Postgresql.CredentialTest do
port: port,
username: username,
password: password,
certificate: "https://some.cert/file.pem",
certificate: "hello",
ssl: false
})

refute is_nil(credential.certificate)

assert credential.certificate == "hello"
end
end
end

0 comments on commit 19e5d15

Please sign in to comment.