Skip to content

Commit

Permalink
feat: add api and metrics token blocklists (#350)
Browse files Browse the repository at this point in the history
* feat: add api and metrics token blocklists
  • Loading branch information
abc3 authored Jun 4, 2024
1 parent 61b629d commit d6897df
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.56
1.1.57
4 changes: 3 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ if config_env() != :test do
global_upstream_ca: upstream_ca,
global_downstream_cert: downstream_cert,
global_downstream_key: downstream_key,
reconnect_on_db_close: System.get_env("RECONNECT_ON_DB_CLOSE") == "true"
reconnect_on_db_close: System.get_env("RECONNECT_ON_DB_CLOSE") == "true",
api_blocklist: System.get_env("API_TOKEN_BLOCKLIST", "") |> String.split(","),
metrics_blocklist: System.get_env("METRICS_TOKEN_BLOCKLIST", "") |> String.split(",")

config :supavisor, Supavisor.Repo,
url: System.get_env("DATABASE_URL", "ecto://postgres:postgres@localhost:6432/postgres"),
Expand Down
6 changes: 5 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ config :supavisor,
proxy_port_transaction: System.get_env("PROXY_PORT_TRANSACTION", "7654") |> String.to_integer(),
secondary_proxy_port: 7655,
secondary_http: 4003,
prom_poll_rate: 500
prom_poll_rate: 500,
api_blocklist: [
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJvbGUiOiJibG9ja2VkIiwiaWF0IjoxNjQ1MTkyODI0LCJleHAiOjE5NjA3Njg4MjR9.y-V3D1N2e8UTXc5PJzmV9cqMteq0ph2wl0yt42akQgA"
],
metrics_blocklist: []

config :supavisor, Supavisor.Repo,
username: "postgres",
Expand Down
8 changes: 5 additions & 3 deletions lib/supavisor_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ defmodule SupavisorWeb.Router do

pipeline :api do
plug(:accepts, ["json"])
plug(:check_auth, :api_jwt_secret)
plug(:check_auth, [:api_jwt_secret, :api_blocklist])
end

pipeline :metrics do
plug(:check_auth, :metrics_jwt_secret)
plug(:check_auth, [:metrics_jwt_secret, :metrics_blocklist])
end

pipeline :openapi do
Expand Down Expand Up @@ -84,10 +84,12 @@ defmodule SupavisorWeb.Router do

defp check_auth(%{request_path: "/api/health"} = conn, _), do: conn

defp check_auth(conn, secret_key) do
defp check_auth(conn, [secret_key, blocklist_key]) do
secret = Application.fetch_env!(:supavisor, secret_key)
blocklist = Application.fetch_env!(:supavisor, blocklist_key)

with ["Bearer " <> token] <- get_req_header(conn, "authorization"),
false <- token in blocklist,
{:ok, _claims} <- Supavisor.Jwt.authorize(token, secret) do
conn
else
Expand Down
23 changes: 22 additions & 1 deletion test/supavisor_web/controllers/tenant_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule SupavisorWeb.TenantControllerTest do

@jwt "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJvbGUiOiJhbm9uIiwiaWF0IjoxNjQ1MTkyODI0LCJleHAiOjE5NjA3Njg4MjR9.M9jrxyvPLkUxWgOYSf5dNdJ8v_eRrq810ShFRT8N-6M"

@blocked_jwt "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJvbGUiOiJibG9ja2VkIiwiaWF0IjoxNjQ1MTkyODI0LCJleHAiOjE5NjA3Njg4MjR9.y-V3D1N2e8UTXc5PJzmV9cqMteq0ph2wl0yt42akQgA"

@user_valid_attrs %{
db_user_alias: "some_db_user",
db_user: "some db_user",
Expand Down Expand Up @@ -51,7 +53,15 @@ defmodule SupavisorWeb.TenantControllerTest do
"Bearer " <> @jwt
)

{:ok, conn: new_conn}
blocked_conn =
conn
|> put_req_header("accept", "application/json")
|> put_req_header(
"authorization",
"Bearer " <> @blocked_jwt
)

{:ok, conn: new_conn, blocked_conn: blocked_conn}
end

describe "create tenant" do
Expand All @@ -66,6 +76,17 @@ defmodule SupavisorWeb.TenantControllerTest do
end
end

describe "create tenant with blocked ip" do
test "renders tenant when data is valid", %{blocked_conn: blocked_conn} do
blocked_conn =
put(blocked_conn, Routes.tenant_path(blocked_conn, :update, "dev_tenant"),
tenant: @create_attrs
)

assert blocked_conn.status == 403
end
end

describe "update tenant" do
setup [:create_tenant]

Expand Down

0 comments on commit d6897df

Please sign in to comment.