Skip to content

test(oauth2lib): add golden vectors and run them against oauth2lib - #216

Merged
agrawalradhika-cell merged 4 commits into
google:masterfrom
agrawalradhika-cell:test/oauth2lib-goldenvectors
Dec 1, 2026
Merged

agrawalradhika-cell merged 4 commits into
google:masterfrom
agrawalradhika-cell:test/oauth2lib-goldenvectors

Conversation

@agrawalradhika-cell

@agrawalradhika-cell agrawalradhika-cell commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Adds golden vectors for oauth2lib: an exported package oauth2lib/goldenvectors with test vectors embedded via //go:embed *.json, and a test that runs every vector against oauth2lib. Tests and test data only; no production changes.

Why

Go gcloud's tests need to check its oauth2lib adapter against the same cases oauth2lib is tested with. Embedding the files in a Go package lets another module read them with goldenvectors.All(), with no Blaze runfiles or file paths (runfiles are banned in Go gcloud's tests, and GoB runs plain go test ./...). The package imports only the standard library.

Format

Each file is one vector (documented in oauth2lib/goldenvectors/README.md):

  • input: a config for BuildCredentials or an impersonation for Impersonate (with its source config).
  • exchanges: every request one Token call must send, in order (method, URL, required headers, exact form or JSON body, and for a JWT assertion its header, claims and exp - iat), with the response to give: status plus JSON body, text body, or a transport_error.
  • transport_error is {"message": "...", "type": "..."}, allowed only on the last exchange. type is empty for a plain network error, or one of client_cert_refused (an error wrapping oauth2lib.ErrAccessDenied, as from a certificate callback or ECP signer), client_timeout (a real http.Client.Timeout), server_cert_untrusted (a *tls.CertificateVerificationError) or client_cert_alert (the server's TLS bad_certificate alert, a *net.OpError with Op remote error).
  • expected: the token (value or self-signed JWT claims, type, expires_in_seconds or exact expiry, exact metadata) or the RefreshError (kind, code, subtype, description, status, and exactly which sentinels it matches), plus the universe domain. kind is a stable id (unknown, reauth_required, access_denied, invalid_grant, transient), not ErrorKind.String().

All() rejects unknown fields and data after the vector, and runs Vector.Validate() on each file (exactly one input and one outcome, known kinds, sentinels and transport error types, RFC 3339 expiry, and so on), so a consumer gets only well-formed vectors.

No private key is checked in: "private_key": "TEST_PRIVATE_KEY_PEM" is replaced with a key the test generates (Config.JSON(pem)). All tokens are obviously fake strings.

Vectors (41)

  • authorized_user (23): refresh; scopes + RAPT + rotated refresh token + ID token; echoed refresh token not reported; scopes from the JSON; token_uri from the JSON vs Config.TokenURL; expires_in as a string or missing; universe domain; scope not granted; revoked refresh token; invalid_rapt; rapt_required; CAA denial vs plain access_denied; 503; network error; no access_token; client certificate refused (access denied, not transient); http.Client.Timeout (transient, not a context error); server certificate untrusted (unknown); the server's TLS bad_certificate alert (access denied); 429 and 500 with an invalid_grant body (transient: status is checked before the body).
  • service_account (6): 2LO; 2LO invalid_grant; 2LO with Config.TokenURL overriding token_uri; self-signed JWT (no request); a key file in a non-default universe, with and without use_self_signed_jwt (both self-sign, as go/auth and google-auth do outside googleapis.com).
  • impersonation (12): impersonated_service_account JSON with an authorized_user source, with a service_account source (which gets the IAM scope), with a source in a non-default universe (IAM host from the source's universe, not from service_account_impersonation_url), with two delegates and a top-level universe_domain that the source inherits, and with all-blank Config.Scopes (cleaned away, so the JSON's scopes are used); Impersonate with a delegate (two hops) and a 600s lifetime; custom IAM endpoint; a source in a non-default universe; IAM 403 PERMISSION_DENIED; IAM client certificate refused; a source that needs reauth.

TestGoldenVectors (oauth2lib/goldenvectors_test.go) runs each one with a transport that checks every request and answers it without any network. Every impersonated_service_account vector also runs as <name>/parse_and_impersonate, through ParseImpersonatedServiceAccountJSON, SourceConfig, BuildCredentials and Impersonate, and must give the same requests and result. Each run also checks creds.JSON() (Config.JSON for BuildCredentials, nil for Impersonate) and, for transport errors, that the cause is still reachable with errors.As. goldenvectors.TestAllWellFormed, TestValidate and TestDecode check the format.

Comparison with Python

I sent the same inputs and responses through google3's google-auth 2.62.2 (google.oauth2.credentials, service_account, impersonated_credentials) and compared the requests for the original 27 vectors. 24 were identical. The other three:

  • impersonated_json_user_source and impersonated_json_sa_source: for a file with "delegates": [] (gcloud always writes the field), Python sends "delegates": [] and oauth2lib sends "delegates": null. This is intended; the vectors record null, and the README lists it under "Known differences from Python".
  • user_scope_not_granted: google-auth only warns, but Python gcloud's own refresh (google_auth_credentials._Refresh) raises the same "Not all requested scopes were granted…" error, which oauth2lib matches.

google-auth also retries a 503 twice; oauth2lib doesn't retry by design (the caller does).

Testing

  • go test -race ./... in oauth2lib/ on Go 1.27.3 and 1.26.0, go vet, go mod tidy -diff, the compat script and its unit tests. oauth2lib coverage unchanged at 97.5%.
  • Dropping rapt from the refresh form makes the two RAPT vectors fail; treating ErrAccessDenied as transient makes the two client-certificate vectors fail; letting a Client.Timeout error match context.DeadlineExceeded makes user_client_timeout fail.
  • Review round (596346c): removing the remote-alert case from classifyTransportError, checking the body before the status, dropping the JSON's delegates, ignoring the top-level universe_domain for the source, ignoring Config.TokenURL for SA keys, losing Timeout() on client timeouts, flattening the server-cert error, or returning non-nil JSON from Impersonate each make the matching new vector or check fail.
  • 47e5644: parseAndImpersonate now cleans Config.Scopes like BuildCredentials. impersonated_json_blank_config_scopes fails on the split path without that cleaning, and on BuildCredentials if validateConfig stops cleaning scopes.

Add package oauth2lib/goldenvectors: test vectors embedded with
//go:embed *.json, so other modules' tests (such as Go gcloud's) can read
them without Blaze runfiles. Each vector gives an input to
BuildCredentials or Impersonate, the requests one Token call must send,
the responses to give, and the token or RefreshError it must return.
The package imports only the standard library; README.md documents the
format.

The 27 vectors cover authorized_user refresh (scopes, RAPT, rotated and
echoed refresh tokens, token URL precedence, expires_in forms, universe
domain), user errors (revoked token, invalid_rapt, rapt_required, CAA and
plain access_denied, 503, network error), service account 2LO and
self-signed JWT, and impersonation (JSON with user and service account
sources, delegates and lifetime, custom endpoint, IAM 403, source reauth).

TestGoldenVectors runs every vector with a transport that checks each
request (method, URL, headers, exact form or JSON body, JWT assertion
signature and claims) and answers without any network.
andyrzhao
andyrzhao previously approved these changes Nov 29, 2026
Comment thread oauth2lib/goldenvectors_test.go Resolved
Comment thread oauth2lib/goldenvectors_test.go Resolved
Comment thread oauth2lib/goldenvectors/README.md Resolved
- Add transport error type client_cert_alert (server TLS bad_certificate alert) and vector user_client_cert_alert.
- Check errors.As on transport errors, and that creds.JSON() is Config.JSON for BuildCredentials and nil for Impersonate.
- Validate rejects empty impersonation scopes and a negative lifetime; explicit JSON null counts as absent.
- impersonated_json_source_universe_domain: use a different host in service_account_impersonation_url.
- New vectors: user_rate_limited_invalid_grant, user_server_error_invalid_grant, service_account_2lo_config_token_url, impersonated_json_delegates_universe_domain.
…dCredentials

parseAndImpersonate checked the raw Config.Scopes, while BuildCredentials trims them and drops blank ones before falling back to the JSON's scopes. Clean them the same way, and add vector impersonated_json_blank_config_scopes (Config.Scopes all blank) so a split-path adapter that skips the cleaning fails.
@agrawalradhika-cell
agrawalradhika-cell merged commit cc75951 into google:master Dec 1, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants