Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions crates/socket-patch-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ pub struct GlobalArgs {
)]
pub patch_server_url: Option<String>,

/// Base URL of a mirror of Socket's patch server: a caching proxy in
/// front of https://patch.socket.dev, such as an Artifactory remote
/// repository. Hosted mode writes its patch references under this base
/// instead of the patch server's own host (same path), and every
/// command accepts lockfile URLs on this origin as hosted patch
/// references. Set it the same way wherever socket-patch runs for the
/// project.
//
// EXPERIMENTAL (hidden from `--help`): for organizations that install
// through their own artifact host. The flag, its variables and its
// warning codes carry no stability guarantee yet; see "Patch mirror
// (experimental)" in CLI_CONTRACT.md.
#[arg(
help_heading = GLOBAL_OPTIONS,
long = "patch-mirror-url",
env = "SOCKET_PATCH_MIRROR_URL",
hide = true
)]
pub patch_mirror_url: Option<String>,

/// Disable network access; fail if remote data is needed.
#[arg(
help_heading = GLOBAL_OPTIONS,
Expand Down Expand Up @@ -626,6 +646,42 @@ impl GlobalArgs {
.map_or_else(|| self.telemetry_auth(), TelemetryAuth::for_client)
}

/// Refuse a `--patch-mirror-url` that cannot prefix a committed URL,
/// once, before any command reads the project. The message says what
/// is wrong and never repeats the value: a base given with credentials
/// in it must not be echoed into a terminal or a CI log.
pub fn validate_patch_mirror(&self) -> Result<(), String> {
let Some(raw) = self
.patch_mirror_url
.as_deref()
.filter(|url| !url.trim().is_empty())
else {
return Ok(());
};
socket_patch_core::patch::redirect::mirror::PatchMirror::parse(raw)
.map(|_| ())
.map_err(|why| {
format!(
"--patch-mirror-url (or SOCKET_PATCH_MIRROR_URL) is not a usable patch \
mirror base URL: {why}"
)
})
}

/// The patch-server mirror this run was given (`--patch-mirror-url`).
/// [`Self::validate_patch_mirror`] refused a base that does not parse
/// before the command started; a struct literal with one has no mirror.
pub(crate) fn patch_mirror(
&self,
) -> Option<socket_patch_core::patch::redirect::mirror::PatchMirror> {
self.patch_mirror_url
.as_deref()
.filter(|url| !url.trim().is_empty())
.and_then(|url| {
socket_patch_core::patch::redirect::mirror::PatchMirror::parse(url).ok()
})
}

/// The vendoring-service config every vendor entry point (`vendor`,
/// `scan`/`get --mode vendored`) builds from the same flags, so they
/// commit byte-identical artifacts for the same patch. Clap validated
Expand Down Expand Up @@ -728,6 +784,7 @@ pub const GLOBAL_ARG_ENV_VARS: &[&str] = &[
"SOCKET_VENDOR_SOURCE",
"SOCKET_VENDOR_URL",
"SOCKET_PATCH_SERVER_URL",
"SOCKET_PATCH_MIRROR_URL",
"SOCKET_OFFLINE",
"SOCKET_STRICT",
"SOCKET_GLOBAL",
Expand Down Expand Up @@ -800,6 +857,7 @@ impl Default for GlobalArgs {
maven_config: None,
vendor_url: None,
patch_server_url: None,
patch_mirror_url: None,
offline: false,
strict: false,
allow_patch_conflicts: false,
Expand Down
9 changes: 7 additions & 2 deletions crates/socket-patch-cli/src/commands/agent_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ pub struct DownloadParams {
/// `--patch-server-url`: the extra origin whose URLs count as hosted
/// when lockfile discovery reads the project's hosted pins.
pub patch_server_url: Option<String>,
/// `--patch-mirror-url`: the patch-server mirror whose origin counts as
/// hosted the same way.
pub patch_mirror_url: Option<String>,
}

impl DownloadParams {
Expand Down Expand Up @@ -1202,8 +1205,10 @@ pub(crate) async fn vendored_download_refusals(
prior: Option<&crate::ecosystem_dispatch::NpmCrawlSnapshot>,
) -> VendorRefusals {
let cwd = params.cwd.as_path();
let origins =
crate::commands::hosted_unwind::patch_server_origins_of(params.patch_server_url.as_deref());
let origins = crate::commands::hosted_unwind::patch_server_origins_of(
params.patch_server_url.as_deref(),
params.patch_mirror_url.as_deref(),
);
let pins = hosted_pins(cwd, origins.clone()).await;
let options = params.crawler_options();
let installed = crate::commands::vendor::installed_lookup(&options, prior);
Expand Down
3 changes: 3 additions & 0 deletions crates/socket-patch-cli/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,7 @@ fn get_download_params(args: &GetArgs, save_only: bool, persist_blobs: bool) ->
ecosystems: args.common.ecosystems.clone(),
persist_blobs,
patch_server_url: args.common.patch_server_url.clone(),
patch_mirror_url: args.common.patch_mirror_url.clone(),
}
}

Expand Down Expand Up @@ -4209,6 +4210,7 @@ mod tests {
ecosystems: None,
persist_blobs: false,
patch_server_url: None,
patch_mirror_url: None,
}
}

Expand Down Expand Up @@ -4960,6 +4962,7 @@ mod tests {
// The vendor-detached posture this fn exists for.
persist_blobs: false,
patch_server_url: None,
patch_mirror_url: None,
}
}

Expand Down
88 changes: 78 additions & 10 deletions crates/socket-patch-cli/src/commands/hosted_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,53 @@ impl HostedLegOutcome {
}

/// The patch-server origins that count as hosted, besides Socket's own:
/// the operator's `--patch-server-url` (discovery's allowlist).
/// the operator's `--patch-server-url` and the origin of their
/// `--patch-mirror-url` (discovery's allowlist).
pub(crate) fn patch_server_origins(common: &GlobalArgs) -> Vec<String> {
patch_server_origins_of(common.patch_server_url.as_deref())
patch_server_origins_of(
common.patch_server_url.as_deref(),
common.patch_mirror_url.as_deref(),
)
}

/// [`patch_server_origins`] from the `--patch-server-url` value itself, for
/// callers that carry it outside a [`GlobalArgs`].
pub(crate) fn patch_server_origins_of(patch_server_url: Option<&str>) -> Vec<String> {
patch_server_url
.into_iter()
.filter(|url| !url.trim().is_empty())
.map(str::to_string)
.collect()
/// [`patch_server_origins`] from the `--patch-server-url` and
/// `--patch-mirror-url` values themselves, for callers that carry them
/// outside a [`GlobalArgs`].
///
/// * `--patch-server-url` is listed as given, and holds its whole origin.
/// * The mirror is listed as a mirror's base
/// ([`socket_patch_core::origin::mirror_entry`]), which holds only what
/// is under that base: a mirrored reference is the patch server's own
/// path under it, and the rest of the mirror's host is not the mirror. A
/// base that does not parse ([`PatchMirror::parse`]) is not listed.
/// * With a mirror, the FIRST entry is always the patch server (the
/// operator's, else Socket's own): an upstream restore downloads from the
/// first entry, and what it downloads is not a patch the mirror was
/// checked for, nor something the run holds the mirror's credentials for.
pub(crate) fn patch_server_origins_of(
patch_server_url: Option<&str>,
patch_mirror_url: Option<&str>,
) -> Vec<String> {
use socket_patch_core::patch::redirect::mirror::PatchMirror;
fn given(url: Option<&str>) -> Option<&str> {
url.map(str::trim).filter(|url| !url.is_empty())
}
let server = given(patch_server_url).map(str::to_string);
let mirror = given(patch_mirror_url)
.and_then(|url| PatchMirror::parse(url).ok())
.map(|mirror| socket_patch_core::origin::mirror_entry(mirror.base()));
match mirror {
None => server.into_iter().collect(),
Some(mirror) => vec![
server.unwrap_or_else(|| {
format!(
"https://{}",
socket_patch_core::origin::SOCKET_PATCH_SERVER_HOST
)
}),
mirror,
],
}
}

/// The gem leg of the unwind's installed-tree care (the "Gem stale-install
Expand Down Expand Up @@ -291,3 +325,37 @@ pub(crate) async fn run_hosted_leg(
.extend(crate::commands::vlt_heal::rollback_heal(common, &unwound).await);
out
}

#[cfg(test)]
mod tests {
use super::patch_server_origins_of;

/// The list every reader recognizes hosted pins against: the patch
/// server as given, the mirror as a mirror's base, and with a mirror
/// the patch server first, because an upstream restore downloads from
/// the first entry and must never ask the mirror.
#[test]
fn the_patch_server_comes_first_and_the_mirror_is_its_base() {
let mirror = "https://repo.example/artifactory/socket-patch";
let listed = format!("mirror+{mirror}");
assert!(patch_server_origins_of(None, None).is_empty());
assert!(patch_server_origins_of(Some(" "), Some("")).is_empty());
assert_eq!(
patch_server_origins_of(Some("http://127.0.0.1:4545"), None),
["http://127.0.0.1:4545"]
);
assert_eq!(
patch_server_origins_of(None, Some(&format!("{mirror}/"))),
["https://patch.socket.dev", listed.as_str()]
);
assert_eq!(
patch_server_origins_of(Some("http://127.0.0.1:4545"), Some(mirror)),
["http://127.0.0.1:4545", listed.as_str()]
);
// A base that does not parse is no mirror: it is not listed, and
// its userinfo never becomes an accepted origin.
assert!(
patch_server_origins_of(None, Some("https://user:secret@repo.example/x")).is_empty()
);
}
}
Loading
Loading