Skip to content

Commit 56c32bb

Browse files
key_client: supply GetRandomBytes API. (#1271)
1 parent 6685f31 commit 56c32bb

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

sdk/core/src/bytes_stream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Stream for BytesStream {
4343
self: Pin<&mut Self>,
4444
_cx: &mut std::task::Context<'_>,
4545
) -> Poll<Option<Self::Item>> {
46-
let mut self_mut = self.get_mut();
46+
let self_mut = self.get_mut();
4747

4848
// we return all the available bytes in one call.
4949
if self_mut.bytes_read < self_mut.bytes.len() {
@@ -75,7 +75,7 @@ impl AsyncRead for BytesStream {
7575
_cx: &mut std::task::Context<'_>,
7676
buf: &mut [u8],
7777
) -> Poll<std::io::Result<usize>> {
78-
let mut self_mut = self.get_mut();
78+
let self_mut = self.get_mut();
7979

8080
if self_mut.bytes_read < self_mut.bytes.len() {
8181
let bytes_read = self_mut.bytes_read;

sdk/security_keyvault/src/clients/key_client.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ impl KeyClient {
8585
{
8686
EncryptBuilder::new(self.clone(), name.into(), encrypt_parameters)
8787
}
88+
89+
/// Get the requested number of bytes containing random values from a managed HSM.
90+
///
91+
/// The `count` parameter is limited to a range between 1 and 128 inclusive.
92+
///
93+
/// This operation requires the `rng` permission to be granted to the HSM. Furthermore,
94+
/// it is only valid for clients that have been built using HSM URLs.
95+
///
96+
/// POST {managedHsmBaseUrl}/rng?api-version=7.4
97+
pub fn get_random_bytes<N>(&self, hsm_name: N, count: u8) -> GetRandomBytesBuilder
98+
where
99+
N: Into<String>,
100+
{
101+
GetRandomBytesBuilder::new(self.clone(), hsm_name.into(), count)
102+
}
88103
}

sdk/security_keyvault/src/keys/models.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,10 @@ pub struct EncryptResult {
403403
)]
404404
pub result: Vec<u8>,
405405
}
406+
407+
#[derive(Debug, Deserialize)]
408+
pub struct GetRandomBytesResult {
409+
/// `value` is encoded as a base64url string.
410+
#[serde(rename = "value", deserialize_with = "deser_base64")]
411+
pub result: Vec<u8>,
412+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::prelude::*;
2+
use azure_core::{headers::Headers, CollectedResponse, Method};
3+
use serde_json::{Map, Value};
4+
5+
operation! {
6+
GetRandomBytes,
7+
client: KeyClient,
8+
hsm_name: String,
9+
count: u8,
10+
}
11+
12+
impl GetRandomBytesBuilder {
13+
pub fn into_future(mut self) -> GetRandomBytes {
14+
Box::pin(async move {
15+
// POST {HSMBaseUrl}//rng?api-version=7.4
16+
let vault_url = format!("https://{}.managedhsm.azure.net/", self.hsm_name);
17+
let mut uri = url::Url::parse(&vault_url)?;
18+
let path = "rng".to_string();
19+
20+
uri.set_path(&path);
21+
22+
let mut request_body = Map::new();
23+
request_body.insert("count".to_owned(), Value::from(self.count));
24+
25+
let headers = Headers::new();
26+
let mut request = self.client.keyvault_client.finalize_request(
27+
uri,
28+
Method::Post,
29+
headers,
30+
Some(Value::Object(request_body).to_string().into()),
31+
)?;
32+
33+
let response = self
34+
.client
35+
.keyvault_client
36+
.send(&mut self.context, &mut request)
37+
.await?;
38+
39+
let response = CollectedResponse::from_response(response).await?;
40+
let body = response.body();
41+
42+
let result = serde_json::from_slice::<GetRandomBytesResult>(body)?;
43+
Ok(result)
44+
})
45+
}
46+
}
47+
48+
type GetRandomBytesResponse = GetRandomBytesResult;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
mod decrypt;
22
mod encrypt;
33
mod get_key;
4+
mod get_random_bytes;
45
mod sign;
56
pub use decrypt::*;
67
pub use encrypt::*;
78
pub use get_key::*;
9+
pub use get_random_bytes::*;
810
pub use sign::*;

0 commit comments

Comments
 (0)