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
use crate::prelude::*;
use azure_core::auth::TokenCredential;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct CertificateClient {
pub(crate) keyvault_client: KeyvaultClient,
}
impl CertificateClient {
pub fn new(
vault_url: &str,
token_credential: Arc<dyn TokenCredential>,
) -> azure_core::Result<Self> {
let keyvault_client = KeyvaultClient::new(vault_url, token_credential)?;
Ok(Self::new_with_client(keyvault_client))
}
pub(crate) fn new_with_client(keyvault_client: KeyvaultClient) -> Self {
Self { keyvault_client }
}
/// Gets a certificate from the Key Vault.
///
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// let certificate = client.get("NAME").await.unwrap();
/// dbg!(&certificate);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn get<N>(&self, name: N) -> GetCertificateBuilder
where
N: Into<String>,
{
GetCertificateBuilder::new(self.clone(), name.into())
}
/// Gets all the versions for a certificate in the Key Vault.
//
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use futures::StreamExt;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// let certificate_versions = client.get_versions("NAME").into_stream().next().await.unwrap();
/// dbg!(&certificate_versions);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn get_versions<N>(&self, name: N) -> GetCertificateVersionsBuilder
where
N: Into<String>,
{
GetCertificateVersionsBuilder::new(self.clone(), name.into())
}
/// Creates a new certificate.
/// If this is the first version, the certificate resource is created.
/// This operation requires the certificates/create permission.
//
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// let certificate = client.create("NAME", "SUBJECT", "ISSUER").await.unwrap();
/// dbg!(&certificate);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn create<N, S, I>(&self, name: N, subject: S, issuer_name: I) -> CreateCertificateBuilder
where
N: Into<String>,
S: Into<String>,
I: Into<String>,
{
CreateCertificateBuilder::new(
self.clone(),
name.into(),
subject.into(),
issuer_name.into(),
)
}
/// Merges a certificate or a certificate chain with a key pair existing on the server.
/// The MergeCertificate operation performs the merging of a certificate or certificate chain with a key
/// pair currently available in the service. This operation requires the certificates/create permission.
//
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// let certificate = client.merge("NAME", vec![String::from("X5C")]).await.unwrap();
/// dbg!(&certificate);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn merge<N, V>(&self, name: N, x5c: V) -> MergeCertificateBuilder
where
N: Into<String>,
V: Into<Vec<String>>,
{
MergeCertificateBuilder::new(self.clone(), name.into(), x5c.into())
}
/// Imports a certificate into a specified key vault.
/// This operation requires the certificates/import permission. The certificate to be imported can be in either PFX
/// or PEM format. If the certificate is in PEM format the PEM file must contain the key as well as x509 certificates.
/// Key Vault will only accept a key in PKCS#8 format.
//
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// let certificate = client.import("NAME", "VALUE").pwd("pwd").await.unwrap();
/// dbg!(&certificate);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn import<N, V>(&self, name: N, value: V) -> ImportCertificateBuilder
where
N: Into<String>,
V: Into<String>,
{
ImportCertificateBuilder::new(self.clone(), name.into(), value.into())
}
pub fn update<N>(&self, name: N) -> UpdateCertificatePropertiesBuilder
where
N: Into<String>,
{
UpdateCertificatePropertiesBuilder::new(self.clone(), name.into())
}
/// Restores a backed up certificate and all its versions.
/// This operation requires the certificates/restore permission.
///
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// client.backup("NAME").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn backup<N>(&self, name: N) -> CertificateBackupBuilder
where
N: Into<String>,
{
CertificateBackupBuilder::new(self.clone(), name.into())
}
/// Deletes a certificate in the Key Vault.
///
/// # Arguments
///
/// * `name` - Name of the certificate
///
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// client.delete("NAME").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn delete<N>(&self, name: N) -> DeleteCertificateBuilder
where
N: Into<String>,
{
DeleteCertificateBuilder::new(self.clone(), name.into())
}
/// Lists all the certificates in the Key Vault.
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use futures::StreamExt;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// let certificates = client.list_certificates().into_stream().next().await.unwrap();
/// dbg!(&certificates);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn list_certificates(&self) -> ListCertificatesBuilder {
ListCertificatesBuilder::new(self.clone())
}
/// Restores a backed up certificate and all its versions.
/// This operation requires the certificates/restore permission.
///
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use std::sync::Arc;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().certificate_client();
/// client.restore_certificate("KUF6dXJlS2V5VmF1bHRTZWNyZXRCYWNrdXBWMS5taW").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn restore_certificate<S>(&self, backup_blob: S) -> RestoreCertificateBuilder
where
S: Into<String>,
{
RestoreCertificateBuilder::new(self.clone(), backup_blob.into())
}
}