Skip to content

feat(auth): implement AuthSessionCache for OAuth2 child sessions - #985

Open
lishuxu wants to merge 2 commits into
apache:mainfrom
lishuxu:feature/oauth-2
Open

lishuxu wants to merge 2 commits into
apache:mainfrom
lishuxu:feature/oauth-2

Conversation

@lishuxu

@lishuxu lishuxu commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@lishuxu
lishuxu force-pushed the feature/oauth-2 branch 4 times, most recently from 672cb4d to 6f4d8c9 Compare October 9, 2026 12:22
Comment thread src/iceberg/catalog/rest/auth/auth_manager.cc Outdated
ICEBERG_ASSIGN_OR_RAISE(auto config,
ChildConfig(*parent_info, parent_info->credential));
ChildConfig(parent_info, parent_info.credential));
return MakeSession(response, config, /*keep_refreshed=*/false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Token-exchange children are pinned to keep_refreshed false, so an exchanged token just expires. Java builds this one through fromTokenExchange with refreshExecutor at OAuth2Manager.java:271, the same as the credential path, so it should probably follow keep_refreshed_ too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, but enabling refresh now would switch the child to the catalog identity, because refresh only uses the parent's credential. I'll enable it once refresh via token exchange lands (TODO in DoRefreshAttempt).

Comment thread src/iceberg/catalog/rest/auth/auth_session_cache.cc Outdated
shuxu.li added 2 commits October 20, 2026 17:42
This commit implements Phase 2 of the OAuth2 enhancement (apache#962), adding
session caching and lifecycle management for contextual and table-scoped
child sessions.

**Core Changes:**

1. **AuthSessionCache implementation**
   - Guava-like cache with expireAfterAccess semantics
   - Loading deduplication: concurrent Get() calls for the same key load once
   - Dual expiration: idle timeout + OAuth2 token expiration
   - Thread-safe with fine-grained locking (loader runs outside lock)
   - Periodic sweep for expired session cleanup
   - Removal listener integration for graceful session shutdown

2. **OAuth2Session enhancements**
   - Add `StopRefreshing()`: non-blocking refresh cancellation for cache eviction
   - Add `ExpiresAt()`: expose token expiration time for cache expiry checks
   - Add `IsRefreshable()`: indicates whether auto-refresh is enabled
   - Extend `OAuth2Info()` to include `headers` (fixes child auth Issue 2)
   - Improve `CloseImpl()`: always wait for active refreshes, defensive cancel
   - Add `StoreScheduledTask()`: handle Close/StopRefreshing race conditions

3. **AuthManager child session handling**
   - Cache contextual sessions by `session_id` (key: "ctx:<session_id>")
   - Cache table sessions by `token` value (key: "tbl:<token>")
   - Fix Issue 2: child sessions now use parent's headers for authentication
   - Credential-based child sessions inherit catalog-level `keep_refreshed`
   - Token/exchange child sessions remain non-refreshed (Phase 2 scope)

4. **Configuration**
   - Add `auth.session-timeout-ms` (default: 1 hour)
   - Document `token-exchange-enabled` as not yet implemented
   - Sweep interval: clamped between 1s and 60s based on timeout

5. **SessionContext extension**
   - Add `session_id` field for stable caller context identification
   - Add `identity` field for descriptive caller identity
   - Add `credentials` map for context-specific auth material

**Testing:**
- 14 new cache tests covering concurrency, expiration, and lifecycle
- Extended auth_manager tests for child session creation

**Notes:**
- Token exchange refresh is not yet implemented (see TODO in DoRefreshAttempt)
- Failed loads are not cached (retry on next Get)
- Orphan sessions (loaded after Close) are stopped immediately
- Cache table sessions that use typed tokens. The child credential is now
  selected once by SelectChildCredential, and the table cache key is the
  value of the token actually used. Previously only the plain token
  property produced a key, so typed tokens (jwt, id_token, ...) skipped
  the cache and ran a token exchange on every table load.
- Evict only non-refreshable sessions when their token expires. Evicting
  a refreshable session could land before an in-flight refresh completed
  and stop refreshing for callers still holding it, such as loaded tables.
  Refreshable sessions now expire only after the idle timeout.

This branch has not been deployed

No deployments
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.

2 participants