ostrya 0.3.1

Pure-Rust async reimplementation of the ostree repository library
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! The receive-side constructors of the trusted keys: the keys of a remote and
//! the keys of a trust group.

use ostrya_core::{KeyFile, Value};

use crate::config::{Remote, SignVerify};
use crate::error::{Error, Result};
use crate::repo::Repo;
use crate::verify::{KeySource, TrustedKeys, Verifiers, build_policy};

impl TrustedKeys {
    /// Creates the trusted keys that a pull from `remote` uses for a commit.
    ///
    /// The keys come from the `[remote "NAME"]` section of the repository
    /// config. The call reads each key source as a pull reads it (see
    /// [`PullVerify`](crate::PullVerify)):
    ///
    /// - `gpg-verify`, with the default `true`
    /// - `gpgkeypath`
    /// - the repository keyring `<repo>/NAME.trustedkeys.gpg`
    /// - the system keyring `/etc/ostree/remotes.d/NAME.trustedkeys.gpg`
    /// - the global GPG trusted directory
    /// - `sign-verify` and the `verification-*` keys
    /// - the system sign-api key store, minus its revoked set
    ///
    /// [`PullTrust::from_remote`](crate::verify::PullTrust::from_remote) reads
    /// the trust of a whole pull. This call is different in three ways: it
    /// needs a section for `remote`, it refuses a section that turns on no
    /// check, and it reads no summary keys and no `PullVerify`.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidFormat`] if the config has no section for `remote`.
    /// - [`Error::InvalidFormat`] if the section turns on no axis: it sets
    ///   `gpg-verify=false` and no `sign-verify`.
    /// - [`Error::Core`] if `gpg-verify` is not a boolean, if a value holds a
    ///   malformed escape sequence, or if a sign-api key is not valid base64.
    /// - [`Error::Signature`] if `sign-verify` names an engine that has no
    ///   trusted key, or if `sign-verify=true` finds no engine with a trusted
    ///   key.
    /// - [`Error::Signature`] if a keyring or a key file cannot be read, or if
    ///   its content is not valid for its engine.
    /// - [`Error::Unsupported`] if `sign-verify` names an engine that this
    ///   build does not verify with.
    /// - [`Error::Unsupported`] if the remote asks for GPG verification and the
    ///   build has no `verify-gpg` feature.
    /// - [`Error::Io`] if a directory of keys cannot be listed.
    pub async fn for_remote(repo: &Repo, remote: &str) -> Result<TrustedKeys> {
        TrustedKeys::for_remote_in(repo, repo.config().keyfile(), remote, true).await
    }

    /// Creates the pull trust of the `[remote "NAME"]` section of `keyfile`.
    ///
    /// If `repo_keyring` is `true`, the repository keyring of the remote is
    /// part of the trust.
    pub(crate) async fn for_remote_in(
        repo: &Repo,
        keyfile: &KeyFile,
        remote: &str,
        repo_keyring: bool,
    ) -> Result<TrustedKeys> {
        let Some(section) = Remote::in_keyfile(keyfile, remote) else {
            return Err(Error::InvalidFormat(format!(
                "remote:{remote} names no [remote \"{remote}\"] section"
            )));
        };
        let gpg = section.gpg_verify()?;
        let sign = section.sign_verify()?;
        if !gpg && sign == SignVerify::Off {
            return Err(Error::InvalidFormat(format!(
                "remote:{remote} turns on no signature check: the remote sets \
                 gpg-verify=false and no sign-verify; write verify=off for no check"
            )));
        }
        let source = KeySource::Remote {
            name: remote,
            section: Some(&section),
            repo_keyring,
        };
        let policy = build_policy(repo, &source, &mut Verifiers::default(), gpg, &sign).await?;
        Ok(TrustedKeys::from_policy(policy))
    }

    /// Creates the keys of the trust group `name` from the key-file group
    /// `group`.
    ///
    /// The policy reader checks the group first. As a result, the group turns
    /// on at least one axis, and an axis reads each key that the group holds.
    pub(crate) async fn for_trust_group(
        repo: &Repo,
        keyfile: &KeyFile,
        name: &str,
        group: &str,
    ) -> Result<TrustedKeys> {
        let section = Remote::view(keyfile, group.to_owned());
        let gpg = keyfile.get_bool(group, "gpg-verify")?.unwrap_or(false);
        let sign = section.sign_verify()?;
        let source = KeySource::Trust {
            name,
            section: &section,
        };
        let policy = build_policy(repo, &source, &mut Verifiers::default(), gpg, &sign).await?;
        Ok(TrustedKeys::from_policy(policy))
    }

    /// Verifies `payload` against every axis of this set.
    ///
    /// `detached` is the detached-metadata dict that holds the signatures. It
    /// is `None` if the payload has no detached metadata. `subject` names the
    /// payload in a message.
    pub(crate) async fn check(
        &self,
        subject: &str,
        payload: &[u8],
        detached: Option<&Value>,
    ) -> Result<()> {
        self.policy().check(subject, payload, detached).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sign::{Ed25519Signer, Signer, append_signature};
    use crate::{CreateOptions, RepoMode};

    /// The base64 of a 64-byte ed25519 secret key (seed, then public key).
    const SECRET_B64: &str =
        "o74ME/dmhvDeYf64dDJQY8kX2piK0M/nyIRWVi30i6DCOzRsHVcvgYToz6zOb5OvK/v8nH6KfLR3dfdsn6ZSyQ==";
    /// The matching 32-byte ed25519 public key.
    const PUBLIC_B64: &str = "wjs0bB1XL4GE6M+szm+Tryv7/Jx+iny0d3X3bJ+mUsk=";
    /// Another ed25519 secret key.
    const OTHER_SECRET_B64: &str =
        "5ILWxT+l9G/u3h0BptRpmSi35C9uog7YDdD+Fp1Xk+Hz52p0NlYh6xBA73kJEJKhKbbnjcE0rsWA5XA/K5Sq5Q==";

    const PAYLOAD: &[u8] = b"the commit bytes";

    /// A scratch directory with an archive repository at `repo`. The guard
    /// removes the directory when it drops.
    struct Scratch {
        dir: std::path::PathBuf,
    }

    impl Scratch {
        fn new(label: &str) -> Scratch {
            let dir = std::env::temp_dir().join(format!(
                "ostrya-trust-{label}-{}-{}",
                std::process::id(),
                crate::write::unique()
            ));
            std::fs::create_dir_all(&dir).unwrap();
            Scratch { dir }
        }

        fn repo(&self) -> Repo {
            ostrya_rt::block_on(Repo::create(
                &self.dir.join("repo"),
                CreateOptions::new(RepoMode::Archive),
            ))
            .unwrap()
        }
    }

    impl Drop for Scratch {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.dir);
        }
    }

    /// A detached-metadata dict that holds `blob` under `key`.
    fn dict_with(key: &str, blob: Vec<u8>) -> Value {
        let mut dict = Value::Array(Vec::new());
        append_signature(&mut dict, key, blob).unwrap();
        dict
    }

    /// The ed25519 signature of `secret` over [`PAYLOAD`], in its dict.
    fn ed25519_signed(secret: &str) -> Value {
        let signer = Ed25519Signer::from_base64(secret).unwrap();
        let blob = ostrya_rt::block_on(signer.sign(PAYLOAD)).unwrap();
        dict_with(signer.metadata_key(), blob)
    }

    /// A trust group verifies a commit against the key that it names. A
    /// signature from this key passes. A commit with no signature fails, and a
    /// signature from another key fails.
    #[test]
    fn a_trust_group_checks_a_commit() {
        let scratch = Scratch::new("group");
        let repo = scratch.repo();
        let group = "ex-ostrya trust \"t\"";
        let keyfile = KeyFile::parse(&format!(
            "[{group}]\nsign-verify=ed25519\nverification-ed25519-key={PUBLIC_B64}\n"
        ))
        .unwrap();
        let keys =
            ostrya_rt::block_on(TrustedKeys::for_trust_group(&repo, &keyfile, "t", group)).unwrap();
        let own = ed25519_signed(SECRET_B64);
        let other = ed25519_signed(OTHER_SECRET_B64);
        let (valid, unsigned, untrusted) = ostrya_rt::block_on(async {
            (
                keys.check("commit", PAYLOAD, Some(&own)).await,
                keys.check("commit", PAYLOAD, None).await,
                keys.check("commit", PAYLOAD, Some(&other)).await,
            )
        });
        valid.expect("a signature from the named key passes");
        let err = unsigned.unwrap_err();
        assert!(err.to_string().contains("carries no signature"), "{err}");
        let err = untrusted.unwrap_err();
        assert!(
            err.to_string()
                .contains("no sign-api signature is from a trusted key"),
            "{err}"
        );
    }

    /// A trust group refuses an engine with no key. The system key store does
    /// not supply a key for the engine.
    #[test]
    fn a_trust_group_engine_needs_a_key() {
        let scratch = Scratch::new("nokey");
        let repo = scratch.repo();
        let group = "ex-ostrya trust \"t\"";
        let keyfile = KeyFile::parse(&format!("[{group}]\nsign-verify=ed25519\n")).unwrap();
        let Err(err) =
            ostrya_rt::block_on(TrustedKeys::for_trust_group(&repo, &keyfile, "t", group))
        else {
            panic!("an engine with no key is refused");
        };
        assert!(
            matches!(&err, Error::Signature(m) if m.contains("no trusted key for signature engine 'ed25519'")),
            "{err}"
        );
    }

    /// The pull trust of a remote reads the keys of its section. The build
    /// refuses a remote that turns on no check, and a remote that the file
    /// does not describe.
    #[test]
    fn the_pull_trust_of_a_remote() {
        let scratch = Scratch::new("remote");
        let repo = scratch.repo();
        let keyfile = KeyFile::parse(&format!(
            "[remote \"origin\"]\nurl=http://localhost/\ngpg-verify=false\n\
             sign-verify=ed25519\nverification-ed25519-key={PUBLIC_B64}\n\
             [remote \"plain\"]\nurl=http://localhost/\ngpg-verify=false\n"
        ))
        .unwrap();
        let keys = ostrya_rt::block_on(TrustedKeys::for_remote_in(&repo, &keyfile, "origin", true))
            .unwrap();
        ostrya_rt::block_on(keys.check("commit", PAYLOAD, Some(&ed25519_signed(SECRET_B64))))
            .expect("a signature from the remote's key passes");
        for remote in ["plain", "absent"] {
            let Err(err) =
                ostrya_rt::block_on(TrustedKeys::for_remote_in(&repo, &keyfile, remote, true))
            else {
                panic!("{remote} is refused");
            };
            assert!(matches!(err, Error::InvalidFormat(_)), "{remote}: {err}");
        }
    }

    /// Returns `true` if `gpg` answers.
    ///
    /// If `gpg` does not answer, a test that needs it skips. If
    /// `OSTRYA_REQUIRE_GNUPG` is set, the absence of `gpg` fails the test.
    #[cfg(feature = "verify-gpg")]
    fn gpg_or_skip() -> bool {
        if crate::gpg::tests::gpg_available() {
            return true;
        }
        assert!(
            std::env::var_os("OSTRYA_REQUIRE_GNUPG").is_none(),
            "OSTRYA_REQUIRE_GNUPG is set and `gpg` is not available"
        );
        eprintln!("skipping: gpg not available");
        false
    }

    /// The pull trust of a remote includes the repository keyring of the
    /// remote only if the source asks for it.
    #[cfg(feature = "verify-gpg")]
    #[test]
    fn the_repository_keyring_takes_part_on_request() {
        use crate::gpg::tests::KeyFixture;

        if !gpg_or_skip() {
            return;
        }
        let key = KeyFixture::new("Central <central@example.org>");
        let signed = dict_with("ostree.gpgsigs", key.sign(PAYLOAD));
        let scratch = Scratch::new("keyring");
        let repo = scratch.repo();
        std::fs::write(
            scratch.dir.join("repo").join("origin.trustedkeys.gpg"),
            key.export(false),
        )
        .unwrap();
        let keyfile = KeyFile::parse("[remote \"origin\"]\nurl=http://localhost/\n").unwrap();
        let check = |repo_keyring: bool| {
            ostrya_rt::block_on(async {
                TrustedKeys::for_remote_in(&repo, &keyfile, "origin", repo_keyring)
                    .await
                    .unwrap()
                    .check("commit", PAYLOAD, Some(&signed))
                    .await
            })
        };
        check(true).expect("the repository keyring holds the key");
        let err = check(false).expect_err("the repository keyring does not take part");
        assert!(matches!(err, Error::Signature(_)), "{err}");
    }

    /// A `remote:` rule from the repository config trusts the repository
    /// keyring of the remote. The same rule from a policy file does not trust
    /// that keyring.
    #[cfg(feature = "verify-gpg")]
    #[test]
    fn a_policy_file_leaves_out_the_repository_keyring() {
        use crate::gpg::tests::KeyFixture;
        use crate::receive::{ReceivePolicy, ReceiveVerify};

        if !gpg_or_skip() {
            return;
        }
        let key = KeyFixture::new("Central <central@example.org>");
        let signed = dict_with("ostree.gpgsigs", key.sign(PAYLOAD));
        let scratch = Scratch::new("policy-keyring");
        scratch.repo();
        let root = scratch.dir.join("repo");
        std::fs::write(root.join("central.trustedkeys.gpg"), key.export(false)).unwrap();
        let groups = "[remote \"central\"]\nurl=http://localhost/\n\
                      [ex-ostrya receive]\nverify=remote:central\n";
        let mut config = std::fs::read_to_string(root.join("config")).unwrap();
        config.push('\n');
        config.push_str(groups);
        std::fs::write(root.join("config"), config).unwrap();
        let file = scratch.dir.join("receive.conf");
        std::fs::write(&file, groups).unwrap();
        let repo = ostrya_rt::block_on(Repo::open(&root)).unwrap();

        let check = |policy: ReceivePolicy| {
            let ReceiveVerify::Keys(keys) = &policy.default_rule.verify else {
                panic!("the default rule checks signatures");
            };
            ostrya_rt::block_on(keys.check("commit", PAYLOAD, Some(&signed)))
        };
        check(ostrya_rt::block_on(ReceivePolicy::from_config(&repo)).unwrap())
            .expect("the repository config trusts the repository keyring");
        let err = check(ostrya_rt::block_on(ReceivePolicy::from_file(&repo, &file)).unwrap())
            .expect_err("a policy file reads no keyring inside the repository");
        assert!(matches!(err, Error::Signature(_)), "{err}");
    }

    /// The environment variable that tells the child process of
    /// [`the_global_gpg_directory_reaches_remotes_alone`] where its fixtures
    /// are.
    #[cfg(feature = "verify-gpg")]
    const FIXTURE_ENV: &str = "OSTRYA_TRUST_TEST_FIXTURES";

    /// The global GPG trusted directory, which `OSTREE_GPG_HOME` names, is part
    /// of the pull trust of a remote. It is not part of a trust group. The test
    /// sets the environment for a child process, so no test of this process
    /// reads the directory.
    #[cfg(feature = "verify-gpg")]
    #[test]
    fn the_global_gpg_directory_reaches_remotes_alone() {
        use crate::gpg::tests::KeyFixture;

        if !gpg_or_skip() {
            return;
        }
        let key = KeyFixture::new("Global <global@example.org>");
        let stranger = KeyFixture::new("Stranger <stranger@example.org>");
        let scratch = Scratch::new("global");
        let global = scratch.dir.join("global");
        std::fs::create_dir(&global).unwrap();
        std::fs::write(global.join("global.gpg"), key.export(false)).unwrap();
        std::fs::write(scratch.dir.join("stranger.gpg"), stranger.export(false)).unwrap();
        std::fs::write(scratch.dir.join("signature"), key.sign(PAYLOAD)).unwrap();

        let child = std::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "receive::trust::tests::global_gpg_directory_subprocess",
                "--exact",
                "--ignored",
                "--nocapture",
            ])
            .env("OSTREE_GPG_HOME", &global)
            .env(FIXTURE_ENV, &scratch.dir)
            .output()
            .expect("re-execute this test binary");
        let stdout = String::from_utf8_lossy(&child.stdout);
        assert!(
            child.status.success() && stdout.contains("1 passed"),
            "the child reported {}:\n{stdout}{}",
            child.status,
            String::from_utf8_lossy(&child.stderr),
        );
    }

    /// The half of [`the_global_gpg_directory_reaches_remotes_alone`] that
    /// builds the trusted keys. It runs only if this test binary runs again
    /// with the environment set.
    #[cfg(feature = "verify-gpg")]
    #[test]
    #[ignore = "helper process for the_global_gpg_directory_reaches_remotes_alone"]
    fn global_gpg_directory_subprocess() {
        let Some(fixtures) = std::env::var_os(FIXTURE_ENV).map(std::path::PathBuf::from) else {
            return;
        };
        let signed = dict_with(
            "ostree.gpgsigs",
            std::fs::read(fixtures.join("signature")).unwrap(),
        );
        let scratch = Scratch::new("global-child");
        let repo = scratch.repo();
        let group = "ex-ostrya trust \"t\"";
        let keyfile = KeyFile::parse(&format!(
            "[remote \"origin\"]\nurl=http://localhost/\n\
             [{group}]\ngpg-verify=true\ngpgkeypath={}\n",
            fixtures.join("stranger.gpg").display()
        ))
        .unwrap();
        ostrya_rt::block_on(async {
            TrustedKeys::for_remote_in(&repo, &keyfile, "origin", true)
                .await
                .unwrap()
                .check("commit", PAYLOAD, Some(&signed))
                .await
                .expect("the global directory holds the key");
            let err = TrustedKeys::for_trust_group(&repo, &keyfile, "t", group)
                .await
                .unwrap()
                .check("commit", PAYLOAD, Some(&signed))
                .await
                .expect_err("a trust group reads no global directory");
            assert!(matches!(err, Error::Signature(_)), "{err}");
        });
    }
}