-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Zero Trust authentication (#1938)
Co-authored-by: José Valim <[email protected]> Co-authored-by: Jonatan Kłosko <[email protected]>
- Loading branch information
1 parent
5d2a1f4
commit efb28fb
Showing
15 changed files
with
341 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
defmodule Livebook.ZTA.Cloudflare do | ||
@moduledoc false | ||
|
||
use GenServer | ||
require Logger | ||
import Plug.Conn | ||
|
||
@assertion "cf-access-jwt-assertion" | ||
@renew_afer 24 * 60 * 60 * 1000 | ||
|
||
defstruct [:name, :req_options, :identity] | ||
|
||
def start_link(opts) do | ||
identity = identity(opts[:identity][:key]) | ||
options = [req_options: [url: identity.certs], identity: identity] | ||
GenServer.start_link(__MODULE__, options, name: opts[:name]) | ||
end | ||
|
||
def authenticate(name, conn) do | ||
token = get_req_header(conn, @assertion) | ||
GenServer.call(name, {:authenticate, token}) | ||
end | ||
|
||
@impl true | ||
def init(options) do | ||
:ets.new(options[:name], [:public, :named_table]) | ||
{:ok, struct!(__MODULE__, options)} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_keys, _from, state) do | ||
keys = get_from_ets(state.name) || request_and_store_in_ets(state) | ||
{:reply, keys, state} | ||
end | ||
|
||
def handle_call({:authenticate, token}, _from, state) do | ||
keys = get_from_ets(state.name) || request_and_store_in_ets(state) | ||
user = authenticate(token, state.identity, keys) | ||
{:reply, user, state} | ||
end | ||
|
||
@impl true | ||
def handle_info(:request, state) do | ||
request_and_store_in_ets(state) | ||
{:noreply, state} | ||
end | ||
|
||
defp request_and_store_in_ets(state) do | ||
Logger.debug("[#{inspect(__MODULE__)}] requesting #{inspect(state.req_options)}") | ||
keys = Req.request!(state.req_options).body["keys"] | ||
:ets.insert(state.name, keys: keys) | ||
Process.send_after(self(), :request, @renew_afer) | ||
keys | ||
end | ||
|
||
defp get_from_ets(name) do | ||
case :ets.lookup(name, :keys) do | ||
[keys: keys] -> keys | ||
[] -> nil | ||
end | ||
end | ||
|
||
defp authenticate(token, identity, keys) do | ||
with [token] <- token, | ||
{:ok, token} <- verify_token(token, keys), | ||
:ok <- verify_iss(token, identity.iss) do | ||
%{name: token.fields["email"]} | ||
else | ||
_ -> nil | ||
end | ||
end | ||
|
||
defp verify_token(token, keys) do | ||
Enum.find_value(keys, fn key -> | ||
case JOSE.JWT.verify(key, token) do | ||
{true, token, _s} -> {:ok, token} | ||
{_, _t, _s} -> nil | ||
end | ||
end) | ||
end | ||
|
||
defp verify_iss(%{fields: %{"iss" => iss}}, iss), do: :ok | ||
defp verify_iss(_, _), do: nil | ||
|
||
defp identity(key) do | ||
%{ | ||
key: key, | ||
key_type: "domain", | ||
iss: "https://#{key}.cloudflareaccess.com", | ||
certs: "https://#{key}.cloudflareaccess.com/cdn-cgi/access/certs", | ||
assertion: "cf-access-jwt-assertion", | ||
email: "cf-access-authenticated-user-email" | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
defmodule Livebook.ZTA.GoogleIAP do | ||
@moduledoc false | ||
|
||
use GenServer | ||
require Logger | ||
import Plug.Conn | ||
|
||
@assertion "cf-access-jwt-assertion" | ||
@renew_afer 24 * 60 * 60 * 1000 | ||
|
||
defstruct [:name, :req_options, :identity] | ||
|
||
def start_link(opts) do | ||
identity = identity(opts[:identity][:key]) | ||
options = [req_options: [url: identity.certs], identity: identity] | ||
GenServer.start_link(__MODULE__, options, name: opts[:name]) | ||
end | ||
|
||
def authenticate(name, conn) do | ||
token = get_req_header(conn, @assertion) | ||
GenServer.call(name, {:authenticate, token}) | ||
end | ||
|
||
@impl true | ||
def init(options) do | ||
:ets.new(options[:name], [:public, :named_table]) | ||
{:ok, struct!(__MODULE__, options)} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_keys, _from, state) do | ||
keys = get_from_ets(state.name) || request_and_store_in_ets(state) | ||
{:reply, keys, state} | ||
end | ||
|
||
def handle_call({:authenticate, token}, _from, state) do | ||
keys = get_from_ets(state.name) || request_and_store_in_ets(state) | ||
user = authenticate(token, state.identity, keys) | ||
{:reply, user, state} | ||
end | ||
|
||
@impl true | ||
def handle_info(:request, state) do | ||
request_and_store_in_ets(state) | ||
{:noreply, state} | ||
end | ||
|
||
defp request_and_store_in_ets(state) do | ||
Logger.debug("[#{inspect(__MODULE__)}] requesting #{inspect(state.req_options)}") | ||
keys = Req.request!(state.req_options).body["keys"] | ||
:ets.insert(state.name, keys: keys) | ||
Process.send_after(self(), :request, @renew_afer) | ||
keys | ||
end | ||
|
||
defp get_from_ets(name) do | ||
case :ets.lookup(name, :keys) do | ||
[keys: keys] -> keys | ||
[] -> nil | ||
end | ||
end | ||
|
||
defp authenticate(token, identity, keys) do | ||
with [token] <- token, | ||
{:ok, token} <- verify_token(token, keys), | ||
:ok <- verify_iss(token, identity.iss) do | ||
%{name: token.fields["email"]} | ||
else | ||
_ -> nil | ||
end | ||
end | ||
|
||
defp verify_token(token, keys) do | ||
Enum.find_value(keys, fn key -> | ||
case JOSE.JWT.verify(key, token) do | ||
{true, token, _s} -> {:ok, token} | ||
{_, _t, _s} -> nil | ||
end | ||
end) | ||
end | ||
|
||
defp verify_iss(%{fields: %{"iss" => iss}}, iss), do: :ok | ||
defp verify_iss(_, _), do: nil | ||
|
||
defp identity(key) do | ||
%{ | ||
key: key, | ||
key_type: "aud", | ||
iss: "https://cloud.google.com/iap", | ||
certs: "https://www.gstatic.com/iap/verify/public_key", | ||
assertion: "x-goog-iap-jwt-assertion", | ||
email: "x-goog-authenticated-user-email" | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="icon" type="image/svg+xml" href={~p"/favicon.svg"} /> | ||
<link rel="alternate icon" type="image/png" href={~p"/favicon.png"} /> | ||
<title><%= @status %> - Livebook</title> | ||
<link rel="stylesheet" href={~p"/assets/app.css"} /> | ||
</head> | ||
<body> | ||
<div class="h-screen flex items-center justify-center bg-gray-900"> | ||
<div class="flex flex-col space-y-4 items-center"> | ||
<a href={~p"/"}> | ||
<img src={~p"/images/logo.png"} height="128" width="128" alt="livebook" /> | ||
</a> | ||
<div class="text-2xl text-gray-50"> | ||
No Numbats allowed here! | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule LivebookWeb.Cookies do | ||
# This module implements the ZTA contract specific to Livebook cookies | ||
@moduledoc false | ||
|
||
def authenticate(_, _conn) do | ||
%{} | ||
end | ||
|
||
def child_spec(_opts) do | ||
%{id: __MODULE__, start: {Function, :identity, [:ignore]}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.