credence_lib/configuration/port/
key.rs

1use super::{super::error::*, loadable_bytes::*};
2
3use {bytes::*, compris::resolve::*, kutil_cli::debug::*, std::path::*};
4
5//
6// Key
7//
8
9/// TLS key.
10#[derive(Clone, Debug, Debuggable, Default, Resolve)]
11pub struct Key {
12    /// Certificates PEM.
13    #[resolve(required)]
14    #[debuggable(as(display), style(symbol))]
15    pub certificates: LoadableBytes,
16
17    /// Private key PEM.
18    #[resolve(required, key = "private-key")]
19    #[debuggable(as(display), style(symbol))]
20    pub private_key: LoadableBytes,
21}
22
23impl Key {
24    /// Validate.
25    pub fn validate<PathT>(&mut self, base_path: PathT) -> Result<(), ConfigurationError>
26    where
27        PathT: AsRef<Path>,
28    {
29        if let LoadableBytes::Path(path) = &mut self.certificates {
30            if !path.is_absolute() {
31                *path = base_path.as_ref().join(&path);
32            }
33        }
34
35        if let LoadableBytes::Path(path) = &mut self.private_key {
36            if !path.is_absolute() {
37                *path = base_path.as_ref().join(&path);
38            }
39        }
40
41        Ok(())
42    }
43
44    /// Ensures both certificates and private key are loaded.
45    pub fn to_bytes(&self) -> Result<(Bytes, Bytes), ConfigurationError> {
46        Ok((self.certificates.to_bytes()?, self.private_key.to_bytes()?))
47    }
48}