vaultrs 0.8.0

An asynchronous Rust client library for the Hashicorp Vault API.
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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
pub mod cert {
    use crate::api;
    use crate::api::pki::requests::{
        GenerateCertificateRequest, GenerateCertificateRequestBuilder, ListCertificatesRequest,
        ReadCertificateRequest, RevokeCertificateRequest, TidyRequest,
    };
    use crate::api::pki::responses::{
        GenerateCertificateResponse, ReadCertificateResponse, RevokeCertificateResponse,
    };
    use crate::client::Client;
    use crate::error::ClientError;

    /// Generates a certificate using the given role and options
    ///
    /// See [GenerateCertificateRequest]
    pub async fn generate(
        client: &impl Client,
        mount: &str,
        role: &str,
        opts: Option<&mut GenerateCertificateRequestBuilder>,
    ) -> Result<GenerateCertificateResponse, ClientError> {
        let mut t = GenerateCertificateRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .role(role)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Lists all certificates
    ///
    /// See [ListCertificatesRequest]
    pub async fn list(client: &impl Client, mount: &str) -> Result<Vec<String>, ClientError> {
        let endpoint = ListCertificatesRequest::builder()
            .mount(mount)
            .build()
            .unwrap();
        Ok(api::exec_with_result(client, endpoint).await?.keys)
    }

    /// Read a certificate using its serial
    ///
    /// See [ReadCertificateRequest]
    pub async fn read(
        client: &impl Client,
        mount: &str,
        serial: &str,
    ) -> Result<ReadCertificateResponse, ClientError> {
        let endpoint = ReadCertificateRequest::builder()
            .mount(mount)
            .serial(serial)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Revokes a certificate using its serial
    ///
    /// See [RevokeCertificateRequest]
    pub async fn revoke(
        client: &impl Client,
        mount: &str,
        serial: &str,
    ) -> Result<RevokeCertificateResponse, ClientError> {
        let endpoint = RevokeCertificateRequest::builder()
            .mount(mount)
            .serial_number(serial)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Tidy's up the certificate backend
    ///
    /// See [TidyRequest]
    pub async fn tidy(client: &impl Client, mount: &str) -> Result<(), ClientError> {
        let endpoint = TidyRequest::builder().mount(mount).build().unwrap();
        api::exec_with_empty_result(client, endpoint).await
    }

    pub mod ca {
        use crate::api;
        use crate::api::pki::responses::SignSelfIssuedResponse;
        use crate::{
            api::pki::{
                requests::{
                    DeleteRootRequest, GenerateRootRequest, GenerateRootRequestBuilder,
                    SignCertificateRequest, SignCertificateRequestBuilder, SignIntermediateRequest,
                    SignIntermediateRequestBuilder, SignSelfIssuedRequest, SubmitCARequest,
                },
                responses::{
                    GenerateRootResponse, SignCertificateResponse, SignIntermediateResponse,
                },
            },
            client::Client,
            error::ClientError,
        };

        /// Deletes the root CA
        ///
        /// See [DeleteRootRequest]
        pub async fn delete(client: &impl Client, mount: &str) -> Result<(), ClientError> {
            let endpoint = DeleteRootRequest::builder().mount(mount).build().unwrap();
            api::exec_with_empty(client, endpoint).await
        }

        /// Generates a new root CA
        ///
        /// See [GenerateRootRequest]
        pub async fn generate(
            client: &impl Client,
            mount: &str,
            cert_type: &str,
            opts: Option<&mut GenerateRootRequestBuilder>,
        ) -> Result<Option<GenerateRootResponse>, ClientError> {
            let mut t = GenerateRootRequest::builder();
            let endpoint = opts
                .unwrap_or(&mut t)
                .mount(mount)
                .cert_type(cert_type)
                .build()
                .unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Signs a certificate using the root CA
        ///
        /// See [SignCertificateRequest]
        pub async fn sign(
            client: &impl Client,
            mount: &str,
            role: &str,
            csr: &str,
            common_name: &str,
            opts: Option<&mut SignCertificateRequestBuilder>,
        ) -> Result<SignCertificateResponse, ClientError> {
            let mut t = SignCertificateRequest::builder();
            let endpoint = opts
                .unwrap_or(&mut t)
                .mount(mount)
                .role(role)
                .csr(csr)
                .common_name(common_name)
                .build()
                .unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Signs an intermediate CA using the root CA
        ///
        /// See [SignIntermediateRequest]
        pub async fn sign_intermediate(
            client: &impl Client,
            mount: &str,
            csr: &str,
            common_name: &str,
            opts: Option<&mut SignIntermediateRequestBuilder>,
        ) -> Result<SignIntermediateResponse, ClientError> {
            let mut t = SignIntermediateRequest::builder();
            let endpoint = opts
                .unwrap_or(&mut t)
                .mount(mount)
                .csr(csr)
                .common_name(common_name)
                .build()
                .unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Signs a self issued certificate using the root CA
        ///
        /// See [SignSelfIssuedRequest]
        pub async fn sign_self_issued(
            client: &impl Client,
            mount: &str,
            certificate: &str,
        ) -> Result<SignSelfIssuedResponse, ClientError> {
            let endpoint = SignSelfIssuedRequest::builder()
                .mount(mount)
                .certificate(certificate)
                .build()
                .unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Configures the root CA
        ///
        /// See [SubmitCARequest]
        pub async fn submit(
            client: &impl Client,
            mount: &str,
            pem_bundle: &str,
        ) -> Result<(), ClientError> {
            let endpoint = SubmitCARequest::builder()
                .mount(mount)
                .pem_bundle(pem_bundle)
                .build()
                .unwrap();
            api::exec_with_empty(client, endpoint).await
        }

        pub mod int {
            use crate::api;
            use crate::api::pki::responses::ImportIssuerResponse;
            use crate::{
                api::pki::{
                    requests::{
                        CrossSignRequest, CrossSignRequestBuilder, GenerateIntermediateRequest,
                        GenerateIntermediateRequestBuilder, SetSignedIntermediateRequest,
                    },
                    responses::{CrossSignResponse, GenerateIntermediateResponse},
                },
                client::Client,
                error::ClientError,
            };

            /// Generates an intermediate CA
            ///
            /// See [GenerateIntermediateRequest]
            pub async fn generate(
                client: &impl Client,
                mount: &str,
                cert_type: &str,
                common_name: &str,
                opts: Option<&mut GenerateIntermediateRequestBuilder>,
            ) -> Result<GenerateIntermediateResponse, ClientError> {
                let mut t = GenerateIntermediateRequest::builder();
                let endpoint = opts
                    .unwrap_or(&mut t)
                    .mount(mount)
                    .cert_type(cert_type)
                    .common_name(common_name)
                    .build()
                    .unwrap();
                api::exec_with_result(client, endpoint).await
            }

            /// Sets the signed CA certificate
            ///
            /// See [SetSignedIntermediateRequest]
            pub async fn set_signed(
                client: &impl Client,
                mount: &str,
                certificate: &str,
            ) -> Result<ImportIssuerResponse, ClientError> {
                let endpoint = SetSignedIntermediateRequest::builder()
                    .mount(mount)
                    .certificate(certificate)
                    .build()
                    .unwrap();
                api::exec_with_result(client, endpoint).await
            }

            /// Generates intermediate CSR
            ///
            /// See [CrossSignRequest]
            #[instrument(skip(client, opts), err)]
            pub async fn cross_sign(
                client: &impl Client,
                mount: &str,
                opts: Option<&mut CrossSignRequestBuilder>,
            ) -> Result<CrossSignResponse, ClientError> {
                let mut t = CrossSignRequest::builder();
                let endpoint = opts.unwrap_or(&mut t).mount(mount).build().unwrap();
                api::exec_with_result(client, endpoint).await
            }
        }
    }

    pub mod crl {
        use crate::api::pki::{
            requests::{
                ReadCRLConfigRequest, RotateCRLsRequest, SetCRLConfigRequest,
                SetCRLConfigRequestBuilder,
            },
            responses::{ReadCRLConfigResponse, RotateCRLsResponse},
        };
        use crate::api::{self, exec_with_empty};
        use crate::client::Client;
        use crate::error::ClientError;

        /// Rotates the CRL
        ///
        /// See [RotateCRLsRequest]
        pub async fn rotate(
            client: &impl Client,
            mount: &str,
        ) -> Result<RotateCRLsResponse, ClientError> {
            let endpoint = RotateCRLsRequest::builder().mount(mount).build().unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Reads the CRL configuration
        ///
        /// See [ReadCRLConfigRequest]
        pub async fn read_config(
            client: &impl Client,
            mount: &str,
        ) -> Result<ReadCRLConfigResponse, ClientError> {
            let endpoint = ReadCRLConfigRequest::builder()
                .mount(mount)
                .build()
                .unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Sets the CRL configuration
        ///
        /// See [SetCRLConfigRequest]
        pub async fn set_config(
            client: &impl Client,
            mount: &str,
            opts: Option<&mut SetCRLConfigRequestBuilder>,
        ) -> Result<(), ClientError> {
            let mut t = SetCRLConfigRequest::builder();
            let endpoint = opts.unwrap_or(&mut t).mount(mount).build().unwrap();
            exec_with_empty(client, endpoint).await
        }
    }

    pub mod urls {
        use crate::api;
        use crate::api::pki::{
            requests::{ReadURLsRequest, SetURLsRequest, SetURLsRequestBuilder},
            responses::ReadURLsResponse,
        };
        use crate::client::Client;
        use crate::error::ClientError;

        /// Reads the configured certificate URLs
        ///
        /// See [ReadURLsRequest]
        pub async fn read(
            client: &impl Client,
            mount: &str,
        ) -> Result<ReadURLsResponse, ClientError> {
            let endpoint = ReadURLsRequest::builder().mount(mount).build().unwrap();
            api::exec_with_result(client, endpoint).await
        }

        /// Sets the configured certificate URLs
        ///
        /// See [SetURLsRequest]
        pub async fn set(
            client: &impl Client,
            mount: &str,
            opts: Option<&mut SetURLsRequestBuilder>,
        ) -> Result<(), ClientError> {
            let mut t = SetURLsRequest::builder();
            let endpoint = opts.unwrap_or(&mut t).mount(mount).build().unwrap();
            api::exec_with_empty(client, endpoint).await
        }
    }
}

pub mod issuer {
    use crate::{
        api::{
            self,
            pki::{
                requests::{
                    DeleteIssuerRequest, ImportIssuerRequest, ListIssuersRequest,
                    ReadIssuerCertificateRequest, SetDefaultIssuerRequest,
                    SignIntermediateIssuerRequest, SignIntermediateIssuerRequestBuilder,
                    UpdateIssuerRequest, UpdateIssuerRequestBuilder,
                },
                responses::{
                    ImportIssuerResponse, ListIssuersResponse, ReadIssuerCertificateResponse,
                    SetDefaultIssuerResponse, SignIntermediateIssuerResponse, UpdateIssuerResponse,
                },
            },
        },
        client::Client,
        error::ClientError,
    };

    /// This endpoint returns a list of issuers currently provisioned in this mount.
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    ///
    /// See [ListIssuersResponse]
    #[instrument(skip(client), err)]
    pub async fn list(
        client: &impl Client,
        mount: &str,
    ) -> Result<ListIssuersResponse, ClientError> {
        let endpoint = ListIssuersRequest::builder().mount(mount).build().unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Read issuer's certificate
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `issuer`: issuer name or id (`default` if not specified)
    ///
    /// See [ReadIssuerCertificateRequest]
    #[instrument(skip(client, issuer), err)]
    pub async fn read(
        client: &impl Client,
        mount: &str,
        issuer: Option<&str>,
    ) -> Result<ReadIssuerCertificateResponse, ClientError> {
        let endpoint = ReadIssuerCertificateRequest::builder()
            .mount(mount)
            .issuer(issuer.unwrap_or("default"))
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Sign Intermediate CSR
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `csr`: PEM-encoded CSR to be signed
    /// * `common_name`: CN for certificate
    /// * `issuer`: name or id of existing issuer ("default" if not specified)
    ///
    /// See [SignIntermediateIssuerRequest]
    #[instrument(skip(client, opts), err)]
    pub async fn sign_intermediate(
        client: &impl Client,
        mount: &str,
        csr: &str,
        common_name: &str,
        issuer: Option<&str>,
        opts: Option<&mut SignIntermediateIssuerRequestBuilder>,
    ) -> Result<SignIntermediateIssuerResponse, ClientError> {
        let mut t = SignIntermediateIssuerRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .csr(csr)
            .common_name(common_name)
            .issuer(issuer.unwrap_or("default"))
            .build()
            .unwrap();

        api::exec_with_result(client, endpoint).await
    }

    /// Import CA certificate and private key bundle
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `pem_bundle`: certificate and private key concatenated in any order
    ///
    /// See [ImportIssuerRequest]
    #[instrument(skip(client), err)]
    pub async fn import(
        client: &impl Client,
        mount: &str,
        pem_bundle: &str,
    ) -> Result<ImportIssuerResponse, ClientError> {
        let endpoint = ImportIssuerRequest::builder()
            .mount(mount)
            .pem_bundle(pem_bundle)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Set default issuer
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `default_issuer`: default issuer id or name
    ///
    /// See [SetDefaultIssuerRequest]
    #[instrument(skip(client), err)]
    pub async fn set_default(
        client: &impl Client,
        mount: &str,
        default_issuer: &str,
    ) -> Result<SetDefaultIssuerResponse, ClientError> {
        let endpoint = SetDefaultIssuerRequest::builder()
            .mount(mount)
            .default_issuer(default_issuer)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// This endpoint allows an operator to manage a single issuer, updating various properties about it,
    /// including its name, an explicitly constructed chain, what the behavior is for signing longer TTL'd certificates,
    /// and what usage modes are set on this issuer.
    ///
    /// WARNING: this will overwrite previous content. It does not update only the provided fields.
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `issuer_ref`: Reference to an existing issuer, either by Vault-generated identifier, the literal string default to refer to the currently configured default issuer, or the name assigned to an issuer.
    ///
    /// See [SetDefaultIssuerRequest]
    #[instrument(skip(client, opts), err)]
    pub async fn update(
        client: &impl Client,
        mount: &str,
        issuer_ref: &str,
        opts: Option<&mut UpdateIssuerRequestBuilder>,
    ) -> Result<UpdateIssuerResponse, ClientError> {
        let mut t = UpdateIssuerRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .issuer_ref(issuer_ref)
            .build()
            .unwrap();

        api::exec_with_result(client, endpoint).await
    }

    /// Delete issuer
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `issuer`: issuer name or id
    ///
    /// See [DeleteIssuerRequest]
    #[instrument(skip(client), err)]
    pub async fn delete(
        client: &impl Client,
        mount: &str,
        issuer: &str,
    ) -> Result<(), ClientError> {
        let endpoint = DeleteIssuerRequest::builder()
            .mount(mount)
            .issuer(issuer)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    pub mod int {
        use crate::{
            api::{
                self,
                pki::{
                    requests::{
                        GenerateIntermediateCSRRequest, GenerateIntermediateCSRRequestBuilder,
                    },
                    responses::GenerateIntermediateCSRResponse,
                },
            },
            client::Client,
            error::ClientError,
        };

        /// Generates an intermediate CSR
        ///
        /// See [GenerateIntermediateCSRRequest]
        #[instrument(skip(client, opts), err)]
        pub async fn generate(
            client: &impl Client,
            mount: &str,
            request_type: &str,
            common_name: &str,
            opts: Option<&mut GenerateIntermediateCSRRequestBuilder>,
        ) -> Result<GenerateIntermediateCSRResponse, ClientError> {
            let mut t = GenerateIntermediateCSRRequest::builder();
            let endpoint = opts
                .unwrap_or(&mut t)
                .mount(mount)
                .request_type(request_type)
                .common_name(common_name)
                .build()
                .unwrap();
            api::exec_with_result(client, endpoint).await
        }
    }
}

pub mod role {
    use crate::api;
    use crate::api::pki::{
        requests::{
            DeleteRoleRequest, ListRolesRequest, ReadRoleRequest, SetRoleRequest,
            SetRoleRequestBuilder,
        },
        responses::{ListRolesResponse, ReadRoleResponse},
    };
    use crate::client::Client;
    use crate::error::ClientError;

    /// Deletes a role
    ///
    /// See [DeleteRoleRequest]
    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
        let endpoint = DeleteRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Lists all roles
    ///
    /// See [ListRolesRequest]
    pub async fn list(client: &impl Client, mount: &str) -> Result<ListRolesResponse, ClientError> {
        let endpoint = ListRolesRequest::builder().mount(mount).build().unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Reads a role
    ///
    /// See [ReadRoleRequest]
    pub async fn read(
        client: &impl Client,
        mount: &str,
        name: &str,
    ) -> Result<ReadRoleResponse, ClientError> {
        let endpoint = ReadRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Creates or updates a role
    ///
    /// See [SetRoleRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        name: &str,
        opts: Option<&mut SetRoleRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = SetRoleRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}

pub mod key {
    use crate::{
        api::{self, pki::requests::DeleteKeyRequest},
        client::Client,
        error::ClientError,
    };

    /// Delete key
    ///
    /// # Arguments
    ///
    /// * `client`: vault client
    /// * `mount`: vault pki mount path
    /// * `key`: key name or id
    ///
    /// See [DeleteKeyRequest]
    #[instrument(skip(client, key), err)]
    pub async fn delete(client: &impl Client, mount: &str, key: &str) -> Result<(), ClientError> {
        let endpoint = DeleteKeyRequest::builder()
            .mount(mount)
            .key(key)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}