-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsecret.rs
149 lines (135 loc) · 4.59 KB
/
secret.rs
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
use super::CustomerKey;
use crate::baseclient;
use crate::baseclient::ClientCoding;
use crate::prelude::*;
use crate::server_suite::config::SERVER_CONFIG;
use crate::util;
use crate::KeyhouseImpl;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Zeroize)]
#[zeroize(drop)]
pub struct Secret {
pub alias: String,
#[serde(
serialize_with = "util::vec_as_base64",
deserialize_with = "util::vec_from_base64"
)]
pub value: Vec<u8>,
pub created_at: u64,
pub updated_at: u64,
#[serde(default)]
#[serde(skip_serializing_if = "String::is_empty")]
pub description: String,
}
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Zeroize)]
#[zeroize(drop)]
pub struct DecodedSecret {
pub alias: String,
pub value: String,
pub created_at: u64,
pub updated_at: u64,
pub description: String,
}
impl DecodedSecret {
pub fn new_base(alias: String, value: String) -> Self {
DecodedSecret {
alias,
value,
created_at: util::epoch(),
updated_at: util::epoch(),
description: String::new(),
}
}
pub fn encode<T: KeyhouseImpl + 'static>(self, key: &CustomerKey) -> Result<Secret> {
let (coder, encoded_key) = key.encode_data_key::<T>()?;
let region_name = &SERVER_CONFIG.get().0.region;
Ok(Secret {
alias: self.alias.clone(),
value: baseclient::encode_data(
coder,
encoded_key,
baseclient::Region(
T::KeyhouseExt::region_from_name(region_name)
.ok_or_else(|| anyhow!("unknown region name in config: {}", region_name))?,
),
self.value.clone().into(),
)?,
created_at: self.created_at,
updated_at: self.updated_at,
description: self.description.clone(),
})
}
pub fn validate_size(&self) -> bool {
self.value.as_bytes().len() <= 1024 * 16
}
}
impl Secret {
pub fn new_base(alias: String) -> Self {
let mut secret: Vec<u8> = vec![0; 32];
rand::thread_rng().fill_bytes(&mut secret[..]);
Secret {
alias,
value: secret,
created_at: util::epoch(),
updated_at: util::epoch(),
description: String::new(),
}
}
pub fn empty_decoded(self) -> DecodedSecret {
DecodedSecret {
alias: self.alias.clone(),
value: String::new(),
created_at: self.created_at,
updated_at: self.updated_at,
description: self.description.clone(),
}
}
pub fn decode<T: KeyhouseImpl + 'static>(self, key: &CustomerKey) -> Result<DecodedSecret> {
let mut decoded = baseclient::DecodedData::decode_data(self.value.clone())?;
let region_name = &SERVER_CONFIG.get().0.region;
if decoded.region
!= Some(baseclient::Region(
T::KeyhouseExt::region_from_name(region_name)
.ok_or_else(|| anyhow!("unknown region name in config: {}", region_name))?,
))
{
return Err(anyhow!("invalid region for data key"));
}
let (key_id, data_key) =
CustomerKey::pre_decode_data_key(&decoded.encoded_key.take().unwrap()[..])?;
if key_id != key.id {
return Err(anyhow!(
"payload key id does not match given key id: {} vs {}",
key.id,
key_id
));
}
let decoded = decoded.final_decode(T::ClientCoding::from_source(
&key.decode_data_key::<T>(data_key)?[..],
)?)?;
let value = String::from_utf8_lossy(&decoded[..]).to_string();
Ok(DecodedSecret {
alias: self.alias.clone(),
value,
created_at: self.created_at,
updated_at: self.updated_at,
description: self.description.clone(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_secret_roundtrip() {
let key = CustomerKey::new_base::<()>("test/test".to_string()).unwrap();
let secret =
DecodedSecret::new_base("test/test/test".to_string(), "test secret".to_string());
let encoded_secret = secret.clone().encode::<()>(&key).unwrap();
let decoded_secret = encoded_secret.clone().decode::<()>(&key).unwrap();
assert_eq!(secret, decoded_secret);
assert_ne!(secret.value.clone().into_bytes(), encoded_secret.value);
}
}