Skip to main content

signstar_yubihsm2/automation/
scenario.rs

1//! Provisioning scenarios.
2
3#[cfg(all(feature = "serde", feature = "cli"))]
4use serde::Deserialize;
5
6use crate::automation::command::AuthenticatedCommandChain;
7#[cfg(feature = "cli")]
8use crate::{
9    Credentials,
10    automation::{Command, command::FileBackedAuthenticatedCommandChain},
11};
12
13/// A list of authenticated chains of commands executed against a YubiHSM2.
14///
15/// Each chain of commands is authenticated using in-memory credentials.
16#[derive(Debug)]
17pub struct Scenario(Vec<AuthenticatedCommandChain>);
18
19impl Scenario {
20    /// Creates a new [`Scenario`] from a list of [`AuthenticatedCommandChain`] objects.
21    pub fn new(chains: Vec<AuthenticatedCommandChain>) -> Self {
22        Self(chains)
23    }
24}
25
26impl AsRef<[AuthenticatedCommandChain]> for Scenario {
27    fn as_ref(&self) -> &[AuthenticatedCommandChain] {
28        self.0.as_slice()
29    }
30}
31
32/// A list of authenticated chains of commands executed against a YubiHSM2.
33///
34/// Each chain of commands is authenticated using file-backed credentials.
35#[cfg(feature = "cli")]
36#[cfg_attr(feature = "serde", derive(Deserialize))]
37#[derive(Debug)]
38pub struct FileBackedScenario(Vec<FileBackedAuthenticatedCommandChain>);
39
40#[cfg(feature = "cli")]
41impl AsRef<[FileBackedAuthenticatedCommandChain]> for FileBackedScenario {
42    fn as_ref(&self) -> &[FileBackedAuthenticatedCommandChain] {
43        self.0.as_slice()
44    }
45}
46
47#[cfg(feature = "cli")]
48impl TryFrom<&FileBackedScenario> for Scenario {
49    type Error = crate::Error;
50
51    fn try_from(value: &FileBackedScenario) -> Result<Self, Self::Error> {
52        let mut output = Vec::new();
53
54        for authenticated_command_chain in value.0.iter() {
55            let creds = Credentials::try_from(&authenticated_command_chain.auth)?;
56            let commands = {
57                let mut commands = Vec::new();
58                for file_backed_command in authenticated_command_chain.commands.iter() {
59                    commands.push(Command::try_from(file_backed_command)?);
60                }
61                commands
62            };
63            output.push(AuthenticatedCommandChain::new(creds, commands));
64        }
65
66        Ok(Self(output))
67    }
68}