openzeppelin_relayer/models/signer/
repository.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use secrets::SecretVec;
use serde::{Deserialize, Serialize, Serializer};

use crate::models::SecretString;

fn serialize_secretvec<S>(_secret: &SecretVec<u8>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_str("[REDACTED]")
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SignerType {
    Test,
    Local,
    AwsKms,
    Vault,
}

#[derive(Debug, Clone, Serialize)]
pub struct SignerRepoModel {
    pub id: String,
    pub config: SignerConfig,
}

#[derive(Debug, Clone, Serialize)]
pub struct LocalSignerConfig {
    #[serde(serialize_with = "serialize_secretvec")]
    pub raw_key: SecretVec<u8>,
}

#[derive(Debug, Clone, Serialize)]
pub struct AwsKmsSignerConfig {}

#[derive(Debug, Clone, Serialize)]
pub struct VaultTransitSignerConfig {
    pub key_name: String,
    pub address: String,
    pub namespace: Option<String>,
    pub role_id: SecretString,
    pub secret_id: SecretString,
    pub pubkey: String,
    pub mount_point: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub enum SignerConfig {
    Test(LocalSignerConfig),
    Local(LocalSignerConfig),
    Vault(LocalSignerConfig),
    VaultCloud(LocalSignerConfig),
    VaultTransit(VaultTransitSignerConfig),
    AwsKms(AwsKmsSignerConfig),
}

impl SignerConfig {
    pub fn get_local(&self) -> Option<&LocalSignerConfig> {
        match self {
            Self::Local(config)
            | Self::Test(config)
            | Self::Vault(config)
            | Self::VaultCloud(config) => Some(config),
            Self::VaultTransit(_) | Self::AwsKms(_) => None,
        }
    }

    pub fn get_aws_kms(&self) -> Option<&AwsKmsSignerConfig> {
        let SignerConfig::AwsKms(config) = self else {
            return None;
        };

        Some(config)
    }

    pub fn get_vault_transit(&self) -> Option<&VaultTransitSignerConfig> {
        let SignerConfig::VaultTransit(config) = self else {
            return None;
        };

        Some(config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{from_str, to_string};

    #[test]
    fn test_signer_type_serialization() {
        assert_eq!(to_string(&SignerType::Test).unwrap(), "\"test\"");
        assert_eq!(to_string(&SignerType::Local).unwrap(), "\"local\"");
        assert_eq!(to_string(&SignerType::AwsKms).unwrap(), "\"awskms\"");
        assert_eq!(to_string(&SignerType::Vault).unwrap(), "\"vault\"");
    }

    #[test]
    fn test_signer_type_deserialization() {
        assert_eq!(
            from_str::<SignerType>("\"test\"").unwrap(),
            SignerType::Test
        );
        assert_eq!(
            from_str::<SignerType>("\"local\"").unwrap(),
            SignerType::Local
        );
        assert_eq!(
            from_str::<SignerType>("\"awskms\"").unwrap(),
            SignerType::AwsKms
        );
        assert_eq!(
            from_str::<SignerType>("\"vault\"").unwrap(),
            SignerType::Vault
        );
    }

    #[test]
    fn test_signer_repo_model_creation() {
        let model = SignerRepoModel {
            id: "test-signer".to_string(),
            config: SignerConfig::Test(LocalSignerConfig {
                raw_key: SecretVec::new(4, |v| v.copy_from_slice(&[1, 2, 3, 4])),
            }),
        };

        assert_eq!(model.id, "test-signer");
        assert!(matches!(model.config, SignerConfig::Test(_)));
    }

    #[test]
    fn test_local_signer_config() {
        let private_key = vec![0, 1, 2, 3, 4, 5];
        let config = LocalSignerConfig {
            raw_key: SecretVec::new(private_key.len(), |v| v.copy_from_slice(&private_key)),
        };

        let test = config.raw_key.borrow();
        assert_eq!(*test, private_key);
    }

    #[test]
    fn test_vault_transit_signer_config() {
        let config = VaultTransitSignerConfig {
            key_name: "transit-key".to_string(),
            address: "https://vault.example.com".to_string(),
            namespace: Some("ns1".to_string()),
            role_id: SecretString::new("role-123"),
            secret_id: SecretString::new("secret-456"),
            pubkey: "mypubkey123".to_string(),
            mount_point: Some("transit".to_string()),
        };

        assert_eq!(config.key_name, "transit-key");
        assert_eq!(config.address, "https://vault.example.com");
        assert_eq!(config.namespace, Some("ns1".to_string()));
        assert_eq!(config.role_id.to_str().as_str(), "role-123");
        assert_eq!(config.secret_id.to_str().as_str(), "secret-456");
        assert_eq!(config.pubkey, "mypubkey123");
        assert_eq!(config.mount_point, Some("transit".to_string()));

        let config2 = VaultTransitSignerConfig {
            key_name: "transit-key".to_string(),
            address: "https://vault.example.com".to_string(),
            namespace: None,
            role_id: SecretString::new("role-123"),
            secret_id: SecretString::new("secret-456"),
            pubkey: "mypubkey123".to_string(),
            mount_point: None,
        };

        assert_eq!(config2.namespace, None);
        assert_eq!(config2.mount_point, None);
    }

    #[test]
    fn test_signer_config_variants() {
        let test_config = SignerConfig::Test(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[1, 2, 3])),
        });

        let local_config = SignerConfig::Local(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[4, 5, 6])),
        });

        let vault_config = SignerConfig::Vault(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[7, 8, 9])),
        });

        let vault_cloud_config = SignerConfig::VaultCloud(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[10, 11, 12])),
        });

        let vault_transit_config = SignerConfig::VaultTransit(VaultTransitSignerConfig {
            key_name: "transit-key".to_string(),
            address: "https://vault.example.com".to_string(),
            namespace: None,
            role_id: SecretString::new("role-123"),
            secret_id: SecretString::new("secret-456"),
            pubkey: "mypubkey123".to_string(),
            mount_point: None,
        });

        let aws_kms_config = SignerConfig::AwsKms(AwsKmsSignerConfig {});

        assert!(matches!(test_config, SignerConfig::Test(_)));
        assert!(matches!(local_config, SignerConfig::Local(_)));
        assert!(matches!(vault_config, SignerConfig::Vault(_)));
        assert!(matches!(vault_cloud_config, SignerConfig::VaultCloud(_)));
        assert!(matches!(
            vault_transit_config,
            SignerConfig::VaultTransit(_)
        ));
        assert!(matches!(aws_kms_config, SignerConfig::AwsKms(_)));
    }

    #[test]
    fn test_signer_config_get_local() {
        let local_config = SignerConfig::Local(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[1, 2, 3])),
        });
        let retrieved = local_config.get_local().unwrap();
        assert_eq!(*retrieved.raw_key.borrow(), vec![1, 2, 3]);

        let test_config = SignerConfig::Test(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[4, 5, 6])),
        });
        let retrieved = test_config.get_local().unwrap();
        assert_eq!(*retrieved.raw_key.borrow(), vec![4, 5, 6]);

        let vault_config = SignerConfig::Vault(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[7, 8, 9])),
        });
        let retrieved = vault_config.get_local().unwrap();
        assert_eq!(*retrieved.raw_key.borrow(), vec![7, 8, 9]);

        let vault_cloud_config = SignerConfig::VaultCloud(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[10, 11, 12])),
        });
        let retrieved = vault_cloud_config.get_local().unwrap();
        assert_eq!(*retrieved.raw_key.borrow(), vec![10, 11, 12]);

        let vault_transit_config = SignerConfig::VaultTransit(VaultTransitSignerConfig {
            key_name: "transit-key".to_string(),
            address: "https://vault.example.com".to_string(),
            namespace: None,
            role_id: SecretString::new("role-123"),
            secret_id: SecretString::new("secret-456"),
            pubkey: "mypubkey123".to_string(),
            mount_point: None,
        });
        assert!(vault_transit_config.get_local().is_none());
    }

    #[test]
    fn test_signer_config_get_aws_kms() {
        let aws_kms_config = SignerConfig::AwsKms(AwsKmsSignerConfig {});
        assert!(aws_kms_config.get_aws_kms().is_some());

        // Test with configs that should return None
        let local_config = SignerConfig::Local(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[1, 2, 3])),
        });
        assert!(local_config.get_aws_kms().is_none());

        let test_config = SignerConfig::Test(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[4, 5, 6])),
        });
        assert!(test_config.get_aws_kms().is_none());
    }

    #[test]
    fn test_signer_config_get_vault_transit() {
        let vault_transit_config = SignerConfig::VaultTransit(VaultTransitSignerConfig {
            key_name: "transit-key".to_string(),
            address: "https://vault.example.com".to_string(),
            namespace: None,
            role_id: SecretString::new("role-123"),
            secret_id: SecretString::new("secret-456"),
            pubkey: "mypubkey123".to_string(),
            mount_point: None,
        });
        let retrieved = vault_transit_config.get_vault_transit().unwrap();
        assert_eq!(retrieved.key_name, "transit-key");
        assert_eq!(retrieved.address, "https://vault.example.com");

        let local_config = SignerConfig::Local(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[1, 2, 3])),
        });
        assert!(local_config.get_vault_transit().is_none());

        let vault_config = SignerConfig::Vault(LocalSignerConfig {
            raw_key: SecretVec::new(3, |v| v.copy_from_slice(&[7, 8, 9])),
        });
        assert!(vault_config.get_vault_transit().is_none());
    }
}