diff --git a/README.md b/README.md index 8923417..e887708 100644 --- a/README.md +++ b/README.md @@ -405,6 +405,83 @@ services, and a token issued for the expense tool would otherwise be a valid credential here. The user is real, the token is real, and they never intended to authenticate against this. +### Signing in + +```bash +modelforgectl login +# to sign in, visit: +# +# https://idp.example.com/authorize?... +# +# opening your browser at https://idp.example.com... +# signed in as sahil@example.com (admin) +# this session expires at 2026-08-31T20:34:32Z + +modelforgectl whoami +# sahil@example.com (user) +# scopes admin +# subject sub-sahil +# issuer https://idp.example.com +# expires 2026-08-31T20:34:32Z +``` + +The credential is stored at `~/.config/modelforge/credential`, mode 0600 in a +0700 directory — both set explicitly rather than left to the umask, since a +umask of 022 leaves a readable credential in a home directory other accounts on +the machine can list. An existing directory is tightened rather than trusted, +because `MkdirAll` leaves the mode of one that already exists alone. +`MODELFORGE_TOKEN` still wins if set, so a script can override whoever happens +to be signed in on the machine it runs on. + +`--no-browser` prints the URL and waits, for a headless box, a remote shell, or +anyone who would rather paste a URL into a browser they trust than have a +process launch one. + +**This is the RFC 8252 native-application flow**, and its shape is dictated by +what a command-line tool can safely do: + +- **No client secret.** One shipped inside a binary is a string every user can + read out of it. The client is public and **PKCE** takes the secret's place — + the test provider recomputes the challenge from the verifier and refuses a + mismatch, so the CLI cannot ship without it and still pass. +- **`state` on every request**, checked before the code is touched. Without it + anybody who can make your browser load a URL can complete a login of their + choosing, and you end up holding *their* session. +- **`nonce` bound to the login**, so a token captured from somewhere else cannot + be replayed into it. +- **Redirect to `127.0.0.1`, not `localhost`.** `localhost` resolves through + DNS, and a hostile resolver points it elsewhere — at which point the + authorization code is delivered to somebody else's machine. The literal + address cannot be redirected. The port is ephemeral, which RFC 8252 requires + providers to accept for exactly this reason. +- **The callback page interpolates nothing.** A provider's `error_description` + reflected into it would be cross-site scripting on a page that has just + handled an authorization code. + +The server advertises where to sign in at `GET /v1/auth/config`, which is +deliberately unauthenticated: a client cannot present a credential before it +knows where to obtain one, and every value there is visible to anybody watching +a browser perform a login. + +`logout` deletes the local file and says plainly that this is **not** +revocation — the token stays valid at the provider until it expires. + +### A bug worth recording + +The first version left `Endpoint.AuthStyle` unset. `x/oauth2` then probes for +the server's preferred client-authentication style: it tries one, and if that +is rejected, retries with the other. + +An authorization code is single-use. So the probe **burns the code**, and the +error you see is the *second* attempt's `invalid_grant` — which points at the +code, when the real problem was whatever made the first request fail. I spent a +while chasing a phantom double-exchange because of it. + +Setting `AuthStyleInParams` explicitly is both correct — RFC 6749 says a public +client sends its id in the body, having no secret for a Basic header — and +removes the failure mode. Verified: one token request per login, where there +had been two. + ### Why a library for the JWT part I wrote the XGBoost scorer rather than binding to libxgboost, and did not write @@ -541,7 +618,7 @@ and a fake agrees with whatever the code happens to do. same database and truncate it between cases, so running packages concurrently has them resetting the database underneath each other. -Coverage is **91.7%** of `internal/`, measured with `-coverpkg` so that code is +Coverage is **90.9%** of `internal/`, measured with `-coverpkg` so that code is attributed to the package that owns it rather than the package running the test — without it the end-to-end suites, which exercise serving and routing through HTTP, would report those packages as untested. @@ -570,6 +647,9 @@ fail silently: | `TestUnsignedTokenIsRejected` | the `alg:none` bypass | | `TestWrongAudienceIsRejected` | a token minted for another service working here | | `TestAuthenticatedButNotAuthorised` | a valid login becoming access by default | +| `TestPKCEIsActuallySent` | a login flow shipping without PKCE | +| `TestCallbackRejectsAForgedState` | a forged callback completing somebody else's login | +| `TestStoredCredentialIsPrivate` | a world-readable credential in a home directory | ## Known limitations @@ -589,9 +669,14 @@ I would rather write these down than let someone discover them. - **The rollout guard reacts to errors, not to quality.** It catches a version that is throwing, not one that is quietly predicting badly — that needs labels, which arrive long after the request. -- **There is no login flow.** The server verifies a JWT somebody already holds; - obtaining one is the identity provider's job. That is the right split, but it - means a browser hitting this API directly gets a 401 rather than a redirect. +- **The login flow is for a terminal, not a browser session.** `modelforgectl + login` gets a person a credential; the server itself has no cookies and no + sign-in page, so a browser pointed straight at the API gets a 401 rather than + a redirect. Adding sessions would mean cookie handling and CSRF defence for + the sake of an API with no UI in front of it. +- **There is no refresh.** When the token expires you log in again. Storing a + refresh token on disk is a longer-lived secret than the one it replaces, and + the sessions here are hours rather than minutes. - **Static-token revocation is as fast as the reload.** A withdrawn token stops working on the next `SIGHUP` or within the poll interval, not instantly across a fleet. OIDC logins do not have this problem — they expire on their own. diff --git a/cmd/modelforge/main.go b/cmd/modelforge/main.go index 5bcd019..de7941e 100644 --- a/cmd/modelforge/main.go +++ b/cmd/modelforge/main.go @@ -39,6 +39,8 @@ func main() { "identity provider URL, enabling per-user login alongside static tokens") flag.StringVar(&cfg.OIDCAudience, "oidc-audience", os.Getenv("MODELFORGE_OIDC_AUDIENCE"), "value a token's aud claim must contain; required with -oidc-issuer") + flag.StringVar(&cfg.OIDCClientID, "oidc-client-id", os.Getenv("MODELFORGE_OIDC_CLIENT_ID"), + "public OAuth client id the CLI logs in as; enables `modelforgectl login`") flag.StringVar(&cfg.OIDCGroupsClaim, "oidc-groups-claim", envOr("MODELFORGE_OIDC_GROUPS_CLAIM", "groups"), "claim carrying group membership") scopeMap := flag.String("oidc-scope-map", os.Getenv("MODELFORGE_OIDC_SCOPE_MAP"), diff --git a/internal/api/api.go b/internal/api/api.go index 19e8c8d..6182002 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -99,6 +99,16 @@ func NewServer(deps Deps) *Server { s.mux.Handle(pattern, mw.RequireFunc(auth.ScopeAdmin, h)) } + // Who am I. Requires only that the caller authenticated at all, so any + // valid credential can ask; the answer is about the caller and reveals + // nothing they do not already hold. + s.mux.Handle("GET /v1/auth/whoami", mw.RequireFunc(auth.ScopePredict, s.handleWhoAmI)) + + // Login metadata, deliberately unauthenticated: a client cannot present a + // credential before it knows where to obtain one, and every value here is + // visible to anybody who watches a browser perform a login. + s.mux.HandleFunc("GET /v1/auth/config", s.handleAuthConfig) + // Operations. Deliberately unauthenticated. // // A liveness probe that needs a credential is a probe that starts failing @@ -558,6 +568,64 @@ func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]any{"status": "ok", "loaded": len(loaded)}) } +// WhoAmIResponse describes the caller. +type WhoAmIResponse struct { + Name string `json:"name"` + Kind string `json:"kind"` + Scopes []auth.Scope `json:"scopes"` + Subject string `json:"subject,omitempty"` + Issuer string `json:"issuer,omitempty"` + Email string `json:"email,omitempty"` + Expires string `json:"expires,omitempty"` +} + +// handleWhoAmI reports the identity behind the current credential. +// +// It exists because "am I logged in, as whom, and with what access" is the +// first question anybody asks when a request is refused, and answering it by +// decoding a JWT locally would report what the token claims rather than what +// this server accepts — which is precisely the thing in doubt. +func (s *Server) handleWhoAmI(w http.ResponseWriter, r *http.Request) { + tok, ok := auth.FromContext(r.Context()) + if !ok { + writeError(w, http.StatusInternalServerError, errors.New("no identity on the request")) + return + } + + kind := "service" + if tok.Human() { + kind = "user" + } + resp := WhoAmIResponse{ + Name: tok.Name, Kind: kind, Scopes: tok.Scopes, + Subject: tok.Subject, Issuer: tok.Issuer, Email: tok.Email, + } + if !tok.NotAfter.IsZero() { + resp.Expires = tok.NotAfter.UTC().Format(time.RFC3339) + } + writeJSON(w, http.StatusOK, resp) +} + +// handleAuthConfig advertises where to log in. +func (s *Server) handleAuthConfig(w http.ResponseWriter, r *http.Request) { + if s.deps.Auth.IsDisabled() { + writeJSON(w, http.StatusOK, map[string]any{"login": false, "reason": "authentication is disabled"}) + return + } + v := s.deps.Auth.OIDC() + if v == nil || !v.LoginEnabled() { + writeJSON(w, http.StatusOK, map[string]any{ + "login": false, + "reason": "this server has no identity provider configured for interactive login", + }) + return + } + cfg := v.PublicConfig() + writeJSON(w, http.StatusOK, map[string]any{ + "login": true, "issuer": cfg.Issuer, "client_id": cfg.ClientID, "audience": cfg.Audience, + }) +} + // --- helpers --- func modelVersion(r *http.Request) (string, int, error) { diff --git a/internal/app/app.go b/internal/app/app.go index c055a03..7a58506 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -78,6 +78,11 @@ type Config struct { // other service would authenticate here. OIDCAudience string + // OIDCClientID is the public OAuth client the CLI logs in as. Without it + // tokens can still be verified, but nobody can obtain one through this + // server. + OIDCClientID string + // OIDCGroupsClaim names the claim carrying group membership. OIDCGroupsClaim string @@ -333,6 +338,7 @@ func buildAuth(ctx context.Context, cfg Config) (*auth.Authenticator, *auth.OIDC verifier, err = auth.NewOIDCVerifier(ctx, auth.OIDCConfig{ Issuer: cfg.OIDCIssuer, Audience: cfg.OIDCAudience, + ClientID: cfg.OIDCClientID, GroupsClaim: cfg.OIDCGroupsClaim, ScopeMap: scopeMap, }, cfg.Logger) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index e902528..227659d 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -194,6 +194,9 @@ func Disabled() *Authenticator { return a } +// OIDC returns the identity provider verifier, or nil if none is configured. +func (a *Authenticator) OIDC() *OIDCVerifier { return a.oidc } + // IsDisabled reports whether authentication is off. func (a *Authenticator) IsDisabled() bool { return a.disabled } diff --git a/internal/auth/oidc.go b/internal/auth/oidc.go index 0ae4bdf..365594c 100644 --- a/internal/auth/oidc.go +++ b/internal/auth/oidc.go @@ -35,6 +35,16 @@ type OIDCConfig struct { // and they never intended to authenticate here. Audience string + // ClientID is the public OAuth client the CLI authenticates as. It is + // advertised to clients so they do not have to be configured separately + // from the server they are talking to. + // + // There is no client secret. The login flow is a native application on a + // user's machine, and a secret shipped inside one is not a secret — it is + // a string every user can read out of the binary. PKCE replaces it, which + // is what RFC 8252 says to do for exactly this case. + ClientID string + // GroupsClaim names the claim carrying a user's group or role // memberships. Providers disagree: Okta and Auth0 commonly use "groups", // Keycloak "roles", Entra ID "roles" or a mapped claim. @@ -74,6 +84,29 @@ type OIDCVerifier struct { ready chan struct{} } +// PublicConfig is the metadata a client needs to start a login. It is +// deliberately public: every value in it is visible to anybody who watches a +// browser perform a login, and withholding it would only stop legitimate +// clients from configuring themselves. +type PublicConfig struct { + Issuer string `json:"issuer"` + ClientID string `json:"client_id"` + Audience string `json:"audience"` +} + +// PublicConfig returns the metadata a client needs to log in. +func (v *OIDCVerifier) PublicConfig() PublicConfig { + return PublicConfig{ + Issuer: v.cfg.Issuer, + ClientID: v.cfg.ClientID, + Audience: v.cfg.Audience, + } +} + +// LoginEnabled reports whether a client can start an interactive login, which +// needs a client id as well as an issuer. +func (v *OIDCVerifier) LoginEnabled() bool { return v.cfg.ClientID != "" } + // Validate checks an OIDC configuration. func (c OIDCConfig) Validate() error { if c.Issuer == "" { diff --git a/internal/cli/cli.go b/internal/cli/cli.go index dd3ec40..059795b 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -8,6 +8,7 @@ package cli import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -38,7 +39,9 @@ type Client struct { func NewClient(baseURL string, out io.Writer) *Client { return &Client{ BaseURL: strings.TrimRight(baseURL, "/"), - Token: os.Getenv("MODELFORGE_TOKEN"), + // The environment wins over a stored login, so a script can override + // whoever happens to be signed in on the machine it runs on. + Token: credential(), // The timeout is generous because pushing a model uploads its // artifact, which is the slowest thing this client does by far. HTTP: &http.Client{Timeout: 2 * time.Minute}, @@ -46,6 +49,14 @@ func NewClient(baseURL string, out io.Writer) *Client { } } +// credential resolves which token to send. +func credential() string { + if t := os.Getenv("MODELFORGE_TOKEN"); t != "" { + return t + } + return LoadCredential() +} + // Usage is the help text. const Usage = `modelforgectl — client for a modelforge server @@ -67,12 +78,16 @@ Commands: drift show drift readings stats show batching and health counters token [+] [--env] mint an API token and print its config entry + login [--no-browser] sign in through your identity provider + logout remove the stored login from this machine + whoami show who the current credential belongs to Flags: -addr URL server address (default http://localhost:8080, or MODELFORGE_ADDR) Environment: - MODELFORGE_TOKEN bearer token sent with every request + MODELFORGE_TOKEN bearer token sent with every request; overrides a stored login + MODELFORGE_CREDENTIAL_FILE where the login command stores its result Scopes: predict (call the serving endpoint), read (inspect models, policies, drift and metrics), admin (everything, including changing what serves traffic). @@ -152,6 +167,19 @@ func Run(args []string, addr string, out io.Writer) int { if v, err = versionArg(args, "stats"); err == nil { err = c.ShowStats(args[1], v) } + case "login": + open := OpenBrowser + if len(args) > 1 && args[1] == "--no-browser" { + // For a headless machine, a remote shell, or anyone who would + // rather paste the URL into a browser they trust than have a + // process launch one for them. + open = func(string) error { return errNoBrowserRequested } + } + err = c.Login(context.Background(), open) + case "logout": + err = c.Logout() + case "whoami": + err = c.WhoAmI() case "token": if len(args) < 3 { err = fmt.Errorf("token needs a name and at least one scope") diff --git a/internal/cli/login.go b/internal/cli/login.go new file mode 100644 index 0000000..9267713 --- /dev/null +++ b/internal/cli/login.go @@ -0,0 +1,462 @@ +package cli + +import ( + "context" + "crypto/rand" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "time" + + "github.com/coreos/go-oidc/v3/oidc" + "golang.org/x/oauth2" +) + +// loginTimeout bounds how long the CLI waits for somebody to finish +// authenticating in their browser. Long enough to find a password manager and +// a second factor; short enough that an abandoned attempt does not leave a +// listener open on a laptop indefinitely. +const loginTimeout = 3 * time.Minute + +// errNoBrowserRequested is returned by the browser opener when --no-browser was +// asked for, so the flow prints the URL and waits rather than reporting a +// failure that did not happen. +var errNoBrowserRequested = errors.New("no browser requested") + +// Login performs an OAuth authorization-code login and stores the result. +// +// This is the native-application flow from RFC 8252, and the shape is dictated +// by what a command-line tool can safely do. It cannot keep a client secret — +// one shipped inside a binary is a string every user can read out of it — so +// the client is public and PKCE takes the secret's place. It redirects to +// 127.0.0.1 on an ephemeral port, which is the one redirect target a native +// application can prove it owns. +func (c *Client) Login(ctx context.Context, openBrowser func(string) error) error { + meta, err := c.authConfig() + if err != nil { + return err + } + if !meta.Login { + return fmt.Errorf("this server does not support interactive login: %s", meta.Reason) + } + + provider, err := oidc.NewProvider(ctx, meta.Issuer) + if err != nil { + return fmt.Errorf("reach the identity provider at %s: %w", meta.Issuer, err) + } + + // A listener on the loopback interface, on whatever port the OS gives us. + // RFC 8252 requires providers to accept any port for a loopback redirect, + // precisely so a native app does not need a fixed one. + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + return fmt.Errorf("open a local callback listener: %w", err) + } + defer listener.Close() + + // 127.0.0.1 rather than localhost, deliberately. localhost resolves + // through DNS, and a hostile resolver can point it somewhere else — at + // which point the authorization code is delivered to somebody else's + // machine. The literal address cannot be redirected. + redirectURL := fmt.Sprintf("http://127.0.0.1:%d/callback", listener.Addr().(*net.TCPAddr).Port) + + endpoint := provider.Endpoint() + // Send the client id in the request body rather than as HTTP Basic auth. + // + // This is what RFC 6749 specifies for a public client, which has no secret + // to put in a Basic header in the first place. It also avoids a failure + // mode that is genuinely nasty to debug: left unset, x/oauth2 probes for + // the server's preferred style by trying one and retrying with the other + // if the first is rejected. An authorization code is single-use, so the + // probe burns it, and the error surfaced is the *second* attempt's + // "invalid_grant" — which points at the code rather than at whatever + // actually went wrong with the first request. Being explicit means one + // request and the real error. + endpoint.AuthStyle = oauth2.AuthStyleInParams + + conf := &oauth2.Config{ + ClientID: meta.ClientID, + Endpoint: endpoint, + RedirectURL: redirectURL, + Scopes: []string{oidc.ScopeOpenID, "profile", "email", "groups"}, + } + + // state defends the callback against a forged request; nonce binds the + // resulting ID token to this particular login, so one captured elsewhere + // cannot be replayed into it. + state, err := randomString() + if err != nil { + return err + } + nonce, err := randomString() + if err != nil { + return err + } + verifier := oauth2.GenerateVerifier() + + authURL := conf.AuthCodeURL(state, + oidc.Nonce(nonce), + oauth2.S256ChallengeOption(verifier), + // The audience this server checks. Providers that scope tokens per + // API need to be told which one is being asked for, or they mint a + // token the server will refuse. + oauth2.SetAuthURLParam("audience", meta.Audience), + ) + + result := make(chan loginResult, 1) + srv := &http.Server{ + Handler: callbackHandler(state, result), + ReadHeaderTimeout: 5 * time.Second, + } + go srv.Serve(listener) //nolint:errcheck // Shutdown below is the exit path + defer func() { + shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + srv.Shutdown(shutdownCtx) //nolint:errcheck // best effort + }() + + // Printed before the browser is launched, and always, because the browser + // may open somewhere the person cannot see it — a different display, a + // remote session — and a URL they can copy is the fallback that always + // works. + fmt.Fprintf(c.Out, "to sign in, visit:\n\n %s\n\n", authURL) + + switch err := openBrowser(authURL); { + case errors.Is(err, errNoBrowserRequested): + fmt.Fprintf(c.Out, "waiting for you to complete the sign-in...\n") + case err != nil: + // Not fatal: the URL is above, and a machine with no browser is a + // perfectly ordinary place to run this. + fmt.Fprintf(c.Out, "(could not open a browser automatically: %v)\n", err) + default: + fmt.Fprintf(c.Out, "opening your browser at %s...\n", meta.Issuer) + } + + waitCtx, cancel := context.WithTimeout(ctx, loginTimeout) + defer cancel() + + var res loginResult + select { + case res = <-result: + case <-waitCtx.Done(): + return fmt.Errorf("timed out after %s waiting for the browser to complete the login", loginTimeout) + } + if res.err != nil { + return res.err + } + + token, err := conf.Exchange(ctx, res.code, oauth2.VerifierOption(verifier)) + if err != nil { + return fmt.Errorf("exchange the authorization code: %w", err) + } + + rawID, ok := token.Extra("id_token").(string) + if !ok || rawID == "" { + // The access token is for calling the provider's own APIs; this server + // authenticates the *identity* token. A provider that returns no ID + // token has not been asked for the openid scope, or is not an OIDC + // provider at all. + return errors.New("the provider returned no id_token; check that the openid scope is permitted for this client") + } + + // Verified locally before it is stored, so a token this server would + // refuse is reported now rather than on the next command. The nonce check + // is the part that matters: it proves the token was minted for this login + // and not captured from another. + idToken, err := provider.Verifier(&oidc.Config{ClientID: meta.Audience}).Verify(ctx, rawID) + if err != nil { + return fmt.Errorf("the provider issued a token this server would reject: %w", err) + } + if idToken.Nonce != nonce { + return errors.New("the returned token does not match this login attempt") + } + + if err := SaveCredential(rawID); err != nil { + return err + } + + c.Token = rawID + who, err := c.whoAmI() + if err != nil { + // Stored anyway: the token verified, so a server that cannot answer + // right now does not make it invalid. + fmt.Fprintf(c.Out, "signed in, but could not confirm with the server: %v\n", err) + return nil + } + fmt.Fprintf(c.Out, "signed in as %s (%s)\n", who.Name, strings.Join(scopeNames(who.Scopes), ", ")) + if who.Expires != "" { + fmt.Fprintf(c.Out, "this session expires at %s\n", who.Expires) + } + return nil +} + +type loginResult struct { + code string + err error +} + +// callbackHandler receives the redirect from the identity provider. +func callbackHandler(wantState string, out chan<- loginResult) http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + + if e := q.Get("error"); e != "" { + msg := e + if d := q.Get("error_description"); d != "" { + msg += ": " + d + } + finish(w, http.StatusBadRequest, "Sign-in failed", msg) + send(out, loginResult{err: fmt.Errorf("the identity provider refused the login: %s", msg)}) + return + } + + // Checked before the code is touched. Without it, anybody who can get + // this browser to load a URL can complete a login of their choosing — + // the classic session-fixation shape, where the victim ends up holding + // the attacker's session. + if q.Get("state") != wantState { + finish(w, http.StatusBadRequest, "Sign-in failed", + "This response did not come from the sign-in that was started here.") + send(out, loginResult{err: errors.New("the callback state did not match; the login was not the one this command started")}) + return + } + + code := q.Get("code") + if code == "" { + finish(w, http.StatusBadRequest, "Sign-in failed", "The provider returned no authorization code.") + send(out, loginResult{err: errors.New("the provider returned no authorization code")}) + return + } + + finish(w, http.StatusOK, "Signed in", "You can close this tab and return to your terminal.") + send(out, loginResult{code: code}) + }) + return mux +} + +// send never blocks, so a duplicate callback cannot wedge the handler. +func send(out chan<- loginResult, r loginResult) { + select { + case out <- r: + default: + } +} + +func finish(w http.ResponseWriter, status int, title, body string) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Cache-Control", "no-store") + w.WriteHeader(status) + // Static markup with no interpolation of anything the provider sent. A + // query parameter reflected into this page would be cross-site scripting + // on a page that has just handled an authorization code. + fmt.Fprintf(w, `modelforge`+ + ``+ + `

%s

%s

`, htmlEscape(title), htmlEscape(body)) +} + +func htmlEscape(s string) string { + r := strings.NewReplacer("&", "&", "<", "<", ">", ">", `"`, """, "'", "'") + return r.Replace(s) +} + +func randomString() (string, error) { + buf := make([]byte, 32) + if _, err := rand.Read(buf); err != nil { + return "", fmt.Errorf("generate random value: %w", err) + } + return base64.RawURLEncoding.EncodeToString(buf), nil +} + +// browserCommand picks how to open a URL on a given platform. +// +// Split from OpenBrowser so the choice can be tested without launching +// anything: the part worth checking is that each platform gets the right +// command and that the URL is passed as an argument rather than interpolated +// into a shell string, which is what keeps a URL containing shell +// metacharacters from becoming a command. +func browserCommand(goos, url string) (string, []string) { + switch goos { + case "darwin": + return "open", []string{url} + case "windows": + return "rundll32", []string{"url.dll,FileProtocolHandler", url} + default: + return "xdg-open", []string{url} + } +} + +// OpenBrowser launches the system browser. +func OpenBrowser(url string) error { + name, args := browserCommand(runtime.GOOS, url) + return exec.Command(name, args...).Start() +} + +// --- credential storage --- + +// CredentialPath is where a login is stored. +func CredentialPath() (string, error) { + if p := os.Getenv("MODELFORGE_CREDENTIAL_FILE"); p != "" { + return p, nil + } + dir, err := os.UserConfigDir() + if err != nil { + return "", fmt.Errorf("locate a config directory: %w", err) + } + return filepath.Join(dir, "modelforge", "credential"), nil +} + +// SaveCredential writes a token to the credential file. +// +// The directory and the file are both created 0700/0600 and the mode is set +// explicitly rather than left to the umask, because a umask of 022 would leave +// a readable credential in a home directory that other accounts on the machine +// can list. +func SaveCredential(token string) error { + path, err := CredentialPath() + if err != nil { + return err + } + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + return fmt.Errorf("create the credential directory: %w", err) + } + if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil { + return fmt.Errorf("write the credential: %w", err) + } + // MkdirAll leaves an existing directory's mode alone, so a directory that + // predates this — or was created by something with a laxer umask — is + // tightened here rather than assumed. + if err := os.Chmod(filepath.Dir(path), 0o700); err != nil { + return fmt.Errorf("secure the credential directory: %w", err) + } + return os.Chmod(path, 0o600) +} + +// LoadCredential reads a stored login, returning "" when there is none. +func LoadCredential() string { + path, err := CredentialPath() + if err != nil { + return "" + } + body, err := os.ReadFile(path) + if err != nil { + return "" + } + return strings.TrimSpace(string(body)) +} + +// DeleteCredential removes a stored login. +func DeleteCredential() (bool, error) { + path, err := CredentialPath() + if err != nil { + return false, err + } + if err := os.Remove(path); err != nil { + if os.IsNotExist(err) { + return false, nil + } + return false, fmt.Errorf("remove the credential: %w", err) + } + return true, nil +} + +// --- server metadata --- + +type authMeta struct { + Login bool `json:"login"` + Reason string `json:"reason"` + Issuer string `json:"issuer"` + ClientID string `json:"client_id"` + Audience string `json:"audience"` +} + +func (c *Client) authConfig() (authMeta, error) { + var meta authMeta + // Deliberately not through c.do: this endpoint is unauthenticated, and a + // client that has not logged in yet has nothing to send. + resp, err := c.HTTP.Get(c.BaseURL + "/v1/auth/config") + if err != nil { + return meta, fmt.Errorf("ask the server where to sign in: %w", err) + } + defer resp.Body.Close() + raw, _ := io.ReadAll(resp.Body) + if resp.StatusCode >= 300 { + return meta, serverError(resp.StatusCode, raw) + } + if err := json.Unmarshal(raw, &meta); err != nil { + return meta, err + } + return meta, nil +} + +type whoAmI struct { + Name string `json:"name"` + Kind string `json:"kind"` + Scopes []string `json:"scopes"` + Subject string `json:"subject"` + Issuer string `json:"issuer"` + Email string `json:"email"` + Expires string `json:"expires"` +} + +func (c *Client) whoAmI() (whoAmI, error) { + var who whoAmI + err := c.get("/v1/auth/whoami", &who) + return who, err +} + +func scopeNames(scopes []string) []string { + if len(scopes) == 0 { + return []string{"no scopes"} + } + return scopes +} + +// WhoAmI prints the current identity. +func (c *Client) WhoAmI() error { + if c.Token == "" { + fmt.Fprintln(c.Out, "not signed in (no MODELFORGE_TOKEN and no stored login)") + return nil + } + who, err := c.whoAmI() + if err != nil { + return err + } + fmt.Fprintf(c.Out, "%s (%s)\n", who.Name, who.Kind) + fmt.Fprintf(c.Out, " scopes %s\n", strings.Join(scopeNames(who.Scopes), ", ")) + if who.Subject != "" { + fmt.Fprintf(c.Out, " subject %s\n", who.Subject) + fmt.Fprintf(c.Out, " issuer %s\n", who.Issuer) + } + if who.Expires != "" { + fmt.Fprintf(c.Out, " expires %s\n", who.Expires) + } + return nil +} + +// Logout removes a stored login. +func (c *Client) Logout() error { + removed, err := DeleteCredential() + if err != nil { + return err + } + if !removed { + fmt.Fprintln(c.Out, "no stored login to remove") + return nil + } + // Worth saying plainly: this deletes a local file. The token remains valid + // at the provider until it expires, and nothing here can recall it. + fmt.Fprintln(c.Out, "removed the stored login from this machine") + fmt.Fprintln(c.Out, "the token itself stays valid until it expires; revoke it at your identity provider "+ + "if it may have been exposed") + return nil +} diff --git a/internal/cli/login_test.go b/internal/cli/login_test.go new file mode 100644 index 0000000..1a5d1b6 --- /dev/null +++ b/internal/cli/login_test.go @@ -0,0 +1,738 @@ +package cli + +import ( + "context" + "crypto/rand" + "crypto/rsa" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "errors" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "net/url" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + jose "github.com/go-jose/go-jose/v4" + "github.com/go-jose/go-jose/v4/jwt" + + "github.com/sahilkalgutkar/modelforge/internal/app" + "github.com/sahilkalgutkar/modelforge/internal/registry" +) + +// loginIDP is an identity provider with a working authorization endpoint and +// token endpoint, so the login flow can be driven end to end. +// +// It enforces PKCE rather than ignoring it: the token endpoint recomputes the +// challenge from the verifier the client sends and refuses a mismatch. A stub +// that accepted any verifier would let the CLI ship without PKCE and every test +// here would still pass, which is exactly the bug worth catching. +type loginIDP struct { + server *httptest.Server + key *rsa.PrivateKey + + mu sync.Mutex + codes map[string]codeGrant + groups []string + email string + issueNoID bool +} + +type codeGrant struct { + challenge string + nonce string +} + +func newLoginIDP(t *testing.T) *loginIDP { + t.Helper() + + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + p := &loginIDP{ + key: key, + codes: map[string]codeGrant{}, + groups: []string{"platform-oncall"}, + email: "sahil@example.com", + } + + mux := http.NewServeMux() + mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) { + //nolint:errcheck // test server + json.NewEncoder(w).Encode(map[string]any{ + "issuer": p.server.URL, + "authorization_endpoint": p.server.URL + "/authorize", + "token_endpoint": p.server.URL + "/token", + "jwks_uri": p.server.URL + "/keys", + "id_token_signing_alg_values_supported": []string{"RS256"}, + }) + }) + mux.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) { + //nolint:errcheck // test server + json.NewEncoder(w).Encode(jose.JSONWebKeySet{Keys: []jose.JSONWebKey{{ + Key: key.Public(), KeyID: "k1", Algorithm: string(jose.RS256), Use: "sig", + }}}) + }) + + // The authorization endpoint. A browser would render a login page here; + // this records the PKCE challenge and redirects straight back. + mux.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + code := "code-" + q.Get("state")[:8] + + p.mu.Lock() + p.codes[code] = codeGrant{challenge: q.Get("code_challenge"), nonce: q.Get("nonce")} + p.mu.Unlock() + + redirect, _ := url.Parse(q.Get("redirect_uri")) + rq := redirect.Query() + rq.Set("code", code) + rq.Set("state", q.Get("state")) + redirect.RawQuery = rq.Encode() + http.Redirect(w, r, redirect.String(), http.StatusFound) + }) + + mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { + //nolint:errcheck // test server + r.ParseForm() + code := r.Form.Get("code") + + p.mu.Lock() + grant, known := p.codes[code] + delete(p.codes, code) // one use only, as a real provider does + groups, email, noID := p.groups, p.email, p.issueNoID + p.mu.Unlock() + + if !known { + http.Error(w, `{"error":"invalid_grant"}`, http.StatusBadRequest) + return + } + // PKCE, actually verified. + sum := sha256.Sum256([]byte(r.Form.Get("code_verifier"))) + if base64.RawURLEncoding.EncodeToString(sum[:]) != grant.challenge { + http.Error(w, `{"error":"invalid_grant","error_description":"pkce mismatch"}`, http.StatusBadRequest) + return + } + + body := map[string]any{"access_token": "opaque", "token_type": "Bearer", "expires_in": 3600} + if !noID { + body["id_token"] = p.mintID(t, email, groups, grant.nonce) + } + w.Header().Set("Content-Type", "application/json") + //nolint:errcheck // test server + json.NewEncoder(w).Encode(body) + }) + + p.server = httptest.NewServer(mux) + t.Cleanup(p.server.Close) + return p +} + +func (p *loginIDP) mintID(t *testing.T, email string, groups []string, nonce string) string { + t.Helper() + signer, err := jose.NewSigner( + jose.SigningKey{Algorithm: jose.RS256, Key: p.key}, + (&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", "k1"), + ) + if err != nil { + t.Fatal(err) + } + now := time.Now() + claims := map[string]any{ + "iss": p.server.URL, "aud": "modelforge", "sub": "sub-" + email, + "email": email, "groups": groups, + "iat": now.Unix(), "exp": now.Add(time.Hour).Unix(), + } + if nonce != "" { + claims["nonce"] = nonce + } + raw, err := jwt.Signed(signer).Claims(claims).Serialize() + if err != nil { + t.Fatal(err) + } + return raw +} + +// loginServer starts a modelforge configured for interactive login. +func loginServer(t *testing.T, p *loginIDP) string { + t.Helper() + + dsn := os.Getenv("MODELFORGE_TEST_DATABASE_URL") + if dsn == "" { + t.Fatal("MODELFORGE_TEST_DATABASE_URL is not set") + } + ctx := context.Background() + reg, err := registry.Open(ctx, dsn) + if err != nil { + t.Fatal(err) + } + if err := reg.Reset(ctx); err != nil { + t.Fatal(err) + } + reg.Close() + + a, err := app.New(ctx, app.Config{ + DatabaseURL: dsn, + ArtifactDir: t.TempDir(), + Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), + OIDCIssuer: p.server.URL, + OIDCAudience: "modelforge", + OIDCClientID: "modelforge-cli", + OIDCScopeMap: []string{"platform-oncall=admin", "ml-eng=read"}, + }) + if err != nil { + t.Fatalf("start server: %v", err) + } + t.Cleanup(a.Close) + + waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + if err := a.WaitForIdentityProvider(waitCtx); err != nil { + t.Fatalf("identity provider never became ready: %v", err) + } + + ts := httptest.NewServer(a.Handler()) + t.Cleanup(ts.Close) + + // Each test gets its own credential file, so a login never touches the + // developer's real one. + t.Setenv("MODELFORGE_CREDENTIAL_FILE", filepath.Join(t.TempDir(), "credential")) + t.Setenv("MODELFORGE_TOKEN", "") + return ts.URL +} + +// browser stands in for a person completing the login: it simply follows the +// URL the CLI would have opened. +func browser(t *testing.T) func(string) error { + t.Helper() + return func(authURL string) error { + go func() { + resp, err := http.Get(authURL) + if err == nil { + resp.Body.Close() + } + }() + return nil + } +} + +// TestLoginEndToEnd is the flow as a person experiences it: run the command, +// authenticate in a browser, and end up holding a working credential. +func TestLoginEndToEnd(t *testing.T) { + p := newLoginIDP(t) + addr := loginServer(t, p) + + var out strings.Builder + c := NewClient(addr, &out) + if err := c.Login(context.Background(), browser(t)); err != nil { + t.Fatalf("Login: %v", err) + } + + if !strings.Contains(out.String(), "signed in as sahil@example.com") { + t.Errorf("login did not report who signed in:\n%s", out.String()) + } + if !strings.Contains(out.String(), "admin") { + t.Errorf("login did not report the granted scopes:\n%s", out.String()) + } + + // The credential is stored and a fresh client picks it up without any + // environment variable. + stored := LoadCredential() + if stored == "" { + t.Fatal("no credential was stored") + } + if out2, code := run(t, addr, "whoami"); code != 0 || + !strings.Contains(out2, "sahil@example.com") { + t.Fatalf("whoami after login: %d %s", code, out2) + } + + // And it actually authenticates against the API. + if out3, code := run(t, addr, "models"); code != 0 { + t.Fatalf("a stored login could not call the API: %d %s", code, out3) + } +} + +// TestStoredCredentialIsPrivate covers the file permissions. A credential left +// world-readable in a home directory is a credential shared with every other +// account on the machine. +func TestStoredCredentialIsPrivate(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "nested", "credential") + t.Setenv("MODELFORGE_CREDENTIAL_FILE", path) + + if err := SaveCredential("a-token"); err != nil { + t.Fatal(err) + } + + fi, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if perm := fi.Mode().Perm(); perm != 0o600 { + t.Errorf("credential file mode = %o, want 600", perm) + } + di, err := os.Stat(filepath.Dir(path)) + if err != nil { + t.Fatal(err) + } + if perm := di.Mode().Perm(); perm != 0o700 { + t.Errorf("credential directory mode = %o, want 700", perm) + } + + // A pre-existing loose directory is tightened rather than trusted, since + // MkdirAll leaves an existing mode alone. + loose := filepath.Join(dir, "loose") + if err := os.MkdirAll(loose, 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("MODELFORGE_CREDENTIAL_FILE", filepath.Join(loose, "credential")) + if err := SaveCredential("another"); err != nil { + t.Fatal(err) + } + di, _ = os.Stat(loose) + if perm := di.Mode().Perm(); perm != 0o700 { + t.Errorf("a pre-existing directory was left at %o, want it tightened to 700", perm) + } +} + +// TestPKCEIsActuallySent is the security property of this flow. Without a +// verifier the provider refuses, which proves the CLI is sending one rather +// than relying on the provider not to check. +func TestPKCEIsActuallySent(t *testing.T) { + p := newLoginIDP(t) + addr := loginServer(t, p) + + var seenChallenge string + var out strings.Builder + c := NewClient(addr, &out) + + err := c.Login(context.Background(), func(authURL string) error { + u, perr := url.Parse(authURL) + if perr != nil { + return perr + } + seenChallenge = u.Query().Get("code_challenge") + if got := u.Query().Get("code_challenge_method"); got != "S256" { + t.Errorf("code_challenge_method = %q, want S256", got) + } + // The plain method is not acceptable: it puts the verifier in the + // authorization request, where anything that can read the redirect can + // also replay the code. + go func() { + resp, gerr := http.Get(authURL) + if gerr == nil { + resp.Body.Close() + } + }() + return nil + }) + if err != nil { + t.Fatalf("Login: %v", err) + } + if seenChallenge == "" { + t.Fatal("no code_challenge was sent; the flow is not using PKCE") + } +} + +// TestCallbackRejectsAForgedState is the CSRF defence. Without it, anybody who +// can make this browser load a URL can complete a login of their choosing, and +// the victim ends up holding the attacker's session. +func TestCallbackRejectsAForgedState(t *testing.T) { + p := newLoginIDP(t) + addr := loginServer(t, p) + + var out strings.Builder + c := NewClient(addr, &out) + c.HTTP = &http.Client{Timeout: 30 * time.Second} + + ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second) + defer cancel() + + err := c.Login(ctx, func(authURL string) error { + u, perr := url.Parse(authURL) + if perr != nil { + return perr + } + // Complete the callback with a state the CLI never issued. + redirect := u.Query().Get("redirect_uri") + "?code=stolen&state=not-the-right-state" + go func() { + resp, gerr := http.Get(redirect) + if gerr == nil { + resp.Body.Close() + } + }() + return nil + }) + if err == nil { + t.Fatal("a callback with a forged state completed the login") + } + if !strings.Contains(err.Error(), "state") { + t.Errorf("error should name the state mismatch: %v", err) + } + if LoadCredential() != "" { + t.Error("a failed login stored a credential anyway") + } +} + +// TestProviderErrorIsReported covers the user declining consent, or the client +// being misconfigured at the provider. +func TestProviderErrorIsReported(t *testing.T) { + p := newLoginIDP(t) + addr := loginServer(t, p) + + var out strings.Builder + c := NewClient(addr, &out) + ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second) + defer cancel() + + err := c.Login(ctx, func(authURL string) error { + u, _ := url.Parse(authURL) + redirect := u.Query().Get("redirect_uri") + + "?error=access_denied&error_description=user+declined&state=" + u.Query().Get("state") + go func() { + resp, gerr := http.Get(redirect) + if gerr == nil { + resp.Body.Close() + } + }() + return nil + }) + if err == nil { + t.Fatal("a provider error completed the login") + } + if !strings.Contains(err.Error(), "access_denied") { + t.Errorf("error should carry the provider's reason: %v", err) + } +} + +// TestLoginWithoutAnIDTokenFails: an access token is for calling the +// provider's APIs, and is not what this server authenticates. +func TestLoginWithoutAnIDTokenFails(t *testing.T) { + p := newLoginIDP(t) + p.issueNoID = true + addr := loginServer(t, p) + + var out strings.Builder + c := NewClient(addr, &out) + err := c.Login(context.Background(), browser(t)) + if err == nil { + t.Fatal("a token response with no id_token was accepted") + } + if !strings.Contains(err.Error(), "openid scope") { + t.Errorf("error should suggest the likely cause: %v", err) + } +} + +// TestLoginAgainstAServerWithoutOIDC explains itself rather than failing +// obscurely. +func TestLoginAgainstAServerWithoutOIDC(t *testing.T) { + addr := serverURL(t) // static tokens only + var out strings.Builder + c := NewClient(addr, &out) + + err := c.Login(context.Background(), browser(t)) + if err == nil { + t.Fatal("login succeeded against a server with no identity provider") + } + if !strings.Contains(err.Error(), "does not support interactive login") { + t.Errorf("error should say the server cannot do this: %v", err) + } +} + +// TestUserInNoMappedGroupCannotLogIn: the refusal happens at the server, and +// the CLI should surface it rather than storing a useless credential. +func TestUserInNoMappedGroupCannotLogIn(t *testing.T) { + p := newLoginIDP(t) + p.groups = []string{"finance"} + p.email = "finance@example.com" + addr := loginServer(t, p) + + var out strings.Builder + c := NewClient(addr, &out) + if err := c.Login(context.Background(), browser(t)); err != nil { + t.Fatalf("Login: %v", err) + } + // The token itself is valid — the provider issued it — so it is stored, + // and the server is the thing that refuses. Saying so is more useful than + // pretending the login failed. + if !strings.Contains(out.String(), "could not confirm with the server") { + t.Errorf("output should report that the server refused this identity:\n%s", out.String()) + } +} + +func TestLogoutRemovesTheCredential(t *testing.T) { + t.Setenv("MODELFORGE_CREDENTIAL_FILE", filepath.Join(t.TempDir(), "credential")) + + var out strings.Builder + c := NewClient("http://localhost:1", &out) + + // Nothing stored yet. + if err := c.Logout(); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "no stored login") { + t.Errorf("logout with nothing stored: %s", out.String()) + } + + if err := SaveCredential("a-token"); err != nil { + t.Fatal(err) + } + out.Reset() + if err := c.Logout(); err != nil { + t.Fatal(err) + } + if LoadCredential() != "" { + t.Error("the credential survived logout") + } + // Worth saying plainly, because deleting a local file is not revocation. + if !strings.Contains(out.String(), "stays valid until it expires") { + t.Errorf("logout should say the token is not revoked:\n%s", out.String()) + } +} + +func TestEnvironmentTokenOverridesAStoredLogin(t *testing.T) { + t.Setenv("MODELFORGE_CREDENTIAL_FILE", filepath.Join(t.TempDir(), "credential")) + if err := SaveCredential("stored-token"); err != nil { + t.Fatal(err) + } + + t.Setenv("MODELFORGE_TOKEN", "") + if got := credential(); got != "stored-token" { + t.Errorf("credential() = %q, want the stored login", got) + } + + // A script should be able to override whoever is signed in on the machine. + t.Setenv("MODELFORGE_TOKEN", "env-token") + if got := credential(); got != "env-token" { + t.Errorf("credential() = %q, want the environment to win", got) + } +} + +func TestWhoAmIWithoutACredential(t *testing.T) { + t.Setenv("MODELFORGE_CREDENTIAL_FILE", filepath.Join(t.TempDir(), "credential")) + t.Setenv("MODELFORGE_TOKEN", "") + + var out strings.Builder + c := NewClient("http://localhost:1", &out) + if err := c.WhoAmI(); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "not signed in") { + t.Errorf("whoami with no credential: %s", out.String()) + } +} + +// TestCallbackPageDoesNotReflectProviderInput is an XSS check on a page that +// has just handled an authorization code. +func TestCallbackPageDoesNotReflectProviderInput(t *testing.T) { + result := make(chan loginResult, 1) + h := callbackHandler("the-state", result) + + rec := httptest.NewRecorder() + req := httptest.NewRequest("GET", + "/callback?error=nope&error_description=%3Cscript%3Ealert(1)%3C/script%3E&state=the-state", nil) + h.ServeHTTP(rec, req) + + if strings.Contains(rec.Body.String(), "