X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/24223057a8dd3a03f1c6457287cb12167c6b67ee..47982d37d1124c7615508ca17b299b6f31a654d7:/lib/controller/localdb/login_oidc.go diff --git a/lib/controller/localdb/login_oidc.go b/lib/controller/localdb/login_oidc.go index 60de70b5d2..e076f7e128 100644 --- a/lib/controller/localdb/login_oidc.go +++ b/lib/controller/localdb/login_oidc.go @@ -22,7 +22,6 @@ import ( "time" "git.arvados.org/arvados.git/lib/controller/api" - "git.arvados.org/arvados.git/lib/controller/railsproxy" "git.arvados.org/arvados.git/lib/controller/rpc" "git.arvados.org/arvados.git/lib/ctrlctx" "git.arvados.org/arvados.git/sdk/go/arvados" @@ -32,27 +31,33 @@ import ( "github.com/coreos/go-oidc" lru "github.com/hashicorp/golang-lru" "github.com/jmoiron/sqlx" + "github.com/sirupsen/logrus" "golang.org/x/oauth2" "google.golang.org/api/option" "google.golang.org/api/people/v1" + "gopkg.in/square/go-jose.v2/jwt" ) -const ( +var ( tokenCacheSize = 1000 tokenCacheNegativeTTL = time.Minute * 5 tokenCacheTTL = time.Minute * 10 + tokenCacheRaceWindow = time.Minute ) type oidcLoginController struct { - Cluster *arvados.Cluster - RailsProxy *railsProxy - Issuer string // OIDC issuer URL, e.g., "https://accounts.google.com" - ClientID string - ClientSecret string - UseGooglePeopleAPI bool // Use Google People API to look up alternate email addresses - EmailClaim string // OpenID claim to use as email address; typically "email" - EmailVerifiedClaim string // If non-empty, ensure claim value is true before accepting EmailClaim; typically "email_verified" - UsernameClaim string // If non-empty, use as preferred username + Cluster *arvados.Cluster + Parent *Conn + Issuer string // OIDC issuer URL, e.g., "https://accounts.google.com" + ClientID string + ClientSecret string + UseGooglePeopleAPI bool // Use Google People API to look up alternate email addresses + EmailClaim string // OpenID claim to use as email address; typically "email" + EmailVerifiedClaim string // If non-empty, ensure claim value is true before accepting EmailClaim; typically "email_verified" + UsernameClaim string // If non-empty, use as preferred username + AcceptAccessToken bool // Accept access tokens as API tokens + AcceptAccessTokenScope string // If non-empty, don't accept access tokens as API tokens unless they contain this scope + AuthParams map[string]string // Additional parameters to pass with authentication request // override Google People API base URL for testing purposes // (normally empty, set by google pkg to @@ -96,7 +101,7 @@ func (ctrl *oidcLoginController) setup() error { } func (ctrl *oidcLoginController) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) { - return noopLogout(ctrl.Cluster, opts) + return logout(ctx, ctrl.Cluster, opts) } func (ctrl *oidcLoginController) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) { @@ -110,14 +115,12 @@ func (ctrl *oidcLoginController) Login(ctx context.Context, opts arvados.LoginOp return loginError(errors.New("missing return_to parameter")) } state := ctrl.newOAuth2State([]byte(ctrl.Cluster.SystemRootToken), opts.Remote, opts.ReturnTo) + var authparams []oauth2.AuthCodeOption + for k, v := range ctrl.AuthParams { + authparams = append(authparams, oauth2.SetAuthURLParam(k, v)) + } return arvados.LoginResponse{ - RedirectLocation: ctrl.oauth2conf.AuthCodeURL(state.String(), - // prompt=select_account tells Google - // to show the "choose which Google - // account" page, even if the client - // is currently logged in to exactly - // one Google account. - oauth2.SetAuthURLParam("prompt", "select_account")), + RedirectLocation: ctrl.oauth2conf.AuthCodeURL(state.String(), authparams...), }, nil } // Callback after OIDC sign-in. @@ -129,10 +132,12 @@ func (ctrl *oidcLoginController) Login(ctx context.Context, opts arvados.LoginOp if err != nil { return loginError(fmt.Errorf("error in OAuth2 exchange: %s", err)) } + ctxlog.FromContext(ctx).WithField("oauth2Token", oauth2Token).Debug("oauth2 exchange succeeded") rawIDToken, ok := oauth2Token.Extra("id_token").(string) if !ok { return loginError(errors.New("error in OAuth2 exchange: no ID token in OAuth2 token")) } + ctxlog.FromContext(ctx).WithField("rawIDToken", rawIDToken).Debug("oauth2Token provided ID token") idToken, err := ctrl.verifier.Verify(ctx, rawIDToken) if err != nil { return loginError(fmt.Errorf("error verifying ID token: %s", err)) @@ -142,7 +147,7 @@ func (ctrl *oidcLoginController) Login(ctx context.Context, opts arvados.LoginOp return loginError(err) } ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{ctrl.Cluster.SystemRootToken}}) - return ctrl.RailsProxy.UserSessionCreate(ctxRoot, rpc.UserSessionCreateOptions{ + return ctrl.Parent.UserSessionCreate(ctxRoot, rpc.UserSessionCreateOptions{ ReturnTo: state.Remote + "," + state.ReturnTo, AuthInfo: *authinfo, }) @@ -172,12 +177,19 @@ func (ctrl *oidcLoginController) getAuthInfo(ctx context.Context, token *oauth2. } else if verified, _ := claims[ctrl.EmailVerifiedClaim].(bool); verified || ctrl.EmailVerifiedClaim == "" { // Fall back to this info if the People API call // (below) doesn't return a primary && verified email. - name, _ := claims["name"].(string) - if names := strings.Fields(strings.TrimSpace(name)); len(names) > 1 { - ret.FirstName = strings.Join(names[0:len(names)-1], " ") - ret.LastName = names[len(names)-1] + givenName, _ := claims["given_name"].(string) + familyName, _ := claims["family_name"].(string) + if givenName != "" && familyName != "" { + ret.FirstName = givenName + ret.LastName = familyName } else { - ret.FirstName = names[0] + name, _ := claims["name"].(string) + if names := strings.Fields(strings.TrimSpace(name)); len(names) > 1 { + ret.FirstName = strings.Join(names[0:len(names)-1], " ") + ret.LastName = names[len(names)-1] + } else if len(names) > 0 { + ret.FirstName = names[0] + } } ret.Email, _ = claims[ctrl.EmailClaim].(string) } @@ -321,7 +333,7 @@ func OIDCAccessTokenAuthorizer(cluster *arvados.Cluster, getdb func(context.Cont // We want ctrl to be nil if the chosen controller is not a // *oidcLoginController, so we can ignore the 2nd return value // of this type cast. - ctrl, _ := chooseLoginController(cluster, railsproxy.NewConn(cluster)).(*oidcLoginController) + ctrl, _ := NewConn(cluster).loginController.(*oidcLoginController) cache, err := lru.New2Q(tokenCacheSize) if err != nil { panic(err) @@ -340,13 +352,14 @@ type oidcTokenAuthorizer struct { } func (ta *oidcTokenAuthorizer) Middleware(w http.ResponseWriter, r *http.Request, next http.Handler) { - if authhdr := strings.Split(r.Header.Get("Authorization"), " "); len(authhdr) > 1 && (authhdr[0] == "OAuth2" || authhdr[0] == "Bearer") { - tok, err := ta.exchangeToken(r.Context(), authhdr[1]) + if ta.ctrl == nil { + // Not using a compatible (OIDC) login controller. + } else if authhdr := strings.Split(r.Header.Get("Authorization"), " "); len(authhdr) > 1 && (authhdr[0] == "OAuth2" || authhdr[0] == "Bearer") { + err := ta.registerToken(r.Context(), authhdr[1]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - r.Header.Set("Authorization", "Bearer "+tok) } next.ServeHTTP(w, r) } @@ -362,24 +375,25 @@ func (ta *oidcTokenAuthorizer) WrapCalls(origFunc api.RoutableFunc) api.Routable return origFunc(ctx, opts) } // Check each token in the incoming request. If any - // are OAuth2 access tokens, swap them out for Arvados - // tokens. - for tokidx, tok := range creds.Tokens { - tok, err = ta.exchangeToken(ctx, tok) + // are valid OAuth2 access tokens, insert/update them + // in the database so RailsAPI's auth code accepts + // them. + for _, tok := range creds.Tokens { + err = ta.registerToken(ctx, tok) if err != nil { return nil, err } - creds.Tokens[tokidx] = tok } - ctxlog.FromContext(ctx).WithField("creds", creds).Debug("(*oidcTokenAuthorizer)WrapCalls: new creds") - ctx = auth.NewContext(ctx, creds) return origFunc(ctx, opts) } } -func (ta *oidcTokenAuthorizer) exchangeToken(ctx context.Context, tok string) (string, error) { - if strings.HasPrefix(tok, "v2/") { - return tok, nil +// registerToken checks whether tok is a valid OIDC Access Token and, +// if so, ensures that an api_client_authorizations row exists so that +// RailsAPI will accept it as an Arvados token. +func (ta *oidcTokenAuthorizer) registerToken(ctx context.Context, tok string) error { + if tok == ta.ctrl.Cluster.SystemRootToken || strings.HasPrefix(tok, "v2/") { + return nil } if cached, hit := ta.cache.Get(tok); !hit { // Fall through to database and OIDC provider checks @@ -387,33 +401,29 @@ func (ta *oidcTokenAuthorizer) exchangeToken(ctx context.Context, tok string) (s } else if exp, ok := cached.(time.Time); ok { // cached negative result (value is expiry time) if time.Now().Before(exp) { - return tok, nil - } else { - ta.cache.Remove(tok) + return nil } + ta.cache.Remove(tok) } else { // cached positive result - aca := cached.(*arvados.APIClientAuthorization) + aca := cached.(arvados.APIClientAuthorization) var expiring bool - if aca.ExpiresAt != "" { - t, err := time.Parse(time.RFC3339Nano, aca.ExpiresAt) - if err != nil { - return "", fmt.Errorf("error parsing expires_at value: %w", err) - } + if !aca.ExpiresAt.IsZero() { + t := aca.ExpiresAt expiring = t.Before(time.Now().Add(time.Minute)) } if !expiring { - return aca.TokenV2(), nil + return nil } } db, err := ta.getdb(ctx) if err != nil { - return "", err + return err } tx, err := db.Beginx() if err != nil { - return "", err + return err } defer tx.Rollback() ctx = ctrlctx.NewWithTransaction(ctx, tx) @@ -428,13 +438,13 @@ func (ta *oidcTokenAuthorizer) exchangeToken(ctx context.Context, tok string) (s var expiring bool err = tx.QueryRowContext(ctx, `select (expires_at is not null and expires_at - interval '1 minute' <= current_timestamp at time zone 'UTC') from api_client_authorizations where api_token=$1`, hmac).Scan(&expiring) if err != nil && err != sql.ErrNoRows { - return "", fmt.Errorf("database error while checking token: %w", err) + return fmt.Errorf("database error while checking token: %w", err) } else if err == nil && !expiring { // Token is already in the database as an Arvados // token, and isn't about to expire, so we can pass it // through to RailsAPI etc. regardless of whether it's // an OIDC access token. - return tok, nil + return nil } updating := err == nil @@ -444,7 +454,11 @@ func (ta *oidcTokenAuthorizer) exchangeToken(ctx context.Context, tok string) (s // server components will accept. err = ta.ctrl.setup() if err != nil { - return "", fmt.Errorf("error setting up OpenID Connect provider: %s", err) + return fmt.Errorf("error setting up OpenID Connect provider: %s", err) + } + if ok, err := ta.checkAccessTokenScope(ctx, tok); err != nil || !ok { + ta.cache.Add(tok, time.Now().Add(tokenCacheNegativeTTL)) + return err } oauth2Token := &oauth2.Token{ AccessToken: tok, @@ -452,35 +466,78 @@ func (ta *oidcTokenAuthorizer) exchangeToken(ctx context.Context, tok string) (s userinfo, err := ta.ctrl.provider.UserInfo(ctx, oauth2.StaticTokenSource(oauth2Token)) if err != nil { ta.cache.Add(tok, time.Now().Add(tokenCacheNegativeTTL)) - return tok, nil + return nil } - ctxlog.FromContext(ctx).WithField("userinfo", userinfo).Debug("(*oidcTokenAuthorizer)exchangeToken: got userinfo") + ctxlog.FromContext(ctx).WithField("userinfo", userinfo).Debug("(*oidcTokenAuthorizer)registerToken: got userinfo") authinfo, err := ta.ctrl.getAuthInfo(ctx, oauth2Token, userinfo) if err != nil { - return "", err + return err } + // Expiry time for our token is one minute longer than our + // cache TTL, so we don't pass it through to RailsAPI just as + // it's expiring. + exp := time.Now().UTC().Add(tokenCacheTTL + tokenCacheRaceWindow) + var aca arvados.APIClientAuthorization if updating { - _, err = tx.ExecContext(ctx, `update api_client_authorizations set expires_at=$1 where api_token=$2`, time.Now().Add(tokenCacheTTL+time.Minute), hmac) + _, err = tx.ExecContext(ctx, `update api_client_authorizations set expires_at=$1 where api_token=$2`, exp, hmac) if err != nil { - return "", fmt.Errorf("error adding OIDC access token to database: %w", err) + return fmt.Errorf("error updating token expiry time: %w", err) } + ctxlog.FromContext(ctx).WithField("HMAC", hmac).Debug("(*oidcTokenAuthorizer)registerToken: updated api_client_authorizations row") } else { - aca, err = createAPIClientAuthorization(ctx, ta.ctrl.RailsProxy, ta.ctrl.Cluster.SystemRootToken, *authinfo) + aca, err = ta.ctrl.Parent.CreateAPIClientAuthorization(ctx, ta.ctrl.Cluster.SystemRootToken, *authinfo) if err != nil { - return "", err + return err } - _, err = tx.ExecContext(ctx, `update api_client_authorizations set api_token=$1 where uuid=$2`, hmac, aca.UUID) + _, err = tx.ExecContext(ctx, `update api_client_authorizations set api_token=$1, expires_at=$2 where uuid=$3`, hmac, exp, aca.UUID) if err != nil { - return "", fmt.Errorf("error adding OIDC access token to database: %w", err) + return fmt.Errorf("error adding OIDC access token to database: %w", err) } aca.APIToken = hmac + ctxlog.FromContext(ctx).WithFields(logrus.Fields{"UUID": aca.UUID, "HMAC": hmac}).Debug("(*oidcTokenAuthorizer)registerToken: inserted api_client_authorizations row") } err = tx.Commit() if err != nil { - return "", err + return err } + aca.ExpiresAt = exp ta.cache.Add(tok, aca) - return aca.TokenV2(), nil + return nil +} + +// Check that the provided access token is a JWT with the required +// scope. If it is a valid JWT but missing the required scope, we +// return a 403 error, otherwise true (acceptable as an API token) or +// false (pass through unmodified). +// +// Return false if configured not to accept access tokens at all. +// +// Note we don't check signature or expiry here. We are relying on the +// caller to verify those separately (e.g., by calling the UserInfo +// endpoint). +func (ta *oidcTokenAuthorizer) checkAccessTokenScope(ctx context.Context, tok string) (bool, error) { + if !ta.ctrl.AcceptAccessToken { + return false, nil + } else if ta.ctrl.AcceptAccessTokenScope == "" { + return true, nil + } + var claims struct { + Scope string `json:"scope"` + } + if t, err := jwt.ParseSigned(tok); err != nil { + ctxlog.FromContext(ctx).WithError(err).Debug("error parsing jwt") + return false, nil + } else if err = t.UnsafeClaimsWithoutVerification(&claims); err != nil { + ctxlog.FromContext(ctx).WithError(err).Debug("error extracting jwt claims") + return false, nil + } + for _, s := range strings.Split(claims.Scope, " ") { + if s == ta.ctrl.AcceptAccessTokenScope { + return true, nil + } + } + ctxlog.FromContext(ctx).WithFields(logrus.Fields{"have": claims.Scope, "need": ta.ctrl.AcceptAccessTokenScope}).Infof("unacceptable access token scope") + return false, httpserver.ErrorWithStatus(errors.New("unacceptable access token scope"), http.StatusUnauthorized) }