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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use std::collections::HashMap;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::{serde::iso8601, OffsetDateTime};

use crate::error::Result;
use crate::utils::aes256gcm::{decrypt, Encryption};

/// An enum that represents the possible return values from the Infisical API
///
/// Infisical returns a 200 response even for errors on their side, but do provide a JSON response
/// with traditional HTTP response codes and additional error information.
#[derive(Deserialize)]
#[serde(untagged)]
pub enum ApiResponse<T> {
    Ok(T),
    Err(ErrorResponse),
}

/// Represents the expected request body for the `/v2/users/me` endpoint
pub struct GetMyUserRequest {
    /// The base url for the Infisical API
    pub base_url: String,
}

/// Represents the successful response for the `/v2/users/me` endpoint
#[derive(Deserialize)]
pub struct GetMyUserResponse {
    /// The Infisical user contained in the response
    pub user: User,
}

/// An Infisical user representation
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct User {
    #[serde(alias = "_id")]
    pub id: String,
    pub email: String,
    pub first_name: String,
    pub last_name: String,
    pub public_key: String,
    pub encrypted_private_key: String,
    pub salt: String,
    pub iv: String,
    pub tag: String,
    #[serde(alias = "__v")]
    pub v: u8,
    pub devices: Vec<UserDevice>,
    pub encryption_version: Option<u8>,
    pub is_mfa_enabled: bool,
    pub mfa_methods: Vec<String>,
    #[serde(flatten)]
    pub audit: Audit,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SimpleUser {
    #[serde(alias = "_id")]
    pub id: String,
    pub email: String,
    pub first_name: String,
    pub last_name: String,
    #[serde(alias = "__v")]
    pub v: u8,
    pub devices: Vec<UserDevice>,
    pub encryption_version: Option<u8>,
    pub is_mfa_enabled: bool,
    pub mfa_methods: Vec<String>,
    #[serde(flatten)]
    pub audit: Audit,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct UserDevice {
    pub ip: String,
    pub user_agent: String,
    #[serde(alias = "_id")]
    pub id: String,
}

pub struct GetMyOrganizationsRequest {
    /// The base url for the Infisical API
    pub base_url: String,
}

#[derive(Deserialize)]
pub struct GetOrganizationsResponse {
    pub organizations: Vec<Organization>,
}

#[derive(Deserialize)]
pub struct Organization {
    #[serde(alias = "_id")]
    pub id: String,
    pub name: String,
    #[serde(alias = "customerId")]
    pub customer_id: String,
    #[serde(flatten)]
    pub audit: Audit,
}

pub struct GetOrganizationMembershipsRequest {
    /// The base url for the Infisical API
    pub base_url: String,
    pub organization_id: String,
}

#[derive(Deserialize)]
pub struct GetOrganizationMembershipsResponse {
    pub memberships: Vec<OrganizationMembership>,
}

#[derive(Deserialize)]
pub struct OrganizationMembership {
    #[serde(alias = "_id")]
    pub id: String,
    pub organization: String,
    pub role: String,
    pub status: String,
    pub user: SimpleUser,
    #[serde(flatten)]
    pub audit: Audit,
}

pub struct UpdateOrganizationMembershipRequest {
    /// The base url for the Infisical API
    pub base_url: String,
    pub organization_id: String,
    pub membership_id: String,
    pub role: String,
}

#[derive(Deserialize)]
pub struct UpdateOrganizationMembershipResponse {
    pub membership: OrganizationMembership,
}

pub struct DeleteOrganizationMembershipRequest {
    /// The base url for the Infisical API
    pub base_url: String,
    pub organization_id: String,
    pub membership_id: String,
}

pub struct DeleteOrganizationMembershipResponse {
    pub membership: OrganizationMembership,
}

pub struct GetProjectsRequest {
    /// The base url for the Infisical API
    pub base_url: String,
    pub organization_id: String,
}

#[derive(Deserialize)]
pub struct GetProjectsResponse {
    pub workspaces: Vec<Workspace>,
}

#[derive(Deserialize)]
pub struct Workspace {
    #[serde(alias = "_id")]
    pub id: String,
    pub name: String,
    pub organization: String,
    pub environments: Vec<Environment>,
}

#[derive(Deserialize)]
pub struct Environment {
    pub name: String,
    pub slug: String,
}

pub struct GetProjectMembershipsRequest {
    pub base_url: String,
    pub workspace_id: String,
}

#[derive(Deserialize)]
pub struct GetProjectMembershipsResponse {
    pub memberships: Vec<ProjectMembership>,
}

#[derive(Deserialize)]
pub struct ProjectMembership {
    #[serde(alias = "_id")]
    pub id: String,
    pub role: String,
    pub user: SimpleUser,
    pub workspace: String,
    #[serde(flatten)]
    pub audit: Audit,
    #[serde(alias = "deniedPermissions")]
    pub denied_permissions: Vec<String>,
}

pub struct UpdateProjectMembershipRequest {
    pub base_url: String,
    pub workspace_id: String,
    pub membership_id: String,
    pub role: String,
}

#[derive(Deserialize)]
pub struct UpdateProjectMembershipResponse {
    pub membership: ProjectMembership,
}

pub struct DeleteProjectMembershipRequest {
    pub base_url: String,
    pub workspace_id: String,
    pub membership_id: String,
}

#[derive(Deserialize)]
pub struct DeleteProjectMembershipResponse {
    pub membership: ProjectMembership,
}

pub struct GetProjectKeyRequest {
    pub base_url: String,
    pub workspace_id: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetProjectKeyResponse {
    pub encrypted_key: String,
    pub nonce: String,
    pub sender: Sender,
    pub receiver: String,
    pub workspace: String,
}

#[derive(Deserialize)]
pub struct Sender {
    #[serde(alias = "publicKey")]
    pub public_key: String,
}

pub struct GetProjectLogsRequest {
    pub base_url: String,
    pub workspace_id: String,
    pub user_id: String,
    pub offset: String,
    pub limit: String,
    pub sort_by: String,
    pub action_names: String,
}

#[derive(Deserialize)]
pub struct GetProjectLogsResponse {
    pub logs: Vec<ProjectLog>,
}

#[derive(Deserialize)]
pub struct ProjectLog {
    #[serde(alias = "_id")]
    pub id: String,
    // This user does not have all fields that the User struct expects
    pub user: SimpleUser,
    pub workspace: String,
    #[serde(alias = "actionNames")]
    pub action_names: Vec<String>,
    pub actions: Vec<ProjectLogAction>,
    pub channel: String,
    #[serde(alias = "ipAddress")]
    pub ip_address: String,
    #[serde(flatten)]
    pub audit: Audit,
}

#[derive(Deserialize)]
pub struct ProjectLogAction {
    pub name: String,
    pub user: String,
    pub workspace: String,
    pub payload: Vec<ProjectLogActionPayload>,
}

#[derive(Deserialize)]
pub struct ProjectLogActionPayload {
    #[serde(alias = "oldSecretVersion")]
    pub old_secret_version: String,
    #[serde(alias = "newSecretVersion")]
    pub new_secret_version: String,
}

#[derive(Serialize)]
pub struct GetProjectSnapshotsRequest {
    #[serde(skip)]
    pub base_url: String,
    #[serde(skip)]
    pub workspace_id: String,
    pub offset: String,
    pub limit: String,
}

#[derive(Deserialize)]
pub struct GetProjectSnapshotsResponse {
    #[serde(alias = "secretSnapshots")]
    pub secret_snapshots: Vec<SecretSnapshot>,
}

#[derive(Deserialize)]
pub struct SecretSnapshot {
    #[serde(alias = "_id")]
    pub id: String,
    pub workspace: String,
    pub version: u8,
    #[serde(alias = "secretVersions")]
    pub secret_versions: Vec<String>,
}

#[derive(Deserialize)]
pub struct ProjectSecretVersion {
    #[serde(alias = "_id")]
    pub id: String,
}

pub struct RollbackProjectToSnapshotRequest {
    pub base_url: String,
    pub workspace_id: String,
    pub version: u8,
}

#[derive(Deserialize)]
pub struct RollbackProjectToSnapshotResponse {
    pub secrets: Vec<EncryptedSecret>,
}

#[derive(Deserialize)]
pub struct RollbackSecret {
    #[serde(alias = "_id")]
    pub id: String,
    pub version: u8,
    pub workspace: String,
    #[serde(alias = "type")]
    pub secret_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<SimpleUser>,
    #[serde(flatten)]
    pub encrypted_secret: EncryptedSecret,
    #[serde(flatten)]
    pub audit: Audit,
}

#[derive(Serialize)]
pub struct CreateProjectSecretsRequest {
    pub base_url: String,
    #[serde(rename = "workspaceId")]
    pub workspace_id: String,
    pub environment: String,
    pub secrets: Vec<SecretToCreate>,
}

#[derive(Serialize)]
pub struct SecretToCreate {
    #[serde(rename = "type")]
    pub secret_type: String,
    #[serde(flatten)]
    pub key: EncryptedKey,
    #[serde(flatten)]
    pub value: EncryptedValue,
    #[serde(flatten)]
    pub comment: EncryptedComment,
}

#[derive(Deserialize)]
pub struct CreateProjectSecretsResponse {
    pub secrets: Vec<EncryptedSecret>,
}

pub struct UpdateSecretsRequest {
    pub base_url: String,
    pub secrets: Vec<SecretToUpdate>,
}

#[derive(Serialize)]
pub struct SecretToUpdate {
    pub id: String,
    #[serde(flatten)]
    pub key: EncryptedKey,
    #[serde(flatten)]
    pub value: EncryptedValue,
}

#[derive(Deserialize)]
pub struct UpdateSecretsResponse {
    pub secrets: Vec<EncryptedSecret>,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetProjectSecretsRequest {
    #[serde(skip)]
    pub base_url: String,
    pub workspace_id: String,
    pub environment: String,
    pub content: String,
}

#[derive(Deserialize)]
pub struct GetProjectSecretsResponse {
    pub secrets: Vec<EncryptedSecret>,
}

pub struct DeleteProjectSecretsRequest {
    pub base_url: String,
    pub secret_ids: Vec<String>,
}

#[derive(Deserialize)]
pub struct DeleteProjectSecretsResponse {
    pub secrets: Vec<EncryptedSecret>,
}

pub struct GetProjectSecretVersionsRequest {
    pub base_url: String,
    pub secret_id: String,
    pub offset: String,
    pub limit: String,
}

#[derive(Deserialize)]
pub struct GetProjectSecretVersionsResponse {
    #[serde(alias = "secretVersions")]
    pub secret_versions: Vec<SecretVersion>,
}

#[derive(Deserialize)]
pub struct SecretVersion {
    pub tags: Vec<String>,
    #[serde(alias = "_id")]
    pub id: String,
    pub secret: String,
    pub version: u8,
    pub workspace: String,
    #[serde(alias = "type")]
    pub secret_type: String,
    pub environment: String,
    #[serde(alias = "isDeleted")]
    pub is_deleted: bool,
    #[serde(flatten)]
    pub key: EncryptedKey,
    #[serde(flatten)]
    pub value: EncryptedValue,
    #[serde(alias = "__v")]
    pub v: u8,
    #[serde(flatten)]
    pub audit: Audit,
}

pub struct RollbackProjectSecretToVersionRequest {
    pub base_url: String,
    pub secret_id: String,
    pub version: u8,
}

pub struct RollbackProjectSecretToVersionResponse {
    pub secret: EncryptedSecret,
}

#[derive(Deserialize)]
pub struct EncryptedSecret {
    #[serde(alias = "_id")]
    pub id: String,
    pub version: u8,
    pub workspace: String,
    #[serde(alias = "type")]
    pub type_name: String,
    #[serde(flatten)]
    pub key: EncryptedKey,
    #[serde(flatten)]
    pub value: EncryptedValue,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub comment: Option<EncryptedComment>,
    // pub tags: Vec<SecretTag>,
    #[serde(flatten)]
    pub audit: Audit,
}

#[derive(Deserialize)]
pub struct SecretTag {
    tag: String,
    slug: String,
}

#[derive(Debug)]
pub struct DecryptedSecret {
    pub id: String,
    pub version: u8,
    pub workspace: String,
    pub type_name: String,
    pub key: String,
    pub value: String,
    pub comment: Option<String>,
    pub audit: Audit,
}

impl EncryptedSecret {
    pub fn decrypt(secret: &EncryptedSecret, private_key: &str) -> Result<DecryptedSecret> {
        let mut comment = None::<String>;
        let key = decrypt(
            &secret.key.ciphertext,
            &secret.key.iv,
            &secret.key.tag,
            private_key,
        )?;
        let value = decrypt(
            &secret.value.ciphertext,
            &secret.value.iv,
            &secret.value.tag,
            private_key,
        )?;

        if let Some(encrypted_comment) = &secret.comment {
            comment = Some(decrypt(
                &encrypted_comment.ciphertext,
                &encrypted_comment.iv,
                &encrypted_comment.tag,
                private_key,
            )?);
        }

        Ok(DecryptedSecret {
            id: secret.id.clone(),
            version: secret.version,
            workspace: secret.workspace.clone(),
            type_name: secret.type_name.clone(),
            key,
            value,
            comment,
            audit: secret.audit.clone(),
        })
    }
}

#[derive(Deserialize, Serialize)]
pub struct EncryptedKey {
    #[serde(rename = "secretKeyCiphertext")]
    pub ciphertext: String,
    #[serde(rename = "secretKeyIV")]
    pub iv: String,
    #[serde(rename = "secretKeyTag")]
    pub tag: String,
}

#[derive(Deserialize, Serialize)]
pub struct EncryptedValue {
    #[serde(rename = "secretValueCiphertext")]
    pub ciphertext: String,
    #[serde(rename = "secretValueIV")]
    pub iv: String,
    #[serde(rename = "secretValueTag")]
    pub tag: String,
}

#[derive(Deserialize, Serialize)]
pub struct EncryptedComment {
    #[serde(rename = "secretCommentCiphertext")]
    pub ciphertext: String,
    #[serde(rename = "secretCommentIV")]
    pub iv: String,
    #[serde(rename = "secretCommentTag")]
    pub tag: String,
}

impl From<Encryption> for EncryptedComment {
    fn from(encryption: Encryption) -> EncryptedComment {
        EncryptedComment {
            ciphertext: encryption.text,
            tag: encryption.tag,
            iv: encryption.nonce,
        }
    }
}

impl From<Encryption> for EncryptedValue {
    fn from(encryption: Encryption) -> EncryptedValue {
        EncryptedValue {
            ciphertext: encryption.text,
            tag: encryption.tag,
            iv: encryption.nonce,
        }
    }
}

impl From<Encryption> for EncryptedKey {
    fn from(encryption: Encryption) -> EncryptedKey {
        EncryptedKey {
            ciphertext: encryption.text,
            tag: encryption.tag,
            iv: encryption.nonce,
        }
    }
}

#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Audit {
    #[serde(with = "iso8601")]
    pub updated_at: OffsetDateTime,
    #[serde(with = "iso8601")]
    pub created_at: OffsetDateTime,
}

pub struct GetServiceTokensRequest {
    pub base_url: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetServiceTokensResponse {
    pub service_token_data: ServiceToken,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServiceToken {
    #[serde(alias = "_id")]
    pub id: String,
    pub name: String,
    pub workspace: String,
    pub environment: String,
    // Current documentation does not claim a SimpleUser is returned, however the endpoint is not
    // working for me so this is a placeholder until functionality is verified
    pub user: SimpleUser,
    #[serde(with = "iso8601")]
    pub expires_at: OffsetDateTime,
    pub encrypted_key: String,
    pub iv: String,
    pub tag: String,
    #[serde(flatten)]
    pub audit: Audit,
}

#[derive(Deserialize, Debug)]
pub struct ErrorResponse {
    #[serde(alias = "type")]
    pub type_name: String,
    pub message: String,
    pub context: HashMap<String, Value>,
    pub level: i16,
    pub level_name: String,
    pub status_code: i16,
    pub datetime_iso: String,
    pub application: String,
    pub extra: Vec<String>,
}