Skip to content

Commit e3d5f90

Browse files
committed
Allow to specify a key for private registries
1 parent 5810b01 commit e3d5f90

File tree

8 files changed

+79
-25
lines changed

8 files changed

+79
-25
lines changed

Cargo.lock

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ log = "0.4"
2727
regex = "1"
2828
structopt = "0.3"
2929
crates-index = { version = "0.15.1", optional = true }
30-
crates-index-diff = "7.1.1"
30+
crates-index-diff = "8.0.0"
3131
reqwest = { version = "0.11", features = ["blocking", "json"] } # TODO: Remove blocking when async is ready
3232
semver = { version = "0.9", features = ["serde"] }
3333
slug = "=0.1.1"
@@ -48,7 +48,7 @@ schemamama = "0.3"
4848
schemamama_postgres = "0.3"
4949
systemstat = "0.1.4"
5050
prometheus = { version = "0.10.0", default-features = false }
51-
rustwide = "0.13"
51+
rustwide = { git = "https://github.com/l4l/rustwide.git", branch = "registry-key" }
5252
mime_guess = "2"
5353
dotenv = "0.15"
5454
zstd = "0.5"

dockerfiles/entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export DOCSRS_PREFIX=/opt/docsrs/prefix
66
export DOCSRS_DOCKER=true
77
export DOCSRS_LOG=${DOCSRS_LOG-"docs-rs,rustwide=info"}
88
export PATH="$PATH:/build/target/release"
9+
export REGISTRY_URL=${REGISTRY_URL:-https://github.com/rust-lang/crates.io-index}
910

1011
# Try migrating the database multiple times if it fails
1112
# This avoids the docker container crashing the first time it's started with
@@ -28,7 +29,7 @@ done
2829
set -e
2930

3031
if ! [ -d "${DOCSRS_PREFIX}/crates.io-index/.git" ]; then
31-
git clone https://github.com/rust-lang/crates.io-index "${DOCSRS_PREFIX}/crates.io-index"
32+
git clone ${REGISTRY_URL} "${DOCSRS_PREFIX}/crates.io-index"
3233
# Prevent new crates built before the container creation to be built
3334
git --git-dir="$DOCSRS_PREFIX/crates.io-index/.git" branch crates-index-diff_last-seen
3435
fi

src/bin/cratesfyi.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ impl BuildSubcommand {
334334
.build_local_package(&path)
335335
.context("Building documentation failed")?;
336336
} else {
337-
let registry_url = ctx.config()?.registry_url.clone();
337+
let config = ctx.config()?;
338+
let registry_url = config.registry_url.clone();
339+
let registry_key = config.registry_key.clone();
338340
builder
339341
.build_package(
340342
&crate_name
@@ -343,7 +345,10 @@ impl BuildSubcommand {
343345
.with_context(|| anyhow!("must specify version if not local"))?,
344346
registry_url
345347
.as_ref()
346-
.map(|s| PackageKind::Registry(s.as_str()))
348+
.map(|s| PackageKind::Registry {
349+
url: s.as_str(),
350+
key: registry_key,
351+
})
347352
.unwrap_or(PackageKind::CratesIo),
348353
)
349354
.context("Building documentation failed")?;
@@ -598,7 +603,7 @@ impl Context for BinContext {
598603
let config = self.config()?;
599604
let path = config.registry_index_path.clone();
600605
if let Some(registry_url) = config.registry_url.clone() {
601-
Index::from_url(path, registry_url)
606+
Index::from_url(path, registry_url, config.registry_key.clone())
602607
} else {
603608
Index::new(path)
604609
}?

src/build_queue.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ impl BuildQueue {
257257
let kind = krate
258258
.registry
259259
.as_ref()
260-
.map(|r| PackageKind::Registry(r.as_str()))
260+
.map(|r| PackageKind::Registry {
261+
url: r.as_str(),
262+
key: self.config.registry_key.clone(),
263+
})
261264
.unwrap_or(PackageKind::CratesIo);
262265

263266
if let Err(err) = builder

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Config {
1111
pub prefix: PathBuf,
1212
pub registry_index_path: PathBuf,
1313
pub registry_url: Option<String>,
14+
pub registry_key: Option<String>,
1415

1516
// Database connection params
1617
pub(crate) database_url: String,
@@ -98,6 +99,15 @@ impl Config {
9899

99100
registry_index_path: env("REGISTRY_INDEX_PATH", prefix.join("crates.io-index"))?,
100101
registry_url: maybe_env("REGISTRY_URL")?,
102+
registry_key: maybe_env::<String>("REGISTRY_KEY").and_then(|key| {
103+
Ok(if let Some(key) = key {
104+
Some(key)
105+
} else {
106+
maybe_env::<String>("REGISTRY_KEY_PATH")?
107+
.map(std::fs::read_to_string)
108+
.transpose()?
109+
})
110+
})?,
101111
prefix: prefix.clone(),
102112

103113
database_url: require_env("DOCSRS_DATABASE_URL")?,

src/docbuilder/rustwide_builder.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use postgres::Client;
1919
use rustwide::cmd::{Command, CommandError, SandboxBuilder, SandboxImage};
2020
use rustwide::logging::{self, LogStorage};
2121
use rustwide::toolchain::ToolchainError;
22-
use rustwide::{Build, Crate, Toolchain, Workspace, WorkspaceBuilder};
22+
use rustwide::{AlternativeRegistry, Build, Crate, Toolchain, Workspace, WorkspaceBuilder};
2323
use serde_json::Value;
2424
use std::collections::{HashMap, HashSet};
2525
use std::path::Path;
@@ -32,7 +32,7 @@ const DUMMY_CRATE_VERSION: &str = "1.0.0";
3232
pub enum PackageKind<'a> {
3333
Local(&'a Path),
3434
CratesIo,
35-
Registry(&'a str),
35+
Registry { url: &'a str, key: Option<String> },
3636
}
3737

3838
pub struct RustwideBuilder {
@@ -242,7 +242,10 @@ impl RustwideBuilder {
242242
let registry_url = self.config.registry_url.clone();
243243
let package_kind = registry_url
244244
.as_ref()
245-
.map(|r| PackageKind::Registry(r.as_str()))
245+
.map(|r| PackageKind::Registry {
246+
url: r.as_str(),
247+
key: self.config.registry_key.clone(),
248+
})
246249
.unwrap_or(PackageKind::CratesIo);
247250
if let Err(err) = self.build_package(name, version, package_kind) {
248251
warn!("failed to build package {} {}: {}", name, version, err);
@@ -309,7 +312,13 @@ impl RustwideBuilder {
309312
let krate = match kind {
310313
PackageKind::Local(path) => Crate::local(path),
311314
PackageKind::CratesIo => Crate::crates_io(name, version),
312-
PackageKind::Registry(registry) => Crate::registry(registry, name, version),
315+
PackageKind::Registry { url, key } => {
316+
let mut registry = AlternativeRegistry::new(url);
317+
if let Some(key) = key {
318+
registry.authenticate_with_ssh_key(key);
319+
}
320+
Crate::registry(registry, name, version)
321+
}
313322
};
314323
krate.fetch(&self.workspace).map_err(FailureError::compat)?;
315324

src/index/mod.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Index {
1515
path: PathBuf,
1616
api: Api,
1717
repository_url: Option<String>,
18+
registry_key: Option<String>,
1819
}
1920

2021
#[derive(Debug, serde::Deserialize, Clone)]
@@ -39,12 +40,31 @@ fn load_config(repo: &git2::Repository) -> Result<IndexConfig> {
3940
Ok(config)
4041
}
4142

43+
fn fetch_options(key: &str) -> git2::FetchOptions<'_> {
44+
let mut fo = git2::FetchOptions::new();
45+
fo.remote_callbacks({
46+
let mut callbacks = git2::RemoteCallbacks::new();
47+
callbacks.credentials(move |_url, username_from_url, _allowed_types| {
48+
git2::Cred::ssh_key_from_memory(username_from_url.unwrap(), None, key, None)
49+
});
50+
callbacks
51+
});
52+
fo
53+
}
54+
4255
impl Index {
43-
pub fn from_url(path: PathBuf, repository_url: String) -> Result<Self> {
56+
pub fn from_url(
57+
path: PathBuf,
58+
repository_url: String,
59+
registry_key: Option<String>,
60+
) -> Result<Self> {
4461
let url = repository_url.clone();
4562
let diff = crates_index_diff::Index::from_path_or_cloned_with_options(
4663
&path,
47-
crates_index_diff::CloneOptions { repository_url },
64+
crates_index_diff::CloneOptions {
65+
repository_url,
66+
fetch_options: registry_key.as_ref().map(|s| fetch_options(s.as_str())),
67+
},
4868
)
4969
.context("initialising registry index repository")?;
5070

@@ -54,6 +74,7 @@ impl Index {
5474
path,
5575
api,
5676
repository_url: Some(url),
77+
registry_key,
5778
})
5879
}
5980

@@ -68,17 +89,23 @@ impl Index {
6889
path,
6990
api,
7091
repository_url: None,
92+
registry_key: None,
7193
})
7294
}
7395

7496
pub(crate) fn diff(&self) -> Result<crates_index_diff::Index> {
75-
let options = self
76-
.repository_url
77-
.clone()
78-
.map(|repository_url| crates_index_diff::CloneOptions { repository_url })
79-
.unwrap_or_default();
80-
let diff = crates_index_diff::Index::from_path_or_cloned_with_options(&self.path, options)
81-
.context("re-opening registry index for diff")?;
97+
let fetch_options = self.registry_key.as_deref().map(fetch_options);
98+
let diff = crates_index_diff::Index::from_path_or_cloned_with_options(
99+
&self.path,
100+
self.repository_url
101+
.clone()
102+
.map(move |repository_url| crates_index_diff::CloneOptions {
103+
repository_url,
104+
fetch_options,
105+
})
106+
.unwrap_or_default(),
107+
)
108+
.context("re-opening registry index for diff")?;
82109
Ok(diff)
83110
}
84111

0 commit comments

Comments
 (0)