From 56f2bf8a5dd958a78decab85d019bf9b2c0446a9 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 4 Oct 2026 18:12:23 +1100 Subject: [PATCH] fix(archieui): a sign-in provider outage is a health issue, not a startup failure --- internal/app/archieui/auth.go | 32 ++++---- internal/app/archieui/readiness.go | 15 ++-- internal/app/archieui/run.go | 7 +- internal/domain/identity/credential.go | 5 +- internal/infrastructure/oidc/flow.go | 75 ++++++++++--------- internal/infrastructure/oidc/oidc.go | 65 ++++++++++++---- internal/infrastructure/oidc/provider_test.go | 60 +++++++++++++++ internal/webui/auth.go | 10 ++- 8 files changed, 196 insertions(+), 73 deletions(-) create mode 100644 internal/infrastructure/oidc/provider_test.go diff --git a/internal/app/archieui/auth.go b/internal/app/archieui/auth.go index 0d8e61da4..1059a52f6 100644 --- a/internal/app/archieui/auth.go +++ b/internal/app/archieui/auth.go @@ -10,20 +10,23 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/oidc" ) -// dashboardAuthenticator builds the OIDC credential check, or nil when no -// provider is configured. -func dashboardAuthenticator(ctx context.Context, opts Options, subjects identity.SubjectResolver, log *slog.Logger) (func(context.Context, string) (identity.Identity, error), error) { +// signInProvider returns the configured identity provider, or nil when none +// is. It does not contact the provider: an outage is a health issue, not a +// reason to stop serving. +func signInProvider(opts Options) (*oidc.Provider, error) { if strings.TrimSpace(opts.OidcIssuer) == "" { return nil, nil } - verifier, err := oidc.New(ctx, oidc.Config{Issuer: opts.OidcIssuer, Audience: opts.OidcAudience}) - if err != nil { - // Failing here rather than serving anyway: a dashboard that cannot verify - // a credential would silently fall back to a gate that names nobody, and - // an operator who configured a provider must not get the unauthenticated - // behaviour instead. - return nil, err + return oidc.NewProvider(oidc.Config{Issuer: opts.OidcIssuer, Audience: opts.OidcAudience}) +} + +// dashboardAuthenticator builds the credential check, or nil when no provider +// is configured. +func dashboardAuthenticator(provider *oidc.Provider, opts Options, subjects identity.SubjectResolver, log *slog.Logger) func(context.Context, string) (identity.Identity, error) { + if provider == nil { + return nil } + verifier := oidc.NewVerifier(provider) log.Info("dashboard authenticates provider credentials", "issuer", opts.OidcIssuer, "audience", opts.OidcAudience) return func(ctx context.Context, credential string) (identity.Identity, error) { // A personal token is bound in the State Store, not issued by the @@ -33,19 +36,18 @@ func dashboardAuthenticator(ctx context.Context, opts Options, subjects identity } value, _, err := identity.Authenticate(ctx, subjects, verifier, credential) return value, err - }, nil + } } // dashboardLoginFlow builds the browser sign-in flow, or nil when not // configured. The client secret is read from the named env var. -func dashboardLoginFlow(ctx context.Context, opts Options) (identity.LoginFlow, error) { - if strings.TrimSpace(opts.OidcIssuer) == "" || strings.TrimSpace(opts.OidcClientID) == "" { +func dashboardLoginFlow(provider *oidc.Provider, opts Options) (identity.LoginFlow, error) { + if provider == nil || strings.TrimSpace(opts.OidcClientID) == "" { return nil, nil } secret := "" if name := strings.TrimSpace(opts.OidcClientSecretEnv); name != "" { secret = os.Getenv(name) } - return oidc.NewFlow(ctx, oidc.Config{Issuer: opts.OidcIssuer, Audience: opts.OidcAudience}, - opts.OidcClientID, secret, opts.OidcRedirectURL) + return oidc.NewFlow(provider, opts.OidcClientID, secret, opts.OidcRedirectURL) } diff --git a/internal/app/archieui/readiness.go b/internal/app/archieui/readiness.go index 3326c7b97..8e0748ff9 100644 --- a/internal/app/archieui/readiness.go +++ b/internal/app/archieui/readiness.go @@ -10,13 +10,14 @@ import ( "github.com/samcharles93/archie-core/internal/domain/messaging" "github.com/samcharles93/archie-core/internal/domain/storecontract" infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access" + "github.com/samcharles93/archie-core/internal/infrastructure/oidc" "github.com/samcharles93/archie-core/internal/infrastructure/readiness" ) -// newReadinessRegistry builds the readiness probes for the Gateway and State -// Store. -func newReadinessRegistry(o Options, tasks storecontract.TaskStore, chat messaging.ChatContract, chain *infraaccess.Live) *health.Registry { - return health.NewRegistry( +// newReadinessRegistry builds the readiness probes for the Gateway, the State +// Store and, when one is configured, the sign-in provider. +func newReadinessRegistry(o Options, tasks storecontract.TaskStore, chat messaging.ChatContract, chain *infraaccess.Live, provider *oidc.Provider) *health.Registry { + probes := []health.Probe{ readiness.NewProblemProbe("access_policies", problemSource(chain)), captureRefusalProbe{store: refusalStore(tasks)}, readiness.NewContractProbe("state_db", o.DependencyTimeout, func(ctx context.Context) error { @@ -27,7 +28,11 @@ func newReadinessRegistry(o Options, tasks storecontract.TaskStore, chat messagi _, err := chat.Snapshot(ctx) return err }), - ) + } + if provider != nil { + probes = append(probes, readiness.NewContractProbe("sign_in_provider", o.DependencyTimeout, provider.Check)) + } + return health.NewRegistry(probes...) } // problemSource keeps a missing chain a nil source, which the probe reports diff --git a/internal/app/archieui/run.go b/internal/app/archieui/run.go index eeeaefc4d..e50957f18 100644 --- a/internal/app/archieui/run.go +++ b/internal/app/archieui/run.go @@ -72,11 +72,12 @@ func Run(ctx context.Context, options Options) error { return err } - authenticate, err := dashboardAuthenticator(ctx, opts, tasks, log) + provider, err := signInProvider(opts) if err != nil { return err } - login, err := dashboardLoginFlow(ctx, opts) + authenticate := dashboardAuthenticator(provider, opts, tasks, log) + login, err := dashboardLoginFlow(provider, opts) if err != nil { return err } @@ -86,7 +87,7 @@ func Run(ctx context.Context, options Options) error { Log: log, Store: tasks, Chat: chat, - Health: newReadinessRegistry(opts, tasks, chat, chain), + Health: newReadinessRegistry(opts, tasks, chat, chain, provider), ControlPlane: tasks.ControlPlane(), Identities: tasks, Authenticate: authenticate, diff --git a/internal/domain/identity/credential.go b/internal/domain/identity/credential.go index bfdbc7bfc..9f5b454c3 100644 --- a/internal/domain/identity/credential.go +++ b/internal/domain/identity/credential.go @@ -55,7 +55,7 @@ type SubjectBinding interface { type LoginFlow interface { // AuthCodeURL returns the provider URL for a sign-in and the PKCE verifier the // callback must present with the code. - AuthCodeURL(state string) (string, string) + AuthCodeURL(ctx context.Context, state string) (string, string, error) // Exchange completes the flow and returns what the provider proved. It // verifies through the same path a presented token takes, so a token obtained // by browser cannot be accepted on terms a presented token would be refused on. @@ -88,6 +88,9 @@ var ( // verify: wrong signature, expired, wrong issuer, wrong audience, or // malformed. It never means the credential was merely unrecognised. ErrCredentialRejected = errors.New("credential rejected") + // ErrProviderUnavailable means the identity provider could not be reached, + // so a credential could be neither accepted nor refused. + ErrProviderUnavailable = errors.New("identity provider unavailable") // ErrSubjectUnbound means a verified subject has no identity bound to it. // The provider authenticated someone archie does not know. ErrSubjectUnbound = errors.New("credential subject is not bound to an identity") diff --git a/internal/infrastructure/oidc/flow.go b/internal/infrastructure/oidc/flow.go index a4a73df4d..3aa193e67 100644 --- a/internal/infrastructure/oidc/flow.go +++ b/internal/infrastructure/oidc/flow.go @@ -16,57 +16,58 @@ import ( // password, holding a session, or writing any of the protocol itself: discovery, // the code exchange, PKCE and token verification are the libraries'. type Flow struct { - verifier *Verifier - oauth oauth2.Config - audience string + provider *Provider + verifier *Verifier + clientID string + clientSecret string + redirectURL string } -// NewFlow discovers the provider and builds the browser flow. ClientID and the -// secret are the provider's registration for this dashboard; the secret is read -// from wherever the operator keeps it and is never logged or stored. -func NewFlow(ctx context.Context, cfg Config, clientID, clientSecret, redirectURL string) (*Flow, error) { - issuer := strings.TrimSpace(cfg.Issuer) - audience := strings.TrimSpace(cfg.Audience) - if issuer == "" { - return nil, fmt.Errorf("oidc: issuer is required") - } - if audience == "" { - return nil, fmt.Errorf("oidc: audience is required") - } +// NewFlow builds the browser flow over provider. ClientID and the secret are +// the provider's registration for this dashboard; the secret is read from +// wherever the operator keeps it and is never logged or stored. +func NewFlow(provider *Provider, clientID, clientSecret, redirectURL string) (*Flow, error) { if strings.TrimSpace(clientID) == "" { return nil, fmt.Errorf("oidc: client id is required for the browser flow") } if strings.TrimSpace(redirectURL) == "" { return nil, fmt.Errorf("oidc: redirect URL is required for the browser flow") } - provider, err := gooidc.NewProvider(ctx, issuer) - if err != nil { - return nil, fmt.Errorf("oidc: discover %q: %w", issuer, err) - } return &Flow{ - verifier: &Verifier{verifier: provider.Verifier(&gooidc.Config{ClientID: audience})}, - oauth: oauth2.Config{ - ClientID: clientID, - ClientSecret: clientSecret, - Endpoint: provider.Endpoint(), - RedirectURL: redirectURL, - // openid is what makes the provider return a subject at all; profile - // and email are what make a name available for a display. - Scopes: []string{gooidc.ScopeOpenID, "profile", "email"}, - }, - audience: audience, + provider: provider, verifier: NewVerifier(provider), + clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL, + }, nil +} + +func (f *Flow) oauth(ctx context.Context) (oauth2.Config, error) { + discovered, err := f.provider.get(ctx) + if err != nil { + return oauth2.Config{}, err + } + return oauth2.Config{ + ClientID: f.clientID, + ClientSecret: f.clientSecret, + Endpoint: discovered.Endpoint(), + RedirectURL: f.redirectURL, + // openid is what makes the provider return a subject at all; profile + // and email are what make a name available for a display. + Scopes: []string{gooidc.ScopeOpenID, "profile", "email"}, }, nil } // AuthCodeURL returns the provider URL to send a browser to, with the // audience requested, and the PKCE verifier. -func (f *Flow) AuthCodeURL(state string) (string, string) { +func (f *Flow) AuthCodeURL(ctx context.Context, state string) (string, string, error) { + config, err := f.oauth(ctx) + if err != nil { + return "", "", err + } codeVerifier := oauth2.GenerateVerifier() - url := f.oauth.AuthCodeURL(state, + url := config.AuthCodeURL(state, oauth2.S256ChallengeOption(codeVerifier), - oauth2.SetAuthURLParam("audience", f.audience), + oauth2.SetAuthURLParam("audience", f.provider.audience), ) - return url, codeVerifier + return url, codeVerifier, nil } // Exchange completes the flow: it trades the code for the provider's tokens and @@ -74,7 +75,11 @@ func (f *Flow) AuthCodeURL(state string) (string, string) { // through. There is no second verification path here, so a token obtained by the // browser cannot be accepted on terms a presented token would be refused on. func (f *Flow) Exchange(ctx context.Context, code, codeVerifier string) (identity.ProviderSession, error) { - token, err := f.oauth.Exchange(ctx, strings.TrimSpace(code), oauth2.VerifierOption(codeVerifier)) + config, err := f.oauth(ctx) + if err != nil { + return identity.ProviderSession{}, err + } + token, err := config.Exchange(ctx, strings.TrimSpace(code), oauth2.VerifierOption(codeVerifier)) if err != nil { return identity.ProviderSession{}, fmt.Errorf("oidc: exchange code: %w", err) } diff --git a/internal/infrastructure/oidc/oidc.go b/internal/infrastructure/oidc/oidc.go index 1370c3626..89337c79f 100644 --- a/internal/infrastructure/oidc/oidc.go +++ b/internal/infrastructure/oidc/oidc.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "strings" + "sync" gooidc "github.com/coreos/go-oidc/v3/oidc" @@ -22,15 +23,19 @@ type Config struct { Audience string } -// Verifier validates tokens against the provider's published signing keys. -type Verifier struct { - verifier *gooidc.IDTokenVerifier +// Provider discovers the identity provider on first use and keeps the result. +// A provider that is down at startup does not stop the dashboard: discovery +// is retried on the next request, and Check reports the outage as health. +type Provider struct { + issuer string + audience string + + mu sync.Mutex + discovered *gooidc.Provider } -// New discovers the provider and builds a verifier from its published keys. -// Discovery happens once, at construction: an unreachable provider is a -// startup failure the operator sees, not a request-time surprise. -func New(ctx context.Context, cfg Config) (*Verifier, error) { +// NewProvider validates cfg without contacting the provider. +func NewProvider(cfg Config) (*Provider, error) { issuer := strings.TrimSpace(cfg.Issuer) audience := strings.TrimSpace(cfg.Audience) if issuer == "" { @@ -39,21 +44,55 @@ func New(ctx context.Context, cfg Config) (*Verifier, error) { if audience == "" { return nil, fmt.Errorf("oidc: audience is required") } - provider, err := gooidc.NewProvider(ctx, issuer) - if err != nil { - return nil, fmt.Errorf("oidc: discover %q: %w", issuer, err) + return &Provider{issuer: issuer, audience: audience}, nil +} + +// get returns the discovered provider, discovering it now if no earlier +// attempt succeeded. +func (p *Provider) get(ctx context.Context) (*gooidc.Provider, error) { + p.mu.Lock() + defer p.mu.Unlock() + if p.discovered != nil { + return p.discovered, nil } - return &Verifier{verifier: provider.Verifier(&gooidc.Config{ClientID: audience})}, nil + discovered, err := gooidc.NewProvider(ctx, p.issuer) + if err != nil { + return nil, fmt.Errorf("%w: discover %q: %w", identity.ErrProviderUnavailable, p.issuer, err) + } + p.discovered = discovered + return discovered, nil +} + +// Check fetches the provider's discovery document afresh, so an outage after +// startup shows as well as one before it. +func (p *Provider) Check(ctx context.Context) error { + _, err := gooidc.NewProvider(ctx, p.issuer) + return err +} + +// Verifier validates tokens against the provider's published signing keys. +type Verifier struct { + provider *Provider +} + +// NewVerifier returns a verifier for provider's tokens. +func NewVerifier(provider *Provider) *Verifier { + return &Verifier{provider: provider} } // Verify checks the token's signature, issuer, audience and expiry against the // provider's keys, and returns what it proved. Every check is the library's: // signature comparison, claim validation and key rotation are not archie's code. func (v *Verifier) Verify(ctx context.Context, rawToken string) (identity.Credential, error) { - if v == nil || v.verifier == nil { + if v == nil || v.provider == nil { return identity.Credential{}, fmt.Errorf("oidc: no verifier is configured") } - token, err := v.verifier.Verify(ctx, strings.TrimSpace(rawToken)) + discovered, err := v.provider.get(ctx) + if err != nil { + return identity.Credential{}, err + } + verifier := discovered.Verifier(&gooidc.Config{ClientID: v.provider.audience}) + token, err := verifier.Verify(ctx, strings.TrimSpace(rawToken)) if err != nil { return identity.Credential{}, fmt.Errorf("oidc: verify: %w", err) } diff --git a/internal/infrastructure/oidc/provider_test.go b/internal/infrastructure/oidc/provider_test.go new file mode 100644 index 000000000..9d3572e39 --- /dev/null +++ b/internal/infrastructure/oidc/provider_test.go @@ -0,0 +1,60 @@ +package oidc + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/identity" +) + +// TestProviderOutageIsUnavailableThenRecovers pins that a provider down at +// startup neither stops the dashboard nor refuses credentials as invalid, and +// that sign-in works once it returns. +func TestProviderOutageIsUnavailableThenRecovers(t *testing.T) { + var up atomic.Bool + var server *httptest.Server + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !up.Load() { + http.Error(w, "down", http.StatusServiceUnavailable) + return + } + _ = json.NewEncoder(w).Encode(map[string]string{ + "issuer": server.URL, "authorization_endpoint": server.URL + "/auth", + "token_endpoint": server.URL + "/token", "jwks_uri": server.URL + "/keys", + }) + })) + defer server.Close() + + provider, err := NewProvider(Config{Issuer: server.URL, Audience: "archie"}) + if err != nil { + t.Fatalf("a down provider must not fail construction: %v", err) + } + verifier := NewVerifier(provider) + ctx := context.Background() + + tests := []struct { + name string + up bool + wantUnavailable bool + }{ + {"down is unavailable, not rejected", false, true}, + {"back up is a normal verification", true, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + up.Store(tt.up) + _, err := verifier.Verify(ctx, "not-a-jwt") + if err == nil || errors.Is(err, identity.ErrProviderUnavailable) != tt.wantUnavailable { + t.Fatalf("verify err %v, want unavailable %v", err, tt.wantUnavailable) + } + if checkErr := provider.Check(ctx); (checkErr != nil) != tt.wantUnavailable { + t.Fatalf("check err %v, want failure %v", checkErr, tt.wantUnavailable) + } + }) + } +} diff --git a/internal/webui/auth.go b/internal/webui/auth.go index 9bc4617a3..8a0db7dc6 100644 --- a/internal/webui/auth.go +++ b/internal/webui/auth.go @@ -206,7 +206,11 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) { http.Error(w, "could not start sign-in", http.StatusInternalServerError) return } - url, verifier := s.Login.AuthCodeURL(state) + url, verifier, err := s.Login.AuthCodeURL(r.Context(), state) + if err != nil { + s.refuseUnidentified(w, r, err) + return + } setFlowCookie(w, r, loginStateCookie, state) setFlowCookie(w, r, loginVerifierCookie, verifier) http.Redirect(w, r, url, http.StatusSeeOther) @@ -286,6 +290,10 @@ func (s *Server) refuseUnidentified(w http.ResponseWriter, r *http.Request, err status := http.StatusUnauthorized reason := "That credential was not accepted." switch { + case errors.Is(err, identity.ErrProviderUnavailable): + // Signing in again cannot help until the provider is back. + http.Error(w, "The sign-in provider is unavailable. Try again shortly.", http.StatusServiceUnavailable) + return case errors.Is(err, identity.ErrIdentityInactive): status, reason = http.StatusForbidden, "That identity may not act." case errors.Is(err, identity.ErrSubjectUnbound):