Skip to content

Commit

Permalink
Support custom HTTP certificate (#2287)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonatan Kłosko <[email protected]>
  • Loading branch information
rodrigues and jonatanklosko committed Oct 22, 2023
1 parent bcf4f32 commit 22d02a9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ The following environment variables can be used to configure Livebook on boot:
are deployed on Livebook startup with the persisted settings. Password-protected
notebooks will receive a random password, unless LIVEBOOK_APPS_PATH_PASSWORD
is set. When deploying using Livebook's Docker image, consider using
`LIVEBOOK_APPS_PATH_WARMUP`.
LIVEBOOK_APPS_PATH_WARMUP.

* LIVEBOOK_APPS_PATH_HUB_ID - deploy only the notebooks in
LIVEBOOK_APPS_PATH that belong to the given Hub ID
Expand All @@ -208,6 +208,10 @@ The following environment variables can be used to configure Livebook on boot:
* LIVEBOOK_BASE_URL_PATH - sets the base url path the web application is
served on. Useful when deploying behind a reverse proxy.

* LIVEBOOK_CACERTFILE - path to a local file containing CA certificates.
Those certificates are used during for server authentication when Livebook
accesses files from external sources.

* LIVEBOOK_COOKIE - sets the cookie for running Livebook in a cluster.
Defaults to a random string that is generated on boot.

Expand Down
4 changes: 4 additions & 0 deletions lib/livebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ defmodule Livebook do
config :livebook, :force_ssl_host, force_ssl_host
end

if cacertfile = Livebook.Config.cacertfile!("LIVEBOOK_CACERTFILE") do
config :livebook, :cacertfile, cacertfile
end

config :livebook,
:cookie,
Livebook.Config.cookie!("LIVEBOOK_COOKIE") ||
Expand Down
15 changes: 15 additions & 0 deletions lib/livebook/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ defmodule Livebook.Config do
Application.fetch_env!(:livebook, :force_ssl_host)
end

@doc """
Returns the application cacertfile if any.
"""
@spec cacertfile() :: String.t() | nil
def cacertfile() do
Application.get_env(:livebook, :cacertfile)
end

@feature_flags Application.compile_env(:livebook, :feature_flags)

@doc """
Expand Down Expand Up @@ -513,6 +521,13 @@ defmodule Livebook.Config do
System.get_env(env)
end

@doc """
Parses application cacertfile from env.
"""
def cacertfile!(env) do
System.get_env(env)
end

@doc """
Parses application service name from env.
"""
Expand Down
10 changes: 9 additions & 1 deletion lib/livebook/utils/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,17 @@ defmodule Livebook.Utils.HTTP do

defp http_ssl_opts() do
# Use secure options, see https://gist.github.com/jonatanklosko/5e20ca84127f6b31bbe3906498e1a1d7

cacert_opt =
if cacertfile = Livebook.Config.cacertfile() do
{:cacertfile, to_charlist(cacertfile)}
else
{:cacerts, @cacerts}
end

[
cacert_opt,
verify: :verify_peer,
cacerts: @cacerts,
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
Expand Down

0 comments on commit 22d02a9

Please sign in to comment.