oxify-model 0.1.0

Data models and types for OxiFY workflows, execution, and configuration
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Secret management types
//!
//! Secure storage and access control for sensitive credentials

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

pub type SecretId = Uuid;

/// Secret metadata and encrypted value
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct Secret {
    /// Unique secret identifier
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: SecretId,

    /// Secret name (must be unique per user/workspace)
    pub name: String,

    /// Optional description
    pub description: Option<String>,

    /// Encrypted secret value
    #[serde(skip_serializing)]
    pub encrypted_value: Vec<u8>,

    /// Encryption metadata
    pub encryption: EncryptionMetadata,

    /// Tags for categorization
    pub tags: Vec<String>,

    /// User or workspace ID that owns this secret
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub owner_id: Uuid,

    /// Creation timestamp
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub created_at: DateTime<Utc>,

    /// Last updated timestamp
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub updated_at: DateTime<Utc>,

    /// Last accessed timestamp
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub last_accessed_at: Option<DateTime<Utc>>,

    /// Expiration date (optional)
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub expires_at: Option<DateTime<Utc>>,

    /// Access control rules
    pub access_control: AccessControl,
}

/// Encryption metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct EncryptionMetadata {
    /// Algorithm used (e.g., "AES-256-GCM")
    pub algorithm: String,

    /// Key derivation function
    pub kdf: String,

    /// Salt for key derivation (base64)
    pub salt: String,

    /// Initialization vector (base64)
    pub iv: String,

    /// Key version for rotation
    pub key_version: u32,
}

/// Access control for secrets
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Default)]
pub struct AccessControl {
    /// Allowed workflow IDs (empty means all)
    pub allowed_workflows: Vec<Uuid>,

    /// Allowed user IDs (empty means owner only)
    pub allowed_users: Vec<Uuid>,

    /// IP whitelist (empty means no restriction)
    pub ip_whitelist: Vec<String>,

    /// Require MFA for access
    pub require_mfa: bool,
}

impl Secret {
    /// Create a new secret (value must be encrypted before)
    pub fn new(
        name: String,
        encrypted_value: Vec<u8>,
        encryption: EncryptionMetadata,
        owner_id: Uuid,
    ) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            name,
            description: None,
            encrypted_value,
            encryption,
            tags: Vec::new(),
            owner_id,
            created_at: now,
            updated_at: now,
            last_accessed_at: None,
            expires_at: None,
            access_control: AccessControl::default(),
        }
    }

    /// Check if secret is expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            Utc::now() > expires_at
        } else {
            false
        }
    }

    /// Check if workflow has access
    pub fn can_access_workflow(&self, workflow_id: &Uuid) -> bool {
        if self.access_control.allowed_workflows.is_empty() {
            return true;
        }
        self.access_control.allowed_workflows.contains(workflow_id)
    }

    /// Check if user has access
    pub fn can_access_user(&self, user_id: &Uuid) -> bool {
        if user_id == &self.owner_id {
            return true;
        }
        if self.access_control.allowed_users.is_empty() {
            return false;
        }
        self.access_control.allowed_users.contains(user_id)
    }

    /// Update last accessed timestamp
    pub fn mark_accessed(&mut self) {
        self.last_accessed_at = Some(Utc::now());
    }

    /// Create a safe view without sensitive data
    pub fn to_safe_view(&self) -> SecretView {
        SecretView {
            id: self.id,
            name: self.name.clone(),
            description: self.description.clone(),
            tags: self.tags.clone(),
            owner_id: self.owner_id,
            created_at: self.created_at,
            updated_at: self.updated_at,
            last_accessed_at: self.last_accessed_at,
            expires_at: self.expires_at,
            is_expired: self.is_expired(),
        }
    }
}

/// Safe view of a secret (no encrypted value)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct SecretView {
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: SecretId,
    pub name: String,
    pub description: Option<String>,
    pub tags: Vec<String>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub owner_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub created_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub updated_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub last_accessed_at: Option<DateTime<Utc>>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub expires_at: Option<DateTime<Utc>>,
    pub is_expired: bool,
}

/// Secret reference in workflow context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecretReference {
    /// Secret ID or name
    pub identifier: String,

    /// Whether identifier is ID or name
    pub is_id: bool,

    /// Target variable name in context
    pub target_variable: String,
}

/// Audit log entry for secret access
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct SecretAuditLog {
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: Uuid,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub secret_id: SecretId,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub user_id: Option<Uuid>,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub workflow_id: Option<Uuid>,

    pub action: SecretAction,

    pub ip_address: Option<String>,

    pub success: bool,

    pub error_message: Option<String>,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub timestamp: DateTime<Utc>,
}

/// Actions that can be performed on secrets
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub enum SecretAction {
    Create,
    Read,
    Update,
    Delete,
    List,
    Rotate,
}

impl std::fmt::Display for SecretAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SecretAction::Create => write!(f, "CREATE"),
            SecretAction::Read => write!(f, "READ"),
            SecretAction::Update => write!(f, "UPDATE"),
            SecretAction::Delete => write!(f, "DELETE"),
            SecretAction::List => write!(f, "LIST"),
            SecretAction::Rotate => write!(f, "ROTATE"),
        }
    }
}

/// Request to create a secret
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct CreateSecretRequest {
    pub name: String,
    pub value: String,
    pub description: Option<String>,
    pub tags: Vec<String>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub expires_at: Option<DateTime<Utc>>,
}

/// Request to update a secret
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct UpdateSecretRequest {
    pub value: Option<String>,
    pub description: Option<String>,
    pub tags: Option<Vec<String>>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub expires_at: Option<DateTime<Utc>>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Duration;

    fn create_test_secret() -> Secret {
        let encryption = EncryptionMetadata {
            algorithm: "AES-256-GCM".to_string(),
            kdf: "PBKDF2".to_string(),
            salt: "base64salt".to_string(),
            iv: "base64iv".to_string(),
            key_version: 1,
        };

        Secret::new(
            "test_secret".to_string(),
            vec![1, 2, 3, 4, 5],
            encryption,
            Uuid::new_v4(),
        )
    }

    #[test]
    fn test_secret_creation() {
        let secret = create_test_secret();
        assert_eq!(secret.name, "test_secret");
        assert_eq!(secret.encrypted_value, vec![1, 2, 3, 4, 5]);
        assert_eq!(secret.encryption.algorithm, "AES-256-GCM");
        assert_eq!(secret.tags.len(), 0);
        assert_eq!(secret.description, None);
        assert_eq!(secret.last_accessed_at, None);
        assert_eq!(secret.expires_at, None);
    }

    #[test]
    fn test_secret_not_expired_when_no_expiration() {
        let secret = create_test_secret();
        assert!(!secret.is_expired());
    }

    #[test]
    fn test_secret_not_expired_when_future_expiration() {
        let mut secret = create_test_secret();
        secret.expires_at = Some(Utc::now() + Duration::days(1));
        assert!(!secret.is_expired());
    }

    #[test]
    fn test_secret_expired_when_past_expiration() {
        let mut secret = create_test_secret();
        secret.expires_at = Some(Utc::now() - Duration::days(1));
        assert!(secret.is_expired());
    }

    #[test]
    fn test_workflow_access_allowed_when_empty_list() {
        let secret = create_test_secret();
        let workflow_id = Uuid::new_v4();
        assert!(secret.can_access_workflow(&workflow_id));
    }

    #[test]
    fn test_workflow_access_allowed_when_in_list() {
        let mut secret = create_test_secret();
        let workflow_id = Uuid::new_v4();
        secret.access_control.allowed_workflows.push(workflow_id);
        assert!(secret.can_access_workflow(&workflow_id));
    }

    #[test]
    fn test_workflow_access_denied_when_not_in_list() {
        let mut secret = create_test_secret();
        let allowed_workflow = Uuid::new_v4();
        let other_workflow = Uuid::new_v4();
        secret
            .access_control
            .allowed_workflows
            .push(allowed_workflow);
        assert!(!secret.can_access_workflow(&other_workflow));
    }

    #[test]
    fn test_user_access_allowed_for_owner() {
        let owner_id = Uuid::new_v4();
        let encryption = EncryptionMetadata {
            algorithm: "AES-256-GCM".to_string(),
            kdf: "PBKDF2".to_string(),
            salt: "base64salt".to_string(),
            iv: "base64iv".to_string(),
            key_version: 1,
        };

        let secret = Secret::new(
            "test_secret".to_string(),
            vec![1, 2, 3, 4, 5],
            encryption,
            owner_id,
        );

        assert!(secret.can_access_user(&owner_id));
    }

    #[test]
    fn test_user_access_denied_for_non_owner_when_empty_list() {
        let secret = create_test_secret();
        let other_user = Uuid::new_v4();
        assert!(!secret.can_access_user(&other_user));
    }

    #[test]
    fn test_user_access_allowed_when_in_list() {
        let mut secret = create_test_secret();
        let user_id = Uuid::new_v4();
        secret.access_control.allowed_users.push(user_id);
        assert!(secret.can_access_user(&user_id));
    }

    #[test]
    fn test_user_access_denied_when_not_in_list() {
        let mut secret = create_test_secret();
        let allowed_user = Uuid::new_v4();
        let other_user = Uuid::new_v4();
        secret.access_control.allowed_users.push(allowed_user);
        assert!(!secret.can_access_user(&other_user));
    }

    #[test]
    fn test_mark_accessed_updates_timestamp() {
        let mut secret = create_test_secret();
        assert_eq!(secret.last_accessed_at, None);

        secret.mark_accessed();
        assert!(secret.last_accessed_at.is_some());

        let first_access = secret.last_accessed_at.unwrap();
        std::thread::sleep(std::time::Duration::from_millis(10));

        secret.mark_accessed();
        let second_access = secret.last_accessed_at.unwrap();
        assert!(second_access > first_access);
    }

    #[test]
    fn test_safe_view_excludes_encrypted_value() {
        let mut secret = create_test_secret();
        secret.description = Some("Test description".to_string());
        secret.tags.push("tag1".to_string());
        secret.tags.push("tag2".to_string());

        let view = secret.to_safe_view();

        assert_eq!(view.id, secret.id);
        assert_eq!(view.name, secret.name);
        assert_eq!(view.description, secret.description);
        assert_eq!(view.tags, secret.tags);
        assert_eq!(view.owner_id, secret.owner_id);
        assert_eq!(view.created_at, secret.created_at);
        assert_eq!(view.updated_at, secret.updated_at);
        assert_eq!(view.last_accessed_at, secret.last_accessed_at);
        assert_eq!(view.expires_at, secret.expires_at);
        assert_eq!(view.is_expired, secret.is_expired());
    }

    #[test]
    fn test_safe_view_reflects_expiration_status() {
        let mut secret = create_test_secret();
        secret.expires_at = Some(Utc::now() - Duration::days(1));

        let view = secret.to_safe_view();
        assert!(view.is_expired);
    }

    #[test]
    fn test_secret_action_display() {
        assert_eq!(SecretAction::Create.to_string(), "CREATE");
        assert_eq!(SecretAction::Read.to_string(), "READ");
        assert_eq!(SecretAction::Update.to_string(), "UPDATE");
        assert_eq!(SecretAction::Delete.to_string(), "DELETE");
        assert_eq!(SecretAction::List.to_string(), "LIST");
        assert_eq!(SecretAction::Rotate.to_string(), "ROTATE");
    }

    #[test]
    fn test_access_control_default() {
        let ac = AccessControl::default();
        assert_eq!(ac.allowed_workflows.len(), 0);
        assert_eq!(ac.allowed_users.len(), 0);
        assert_eq!(ac.ip_whitelist.len(), 0);
        assert!(!ac.require_mfa);
    }

    #[test]
    fn test_encryption_metadata_fields() {
        let encryption = EncryptionMetadata {
            algorithm: "AES-256-GCM".to_string(),
            kdf: "PBKDF2".to_string(),
            salt: "base64salt".to_string(),
            iv: "base64iv".to_string(),
            key_version: 1,
        };

        assert_eq!(encryption.algorithm, "AES-256-GCM");
        assert_eq!(encryption.kdf, "PBKDF2");
        assert_eq!(encryption.salt, "base64salt");
        assert_eq!(encryption.iv, "base64iv");
        assert_eq!(encryption.key_version, 1);
    }

    #[test]
    fn test_secret_reference() {
        let reference = SecretReference {
            identifier: "my-api-key".to_string(),
            is_id: false,
            target_variable: "api_key".to_string(),
        };

        assert_eq!(reference.identifier, "my-api-key");
        assert!(!reference.is_id);
        assert_eq!(reference.target_variable, "api_key");
    }

    #[test]
    fn test_create_secret_request() {
        let request = CreateSecretRequest {
            name: "test_secret".to_string(),
            value: "secret_value".to_string(),
            description: Some("Test description".to_string()),
            tags: vec!["tag1".to_string(), "tag2".to_string()],
            expires_at: None,
        };

        assert_eq!(request.name, "test_secret");
        assert_eq!(request.value, "secret_value");
        assert_eq!(request.description, Some("Test description".to_string()));
        assert_eq!(request.tags.len(), 2);
    }

    #[test]
    fn test_update_secret_request() {
        let request = UpdateSecretRequest {
            value: Some("new_value".to_string()),
            description: Some("New description".to_string()),
            tags: Some(vec!["new_tag".to_string()]),
            expires_at: None,
        };

        assert_eq!(request.value, Some("new_value".to_string()));
        assert_eq!(request.description, Some("New description".to_string()));
        assert!(request.tags.is_some());
    }
}