cat_gateway/settings/
signed_doc.rs

1//! Command line and environment variable settings for the Catalyst Signed Docs
2
3use std::{str::FromStr, time::Duration};
4
5use super::str_env_var::StringEnvVar;
6
7/// Default number value of `future_threshold`, 30 seconds.
8const DEFAULT_FUTURE_THRESHOLD: Duration = Duration::from_secs(30);
9
10/// Default number value of `past_threshold`, 10 minutes.
11const DEFAULT_PAST_THRESHOLD: Duration = Duration::from_secs(60 * 10);
12
13/// Configuration for the Catalyst Signed Documents validation.
14#[derive(Clone)]
15pub(crate) struct EnvVars {
16    /// The Catalyst Signed Document threshold, document cannot be too far in the future.
17    future_threshold: Duration,
18
19    /// The Catalyst Signed Document threshold, document cannot be too far behind.
20    past_threshold: Duration,
21
22    /// The Catalyst Signed Document Admin Catalyst ID from the `SIGNED_DOC_ADMIN_KEYS`
23    /// env.
24    admin_key: Option<catalyst_signed_doc::CatalystId>,
25}
26
27impl EnvVars {
28    /// Create a config for Catalyst Signed Document validation configuration.
29    pub(super) fn new() -> Self {
30        let future_threshold =
31            StringEnvVar::new_as_duration("SIGNED_DOC_FUTURE_THRESHOLD", DEFAULT_FUTURE_THRESHOLD);
32
33        let past_threshold =
34            StringEnvVar::new_as_duration("SIGNED_DOC_PAST_THRESHOLD", DEFAULT_PAST_THRESHOLD);
35
36        let admin_key = string_to_catalyst_id(
37            &StringEnvVar::new_optional("SIGNED_DOC_ADMIN_KEYS", false)
38                .map(|v| v.as_string())
39                .unwrap_or_default(),
40        );
41
42        if admin_key.is_none() {
43            tracing::error!("Missing or invalid Catalyst ID for Admin. This is required.");
44        }
45
46        Self {
47            future_threshold,
48            past_threshold,
49            admin_key,
50        }
51    }
52
53    /// The Catalyst Signed Document threshold, document cannot be too far in the future
54    /// (in seconds).
55    pub(crate) fn future_threshold(&self) -> Duration {
56        self.future_threshold
57    }
58
59    /// The Catalyst Signed Document threshold, document cannot be too far behind
60    /// (in seconds).
61    pub(crate) fn past_threshold(&self) -> Duration {
62        self.past_threshold
63    }
64
65    /// The Catalyst Signed Document Admin key.
66    #[allow(dead_code)]
67    pub(crate) fn admin_key(&self) -> Option<&catalyst_signed_doc::CatalystId> {
68        self.admin_key.as_ref()
69    }
70}
71
72/// Convert an Envvar into the Catalyst ID type, `None` if missing or invalid value.
73fn string_to_catalyst_id(s: &str) -> Option<catalyst_signed_doc::CatalystId> {
74    catalyst_signed_doc::CatalystId::from_str(s)
75        .inspect_err(|err| {
76            tracing::error!(
77                err = ?err,
78                "Cannot parse Admin CatalystId entry"
79            );
80        })
81        .ok()
82}