Skip to content

Ruby SDK: a token provider resent once on a 401, and ~/.loom/token - #1697

Merged
zlareb1 merged 3 commits into
ClickHouse:mainfrom
rahulsjha:sdk-ruby-credentials
Nov 30, 2026
Merged

zlareb1 merged 3 commits into
ClickHouse:mainfrom
rahulsjha:sdk-ruby-credentials

Conversation

@rahulsjha

@rahulsjha rahulsjha commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Related: #55

Changelog category (leave one):

  • New Feature

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_provider that hands over the token for each request and is asked once more after a 401, and, with no key set, it reads ~/.loom/token as 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 a 401 once with a fresh one (transport.ts), and both other SDKs fall back to ~/.loom/token (token_from_file in transport.py, tokenFromFile in credentials.ts). The Ruby SDK only took a fixed api_key.

Scope and risk

Part of #55. Ruby SDK only. A client with an api_key or LOOM_API_KEY behaves exactly as before.

The stored loom login session (~/.loom/credentials: the profile's access token, refreshed under the ~/.loom/.credentials.lock.d lock 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.

Ruby SDK token provider: the suite on Ruby 3.1 and 3.3, and three mutants

  • sdk/ruby/lib/clickhouse/loom/credentials.rb (new), ClickHouse::Loom::Credentials:
    • token_from_file: the first line of ~/.loom/token, stripped; nil when it is missing, unreadable or blank, as token_from_file in transport.py.
    • provider_token: asks a provider for token(refused), or calls a Proc (with refused, or with no argument for a zero-arity lambda).
    • check_origin!: a provider that responds to origin is refused (ArgumentError, no request sent) for a client on another origin, so its token never goes to a server it was not made for, as providerBearer does.
  • Client.new(token_provider:) and Loom.configure's token_provider. The credential, first found wins: token_provider:, api_key:, the configured token_provider, the configured api_key, LOOM_API_KEY, then ~/.loom/token. Both api_key: and token_provider: raise ArgumentError, as the TS Transport does.
  • The 401 resend, in send_with_retries:
    • before every attempt the provider is asked and the Authorization header set from its answer; an empty token sends none;
    • a 401 to a provider's non-empty token is sent once more with refused set to that token: the same call, so the same X-Request-Id and Idempotency-Key, and it spends no retry attempt (the server refused before running the op);
    • a provider that returns the refused token again has nothing fresher, and the 401 is raised; so is a second 401;
    • an info line, memory.get 401; retrying once with a fresh token (rid=...), the TS line.
    • A 401 to a static key is raised as before.
  • README: a "Tokens" section, the configuration table, and the "Not carried over yet" line now names the stored login instead of the 401 resend.
  • test/test_helper.rb: every test gets its own HOME, so a developer's real ~/.loom/token can't reach a test.

Tests

sdk/ruby/test/credentials_test.rb, 18 tests against the local fake server:

  • ~/.loom/token is used as the last credential, only its first line; blank or missing sends no header; LOOM_API_KEY and a provider both win over it;
  • a provider's token goes on every request, asked with refused = nil;
  • 401 then 200: two requests, Bearer stale then Bearer fresh, the provider asked with nil then "stale", one request id, X-Loom-Retry-Count 0 then 1;
  • a provider with nothing fresher: one request, AuthInvalid raised; a second 401 is raised after two requests; a static key's 401 is not resent;
  • the 401 resend spends no attempt: 401, 503, 200 with max_attempts: 2 succeeds;
  • an empty token sends no header; Proc and zero-argument lambda providers;
  • api_key: with token_provider: raises; a provider bound to another origin raises before any request, and one bound to this origin is used;
  • the configured provider is used unless an api_key: is passed;
  • neither token appears in a debug log or in inspect.

On ruby:3.1-slim and ruby:3.3-slim as a non-root user, the whole Ruby suite passes (113 runs, 0 failures). Each of three mutants fails the new tests: dropping the 401 resend, sending the refused token again instead of raising the 401, 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.configure can take a token_provider (object with token(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). Static api_key / LOOM_API_KEY behavior 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 both api_key and token_provider raises ArgumentError. New Credentials module 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 HOME and 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.

Copilot AI balanced review requested due to automatic review settings November 28, 2026 09:37
@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 29, 2026 12:57am UTC
loom-telemetry Ready Ready Preview Nov 29, 2026 12:57am UTC

Request Review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread sdk/ruby/lib/clickhouse/loom/client.rb Resolved
@zlareb1 zlareb1 self-assigned this Nov 28, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sdk/ruby/lib/clickhouse/loom/client.rb Resolved
@zlareb1

zlareb1 commented Nov 28, 2026

Copy link
Copy Markdown
Member

The format job fails on the adoption page with the same formatter output on clean main. The pinned formatter passes locally with the repair. PR job · Main job · Repair #1706

@rahulsjha

Copy link
Copy Markdown
Contributor Author

Thanks @zlareb1, I'll rebase on main once #1706 is in so the format job runs green on this branch.

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 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rahulsjha

Copy link
Copy Markdown
Contributor Author

Thanks @zlareb1 for the approval and for tracking down the failing jobs. The format job and the pytest unit jobs fail the same way on clean main, so I'll merge main into this branch once #1706 and #1740 are in, and confirm the checks run green here then. Nothing else in this PR changes.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@zlareb1
zlareb1 merged commit d0da06c into ClickHouse:main Nov 30, 2026
68 of 73 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