Skip to content

Commit cb5aba6

Browse files
authored
separate parameter lifetime from token credential lifetime (#353)
* separate parameter lifetime from token credential lifetime * use lifetime elision and add example to show use with differing lifetimes * format example
1 parent d15c5d2 commit cb5aba6

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

sdk/key_vault/examples/pass_client.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use azure_identity::token_credentials::{ClientSecretCredential, TokenCredentialOptions};
2+
use azure_key_vault::KeyClient;
3+
use std::env;
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable.");
8+
let client_secret =
9+
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable.");
10+
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
11+
let keyvault_url =
12+
env::var("KEYVAULT_URL").expect("Missing KEYVAULT_URL environment variable.");
13+
14+
let creds = ClientSecretCredential::new(
15+
tenant_id,
16+
client_id,
17+
client_secret,
18+
TokenCredentialOptions::default(),
19+
);
20+
let mut client = KeyClient::new(&keyvault_url, &creds)?;
21+
22+
get_secret(&mut client).await?;
23+
24+
Ok(())
25+
}
26+
27+
async fn get_secret(
28+
client: &mut KeyClient<'_, ClientSecretCredential>,
29+
) -> Result<(), Box<dyn std::error::Error>> {
30+
let secret_name = env::var("SECRET_NAME").expect("Missing SECRET_NAME environment variable.");
31+
let secrets = client.get_secret(&secret_name).await?;
32+
dbg!(&secrets);
33+
34+
Ok(())
35+
}

sdk/key_vault/src/secret.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
138138
///
139139
/// Runtime::new().unwrap().block_on(example());
140140
/// ```
141-
pub async fn get_secret(&mut self, secret_name: &'a str) -> Result<KeyVaultSecret, Error> {
141+
pub async fn get_secret(&mut self, secret_name: &str) -> Result<KeyVaultSecret, Error> {
142142
Ok(self.get_secret_with_version(secret_name, "").await?)
143143
}
144144

@@ -166,8 +166,8 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
166166
/// ```
167167
pub async fn get_secret_with_version(
168168
&mut self,
169-
secret_name: &'a str,
170-
secret_version_name: &'a str,
169+
secret_name: &str,
170+
secret_version_name: &str,
171171
) -> Result<KeyVaultSecret, Error> {
172172
let mut uri = self.vault_url.clone();
173173
uri.set_path(&format!("secrets/{}/{}", secret_name, secret_version_name));
@@ -268,7 +268,7 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
268268
/// ```
269269
pub async fn get_secret_versions(
270270
&mut self,
271-
secret_name: &'a str,
271+
secret_name: &str,
272272
) -> Result<Vec<KeyVaultSecretBaseIdentifier>, Error> {
273273
let mut secret_versions = Vec::<KeyVaultSecretBaseIdentifier>::new();
274274

@@ -332,8 +332,8 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
332332
/// ```
333333
pub async fn set_secret(
334334
&mut self,
335-
secret_name: &'a str,
336-
new_secret_value: &'a str,
335+
secret_name: &str,
336+
new_secret_value: &str,
337337
) -> Result<(), Error> {
338338
let mut uri = self.vault_url.clone();
339339
uri.set_path(&format!("secrets/{}", secret_name));
@@ -379,8 +379,8 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
379379
/// ```
380380
pub async fn update_secret_enabled(
381381
&mut self,
382-
secret_name: &'a str,
383-
secret_version: &'a str,
382+
secret_name: &str,
383+
secret_version: &str,
384384
enabled: bool,
385385
) -> Result<(), Error> {
386386
let mut attributes = Map::new();
@@ -420,8 +420,8 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
420420
/// ```
421421
pub async fn update_secret_recovery_level(
422422
&mut self,
423-
secret_name: &'a str,
424-
secret_version: &'a str,
423+
secret_name: &str,
424+
secret_version: &str,
425425
recovery_level: RecoveryLevel,
426426
) -> Result<(), Error> {
427427
let mut attributes = Map::new();
@@ -465,8 +465,8 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
465465
/// ```
466466
pub async fn update_secret_expiration_time(
467467
&mut self,
468-
secret_name: &'a str,
469-
secret_version: &'a str,
468+
secret_name: &str,
469+
secret_version: &str,
470470
expiration_time: DateTime<Utc>,
471471
) -> Result<(), Error> {
472472
let mut attributes = Map::new();
@@ -483,8 +483,8 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
483483

484484
async fn update_secret(
485485
&mut self,
486-
secret_name: &'a str,
487-
secret_version: &'a str,
486+
secret_name: &str,
487+
secret_version: &str,
488488
attributes: Map<String, Value>,
489489
) -> Result<(), Error> {
490490
let mut uri = self.vault_url.clone();
@@ -521,7 +521,7 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
521521
///
522522
/// Runtime::new().unwrap().block_on(example());
523523
/// ```
524-
pub async fn restore_secret(&mut self, backup_blob: &'a str) -> Result<(), Error> {
524+
pub async fn restore_secret(&mut self, backup_blob: &str) -> Result<(), Error> {
525525
let mut uri = self.vault_url.clone();
526526
uri.set_path("secrets/restore");
527527
uri.set_query(Some(API_VERSION_PARAM));
@@ -561,7 +561,7 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
561561
/// ```
562562
pub async fn backup_secret(
563563
&mut self,
564-
secret_name: &'a str,
564+
secret_name: &str,
565565
) -> Result<KeyVaultSecretBackupBlob, Error> {
566566
let mut uri = self.vault_url.clone();
567567
uri.set_path(&format!("secrets/{}/backup", secret_name));
@@ -604,7 +604,7 @@ impl<'a, T: TokenCredential> KeyClient<'a, T> {
604604
///
605605
/// Runtime::new().unwrap().block_on(example());
606606
/// ```
607-
pub async fn delete_secret(&mut self, secret_name: &'a str) -> Result<(), Error> {
607+
pub async fn delete_secret(&mut self, secret_name: &str) -> Result<(), Error> {
608608
let mut uri = self.vault_url.clone();
609609
uri.set_path(&format!("secrets/{}", secret_name));
610610
uri.set_query(Some(API_VERSION_PARAM));

0 commit comments

Comments
 (0)