Skip to main content

compose_validatr/
secrets.rs

1//! Secret fields and validation
2
3use crate::compose::Compose;
4use serde::{Deserialize, Serialize};
5
6use crate::{compose::Validate, errors::ValidationErrors};
7
8/// Represents the top level [Secrets](https://docs.docker.com/compose/compose-file/09-secrets/) element
9#[derive(Debug, Clone, Deserialize, Serialize)]
10pub struct Secret {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub file: Option<String>,
13
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub environment: Option<String>,
16
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub external: Option<bool>,
19
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub name: Option<String>,
22}
23
24impl Validate for Secret {
25    fn validate(&self, _: &Compose, _: &mut ValidationErrors) {
26        // Nothing to validate
27        // Not interested in validating the existence of files on host
28        ()
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use serde_yaml;
36
37    #[test]
38    fn test_deserialize_secrets() {
39        let yaml = r#"
40        file: "path/to/secret1"
41        environment: "ENV_VAR"
42        external: true
43        name: "named_secret"
44        "#;
45
46        let secrets: Secret = serde_yaml::from_str(yaml).unwrap();
47
48        assert_eq!(secrets.file.unwrap(), "path/to/secret1".to_string());
49        assert_eq!(secrets.environment.unwrap(), "ENV_VAR".to_string());
50        assert_eq!(secrets.external.unwrap(), true);
51        assert_eq!(secrets.name.unwrap(), "named_secret".to_string());
52    }
53}