openzeppelin_relayer/config/config_file/signer/
turnkey.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
//! Configuration for Turnkey signer
//!
//! This module provides configuration for using Turnkey
//! as a signing mechanism. Turnkey is a custody platform that offers secure key management
//! and signing services without exposing private keys.
//!
//! The configuration supports:
//! - API credentials (public key and private key) for authenticating with Turnkey
//! - Organization ID to identify the Turnkey organization
//! - Private key ID to identify the specific private key within Turnkey
//! - Public key representation for verification
//!
//! Turnkey allows for secure signing operations where private keys are managed and
//! protected within Turnkey's secure infrastructure.
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 TurnkeySignerFileConfig {
    #[validate(length(min = 1, message = "api_public_key field cannot be empty"))]
    pub api_public_key: String,
    #[validate(custom(function = "validate_plain_or_env_value"))]
    pub api_private_key: PlainOrEnvValue,
    #[validate(length(min = 1, message = "organization_id field cannot be empty"))]
    pub organization_id: String,
    #[validate(length(min = 1, message = "private_key_id cannot be empty"))]
    pub private_key_id: String,
    #[validate(length(min = 1, message = "public_key cannot be empty"))]
    pub public_key: String,
}

impl SignerConfigValidate for TurnkeySignerFileConfig {
    fn validate(&self) -> Result<(), ConfigFileError> {
        validate_with_validator(self)
    }
}
#[cfg(test)]
mod tests {
    use crate::models::SecretString;

    use super::*;

    #[test]
    fn test_vault_transit_signer_file_config_valid() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new("api_private_key"),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_key: "public-key".to_string(),
        };

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

    #[test]
    fn test_turnkey_signer_file_config_empty_api_public_key() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new("api_private_key"),
            },
            api_public_key: "".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_key: "public-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("api_public_key"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_turnkey_signer_file_config_empty_api_private_key() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new(""),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_key: "public-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("api_private_key"));
        }
    }

    #[test]
    fn test_turnkey_signer_file_config_empty_organization_id() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new("api_private_key"),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_key: "public-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("organization_id"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_turnkey_signer_file_config_empty_private_key_id() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new("api_private_key"),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "".to_string(),
            public_key: "public-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("private_key_id"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_turnkey_signer_file_config_empty_public_key() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new("api_private_key"),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_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("public_key"));
            assert!(error_message.contains("cannot be empty"));
        }
    }

    #[test]
    fn test_turnkey_signer_file_config_multiple_errors() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new(""),
            },
            api_public_key: "".to_string(),
            organization_id: "".to_string(),
            private_key_id: "".to_string(),
            public_key: "".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("api_public_key"));
                assert!(msg.contains("api_private_key"));
                assert!(msg.contains("organization_id"));
                assert!(msg.contains("private_key_id"));
                assert!(msg.contains("public_key"));
            } else {
                panic!("Expected ConfigFileError::InvalidFormat, got {:?}", e);
            }
        }
    }

    #[test]
    fn test_serde_deserialize() {
        let json = r#"
        {
            "api_public_key": "turnkey-api-public-key",
            "api_private_key": {
                "type": "plain",
                "value": "turnkey-api-private-key"
            },
            "organization_id": "org-123456",
            "private_key_id": "key-123456",
            "public_key": "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEd+vn+WOG+lGUiJCzHsj8VItmr7Lmdv/Zr+tIhJM7rM+QT9QEzvEX2jWOPyXrvCwUyvVgWoMwUYIo3hd1PFTy7A=="
        }
        "#;

        let config: TurnkeySignerFileConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.api_public_key, "turnkey-api-public-key");
        assert_eq!(
            config
                .api_private_key
                .get_value()
                .unwrap()
                .to_str()
                .as_str(),
            "turnkey-api-private-key"
        );
        assert_eq!(config.organization_id, "org-123456");
        assert_eq!(config.private_key_id, "key-123456");
        assert_eq!(config.public_key, "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEd+vn+WOG+lGUiJCzHsj8VItmr7Lmdv/Zr+tIhJM7rM+QT9QEzvEX2jWOPyXrvCwUyvVgWoMwUYIo3hd1PFTy7A==");
    }

    #[test]
    fn test_serde_unknown_field() {
        let json = r#"
        {
            "api_public_key": "turnkey-api-public-key",
            "api_private_key": {
                "type": "plain",
                "value": "turnkey-api-private-key"
            },
            "organization_id": "org-123456",
            "private_key_id": "key-123456",
            "public_key": "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEd+vn+WOG+lGUiJCzHsj8VItmr7Lmdv/Zr+tIhJM7rM+QT9QEzvEX2jWOPyXrvCwUyvVgWoMwUYIo3hd1PFTy7A==",
            "unknown_field": "should cause error"
        }
        "#;

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

    #[test]
    fn test_serde_serialize_deserialize() {
        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Plain {
                value: SecretString::new("api_private_key"),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_key: "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEd+vn+WOG+lGUiJCzHsj8VItmr7Lmdv/Zr+tIhJM7rM+QT9QEzvEX2jWOPyXrvCwUyvVgWoMwUYIo3hd1PFTy7A==".to_string(),
        };

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

        assert_eq!(config.api_public_key, deserialized.api_public_key);
        assert_eq!(config.organization_id, deserialized.organization_id);
        assert_eq!(config.private_key_id, deserialized.private_key_id);
        assert_eq!(config.public_key, deserialized.public_key);
    }

    #[test]
    fn test_turnkey_signer_file_config_env_variable() {
        let env_var_name = "TEST_API_PRIVATE_KEY";
        std::env::set_var(env_var_name, "env-api-private-key");

        let config = TurnkeySignerFileConfig {
            api_private_key: PlainOrEnvValue::Env {
                value: env_var_name.to_string(),
            },
            api_public_key: "public-key".to_string(),
            organization_id: "org-id".to_string(),
            private_key_id: "private-key-id".to_string(),
            public_key: "public-key".to_string(),
        };

        assert!(SignerConfigValidate::validate(&config).is_ok());
        assert_eq!(
            config
                .api_private_key
                .get_value()
                .unwrap()
                .to_str()
                .as_str(),
            "env-api-private-key"
        );

        std::env::remove_var(env_var_name);
    }
}