Route Authorization: Basic routable tokens in session_token

What does this MR do and why?

Stacked on !1412 (merged). Merge !1412 (merged) first; this then retargets to main.

Adds two session_token rules that route by the routable token in an Authorization: Basic header, the same way the ruleset already routes one in Authorization: Bearer, PRIVATE-TOKEN or JOB-TOKEN:

  • session_token_header_authorization_basic_routable_token: the password is a token in the routable .01. format, whatever its prefix. Today that is a PAT.
  • session_token_header_authorization_basic_jwt: the password is a JWT-shaped token, <prefix>-<header>.<payload>.<signature>. Today only the glcbt- prefix of a CI job token is matched.

The rules are named after the token shape they match, not after one token type, so a token type that adopts either shape later (a deploy token, an OAuth access token, an LFS token) is routed by a rule whose name already covers it. A new .01. token needs no change here; a new JWT prefix needs its prefix added to the JWT rule, and test/rules/session_token_basic.spec.ts adjusted, since it checks that rule against session_token_header_job_token.

Both decode the header with the existing decode-base64-match transform, then read c with the existing routable-token-payload / routable-json transforms, and classify by CELL_ID. The pattern for both is the finished rule that has been sitting unused in test/fixtures/transform.json. The token patterns are copied verbatim from the production Bearer PAT and JOB-TOKEN rules.

The motivating traffic is the /jwt/auth Basic-credential branch of the Container Registry Routing Service: docker login, and especially docker login -u gitlab-ci-token -p $CI_JOB_TOKEN, carry a credential and no repository scope. /jwt/auth already reaches the rule engine through the generated /jwt/* guard, so this MR routes those requests by cell on its own. !1411 (merged) adds scope-based routing in front of it and falls back here.

The one code change: decode-base64-match no longer throws

decodeBase64Match used to throw InvalidBase64 on bad base64 and InvalidTokenPayloadError on a decoded credential with no :, an empty username or an empty password. Inside the engine either one becomes a 400 Bad Request. Most Basic credentials are not routable tokens (passwords, deploy tokens, OAuth client_secret_basic), so with the throw in place, these rules would have turned today's clean proxies to the Legacy Cell into 400s for every request that reaches the engine. The transform now returns a miss for a credential it cannot read. Without this change, some of the new specs fail with 400. No production ruleset used the transform before.

Behaviour change, and why it is acceptable

The engine is path-blind, so these rules apply to every request that reaches it carrying a routable Basic credential, not only /jwt/auth: git over HTTPS when its claim lookup falls back, /api/* with a Basic PAT, and so on. Those requests used to go to the Legacy Cell and now go to the cell the token names. This is parity: the same PAT presented as Bearer is already routed this way, and two doors deriving different cells from the same token is the inconsistency to avoid. Non-routable Basic credentials, and malformed ones, fall through to the remaining rules exactly as before.

Accepted at parity with the existing token rules:

  • A password that has a routable token's shape but a corrupt payload still gets a 400 (a 500 for a job token whose payload is not JSON), as Bearer and JOB-TOKEN do today.
  • Job tokens carrying an instance prefix are missed, as by the existing glcbt- rules.
  • No CRC or signature verification. The router emits a routing hint and the destination cell re-authenticates.
  • One unauthenticated Topology Service call per routable Basic request, as for Bearer.

There is no feature flag: rulesets are selected by GITLAB_RULES_CONFIG, and this ships with the deploy.

References

How to set up and validate locally

npm test covers every case without a running stack (test/index_test/session_token.spec.ts, with credential from Authorization: Basic header). On a GDK, no flag or ruleset change is needed: GDK already runs the router with GITLAB_RULES_CONFIG=session_token and the Topology Service enabled, and the rule lines below are logged at info.

cd <gdk>/gitlab-http-router && git checkout swh/session-token-basic-auth-rules
gdk restart gitlab-http-router && gdk tail gitlab-http-router

GDK's Topology Service has one cell, 1, at 127.0.0.1:3333, which is also GITLAB_PROXY_HOST. A request routed to cell 1 and one proxied to the Legacy Cell therefore return the same response, so the ruleApplied log line is what tells them apart. The tokens below carry c:1 and only need to be well-formed, not valid, so every response from Rails is a 401.

A routable PAT as the Basic password: classified, routed to cell 1.

curl -H "Authorization: Basic $(printf '%s' 'someone:glpat-xxxxxxxxxxxxxxxYzoxAw.01.06abcdefg' | base64)" 'http://127.0.0.1:3000/api/v4/user'
{ ruleApplied: 'session_token_header_authorization_basic_routable_token', ruleAction: 'classify' }
{ source: 'http://localhost:9096/v1/classify', fetchStatus: 200, ... }
{ proxyHost: '127.0.0.1:3333' }

The same PAT as Bearer, for comparison: same cell, same Topology Service call.

curl -H 'Authorization: Bearer glpat-xxxxxxxxxxxxxxxYzoxAw.01.06abcdefg' 'http://127.0.0.1:3000/api/v4/user'
{ ruleApplied: 'session_token_header_authorization_pat', ruleAction: 'classify' }

A CI job token on /jwt/auth, as docker login -u gitlab-ci-token -p $CI_JOB_TOKEN sends it: classified, routed to cell 1.

curl -H "Authorization: Basic $(printf '%s' 'gitlab-ci-token:glcbt-e30.eyJjIjoiMSJ9.sig' | base64)" 'http://127.0.0.1:3000/jwt/auth?service=container_registry&offline_token=true'
{ ruleApplied: 'session_token_header_authorization_basic_jwt', ruleAction: 'classify' }

A routable PAT naming a cell the Topology Service does not know: classified, then the usual fallback.

curl -H "Authorization: Basic $(printf '%s' 'someone:glpat-xxxxxxxxxxxxxxxYzoyAw.01.06abcdefg' | base64)" 'http://127.0.0.1:3000/api/v4/user'   # c:2
{ ruleApplied: 'session_token_header_authorization_basic_routable_token', ruleAction: 'classify' }
{ msg: 'Topology Service error, falling back to GITLAB_PROXY_HOST', error: 'Client Error: Not Found', ... }

A job token with no c claim: fails validate, proxied to the Legacy Cell.

curl -H "Authorization: Basic $(printf '%s' 'gitlab-ci-token:glcbt-e30.eyJvIjoiMSJ9.sig' | base64)" 'http://127.0.0.1:3000/jwt/auth?service=container_registry&offline_token=true'   # {"o":"1"}
{ ruleApplied: 'session_token_proxy', ruleAction: 'proxy' }

Basic credentials that are not routable tokens: proxied to the Legacy Cell, exactly as before.

curl -H "Authorization: Basic $(printf '%s' 'someone:hunter2' | base64)" 'http://127.0.0.1:3000/jwt/auth?service=container_registry&offline_token=true'
{ ruleApplied: 'session_token_proxy', ruleAction: 'proxy' }
curl -H "Authorization: Basic $(printf '%s' 'username' | base64)" 'http://127.0.0.1:3000/api/v4/user'   # no ":" separator
{ ruleApplied: 'session_token_proxy', ruleAction: 'proxy' }

Invalid base64: proxied, not rejected by the router.

curl -i -H 'Authorization: Basic abcde' 'http://127.0.0.1:3000/api/v4/user'
HTTP/1.1 400 Bad Request
{ ruleApplied: 'session_token_proxy', ruleAction: 'proxy' }
{ msg: 'fetch upstream: headers received', proxyHost: '127.0.0.1:3333', upstreamStatus: 400, ... }

The 400 here comes from Workhorse, which rejects the header itself, the same response curl -H 'Authorization: Basic abcde' http://127.0.0.1:3333/api/v4/user gets directly. The upstreamStatus: 400 line shows the router proxied the request. Before the decode-base64-match change, the engine answered 400 itself, and no fetch upstream line was logged.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Sangwoo Han

Merge request reports

Loading
Loading