Skip to content

Commit 64c73b1

Browse files
Reuse configured index credentials during tool upgrades (#21275)
## Summary - Reuse configured index credentials when a tool receipt refers to the same index, allowing subprocess keyring authentication during `uv tool upgrade`. - Keep credentials out of tool receipts and preserve existing index precedence when the configured index differs. Closes #21216.
1 parent 45e75a4 commit 64c73b1

2 files changed

Lines changed: 120 additions & 6 deletions

File tree

‎crates/uv/src/commands/tool/upgrade.rs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use std::str::FromStr;
77
use tracing::{debug, trace};
88

99
use uv_cache::Cache;
10+
use uv_cache_key::CanonicalUrl;
1011
use uv_client::BaseClientBuilder;
1112
use uv_configuration::{Concurrency, Constraints, DryRun, HashCheckingMode, TargetTriple};
1213
use uv_distribution::LoweredExtraBuildDependencies;
13-
use uv_distribution_types::{ExtraBuildRequires, Name, Requirement, RequirementSource};
14+
use uv_distribution_types::{ExtraBuildRequires, Index, Name, Requirement, RequirementSource};
1415
use uv_errors::{ErrorOptions, Hints, write_error_chain_with_options};
1516
use uv_fs::CWD;
1617
use uv_installer::{InstallationStrategy, Planner, SitePackages};
@@ -318,11 +319,29 @@ async fn upgrade_tool(
318319
}
319320
};
320321

322+
// Restore credentials from user configuration when the receipt refers to the same index.
323+
// Receipts intentionally omit credentials, including usernames needed for keyring lookups.
324+
let mut receipt = ResolverInstallerOptions::from(existing_tool_receipt.options().clone());
325+
if let (Some(stored), Some(configured)) = (
326+
receipt.indexes.index_url.as_ref(),
327+
filesystem.indexes.index_url.as_ref(),
328+
) {
329+
let stored = Index::from(stored.clone());
330+
let configured = Index::from(configured.clone());
331+
332+
if stored.raw_url().username().is_empty()
333+
&& stored.raw_url().password().is_none()
334+
&& (!configured.raw_url().username().is_empty()
335+
|| configured.raw_url().password().is_some())
336+
&& CanonicalUrl::new(stored.raw_url().clone())
337+
== CanonicalUrl::new(configured.raw_url().clone())
338+
{
339+
receipt.indexes.index_url = Some(configured.into());
340+
}
341+
}
342+
321343
// Resolve the appropriate settings, preferring: CLI > receipt > user.
322-
let options = args.clone().combine(
323-
ResolverInstallerOptions::from(existing_tool_receipt.options().clone())
324-
.combine(filesystem.clone()),
325-
);
344+
let options = args.clone().combine(receipt.combine(filesystem.clone()));
326345
let settings = ResolverInstallerSettings::from(options.clone());
327346

328347
let build_constraint_requirements = existing_tool_receipt.build_constraints().to_vec();

‎crates/uv/tests/tool/tool_upgrade.rs‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use wiremock::{
1414

1515
use uv_static::EnvVars;
1616

17-
use uv_test::uv_snapshot;
17+
use uv_test::{uv_snapshot, venv_bin_path};
1818

1919
#[test]
2020
fn tool_upgrade_empty() {
@@ -1468,6 +1468,101 @@ fn tool_upgrade_excludes() {
14681468
");
14691469
}
14701470

1471+
/// Reuse the configured index username when the tool receipt has stripped its credentials.
1472+
///
1473+
/// See: <https://github.com/astral-sh/uv/issues/21216>
1474+
#[tokio::test]
1475+
async fn tool_upgrade_index_url_keyring_auth() -> Result<()> {
1476+
let keyring_context = uv_test::test_context!("3.12");
1477+
keyring_context
1478+
.pip_install()
1479+
.arg(
1480+
keyring_context
1481+
.workspace_root
1482+
.join("test")
1483+
.join("packages")
1484+
.join("keyring_test_plugin"),
1485+
)
1486+
.assert()
1487+
.success();
1488+
1489+
let proxy = crate::pypi_proxy::start().await;
1490+
let context = uv_test::test_context!("3.12")
1491+
.with_exclude_newer("2025-01-18T00:00:00Z")
1492+
.with_filtered_exe_suffix();
1493+
let tool_dir = context.temp_dir.child("tools");
1494+
let bin_dir = context.temp_dir.child("bin");
1495+
let path = std::env::join_paths([venv_bin_path(&keyring_context.venv), bin_dir.to_path_buf()])?;
1496+
let credentials = format!(
1497+
r#"{{"{host}": {{"public": "heron"}}}}"#,
1498+
host = proxy.host_port()
1499+
);
1500+
1501+
let uv_toml = context.temp_dir.child("uv.toml");
1502+
uv_toml.write_str(&format!(
1503+
indoc! {r#"
1504+
index-url = "{}"
1505+
keyring-provider = "subprocess"
1506+
"#},
1507+
proxy.username_url("public", "/basic-auth/simple/")
1508+
))?;
1509+
1510+
context
1511+
.tool_install()
1512+
.arg("executable-application")
1513+
.arg("--index-url")
1514+
.arg(proxy.username_url("public", "/basic-auth/simple"))
1515+
.arg("--config-file")
1516+
.arg(uv_toml.as_os_str())
1517+
.env_remove(EnvVars::UV_DEFAULT_INDEX)
1518+
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str())
1519+
.env(EnvVars::XDG_BIN_HOME, bin_dir.as_os_str())
1520+
.env(EnvVars::KEYRING_TEST_CREDENTIALS, &credentials)
1521+
.env(EnvVars::PATH, &path)
1522+
.assert()
1523+
.success();
1524+
1525+
let receipt = fs_err::read_to_string(
1526+
tool_dir
1527+
.join("executable-application")
1528+
.join("uv-receipt.toml"),
1529+
)?;
1530+
insta::with_settings!({
1531+
filters => context.filters(),
1532+
}, {
1533+
assert_snapshot!(receipt, @r#"
1534+
[tool]
1535+
requirements = [{ name = "executable-application" }]
1536+
entrypoints = [
1537+
{ name = "app", install-path = "[TEMP_DIR]/bin/app", from = "executable-application" },
1538+
]
1539+
1540+
[tool.options]
1541+
index-url = "http://[LOCALHOST]/basic-auth/simple"
1542+
keyring-provider = "subprocess"
1543+
exclude-newer = "2025-01-18T00:00:00Z"
1544+
"#);
1545+
});
1546+
1547+
uv_snapshot!(context.filters(), context.tool_upgrade()
1548+
.arg("executable-application")
1549+
.arg("--config-file")
1550+
.arg(uv_toml.as_os_str())
1551+
.env_remove(EnvVars::UV_DEFAULT_INDEX)
1552+
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str())
1553+
.env(EnvVars::XDG_BIN_HOME, bin_dir.as_os_str())
1554+
.env(EnvVars::KEYRING_TEST_CREDENTIALS, &credentials)
1555+
.env(EnvVars::PATH, &path), @"
1556+
exit_code: 0 (success)
1557+
----- stderr -----
1558+
Keyring request for public@http://[LOCALHOST]/basic-auth/simple/
1559+
Keyring request for public@[LOCALHOST]
1560+
Nothing to upgrade
1561+
");
1562+
1563+
Ok(())
1564+
}
1565+
14711566
/// When upgrading a tool from an authenticated index with invalid credentials,
14721567
/// the command should fail with an auth error rather than silently reporting
14731568
/// "Nothing to upgrade".

0 commit comments

Comments
 (0)