1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Release source contract.
//!
//! Phase 2+ adapters implement [`ReleaseSource`] and register themselves in
//! [`SOURCE_REGISTRY`]. This wave only establishes the types and the registry;
//! ingestion still goes through deps.dev and the registry fetchers in
//! `crate::release::artifact`, so behavior is unchanged.
use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use serde_json::Value;
use crate::release::http::RegistryHttp;
use crate::release::sources::{
apache::ApacheSource,
bitbucket::BitbucketSource,
cargo::CargoSource,
composer::ComposerSource,
cpan::CpanSource,
distro::{DistroSource, ManifestHttp, ReqwestManifestHttp},
docker::{DockerHttp, DockerSource, ReqwestDockerHttp},
gem::GemSource,
gitee::GiteeSource,
github::{GitHttp, GithubSource, ReqwestGitHttp},
gitlab::GitlabSource,
gnu::GnuSource,
golang::GolangSource,
maven::MavenSource,
npm::NpmSource,
nuget::NugetSource,
pypi::PyPiSource,
sourceforge::SourceforgeSource,
};
/// A source-neutral package reference derived from a purl.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectRef {
pub purl_type: String,
pub name: String,
pub purl: String,
}
/// One version offered by a source, normalized for the release records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VersionInfo {
pub raw: String,
pub version: String,
pub deprecated: bool,
pub reason: String,
}
/// Artifact metadata before any download or hashing fallback.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArtifactInfo {
pub file_name: String,
pub url: String,
pub sha256: Option<String>,
pub size: Option<u64>,
pub mime_type: Option<String>,
}
/// A registry status signal for one version.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusInfo {
pub status: String,
pub kind: Option<String>,
pub reason: String,
}
/// What a source can answer without fabricating data.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SourceCapabilities {
pub versions: bool,
pub artifacts: bool,
pub statuses: bool,
pub project_meta: bool,
}
/// Typed reason a source cannot answer a request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnsupportedReason {
UnknownPurlType(String),
NoVersions,
NoArtifacts,
NoStatuses,
Unavailable(&'static str),
}
/// Source adapter contract. Implementations are injected with an HTTP client
/// and must not call the network from tests directly.
#[async_trait]
pub trait ReleaseSource: Send + Sync {
fn name(&self) -> &'static str;
fn capabilities(&self) -> SourceCapabilities;
async fn versions(&self, project: &ProjectRef) -> Result<Vec<VersionInfo>>;
async fn artifacts(
&self,
project: &ProjectRef,
version: &VersionInfo,
) -> Result<Vec<ArtifactInfo>>;
async fn statuses(
&self,
project: &ProjectRef,
version: &VersionInfo,
) -> Result<Vec<StatusInfo>>;
async fn project_meta(&self, project: &ProjectRef) -> Result<Value>;
}
/// One purl type's source adapter and deps.dev/deps.dev-style system name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceRegistration {
pub purl_type: &'static str,
pub source: &'static str,
pub release_system: &'static str,
}
/// Every registered purl type. deps.dev systems exist for the first seven;
/// the rest are source adapters that do not use deps.dev.
pub const SOURCE_REGISTRY: &[SourceRegistration] = &[
SourceRegistration {
purl_type: "npm",
source: "npm",
release_system: "NPM",
},
SourceRegistration {
purl_type: "pypi",
source: "pypi",
release_system: "PYPI",
},
SourceRegistration {
purl_type: "maven",
source: "maven",
release_system: "MAVEN",
},
SourceRegistration {
purl_type: "nuget",
source: "nuget",
release_system: "NUGET",
},
SourceRegistration {
purl_type: "gem",
source: "gem",
release_system: "RUBYGEMS",
},
SourceRegistration {
purl_type: "cargo",
source: "cargo",
release_system: "CARGO",
},
SourceRegistration {
purl_type: "golang",
source: "golang",
release_system: "GO",
},
SourceRegistration {
purl_type: "rpm",
source: "distro",
release_system: "",
},
SourceRegistration {
purl_type: "deb",
source: "distro",
release_system: "",
},
SourceRegistration {
purl_type: "apk",
source: "distro",
release_system: "",
},
SourceRegistration {
purl_type: "github",
source: "github",
release_system: "",
},
SourceRegistration {
purl_type: "gitlab",
source: "gitlab",
release_system: "",
},
SourceRegistration {
purl_type: "bitbucket",
source: "bitbucket",
release_system: "",
},
SourceRegistration {
purl_type: "composer",
source: "composer",
release_system: "",
},
SourceRegistration {
purl_type: "cpan",
source: "cpan",
release_system: "",
},
SourceRegistration {
purl_type: "apache",
source: "apache",
release_system: "",
},
SourceRegistration {
purl_type: "gitee",
source: "gitee",
release_system: "",
},
SourceRegistration {
purl_type: "docker",
source: "docker",
release_system: "",
},
SourceRegistration {
purl_type: "oci",
source: "docker",
release_system: "",
},
SourceRegistration {
purl_type: "sourceforge",
source: "sourceforge",
release_system: "",
},
SourceRegistration {
purl_type: "gnu",
source: "gnu",
release_system: "",
},
];
pub fn source_for(purl_type: &str) -> Option<&'static SourceRegistration> {
SOURCE_REGISTRY
.iter()
.find(|entry| entry.purl_type == purl_type)
}
/// Credentials for source adapters. Values are never logged.
#[derive(Clone, Default)]
pub struct ReleaseTokens {
pub github: Option<String>,
pub gitlab: Option<String>,
pub gitee: Option<String>,
}
/// Factory options: credentials plus the optional local/mirror base URL used
/// by the adapters that expose a configurable endpoint.
#[derive(Clone, Default)]
pub struct SourceOptions {
pub tokens: ReleaseTokens,
pub base_url: Option<String>,
}
impl SourceOptions {
fn base_url(&self) -> Option<&str> {
self.base_url
.as_deref()
.map(str::trim)
.filter(|url| !url.is_empty())
}
}
/// Transports for the adapters that expose a custom HTTP seam. `build_source`
/// fills every field with the production transport; tests inject fakes.
#[derive(Clone, Default)]
pub struct SourceTransports {
pub git: Option<Arc<dyn GitHttp>>,
pub manifest: Option<Arc<dyn ManifestHttp>>,
pub docker: Option<Arc<dyn DockerHttp>>,
}
impl SourceTransports {
/// Production transports for git hosting, OCI manifests, and Docker Hub.
pub fn production() -> Result<Self> {
Ok(Self {
git: Some(Arc::new(ReqwestGitHttp::new()?)),
manifest: Some(Arc::new(ReqwestManifestHttp::new()?)),
docker: Some(Arc::new(ReqwestDockerHttp::new()?)),
})
}
fn git(&self) -> Result<Arc<dyn GitHttp>, UnsupportedReason> {
self.git.clone().ok_or(UnsupportedReason::Unavailable(
"git transport is not configured",
))
}
fn manifest(&self) -> Result<Arc<dyn ManifestHttp>, UnsupportedReason> {
self.manifest.clone().ok_or(UnsupportedReason::Unavailable(
"manifest transport is not configured",
))
}
fn docker(&self) -> Result<Arc<dyn DockerHttp>, UnsupportedReason> {
self.docker.clone().ok_or(UnsupportedReason::Unavailable(
"docker transport is not configured",
))
}
}
/// Construct the adapter for one purl type with production transports.
pub fn build_source(
purl_type: &str,
http: Arc<dyn RegistryHttp>,
options: &SourceOptions,
) -> Result<Box<dyn ReleaseSource>, UnsupportedReason> {
let transports = SourceTransports::production()
.map_err(|_| UnsupportedReason::Unavailable("source transport is unavailable"))?;
build_source_with_transports(purl_type, http, options, &transports)
}
/// Construct the adapter for one purl type with injected transports.
///
/// Every registered adapter is constructed here; unknown and not-yet-adapted
/// purl types return a typed [`UnsupportedReason`].
pub fn build_source_with_transports(
purl_type: &str,
http: Arc<dyn RegistryHttp>,
options: &SourceOptions,
transports: &SourceTransports,
) -> Result<Box<dyn ReleaseSource>, UnsupportedReason> {
let Some(registration) = source_for(purl_type) else {
return Err(UnsupportedReason::UnknownPurlType(purl_type.to_owned()));
};
let base = options.base_url();
let source: Box<dyn ReleaseSource> = match registration.source {
"pypi" => Box::new(PyPiSource::new(http)),
"cargo" => Box::new(CargoSource::new(http)),
"gem" => Box::new(GemSource::new(http)),
"github" => Box::new(GithubSource::new(
transports.git()?,
options.tokens.github.clone(),
)),
"gitlab" => Box::new(GitlabSource::new(
transports.git()?,
options.tokens.gitlab.clone(),
)),
"bitbucket" => Box::new(BitbucketSource::new(transports.git()?)),
"distro" => Box::new(DistroSource::with_clients(http, transports.manifest()?)),
"composer" => Box::new(match base {
Some(base) => ComposerSource::with_base_url(http, base),
None => ComposerSource::new(http),
}),
"cpan" => Box::new(match base {
Some(base) => CpanSource::with_base_url(http, base),
None => CpanSource::new(http),
}),
"apache" => Box::new(match base {
Some(base) => ApacheSource::with_bases(http, base, base),
None => ApacheSource::new(http),
}),
"gitee" => {
let source = match base {
Some(base) => GiteeSource::with_base_url(http, base),
None => GiteeSource::new(http),
};
Box::new(match options.tokens.gitee.clone() {
Some(token) => source.with_token(token),
None => source,
})
}
"gnu" => Box::new(match base {
Some(base) => GnuSource::with_base_url(http, base),
None => GnuSource::new(http),
}),
"docker" => Box::new(DockerSource::new(transports.docker()?)),
"sourceforge" => Box::new(match base {
Some(base) => SourceforgeSource::with_bases(http, base, base),
None => SourceforgeSource::new(http),
}),
"npm" => Box::new(NpmSource::new(http)),
"maven" => Box::new(MavenSource::new(http)),
"nuget" => Box::new(NugetSource::new(http)),
"golang" => Box::new(GolangSource::new(http)),
other => return Err(UnsupportedReason::UnknownPurlType(other.to_owned())),
};
Ok(source)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::constants::{RELEASE_SYSTEMS, release_system};
#[test]
fn registry_covers_release_systems_and_adapters() {
// Every deps.dev-backed release system has a registry entry.
for (purl_type, system) in RELEASE_SYSTEMS {
let entry = source_for(purl_type).unwrap_or_else(|| panic!("missing {purl_type}"));
assert_eq!(entry.release_system, *system, "{purl_type}");
}
for entry in SOURCE_REGISTRY {
assert_eq!(source_for(entry.purl_type), Some(entry));
if entry.release_system.is_empty() {
assert_eq!(release_system(entry.purl_type), None, "{}", entry.purl_type);
} else {
assert_eq!(
release_system(entry.purl_type),
Some(entry.release_system),
"{}",
entry.purl_type
);
}
}
assert!(source_for("unknown-ecosystem").is_none());
}
}