1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Types for working with the secret store

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};
use zeroize::{Zeroize, Zeroizing};

/// A type that is either a reference to a secret, serialized as
/// a URI string like secrets://<key>, or a plain literal.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MaybeSecret<T: Zeroize> {
    Secret(String),
    Plain(Zeroizing<T>),
}

impl<T: Zeroize> MaybeSecret<T> {
    pub fn key(&self) -> Option<String> {
        match self {
            MaybeSecret::Secret(s) => Some(s.clone()),
            MaybeSecret::Plain(_) => None,
        }
    }

    pub fn secret<S: AsRef<str>>(key: S) -> Self {
        MaybeSecret::Secret(key.as_ref().to_string())
    }

    pub fn plain(t: T) -> Self {
        MaybeSecret::Plain(Zeroizing::new(t))
    }
}

impl<T: Clone + Zeroize> MaybeSecret<T> {
    pub fn to_plain(&self) -> Option<Zeroizing<T>> {
        match self {
            MaybeSecret::Secret(_) => None,
            MaybeSecret::Plain(t) => Some(t.clone()),
        }
    }
}

// Most useful implementations of T for MaybeSecret will require
// a FromStr implementation.  If you don't have one handy, use
// this macro to get a reasonable-ish one using serde_json.
#[macro_export]
macro_rules! from_str_json {
    ($t:ty) => {
        impl std::str::FromStr for $t {
            type Err = serde_json::Error;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                serde_json::from_str(s)
            }
        }
    };
}

impl<T: Display + Serialize + Zeroize> Display for MaybeSecret<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &*self {
            MaybeSecret::Secret(s) => write!(f, "secrets://{}", s),
            MaybeSecret::Plain(s) => {
                write!(f, "{}", serde_json::to_string(&**s).map_err(|_| std::fmt::Error)?)
            }
        }
    }
}

impl<T: FromStr + DeserializeOwned + Zeroize> FromStr for MaybeSecret<T> {
    type Err = serde_json::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.starts_with("secrets://") {
            Ok(MaybeSecret::Secret(s[10..].to_string()))
        } else {
            Ok(MaybeSecret::Plain(Zeroizing::new(serde_json::from_str(s)?)))
        }
    }
}

impl<T: Serialize + Zeroize> Serialize for MaybeSecret<T> {
    fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        match self {
            MaybeSecret::Secret(s) => ser.serialize_str(&format!("secrets://{}", s)),
            MaybeSecret::Plain(t) => (&*t).serialize(ser),
        }
    }
}

impl<'de, T: DeserializeOwned + FromStr + Zeroize> Deserialize<'de> for MaybeSecret<T> {
    fn deserialize<D>(de: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Serialize, Deserialize)]
        #[serde(untagged)]
        enum Format<T> {
            SecretOrString(String),
            Plain(T),
        }
        match Format::<T>::deserialize(de)? {
            Format::SecretOrString(s) => {
                if s.starts_with("secrets://") {
                    Ok(MaybeSecret::Secret(s[10..].to_string()))
                } else {
                    // using FromStr here is hacky but it works for the
                    // important cases of T = String, &str, etc... at
                    // the cost of requiring FromStr from structs
                    //
                    // if you're looking for some dumb FromStr to use
                    // try the FromStrJson macro in derive
                    //
                    // maybe there's some trick leveraging auto(de)ref
                    // specialization [https://lukaskalbertodt.github.io/2019/12/05/generalized-autoref-based-specialization.html]
                    // that could help here?
                    Ok(MaybeSecret::Plain(Zeroizing::new(
                        T::from_str(&s)
                            .map_err(|_| serde::de::Error::custom("could not FromStr"))?,
                    )))
                }
            }
            Format::Plain(t) => Ok(MaybeSecret::Plain(Zeroizing::new(t))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use zeroize::ZeroizeOnDrop;

    #[test]
    fn test_from_str() {
        let x: MaybeSecret<u64> = "secrets://foo".parse().unwrap();
        assert_eq!(x, MaybeSecret::secret("foo"));
        let y: MaybeSecret<u64> = "42".parse().unwrap();
        assert_eq!(y, MaybeSecret::plain(42u64));
    }

    #[test]
    fn test_serde() {
        let x: MaybeSecret<u64> = MaybeSecret::secret("asdf");
        let y = serde_json::to_string(&x).unwrap();
        let z = serde_json::from_str(&y).unwrap();
        assert_eq!(x, z);
        let x: MaybeSecret<u64> = MaybeSecret::plain(42);
        let y = serde_json::to_string(&x).unwrap();
        let z = serde_json::from_str(&y).unwrap();
        assert_eq!(x, z);
        let x: MaybeSecret<String> = MaybeSecret::plain("hahaha".to_string());
        let y = serde_json::to_string(&x).unwrap();
        let z = serde_json::from_str(&y).unwrap();
        assert_eq!(x, z);
    }

    #[test]
    fn test_serde_yaml() {
        let x: MaybeSecret<u64> = MaybeSecret::secret("asdf");
        let y = serde_yaml::to_string(&x).unwrap();
        let z = serde_yaml::from_str(&y).unwrap();
        assert_eq!(x, z);
        let x: MaybeSecret<u64> = MaybeSecret::plain(42);
        let y = serde_yaml::to_string(&x).unwrap();
        let z = serde_yaml::from_str(&y).unwrap();
        assert_eq!(x, z);
        let x: MaybeSecret<String> = MaybeSecret::plain("hahaha".to_string());
        let y = serde_yaml::to_string(&x).unwrap();
        let z = serde_yaml::from_str(&y).unwrap();
        assert_eq!(x, z);
    }

    #[test]
    fn test_serde_complex() {
        #[derive(
            Debug, PartialEq, Eq, Serialize, Deserialize, Zeroize, ZeroizeOnDrop,
        )]
        struct Foo {
            bar: u64,
            baz: String,
        }
        from_str_json!(Foo);
        let x: MaybeSecret<Foo> =
            MaybeSecret::plain(Foo { bar: 42, baz: "asdf".to_string() });
        let y = serde_json::to_string(&x).unwrap();
        let z = serde_json::from_str(&y).unwrap();
        assert_eq!(x, z);
        let yy = serde_yaml::to_string(&x).unwrap();
        let zz = serde_yaml::from_str(&yy).unwrap();
        assert_eq!(x, zz);
        let x: MaybeSecret<Foo> = MaybeSecret::secret("my_secret_key");
        let y = serde_json::to_string(&x).unwrap();
        let z = serde_json::from_str(&y).unwrap();
        assert_eq!(x, z);
        let yy = serde_yaml::to_string(&x).unwrap();
        let zz = serde_yaml::from_str(&yy).unwrap();
        assert_eq!(x, zz);
    }
}