Skip to content

Commit

Permalink
Restore seeding of authentication cache from index URLs (#3124)
Browse files Browse the repository at this point in the history
Roughly reverts
f7820ce
to reduce possible race conditions for pre-authenticated index URLs

Part of:

- #3123
- #3122
  • Loading branch information
zanieb authored Apr 19, 2024
1 parent 5ca5d7d commit 822ae19
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/uv-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@ mod keyring;
mod middleware;
mod netloc;

use std::sync::Arc;

use cache::CredentialsCache;
use credentials::Credentials;

pub use keyring::KeyringProvider;
pub use middleware::AuthMiddleware;
use netloc::NetLoc;
use once_cell::sync::Lazy;
use url::Url;

// TODO(zanieb): Consider passing a cache explicitly throughout

/// Global authentication cache for a uv invocation
///
/// This is used to share credentials across uv clients.
pub(crate) static CREDENTIALS_CACHE: Lazy<CredentialsCache> = Lazy::new(CredentialsCache::default);

/// Populate the global authentication store with credentials on a URL, if there are any.
///
/// Returns `true` if the store was updated.
pub fn store_credentials_from_url(url: &Url) -> bool {
if let Some(credentials) = Credentials::from_url(url) {
CREDENTIALS_CACHE.insert(url, Arc::new(credentials));
true
} else {
false
}
}
6 changes: 6 additions & 0 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use distribution_types::{IndexLocations, LocalEditable, LocalEditables, Verbatim
use install_wheel_rs::linker::LinkMode;
use platform_tags::Tags;
use requirements_txt::EditableRequirement;
use uv_auth::store_credentials_from_url;
use uv_cache::Cache;
use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder};
use uv_configuration::KeyringProviderType;
Expand Down Expand Up @@ -219,6 +220,11 @@ pub(crate) async fn pip_compile(
let index_locations =
index_locations.combine(index_url, extra_index_urls, find_links, no_index);

// Add all authenticated sources to the cache.
for url in index_locations.urls() {
store_credentials_from_url(url);
}

// Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
.native_tls(native_tls)
Expand Down
6 changes: 6 additions & 0 deletions crates/uv/src/commands/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use pep508_rs::{MarkerEnvironment, Requirement};
use platform_tags::Tags;
use pypi_types::{Metadata23, Yanked};
use requirements_txt::EditableRequirement;
use uv_auth::store_credentials_from_url;
use uv_cache::Cache;
use uv_client::{
BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClient, RegistryClientBuilder,
Expand Down Expand Up @@ -203,6 +204,11 @@ pub(crate) async fn pip_install(
let index_locations =
index_locations.combine(index_url, extra_index_urls, find_links, no_index);

// Add all authenticated sources to the cache.
for url in index_locations.urls() {
store_credentials_from_url(url);
}

// Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
.native_tls(native_tls)
Expand Down
6 changes: 6 additions & 0 deletions crates/uv/src/commands/pip_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use install_wheel_rs::linker::LinkMode;
use platform_tags::Tags;
use pypi_types::Yanked;
use requirements_txt::EditableRequirement;
use uv_auth::store_credentials_from_url;
use uv_cache::{ArchiveTarget, ArchiveTimestamp, Cache};
use uv_client::{
BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClient, RegistryClientBuilder,
Expand Down Expand Up @@ -150,6 +151,11 @@ pub(crate) async fn pip_sync(
let index_locations =
index_locations.combine(index_url, extra_index_urls, find_links, no_index);

// Add all authenticated sources to the cache.
for url in index_locations.urls() {
store_credentials_from_url(url);
}

// Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
.native_tls(native_tls)
Expand Down
6 changes: 6 additions & 0 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use thiserror::Error;
use distribution_types::{DistributionMetadata, IndexLocations, Name, ResolvedDist};
use install_wheel_rs::linker::LinkMode;
use pep508_rs::Requirement;
use uv_auth::store_credentials_from_url;
use uv_cache::Cache;
use uv_client::{Connectivity, FlatIndexClient, RegistryClientBuilder};
use uv_configuration::KeyringProviderType;
Expand Down Expand Up @@ -119,6 +120,11 @@ async fn venv_impl(
find_default_python(cache).into_diagnostic()?
};

// Add all authenticated sources to the cache.
for url in index_locations.urls() {
store_credentials_from_url(url);
}

writeln!(
printer.stderr(),
"Using Python {} interpreter at: {}",
Expand Down

0 comments on commit 822ae19

Please sign in to comment.