Skip to content

Commit

Permalink
Add SSL support to connection cell (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmithstubbs authored Aug 31, 2023
1 parent c292a3f commit d715c8b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/assets/connection_cell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ export function init(ctx, info) {
:grow
:required
/>
<BaseSwitch
name="use_ssl"
label="SSL"
v-model="fields.use_ssl"
/>
<BaseSwitch
name="use_ipv6"
label="IPv6"
Expand Down
14 changes: 11 additions & 3 deletions lib/kino_db/connection_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule KinoDB.ConnectionCell do
"database_path" => attrs["database_path"] || "",
"port" => attrs["port"] || default_port,
"use_ipv6" => Map.get(attrs, "use_ipv6", false),
"use_ssl" => Map.get(attrs, "use_ssl", false),
"username" => attrs["username"] || "",
"password" => password,
"use_password_secret" => Map.has_key?(attrs, "password_secret") || password == "",
Expand Down Expand Up @@ -138,8 +139,8 @@ defmodule KinoDB.ConnectionCell do

type when type in ["postgres", "mysql"] ->
if fields["use_password_secret"],
do: ~w|database hostname port use_ipv6 username password_secret|,
else: ~w|database hostname port use_ipv6 username password|
do: ~w|database hostname port use_ipv6 use_ssl username password_secret|,
else: ~w|database hostname port use_ipv6 use_ssl username password|
end

Map.take(fields, @default_keys ++ connection_keys)
Expand Down Expand Up @@ -322,6 +323,13 @@ defmodule KinoDB.ConnectionCell do
database: attrs["database"]
]

opts =
if attrs["use_ssl"] do
opts ++ [ssl: attrs["use_ssl"]]
else
opts
end

if attrs["use_ipv6"] do
opts ++ [socket_options: [:inet6]]
else
Expand Down Expand Up @@ -355,7 +363,7 @@ defmodule KinoDB.ConnectionCell do

defp missing_dep(%{"type" => "postgres"}) do
unless Code.ensure_loaded?(Postgrex) do
~s/{:postgrex, "~> 0.16.3"}/
~s/{:postgrex, "~> 0.17.3"}/
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule KinoDB.MixProject do
[
{:kino, "~> 0.7"},
{:table, "~> 0.1.2"},
{:postgrex, "~> 0.16.3 or ~> 0.17", optional: true},
{:postgrex, "~> 0.17.3 or ~> 0.18", optional: true},
{:exqlite, "~> 0.11", optional: true},
{:myxql, "~> 0.6.2 or ~> 0.7", optional: true},
{:db_connection, "~> 2.4.2", optional: true},
Expand Down
16 changes: 16 additions & 0 deletions test/kino_db/connection_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule KinoDB.ConnectionCellTest do
"hostname" => "localhost",
"port" => 4444,
"use_ipv6" => false,
"use_ssl" => false,
"username" => "admin",
"password" => "pass",
"use_password_secret" => false,
Expand Down Expand Up @@ -87,6 +88,21 @@ defmodule KinoDB.ConnectionCellTest do
{:ok, db} = Kino.start_child({Postgrex, opts})\
'''

attrs = Map.put(@attrs, "use_ssl", true)

assert ConnectionCell.to_source(attrs) === ~s'''
opts = [
hostname: "localhost",
port: 4444,
username: "admin",
password: "pass",
database: "default",
ssl: true
]
{:ok, db} = Kino.start_child({Postgrex, opts})\
'''

attrs = Map.delete(@attrs, "password") |> Map.merge(%{"password_secret" => "PASS"})

assert ConnectionCell.to_source(attrs) === ~s'''
Expand Down

0 comments on commit d715c8b

Please sign in to comment.