Repository navigation
Ruby SDK: a token provider resent once on a 401, and ~/.loom/token - #1697
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
🟡 Changes recommended
The mutable base_url allows the construction-time origin check to be bypassed, causing subsequent requests to send the bound token to another origin.
1 open issue
What changed in this PR
Adds token-provider authentication and a ~/.loom/token fallback to the Ruby SDK, advancing the credential support requested in #55. Stored loom login sessions remain out of scope.
Changes:
- Adds configurable token providers, credential precedence, and provider-origin validation.
- Resends a provider-authenticated request once after a 401, preserving request identifiers without consuming a retry attempt.
- Documents token behavior and adds credential tests with isolated home directories.
| File | Description |
|---|---|
sdk/ruby/test/test_helper.rb |
Isolates each test’s home directory from real credentials. |
sdk/ruby/test/credentials_test.rb |
Tests credential selection, token renewal, origin checks, and redaction. |
sdk/ruby/README.md |
Documents token providers, file fallback, and remaining limitations. |
sdk/ruby/lib/clickhouse/loom/credentials.rb |
Adds token-file reading, provider invocation, and origin validation. |
sdk/ruby/lib/clickhouse/loom/configuration.rb |
Exposes the configurable token provider. |
sdk/ruby/lib/clickhouse/loom/client.rb |
Integrates credential resolution and one-time authentication resends. |
Review effort: Balanced
zlareb1
left a comment
There was a problem hiding this comment.
Freezing base_url fixes the earlier thread. The provider can still change its origin after client construction, so the request path needs to enforce the binding for each token it sends.
|
The format job fails on the adoption page with the same formatter output on clean |
The origin was checked only when the client was built. A provider whose origin changes later, including while it refreshes after a 401, is now refused at dispatch before its token goes out.
zlareb1
left a comment
There was a problem hiding this comment.
The client keeps its original destination and checks the provider’s origin after obtaining each token, including on a 401 refresh. The two regression cases cover both paths. No blocking findings on this head.
zlareb1
left a comment
There was a problem hiding this comment.
ClickHouse expires the adoption directory test’s fixed snapshot rows before the view reads them. Clean main reproduces the failure. The test passes with UTC-relative fixture dates. Failed unit job · Repair #1740
zlareb1
left a comment
There was a problem hiding this comment.
The tunable-constant gate flags the same duplicate coverage limit on this head and on clean main. The coverage repair keeps the store's existing bounds. PR job · Main job · Coverage repair #1722

Related: #55
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
The Ruby SDK takes a
token_providerthat hands over the token for each request and is asked once more after a401, and, with no key set, it reads~/.loom/tokenas the TypeScript and Python SDKs do.Why
This is the first half of item 3 on #55 (credentials and tokens). The TypeScript SDK sends a
TokenProvider's token on every request and resends a401once with a fresh one (transport.ts), and both other SDKs fall back to~/.loom/token(token_from_fileintransport.py,tokenFromFileincredentials.ts). The Ruby SDK only took a fixedapi_key.Scope and risk
Part of #55. Ruby SDK only. A client with an
api_keyorLOOM_API_KEYbehaves exactly as before.The stored
loom loginsession (~/.loom/credentials: the profile's access token, refreshed under the~/.loom/.credentials.lock.dlock the Python and TS SDKs share) is the second half. It plugs into the provider added here, and I've kept it for its own PR because the lock protocol has to match the other two SDKs exactly, or two processes could spend the same refresh token. Until then, a user who has both a stored login and a token file gets the token file from Ruby, where the TS and Python SDKs use the login. The README says the stored login isn't read yet.sdk/ruby/lib/clickhouse/loom/credentials.rb(new),ClickHouse::Loom::Credentials:token_from_file: the first line of~/.loom/token, stripped;nilwhen it is missing, unreadable or blank, astoken_from_fileintransport.py.provider_token: asks a provider fortoken(refused), or calls a Proc (withrefused, or with no argument for a zero-arity lambda).check_origin!: a provider that responds tooriginis refused (ArgumentError, no request sent) for a client on another origin, so its token never goes to a server it was not made for, asproviderBearerdoes.Client.new(token_provider:)andLoom.configure'stoken_provider. The credential, first found wins:token_provider:,api_key:, the configuredtoken_provider, the configuredapi_key,LOOM_API_KEY, then~/.loom/token. Bothapi_key:andtoken_provider:raiseArgumentError, as the TSTransportdoes.401resend, insend_with_retries:Authorizationheader set from its answer; an empty token sends none;401to a provider's non-empty token is sent once more withrefusedset to that token: the same call, so the sameX-Request-IdandIdempotency-Key, and it spends no retry attempt (the server refused before running the op);401is raised; so is a second401;infoline,memory.get 401; retrying once with a fresh token (rid=...), the TS line.401to a static key is raised as before.401resend.test/test_helper.rb: every test gets its ownHOME, so a developer's real~/.loom/tokencan't reach a test.Tests
sdk/ruby/test/credentials_test.rb, 18 tests against the local fake server:~/.loom/tokenis used as the last credential, only its first line; blank or missing sends no header;LOOM_API_KEYand a provider both win over it;refused = nil;401then200: two requests,Bearer stalethenBearer fresh, the provider asked withnilthen"stale", one request id,X-Loom-Retry-Count0then1;AuthInvalidraised; a second401is raised after two requests; a static key's401is not resent;401resend spends no attempt:401,503,200withmax_attempts: 2succeeds;api_key:withtoken_provider:raises; a provider bound to another origin raises before any request, and one bound to this origin is used;api_key:is passed;inspect.On
ruby:3.1-slimandruby:3.3-slimas a non-root user, the whole Ruby suite passes (113 runs, 0 failures). Each of three mutants fails the new tests: dropping the401resend, sending the refused token again instead of raising the401, and not reading~/.loom/token.Note
Medium Risk
Touches auth header handling and automatic 401 re-send, but existing static API key paths are preserved and origin checks limit token misrouting.
Overview
The Ruby SDK adds dynamic authentication aligned with the TypeScript and Python SDKs: clients and
Loom.configurecan take atoken_provider(object withtoken(refused)or a Proc) that supplies the bearer on every request. On a 401, the client asks once more with the refused token and retries the same call (same request/idempotency headers, no retry-policy attempt consumed). Staticapi_key/LOOM_API_KEYbehavior is unchanged and does not auto-refresh on 401.Credential resolution is now explicit:
token_provider→api_key→ config → env →~/.loom/token(first line). Passing bothapi_keyandtoken_providerraisesArgumentError. NewCredentialsmodule handles file tokens, provider invocation, and origin binding so a provider tied to another host never sends tokens to the wrong server.README documents the Tokens section and updated defaults; tests use an isolated
HOMEand add broad coverage for file fallback, 401 refresh, and origin checks.~/.loom/credentials(loom login) is still explicitly out of scope.Reviewed by Cursor Bugbot for commit 021556d. Bugbot is set up for automated code reviews on this repo. Configure here.