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
//! This module houses the [`BinarySerializable`] and [`BinaryDeserializable`]
//! traits and implementations for the natively supported primitive secret
//! types.
//!
//! This is intentionally not implemented as a global impl for all
//! `TryFrom<&[u8]>`/`TryInto<Vec<u8>>` as the binary conversion must be stable
//! to survive cold storage. Keep in mind compatibility with other SecureStore
//! clients accessing or setting secrets in the same vault from
//! different languages or frameworks when implementing these traits, if that is
//! a concern.

/// A trait enabling directly saving a secret of the implementing type to the
/// SecureStore vault.
///
/// `BinarySerializable` implementations for `String`, `&str`, `Vec<u8>`, and
/// `&[u8]` are provided by this crate; implement this trait to support directly
/// setting other types via calls to
/// [`SecretsManager::set()`](crate::SecretsManager::set) but make sure to keep
/// compatibility with other clients accessing the same store from different
/// languages or frameworks in mind, if that is a concern.
pub trait BinarySerializable {
    fn serialize<'a>(&'a self) -> &'a [u8];
}

impl BinarySerializable for String {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self.as_bytes()
    }
}

impl BinarySerializable for &str {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self.as_bytes()
    }
}

impl BinarySerializable for Vec<u8> {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self.as_slice()
    }
}

impl BinarySerializable for &[u8] {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self
    }
}

/// A trait enabling directly retrieving a secret from the SecureStore vault as
/// an instance of the implementing type.
///
/// `BinaryDeserializable` implementations for `String` and `Vec<u8>` are
/// provided by this crate; implement this trait to support directly retrieving
/// other owned types via calls to
/// [`SecretsManager::get()`](crate::SecretsManager::set) but make sure to keep
/// compatibility with other clients accessing the same store from different
/// languages or frameworks in mind, if that is a concern.
pub trait BinaryDeserializable
where
    Self: Sized,
{
    type Error: std::error::Error + Send + Sync + 'static;
    fn deserialize(bytes: Vec<u8>) -> Result<Self, Self::Error>;
}

impl BinaryDeserializable for String {
    type Error = std::string::FromUtf8Error;
    fn deserialize(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        Ok(String::from_utf8(bytes)?)
    }
}

impl BinaryDeserializable for Vec<u8> {
    type Error = std::convert::Infallible;
    fn deserialize(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        Ok(bytes)
    }
}