wdl-modules 0.2.1

Implementation of the WDL module specification
Documentation
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
404
405
406
407
408
409
410
//! Top-level error type for the resolver layer.

use std::path::PathBuf;

use semver::Version;
use thiserror::Error;

use crate::hash::ContentHash;
use crate::hash::HashError;
use crate::lockfile::LockfileError;
use crate::manifest::ManifestError;
use crate::module_walk::ModuleWalkError;
use crate::signing::VerifyingKey;
use crate::version_requirement::VersionRequirement;

/// An error returned by the [`Resolver`](crate::Resolver) trait or
/// any resolver-layer operation.
#[derive(Debug, Error)]
pub enum ResolverError {
    /// The symbolic path's head does not appear in the consumer's
    /// `dependencies` map.
    #[error("`{name}` is not a declared dependency")]
    NotADependency {
        /// The undeclared dependency name.
        name: String,
    },

    /// A required file was not found, or was excluded.
    #[error("{}", missing_file_message(.dep, .path, .kind))]
    MissingFile {
        /// The owning dependency.
        dep: String,
        /// The relative path that was looked up.
        path: PathBuf,
        /// Which kind of lookup failed.
        kind: MissingFileKind,
    },

    /// A path-prefixed Git tag's `module.json` declares a different
    /// version than the tag itself.
    #[error("tag `{tag}` points to a `module.json` declaring version `{declared}`")]
    TagManifestMismatch {
        /// The tag name (after stripping any path prefix).
        tag: String,
        /// The version declared in the tagged commit's `module.json`.
        declared: Version,
    },

    /// The dependency graph contains a cycle.
    #[error("dependency cycle: {}", format_cycle(.path))]
    Cycle {
        /// The cycle path, in resolution order.
        path: Vec<String>,
    },

    /// No discovered version satisfies the dependency's version
    /// requirement.
    #[error(
        "no version satisfies `{dep}` requirement `{requirement}` (considered: {})",
        format_versions(.considered)
    )]
    NoSatisfyingVersion {
        /// The dependency name.
        dep: String,
        /// The unmet version requirement.
        requirement: VersionRequirement,
        /// The versions discovered before filtering by the requirement.
        considered: Vec<Version>,
    },

    /// A dependency is not present in the lockfile. Run
    /// `sprocket module lock` to update it.
    #[error("`{dep}` is not in `module-lock.json`; run `sprocket module lock` to update")]
    NotInLockfile {
        /// The missing dependency.
        dep: String,
    },

    /// The manifest source for a dependency does not match the
    /// lockfile source. Run `sprocket module lock` to update.
    #[error(
        "`{dep}` manifest source differs from the lockfile; run `sprocket module lock` to update"
    )]
    LockfileSourceMismatch {
        /// The dependency whose source changed.
        dep: String,
    },

    /// A cached module's content hash does not match the lockfile's
    /// recorded checksum.
    #[error(
        "cached `{dep}` content hash does not match the lockfile (expected `{expected}`, observed \
         `{observed}`)"
    )]
    ChecksumMismatch {
        /// The owning dependency.
        dep: String,
        /// The hash recorded in the lockfile.
        expected: ContentHash,
        /// The hash observed in the cache.
        observed: ContentHash,
    },

    /// A cached module's signature key does not match the lockfile's
    /// recorded signer.
    #[error(
        "signer for `{dep}` has changed since the lockfile was written (run `sprocket module \
         trust {dep}` to accept the new key)"
    )]
    SignerKeyMismatch {
        /// The owning dependency.
        dep: String,
        /// The signer key recorded in the lockfile.
        expected: Box<VerifyingKey>,
        /// The signer key observed in the cache.
        observed: Box<VerifyingKey>,
    },

    /// A dependency was signed when the lockfile was written but is now
    /// unsigned. This prevents a supply-chain downgrade where an
    /// attacker strips the signature from a module whose content hash
    /// has not changed (since `module.sig` is excluded from the hash).
    #[error("`{dep}` was signed when locked but is now unsigned; this may indicate tampering")]
    SignatureDowngrade {
        /// The owning dependency.
        dep: String,
        /// The signer key recorded in the lockfile.
        expected_signer: Box<VerifyingKey>,
    },

    /// A Git tag or branch named in a dependency's selector does not
    /// exist on the remote.
    #[error("`{dep}` selector references unknown {kind} `{name}`")]
    UnknownGitRef {
        /// The owning dependency.
        dep: String,
        /// The kind of ref that was missing.
        kind: GitRefKind,
        /// The ref name as it appeared in the manifest.
        name: String,
    },

    /// A `commit` selector did not parse as a valid 40-character lowercase
    /// hex SHA.
    #[error("`{dep}` `commit` value `{value}` is not a valid Git commit SHA")]
    InvalidCommit {
        /// The owning dependency.
        dep: String,
        /// The unparsable value.
        value: String,
    },

    /// A `module.sig` file was present but failed to verify against the
    /// observed content hash.
    #[error(
        "`{dep}` signature does not match observed content (signer: `{}`)",
        signer.to_openssh()
    )]
    SignatureVerificationFailed {
        /// The owning dependency.
        dep: String,
        /// The signer key from the rejected `module.sig`.
        signer: Box<VerifyingKey>,
    },

    /// A `module.sig` file failed to parse.
    #[error("`{dep}` `module.sig` failed to parse")]
    SignatureParse {
        /// The owning dependency.
        dep: String,
        /// The underlying parse error.
        #[source]
        source: crate::signing::SignatureFileError,
    },

    /// A manifest `exclude` pattern is not a valid glob.
    #[error("invalid `exclude` pattern `{pattern}`")]
    InvalidExclude {
        /// The offending pattern.
        pattern: String,
        /// The underlying glob error.
        #[source]
        source: globset::Error,
    },

    /// `require_signed` is enabled and the dependency is unsigned.
    #[error("`{dep}` is unsigned but `require_signed` is enabled")]
    RequireSignedViolation {
        /// The unsigned dependency.
        dep: String,
    },

    /// A transitive dependency declared a local-path source from a
    /// non-local parent.
    #[error(
        "`{dep}` declares a local-path source but is reachable through a non-local parent; only \
         locally-rooted projects may use local-path dependencies"
    )]
    LocalPathInTransitive {
        /// The offending dependency name.
        dep: String,
    },

    /// A dependency declared by the consumer was missing from the
    /// freshly-resolved tree and not satisfied by the prior lockfile.
    #[error("`{dep}` is declared by the consumer but absent from the freshly-resolved tree")]
    MissingFreshDependency {
        /// The missing dependency name.
        dep: String,
    },

    /// A Git URL violates the configured scheme policy.
    #[error("`{dep}` git URL `{url}` uses scheme `{scheme}` which is not allowed by policy")]
    GitUrlPolicyViolation {
        /// The owning dependency.
        dep: String,
        /// The rejected URL.
        url: String,
        /// The rejected scheme.
        scheme: String,
    },

    /// DNS resolution for a Git URL's hostname failed. The resolver
    /// rejects the URL rather than allowing a potentially spoofed host
    /// through.
    #[error("`{dep}` git URL `{url}` host `{host}` could not be resolved")]
    GitHostResolutionFailed {
        /// The owning dependency.
        dep: String,
        /// The URL that failed resolution.
        url: String,
        /// The hostname that could not be resolved.
        host: String,
    },

    /// A Git URL violates the configured host policy.
    #[error("`{dep}` git URL `{url}` targets host `{host}` which is not allowed by policy")]
    GitHostPolicyViolation {
        /// The owning dependency.
        dep: String,
        /// The rejected URL.
        url: String,
        /// The rejected host.
        host: String,
    },

    /// A Git URL's host is not in the configured allow list for its scope.
    #[error(
        "`{dep}` git URL `{url}` targets host `{host}` which is not in the configured allow list; \
         to allow it, add `{host}` to `{config_key}` in the `[modules]` section of your \
         `sprocket.toml`"
    )]
    GitHostNotAllowed {
        /// The owning dependency.
        dep: String,
        /// The rejected URL.
        url: String,
        /// The rejected host.
        host: String,
        /// The config key that would permit the host for this scope.
        config_key: &'static str,
    },

    /// A materialized module tree exceeded configured resource limits.
    #[error("`{dep}` materialized tree exceeds limits (files: {files}, bytes: {bytes})")]
    MaterializedTreeLimitExceeded {
        /// The owning dependency.
        dep: String,
        /// Number of files observed.
        files: usize,
        /// Total bytes observed.
        bytes: u64,
    },

    /// A Git operation failed.
    #[error(transparent)]
    Git(#[from] crate::resolver::git::GitError),

    /// A materialized file resolved through a symlink that escapes the
    /// module root.
    #[error("`{dep}` materialized path escapes module root: `{path}`")]
    MaterializedSymlinkEscape {
        /// The owning dependency.
        dep: String,
        /// The escaping path as observed before canonicalization.
        path: PathBuf,
    },

    /// An I/O error.
    #[error("i/o error at `{path}`")]
    Io {
        /// The path involved.
        path: PathBuf,
        /// The underlying I/O error.
        #[source]
        source: std::io::Error,
    },

    /// A module-walk error (symlink containment, metadata target, etc.).
    #[error(transparent)]
    Walk(#[from] ModuleWalkError),

    /// Hashing a cache leaf or local path failed.
    #[error(transparent)]
    Hash(#[from] HashError),

    /// A `Manifest` parse or validation error.
    #[error(transparent)]
    Manifest(#[from] ManifestError),

    /// A `Lockfile` parse or validation error.
    #[error(transparent)]
    Lockfile(#[from] LockfileError),

    /// A `RelativePath` validation error.
    #[error(transparent)]
    RelativePath(#[from] crate::relative_path::RelativePathError),
}

/// The kind of Git reference named in a [`ResolverError::UnknownGitRef`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GitRefKind {
    /// The reference was an annotated or lightweight tag.
    Tag,
    /// The reference was a branch (head).
    Branch,
}

impl std::fmt::Display for GitRefKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Tag => f.write_str("tag"),
            Self::Branch => f.write_str("branch"),
        }
    }
}

/// The kind of file lookup that failed in a [`ResolverError::MissingFile`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MissingFileKind {
    /// The dependency's entrypoint file (manifest `entrypoint` or default
    /// `index.wdl`) was missing.
    Entrypoint,
    /// The symbolic-import sub-path resolved to a file that does not
    /// exist.
    SubPath,
    /// The path exists but the manifest's `exclude` masks it.
    Excluded,
}

/// Renders the message for a [`ResolverError::MissingFile`].
fn missing_file_message(dep: &str, path: &std::path::Path, kind: &MissingFileKind) -> String {
    let p = path.display();
    match kind {
        MissingFileKind::Entrypoint => {
            format!("`{dep}` declares entrypoint `{p}` but the file does not exist")
        }
        MissingFileKind::SubPath => format!("`{dep}/{p}` not found"),
        MissingFileKind::Excluded => format!("`{dep}/{p}` is excluded by the module manifest"),
    }
}

/// Renders a cycle path as a chain of arrows for error display.
fn format_cycle(path: &[String]) -> String {
    path.join("")
}

/// Renders a list of versions for error display, or `<none>` when empty.
fn format_versions(versions: &[Version]) -> String {
    if versions.is_empty() {
        return "<none>".to_string();
    }
    versions
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
        .join(", ")
}

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

    fn dep() -> String {
        "foo".to_string()
    }

    #[test]
    fn missing_file_kind_renders_distinctly() {
        let entry = ResolverError::MissingFile {
            dep: dep(),
            path: "index.wdl".into(),
            kind: MissingFileKind::Entrypoint,
        };
        let sub = ResolverError::MissingFile {
            dep: dep(),
            path: "missing.wdl".into(),
            kind: MissingFileKind::SubPath,
        };
        let excl = ResolverError::MissingFile {
            dep: dep(),
            path: "internal/x.wdl".into(),
            kind: MissingFileKind::Excluded,
        };

        assert!(entry.to_string().contains("entrypoint"));
        assert!(sub.to_string().contains("not found"));
        assert!(excl.to_string().contains("excluded"));
    }
}