fix(archieui): a sign-in provider outage is a health issue, not a startup failure
This commit is contained in:
1 parent
90399f5025
commit
56f2bf8a5d
8 files changed
+196
-73
No files matched your search
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user