Skip to content

Commit e8de281

Browse files
test oidc setup plural
1 parent 63af97c commit e8de281

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

.github/workflows/test-pr.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: CI / Demo PR
2+
3+
env:
4+
DOCKER_METADATA_PR_HEAD_SHA: 'true'
5+
6+
on:
7+
push:
8+
branches:
9+
- master
10+
- genstage-stack-gs-reconciler
11+
jobs:
12+
pr:
13+
permissions:
14+
id-token: write
15+
contents: read
16+
name: Generate PR
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout the repo
20+
uses: actions/checkout@v3
21+
with:
22+
fetch-depth: 0
23+
- name: setup plural
24+
uses: pluralsh/setup-plural@535f6523af77c86b90ce118e17b6362576689ba7
25+
with:
26+
27+
consoleUrl: https://console.plrldemo.onplural.sh

lib/console/deployments/settings.ex

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule Console.Deployments.Settings do
77
alias Console.Services.Users
88
alias Console.Deployments.{Clusters, Services}
99
alias Console.Schema.{DeploymentSettings, User, Project, BootstrapToken, CloudConnection, FederatedCredential}
10+
require Logger
1011

1112
@agent_vsn File.read!("AGENT_VERSION") |> String.trim()
1213
@kube_vsn File.read!("KUBE_VERSION") |> String.trim()
@@ -327,12 +328,12 @@ defmodule Console.Deployments.Settings do
327328
"""
328329
@spec exchange_token(binary, binary) :: {:ok, binary} | Console.error()
329330
def exchange_token(token, email) do
330-
user = Users.get_user_by_email!(email)
331+
user = Users.cached_user_by_email!(email)
331332

332-
with {:token, {:ok, %{"iss" => issuer}}} <- {:token, Joken.peek_claims(token)},
333+
with {:token, {:ok, %{"iss" => issuer, "aud" => aud}}} <- {:token, Joken.peek_claims(token)},
333334
{:config, {:ok, {conf, jwks}}} <- {:config, issuer_configuration(issuer)},
334335
opts = %{client_jwks: JOSE.JWK.generate_key(16)},
335-
ctx = Oidcc.ClientContext.from_manual(conf, jwks, "dummy_id", "dummy_secret", opts),
336+
ctx = Oidcc.ClientContext.from_manual(conf, jwks, aud, "dummy_secret", opts),
336337
opts = %{signing_algs: ctx.provider_configuration.id_token_signing_alg_values_supported},
337338
{:validate, {:ok, claims}} <- {:validate, Oidcc.Token.validate_jwt(token, ctx, opts)} do
338339
FederatedCredential.for_issuer(issuer)
@@ -344,12 +345,16 @@ defmodule Console.Deployments.Settings do
344345
scopes = Enum.flat_map(credentials, & &1.scopes || [])
345346
|> Enum.uniq()
346347
sign_token(user, scopes)
347-
_ -> {:error, "no federated credential for #{email} match jwt claims"}
348+
_ -> {:error, "no federated credentials for #{email} match jwt claims"}
348349
end
349350
else
350-
{:token, _} -> {:error, "invalid jwt format"}
351-
{:config, _} -> {:error, "invalid issuer url from jwt"}
352-
{:validate, _} -> {:error, "could not validate jwt"}
351+
{:token, _} -> {:error, "invalid jwt format, must include iss and aud claims"}
352+
{:config, _} = res ->
353+
Logger.error("failed to fetch issuer configuration: #{inspect(res)}")
354+
{:error, "invalid issuer url from jwt"}
355+
{:validate, _} = res ->
356+
Logger.error("failed to validate jwt: #{inspect(res)}")
357+
{:error, "could not validate jwt"}
353358
end
354359
end
355360

@@ -360,12 +365,22 @@ defmodule Console.Deployments.Settings do
360365
end
361366
end
362367

368+
@quirks %{
369+
quirks: %{
370+
allow_issuer_mismatch: true,
371+
document_overrides: %{
372+
# needed for atypical oidc implementations like Github's
373+
"authorization_endpoint" => "https://example.com/ignore/oauth/authorize",
374+
}
375+
}
376+
}
377+
363378
@doc """
364379
Fetches the issuer configuration from the issuer url
365380
"""
366381
@decorate cacheable(cache: @cache_adapter, key: {:issuer_configuration, issuer}, opts: [ttl: :timer.minutes(60)])
367382
def issuer_configuration(issuer) do
368-
with {:ok, {conf, _}} <- Oidcc.ProviderConfiguration.load_configuration(issuer, %{quirks: %{allow_issuer_mismatch: true}}),
383+
with {:ok, {conf, _}} <- Oidcc.ProviderConfiguration.load_configuration(issuer, @quirks),
369384
{:ok, {jwks, _}} <- Oidcc.ProviderConfiguration.load_jwks(conf.jwks_uri),
370385
do: {:ok, {conf, jwks}}
371386
end

lib/console/services/users.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule Console.Services.Users do
4848
@decorate cacheable(cache: @cache_adapter, key: :console_bot, opts: [ttl: @ttl])
4949
def console(), do: Repo.get_by(User, email: "[email protected]")
5050

51-
@decorate cacheable(cache: Console.Cache, key: {:access, token}, opts: [ttl: @ttl])
51+
@decorate cacheable(cache: @cache_adapter, key: {:access, token}, opts: [ttl: @ttl])
5252
def get_by_token(token) do
5353
Repo.get_by(AccessToken, token: token)
5454
|> Repo.preload([:user])
@@ -72,6 +72,9 @@ defmodule Console.Services.Users do
7272

7373
def get_user_by_email!(email), do: Repo.get_by!(User, email: email)
7474

75+
@decorate cacheable(cache: @cache_adapter, key: {:user_by_email, email}, opts: [ttl: @ttl])
76+
def cached_user_by_email!(email), do: get_user_by_email!(email)
77+
7578
@spec get_group!(binary) :: Group.t
7679
def get_group!(id), do: Repo.get!(Group, id)
7780

lib/console_web/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ defmodule ConsoleWeb.Router do
1616
scope "/v1", ConsoleWeb do
1717
pipe_through [:api]
1818

19+
post "/token/exchange", JWTController, :exchange
1920
get "/dashboard/cluster", WebhookController, :cluster
2021
end
2122

@@ -79,7 +80,6 @@ defmodule ConsoleWeb.Router do
7980
pipe_through [:auth]
8081

8182
scope "/v1", ConsoleWeb do
82-
post "/token/exchange", JWTController, :exchange
8383
get "/digests", GitController, :digest
8484
get "/compliance/report", ComplianceController, :report
8585
get "/compliance/report/:name", ComplianceController, :report

0 commit comments

Comments
 (0)