mithril_aggregator/services/snapshotter/ancillary_signer/
with_secret_key.rs

1use async_trait::async_trait;
2use slog::{debug, Logger};
3
4use mithril_common::crypto_helper::{ManifestSignature, ManifestSigner};
5use mithril_common::entities::AncillaryFilesManifest;
6use mithril_common::logging::LoggerExtensions;
7use mithril_common::StdResult;
8
9use super::AncillarySigner;
10
11/// Ancillary signer that uses an in memory secret key to sign the ancillary manifest.
12pub struct AncillarySignerWithSecretKey {
13    signer: ManifestSigner,
14    logger: Logger,
15}
16
17impl AncillarySignerWithSecretKey {
18    /// Create a new instance of `AncillarySignerWithSecretKey`.
19    pub fn new(signer: ManifestSigner, logger: Logger) -> Self {
20        Self {
21            signer,
22            logger: logger.new_with_component_name::<Self>(),
23        }
24    }
25}
26
27#[async_trait]
28impl AncillarySigner for AncillarySignerWithSecretKey {
29    async fn compute_ancillary_manifest_signature(
30        &self,
31        manifest: &AncillaryFilesManifest,
32    ) -> StdResult<ManifestSignature> {
33        debug!(self.logger, ">> compute_ancillary_manifest_signature");
34        let manifest_hash = manifest.compute_hash();
35        Ok(self.signer.sign(&manifest_hash))
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use std::collections::BTreeMap;
42    use std::path::PathBuf;
43
44    use crate::test_tools::TestLogger;
45
46    use super::*;
47
48    #[tokio::test]
49    async fn computed_signature_signs_manifest_hash() {
50        let manifest = AncillaryFilesManifest {
51            data: BTreeMap::from([(PathBuf::from("path/whatever"), "whatever_hash".to_string())]),
52            signature: None,
53        };
54
55        let signer = ManifestSigner::create_deterministic_signer();
56        let verifier = signer.create_verifier();
57        let ancillary_signer = AncillarySignerWithSecretKey::new(signer, TestLogger::stdout());
58
59        let signature = ancillary_signer
60            .compute_ancillary_manifest_signature(&manifest)
61            .await
62            .unwrap();
63
64        verifier
65            .verify(&manifest.compute_hash(), &signature)
66            .expect("signature should be valid");
67    }
68}