openzeppelin_relayer/config/config_file/signer/
vault_cloud.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Configuration for HashiCorp Vault Cloud signer
//!
//! This module provides configuration for integrating with HashiCorp Cloud Platform (HCP) Vault,
//! which is the managed service offering of Vault. The configuration handles the OAuth2 client
//! credentials flow required for authenticating with HCP.
//!
//! The configuration supports:
//! - Client ID and Secret for OAuth2 authentication
//! - Organization ID for the HCP account
//! - Project ID within the organization
//! - Application name for identification in logs and metrics
//! - Key name to use for signing operations
//!
//! HCP Vault differs from self-hosted Vault by requiring OAuth-based authentication
//! instead of token or AppRole based authentication methods.
use crate::{
    config::ConfigFileError,
    models::{validate_plain_or_env_value, PlainOrEnvValue},
};
use serde::{Deserialize, Serialize};
use validator::Validate;

use super::{validate_with_validator, SignerConfigValidate};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Validate)]
#[serde(deny_unknown_fields)]
pub struct VaultCloudSignerFileConfig {
    #[validate(length(min = 1, message = "Client ID cannot be empty"))]
    pub client_id: String,
    #[validate(custom(function = "validate_plain_or_env_value"))]
    pub client_secret: PlainOrEnvValue,
    #[validate(length(min = 1, message = "Organization ID cannot be empty"))]
    pub org_id: String,
    #[validate(length(min = 1, message = "Project ID cannot be empty"))]
    pub project_id: String,
    #[validate(length(min = 1, message = "Application name cannot be empty"))]
    pub app_name: String,
    #[validate(length(min = 1, message = "Key name cannot be empty"))]
    pub key_name: String,
}

impl SignerConfigValidate for VaultCloudSignerFileConfig {
    fn validate(&self) -> Result<(), ConfigFileError> {
        validate_with_validator(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::SecretString;

    #[test]
    fn test_vault_cloud_signer_file_config_valid() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        assert!(Validate::validate(&config).is_ok());
        assert!(SignerConfigValidate::validate(&config).is_ok());
    }

    #[test]
    fn test_vault_cloud_signer_file_config_empty_client_id() {
        let config = VaultCloudSignerFileConfig {
            client_id: "".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let result = SignerConfigValidate::validate(&config);
        assert!(result.is_err());
        if let Err(e) = result {
            let error_message = format!("{:?}", e);
            assert!(error_message.contains("client_id"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_vault_cloud_signer_file_config_empty_client_secret() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new(""),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let result = SignerConfigValidate::validate(&config);
        assert!(result.is_err());
        if let Err(e) = result {
            let error_message = format!("{:?}", e);
            assert!(error_message.contains("client_secret"));
        }
    }

    #[test]
    fn test_vault_cloud_signer_file_config_empty_org_id() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let result = SignerConfigValidate::validate(&config);
        assert!(result.is_err());
        if let Err(e) = result {
            let error_message = format!("{:?}", e);
            assert!(error_message.contains("org_id"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_vault_cloud_signer_file_config_empty_project_id() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let result = SignerConfigValidate::validate(&config);
        assert!(result.is_err());
        if let Err(e) = result {
            let error_message = format!("{:?}", e);
            assert!(error_message.contains("project_id"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_vault_cloud_signer_file_config_empty_app_name() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let result = SignerConfigValidate::validate(&config);
        assert!(result.is_err());
        if let Err(e) = result {
            let error_message = format!("{:?}", e);
            assert!(error_message.contains("app_name"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_vault_cloud_signer_file_config_empty_key_name() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "".to_string(),
        };

        let result = SignerConfigValidate::validate(&config);
        assert!(result.is_err());
        if let Err(e) = result {
            let error_message = format!("{:?}", e);
            assert!(error_message.contains("key_name"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_vault_cloud_signer_file_config_multiple_errors() {
        // Config with multiple validation errors
        let config = VaultCloudSignerFileConfig {
            client_id: "".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new(""),
            },
            org_id: "".to_string(),
            project_id: "".to_string(),
            app_name: "".to_string(),
            key_name: "".to_string(),
        };

        let result = validate_with_validator(&config);
        assert!(result.is_err());

        if let Err(e) = result {
            if let ConfigFileError::InvalidFormat(msg) = e {
                assert!(msg.contains("client_id"));
                assert!(msg.contains("client_secret"));
                assert!(msg.contains("org_id"));
                assert!(msg.contains("project_id"));
                assert!(msg.contains("app_name"));
                assert!(msg.contains("key_name"));
            } else {
                panic!("Expected ConfigFileError::InvalidFormat, got {:?}", e);
            }
        }
    }

    #[test]
    fn test_serde_deserialize() {
        let json = r#"
        {
            "client_id": "client-123",
            "client_secret": {
                "type": "plain",
                "value":"secret-abc"
            },
            "org_id": "org-456",
            "project_id": "proj-789",
            "app_name": "my-cloud-app",
            "key_name": "hcp-key"
        }
        "#;

        let config: VaultCloudSignerFileConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.client_id, "client-123");
        assert_eq!(
            config.client_secret.get_value().unwrap().to_str().as_str(),
            "secret-abc"
        );
        assert_eq!(config.org_id, "org-456");
        assert_eq!(config.project_id, "proj-789");
        assert_eq!(config.app_name, "my-cloud-app");
        assert_eq!(config.key_name, "hcp-key");
    }

    #[test]
    fn test_serde_unknown_field() {
        let json = r#"
        {
            "client_id": "client-123",
            "client_secret": "secret-abc",
            "org_id": "org-456",
            "project_id": "proj-789",
            "app_name": "my-cloud-app",
            "key_name": "hcp-key",
            "unknown_field": "should cause error"
        }
        "#;

        let result: Result<VaultCloudSignerFileConfig, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_serde_serialize_deserialize() {
        let config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let serialized = serde_json::to_string(&config).unwrap();
        let deserialized: VaultCloudSignerFileConfig = serde_json::from_str(&serialized).unwrap();

        assert_eq!(config.app_name, deserialized.app_name);
        assert_eq!(config.client_id, deserialized.client_id);
        assert_eq!(config.key_name, deserialized.key_name);
        assert_eq!(config.org_id, deserialized.org_id);
        assert_eq!(config.project_id, deserialized.project_id);
        assert_ne!(config.client_secret, deserialized.client_secret);
    }

    #[test]
    fn test_serde_pretty_json() {
        let json = r#"{
        "client_id": "client-123",
        "client_secret": {
            "type": "plain",
            "value":"secret-abc"
        },
        "org_id": "org-456",
        "project_id": "proj-789",
        "app_name": "my-cloud-app",
        "key_name": "hcp-key"
        }"#;

        let config: VaultCloudSignerFileConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.client_id, "client-123");
        assert_eq!(
            config.client_secret.get_value().unwrap().to_str().as_str(),
            "secret-abc"
        );
    }

    #[test]
    fn test_validate_with_validator() {
        let valid_config = VaultCloudSignerFileConfig {
            client_id: "client-123".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        let invalid_config = VaultCloudSignerFileConfig {
            client_id: "".to_string(),
            client_secret: PlainOrEnvValue::Plain {
                value: SecretString::new("secret-abc"),
            },
            org_id: "org-456".to_string(),
            project_id: "proj-789".to_string(),
            app_name: "my-cloud-app".to_string(),
            key_name: "hcp-key".to_string(),
        };

        assert!(Validate::validate(&valid_config).is_ok());
        assert!(Validate::validate(&invalid_config).is_err());
    }
}