Skip to content

Commit 5e3d035

Browse files
committed
Re-record tests and override rand::random
Had to trick cryptographic operations to use a recorded seed.
1 parent d01ede8 commit 5e3d035

File tree

9 files changed

+112
-47
lines changed

9 files changed

+112
-47
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/keyvault/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "rust",
44
"TagPrefix": "rust/keyvault",
5-
"Tag": "rust/keyvault_5961c5368d"
5+
"Tag": "rust/keyvault_257c804570"
66
}

sdk/keyvault/azure_security_keyvault_certificates/README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ az login
5050

5151
Instantiate a `DeveloperToolsCredential` to pass to the client. The same instance of a token credential can be used with multiple clients if they will be authenticating with the same identity.
5252

53+
### Instantiate a client
54+
55+
```rust no_run
56+
use azure_core::base64;
57+
use azure_identity::DeveloperToolsCredential;
58+
use azure_security_keyvault_certificates::CertificateClient;
59+
60+
#[tokio::main]
61+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
62+
// Create a new certificate client
63+
let credential = DeveloperToolsCredential::new(None)?;
64+
let client = CertificateClient::new(
65+
"https://your-key-vault-name.vault.azure.net/",
66+
credential.clone(),
67+
None,
68+
)?;
69+
70+
// Get a certificate using the certificate client.
71+
let certificate = client
72+
.get_certificate("certificate-name", None)
73+
.await?
74+
.into_model()?;
75+
println!(
76+
"Thumbprint: {:?}",
77+
certificate.x509_thumbprint.map(base64::encode_url_safe)
78+
);
79+
80+
Ok(())
81+
}
82+
```
83+
5384
## Key concepts
5485

5586
### Certificate
@@ -204,12 +235,10 @@ use azure_security_keyvault_certificates::{
204235
};
205236
use azure_security_keyvault_keys::{
206237
models::{SignParameters, SignatureAlgorithm},
207-
KeyClient,
208238
};
209239
use openssl::sha::sha256;
210240

211-
// Use test data to sign.
212-
let plaintext = "test data to sign";
241+
let plaintext = "plaintext";
213242

214243
// Create an EC certificate policy for signing.
215244
let policy = CertificatePolicy {
@@ -244,12 +273,7 @@ client
244273
// Hash the plaintext to be signed.
245274
let digest = sha256(plaintext.as_bytes()).to_vec();
246275

247-
// Create a KeyClient using the certificate to sign the digest.
248-
let key_client = KeyClient::new(
249-
client.endpoint().as_str(),
250-
recording.credential(),
251-
None,
252-
)?;
276+
// Use a KeyClient using the certificate to sign the digest.
253277
let body = SignParameters {
254278
algorithm: Some(SignatureAlgorithm::Es256),
255279
value: Some(digest),

sdk/keyvault/azure_security_keyvault_certificates/tests/readme.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use azure_core::{
77
time::Duration,
88
};
99
use azure_core_test::{recorded, TestContext, TestMode};
10-
use azure_security_keyvault_certificates::CertificateClient;
10+
use azure_security_keyvault_certificates::{CertificateClient, CertificateClientOptions};
11+
use azure_security_keyvault_keys::{KeyClient, KeyClientOptions};
1112
use include_file::include_markdown;
1213

1314
#[recorded::test]
1415
async fn readme(ctx: TestContext) -> Result<()> {
15-
use azure_security_keyvault_certificates::CertificateClientOptions;
16-
1716
let recording = ctx.recording();
1817

1918
let mut options = CertificateClientOptions::default();
@@ -25,6 +24,15 @@ async fn readme(ctx: TestContext) -> Result<()> {
2524
Some(options),
2625
)?;
2726

27+
let mut key_options = KeyClientOptions::default();
28+
recording.instrument(&mut key_options.client_options);
29+
30+
let key_client = KeyClient::new(
31+
client.endpoint().as_str(),
32+
recording.credential(),
33+
Some(key_options),
34+
)?;
35+
2836
// Each macro invocation is in its own block to prevent errors with duplicate imports.
2937
println!("Create a certificate");
3038
include_markdown!("README.md", "create_certificate", scope);

sdk/keyvault/azure_security_keyvault_keys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ azure_security_keyvault_test = { path = "../azure_security_keyvault_test" }
3232
criterion.workspace = true
3333
include-file.workspace = true
3434
rand.workspace = true
35+
rand_chacha.workspace = true
3536
reqwest.workspace = true
3637
sha2.workspace = true
3738
tokio.workspace = true

sdk/keyvault/azure_security_keyvault_keys/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ Use the `az keyvault security-domain download` command to download the security
7777
az keyvault security-domain download --hsm-name <your-key-vault-name> --sd-wrapping-keys ./certs/cert_0.cer ./certs/cert_1.cer ./certs/cert_2.cer --sd-quorum 2 --security-domain-file ContosoHSM-SD.json
7878
```
7979

80+
### Instantiate a client
81+
82+
```rust no_run
83+
use azure_identity::DeveloperToolsCredential;
84+
use azure_security_keyvault_keys::KeyClient;
85+
86+
#[tokio::main]
87+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
88+
// Create a new key client
89+
let credential = DeveloperToolsCredential::new(None)?;
90+
let client = KeyClient::new(
91+
"https://your-key-vault-name.vault.azure.net/",
92+
credential.clone(),
93+
None,
94+
)?;
95+
96+
// Get a key using the key client.
97+
let key = client
98+
.get_key("key-name", None)
99+
.await?
100+
.into_model()?;
101+
println!("JWT: {:?}", key.key);
102+
103+
Ok(())
104+
}
105+
```
106+
80107
## Key concepts
81108

82109
### Key
@@ -93,7 +120,7 @@ We guarantee that all client instance methods are thread-safe and independent of
93120

94121
## Examples
95122

96-
The following section provides several code snippets using a `KeyClient` like we instantiated above, covering some of the most common Azure Key Vault keys service related tasks:
123+
The following section provides several code snippets using a `KeyClient` like we [instantiated above](#instantiate-a-client):
97124

98125
* [Create a key](#create-a-key)
99126
* [Retrieve a key](#retrieve-a-key)

sdk/keyvault/azure_security_keyvault_keys/tests/readme.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ use azure_core::{
77
time::Duration,
88
};
99
use azure_core_test::{recorded, TestContext, TestMode};
10-
use azure_security_keyvault_keys::KeyClient;
10+
use azure_security_keyvault_keys::{KeyClient, KeyClientOptions};
1111
use include_file::include_markdown;
1212

1313
#[recorded::test]
1414
async fn readme(ctx: TestContext) -> Result<()> {
15-
use azure_security_keyvault_keys::KeyClientOptions;
16-
1715
let recording = ctx.recording();
1816

1917
let mut options = KeyClientOptions::default();
@@ -39,6 +37,7 @@ async fn readme(ctx: TestContext) -> Result<()> {
3937
include_markdown!("README.md", "list_keys", scope);
4038

4139
println!("Encrypt and decrypt");
40+
rand::seed(recording.random());
4241
include_markdown!("README.md", "encrypt_decrypt", scope);
4342

4443
println!("Handle errors");
@@ -63,3 +62,30 @@ async fn readme(ctx: TestContext) -> Result<()> {
6362

6463
Ok(())
6564
}
65+
66+
/// Override `use rand::random` import in README.md to use recorded seed.
67+
mod rand {
68+
// cspell:ignore Seedable
69+
#![allow(static_mut_refs)]
70+
use rand::{
71+
distr::{Distribution, StandardUniform},
72+
Rng, SeedableRng,
73+
};
74+
use rand_chacha::ChaCha20Rng;
75+
use std::sync::OnceLock;
76+
77+
static mut RNG: OnceLock<ChaCha20Rng> = OnceLock::new();
78+
79+
pub fn random<T>() -> T
80+
where
81+
StandardUniform: Distribution<T>,
82+
{
83+
unsafe { RNG.get_mut().expect("expected ChaCha20 rng").random() }
84+
}
85+
86+
pub fn seed(seed: [u8; 32]) {
87+
unsafe {
88+
RNG.set(ChaCha20Rng::from_seed(seed)).expect("set seed");
89+
}
90+
}
91+
}

sdk/keyvault/azure_security_keyvault_secrets/README.md

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@ az login
5050

5151
Instantiate a `DeveloperToolsCredential` to pass to the client. The same instance of a token credential can be used with multiple clients if they will be authenticating with the same identity.
5252

53-
### Set and Get a Secret
53+
### Instantiate a client
5454

5555
```rust no_run
5656
use azure_identity::DeveloperToolsCredential;
57-
use azure_security_keyvault_secrets::{
58-
models::{Secret, SecretClientGetSecretOptions, SetSecretParameters},
59-
ResourceExt, SecretClient,
60-
};
57+
use azure_security_keyvault_secrets::SecretClient;
6158

6259
#[tokio::main]
6360
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -69,29 +66,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6966
None,
7067
)?;
7168

72-
// Create a new secret using the secret client.
73-
let secret_set_parameters = SetSecretParameters {
74-
value: Some("secret-value".into()),
75-
..Default::default()
76-
};
77-
78-
let secret: Secret = client
79-
.set_secret("secret-name", secret_set_parameters.try_into()?, None)
80-
.await?
81-
.into_model()?;
82-
83-
// Get version of created secret.
84-
let secret_version = secret.resource_id()?.version;
85-
86-
// Retrieve a secret using the secret client.
87-
let secret: Secret = client
88-
.get_secret("secret-name", Some(SecretClientGetSecretOptions {
89-
secret_version,
90-
..Default::default()
91-
}))
69+
// Get a secret using the secret client.
70+
let secret = client
71+
.get_secret("secret-name", None)
9272
.await?
9373
.into_model()?;
94-
println!("{:?}", secret.value);
74+
println!("Secret: {:?}", secret.value);
9575

9676
Ok(())
9777
}
@@ -113,7 +93,7 @@ We guarantee that all client instance methods are thread-safe and independent of
11393

11494
## Examples
11595

116-
The following section provides several code snippets using a `SecretClient` like we instantiated above, covering some of the most common Azure Key Vault secrets service related tasks:
96+
The following section provides several code snippets using a `SecretClient` like we [instantiated above](#instantiate-a-client):
11797

11898
* [Create a secret](#create-a-secret)
11999
* [Retrieve a secret](#retrieve-a-secret)

sdk/keyvault/azure_security_keyvault_secrets/tests/readme.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ use azure_core::{
77
time::Duration,
88
};
99
use azure_core_test::{recorded, TestContext, TestMode};
10-
use azure_security_keyvault_secrets::SecretClient;
10+
use azure_security_keyvault_secrets::{SecretClient, SecretClientOptions};
1111
use include_file::include_markdown;
1212

1313
#[recorded::test]
1414
async fn readme(ctx: TestContext) -> Result<()> {
15-
use azure_security_keyvault_secrets::SecretClientOptions;
16-
1715
let recording = ctx.recording();
1816

1917
let mut options = SecretClientOptions::default();

0 commit comments

Comments
 (0)