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
use crate::prelude::*;
use azure_core::auth::TokenCredential;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct SecretClient {
pub(crate) keyvault_client: KeyvaultClient,
}
impl SecretClient {
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 secret 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().secret_client();
/// let secret = client.get("SECRET_NAME").await.unwrap();
/// dbg!(&secret);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn get<N>(&self, name: N) -> GetSecretBuilder
where
N: Into<String>,
{
GetSecretBuilder::new(self.clone(), name.into())
}
/// Sets the value of a secret in 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().secret_client();
/// client.set("SECRET_NAME", "NEW_VALUE").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn set<N, S>(&self, name: N, value: S) -> SetSecretBuilder
where
N: Into<String>,
S: Into<String>,
{
SetSecretBuilder::new(self.clone(), name.into(), value.into())
}
/// Updates the metadata associated with a secret
///
/// # 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 client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().secret_client();
/// client.update("SECRET_NAME").enabled(false).await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn update<N>(&self, name: N) -> UpdateSecretBuilder
where
N: Into<String>,
{
UpdateSecretBuilder::new(self.clone(), name.into())
}
/// Gets all the versions for a secret in the Key Vault.
//
/// # Example
///
/// ```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 client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().secret_client();
/// let secret_versions = client.get_versions("SECRET_NAME").into_stream().next().await.unwrap();
/// dbg!(&secret_versions);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn get_versions<N>(&self, name: N) -> GetSecretVersionsBuilder
where
N: Into<String>,
{
GetSecretVersionsBuilder::new(self.clone(), name.into())
}
/// Restores a backed up secret and all its versions.
/// This operation requires the secrets/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 client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().secret_client();
/// client.backup("SECRET_NAME").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn backup<N>(&self, name: N) -> BackupSecretBuilder
where
N: Into<String>,
{
BackupSecretBuilder::new(self.clone(), name.into())
}
/// Deletes a secret in 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 client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// Arc::new(creds),
/// ).unwrap().secret_client();
/// client.delete("SECRET_NAME").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn delete<N>(&self, name: N) -> DeleteSecretBuilder
where
N: Into<String>,
{
DeleteSecretBuilder::new(self.clone(), name.into())
}
/// Lists all the secrets in the Key Vault.
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
/// use futures::stream::StreamExt;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// std::sync::Arc::new(creds),
/// ).unwrap().secret_client();
/// let secrets = client.list_secrets().into_stream().next().await;
/// dbg!(&secrets);
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn list_secrets(&self) -> ListSecretsBuilder {
ListSecretsBuilder::new(self.clone())
}
/// Restores a backed up secret and all its versions.
/// This operation requires the secrets/restore permission.
///
/// # Example
///
/// ```no_run
/// use azure_security_keyvault::KeyvaultClient;
/// use azure_identity::DefaultAzureCredential;
/// use tokio::runtime::Runtime;
///
/// async fn example() {
/// let creds = DefaultAzureCredential::default();
/// let mut client = KeyvaultClient::new(
/// &"KEYVAULT_URL",
/// std::sync::Arc::new(creds),
/// ).unwrap().secret_client();
/// client.restore_secret("KUF6dXJlS2V5VmF1bHRTZWNyZXRCYWNrdXBWMS5taW").await.unwrap();
/// }
///
/// Runtime::new().unwrap().block_on(example());
/// ```
pub fn restore_secret<S>(&self, backup_blob: S) -> RestoreSecretBuilder
where
S: Into<String>,
{
RestoreSecretBuilder::new(self.clone(), backup_blob.into())
}
}