Skip to content

feat: support per-request header override - #2631

Merged
jackye1995 merged 3 commits into
lancedb:mainfrom
jackye1995:auth-manager
Sep 10, 2025
Merged

jackye1995 merged 3 commits into
lancedb:mainfrom
jackye1995:auth-manager

Conversation

@jackye1995

@jackye1995 jackye1995 commented Sep 5, 2025 •

Copy link
Copy Markdown
Contributor

Summary

This PR introduces a HeaderProvider which is called for all remote HTTP calls to get the latest headers to inject. This is useful for features like adding the latest auth tokens where the header provider can auto-refresh tokens internally and each request always set the refreshed token.

@github-actions github-actions Bot added enhancement New feature or request Python Python SDK Rust Rust related issues labels Sep 5, 2025
@jackye1995
jackye1995 marked this pull request as ready for review September 5, 2025 06:36
@wjones127 wjones127 self-assigned this Sep 5, 2025

@westonpace westonpace left a comment

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.

A few suggestions for cleanup but looks ok to me

Comment thread python/python/lancedb/remote/header.py Outdated
Comment on lines +64 to +68
Thread Safety
-------------
Implementations should be thread-safe as get_headers() may be called
concurrently from multiple threads. Use locks or other synchronization
primitives if your implementation maintains mutable state.

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.

This would only be a problem if the header provider called into underlying C routines right? A pure python header provider doesn't need to worry?

Comment thread python/python/lancedb/remote/header.py Resolved
with self._refresh_lock:
# Check again inside the lock in case another thread refreshed
if self._needs_refresh():
token_data = self._token_fetcher()

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.

Is this going to do blocking I/O?

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.

yes

Comment thread python/src/header.rs
#[async_trait]
impl lancedb::remote::HeaderProvider for PyHeaderProvider {
async fn get_headers(&self) -> lancedb::error::Result<HashMap<String, String>> {
self.get_headers_internal()

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.

Should we spawn this call on a dedicated blocking task with tokio's spawn_blocking?

Comment thread python/src/header.rs Outdated
/// A wrapper around a Python HeaderProvider that can be called from Rust
#[derive(Clone)]
pub struct PyHeaderProvider {
provider: Arc<RwLock<Option<Py<PyAny>>>>,

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.

Does this need to be an async mutex? It looks like it only gets set once from the constructor. Actually, is a mutex needed here at all? It doesn't look like this is ever modified.

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.

yes you are right, originally I implemented the python HeaderProvider async, let me remove this

Comment thread python/src/header.rs Outdated
}

/// Get headers from the Python provider (internal implementation)
async fn get_headers_internal(&self) -> Result<HashMap<String, String>, String> {

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.

This method could be sync if you get rid of the async mutex

Add HeaderProvider authentication system for dynamic header management:
- Python HeaderProvider abstract base class with StaticHeaderProvider and OAuthProvider implementations
- Rust FFI bridge to call Python providers from Rust code
- HTTP client modifications to apply dynamic headers before each request
- Comprehensive test coverage for all authentication patterns

This enables OAuth token refresh, API key rotation, and custom authentication schemes.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@jackye1995

Copy link
Copy Markdown
Contributor Author

looks like there is in general a problem that building the lancedb wheel for doc code testing take a long time, I will publish another PR to fix that, and merge this for a prerelease first.

@jackye1995
jackye1995 merged commit 8da74dc into lancedb:main Sep 10, 2025
Xuanwo added a commit that referenced this pull request Oct 22, 2026
…4632)

## Summary

#3971 added `test_zero_lifetime_token_is_refreshed`, but its expected
headers omit `x-lancedb-credential-type: oidc`, which
`OAuthProvider.get_headers()` has emitted since #2631/#4070. The test
fails against current main and breaks the PR CI matrix for every open
PR.

## Changes

- Align the two zap assertions in
`test_zero_lifetime_token_is_refreshed` with the provider contract (see
`test_token_refresh` and `test_sync_method`).

## Tests

- `pytest python/tests/test_header_provider.py -q`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Python Python SDK Rust Rust related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants