Skip to content

Commit f247f90

Browse files
authored
actively avoid panic at runtime in keyvault crate (#1312)
This PR updates a handful of operations to not use `unwrap` in the operations and adds clippy configuration to deny the use of `unwrap` or `expect` (with the exception of unit tests) in the crate moving forward. This also adds a top level clippy.toml that explicitly allows `unwrap` and `expect` when used within `#[cfg(test)]` blocks.
1 parent 6a95acf commit f247f90

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allow-unwrap-in-tests = true
2+
allow-expect-in-tests = true

sdk/security_keyvault/src/keys/operations/decrypt.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ impl DecryptBuilder {
2828

2929
let algorithm = match self.decrypt_parameters.decrypt_parameters_encryption {
3030
CryptographParamtersEncryption::Rsa(RsaEncryptionParameters { algorithm }) => {
31-
request_body
32-
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
31+
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
3332
algorithm
3433
}
3534
CryptographParamtersEncryption::AesGcm(AesGcmEncryptionParameters {
@@ -38,25 +37,21 @@ impl DecryptBuilder {
3837
authentication_tag,
3938
additional_authenticated_data,
4039
}) => {
40+
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
41+
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
4142
request_body
42-
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
43-
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
44-
request_body.insert(
45-
"tag".to_owned(),
46-
serde_json::to_value(authentication_tag).unwrap(),
47-
);
43+
.insert("tag".to_owned(), serde_json::to_value(authentication_tag)?);
4844
if let Some(aad) = additional_authenticated_data {
49-
request_body.insert("aad".to_owned(), serde_json::to_value(aad).unwrap());
45+
request_body.insert("aad".to_owned(), serde_json::to_value(aad)?);
5046
};
5147
algorithm
5248
}
5349
CryptographParamtersEncryption::AesCbc(AesCbcEncryptionParameters {
5450
algorithm,
5551
iv,
5652
}) => {
57-
request_body
58-
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
59-
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
53+
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
54+
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
6055
algorithm
6156
}
6257
};

sdk/security_keyvault/src/keys/operations/encrypt.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ impl EncryptBuilder {
2828

2929
let algorithm = match self.encrypt_parameters.encrypt_parameters_encryption {
3030
CryptographParamtersEncryption::Rsa(RsaEncryptionParameters { algorithm }) => {
31-
request_body
32-
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
31+
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
3332
algorithm
3433
}
3534
CryptographParamtersEncryption::AesGcm(AesGcmEncryptionParameters {
@@ -38,25 +37,21 @@ impl EncryptBuilder {
3837
authentication_tag,
3938
additional_authenticated_data,
4039
}) => {
40+
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
41+
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
4142
request_body
42-
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
43-
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
44-
request_body.insert(
45-
"tag".to_owned(),
46-
serde_json::to_value(authentication_tag).unwrap(),
47-
);
43+
.insert("tag".to_owned(), serde_json::to_value(authentication_tag)?);
4844
if let Some(aad) = additional_authenticated_data {
49-
request_body.insert("aad".to_owned(), serde_json::to_value(aad).unwrap());
45+
request_body.insert("aad".to_owned(), serde_json::to_value(aad)?);
5046
};
5147
algorithm
5248
}
5349
CryptographParamtersEncryption::AesCbc(AesCbcEncryptionParameters {
5450
algorithm,
5551
iv,
5652
}) => {
57-
request_body
58-
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
59-
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
53+
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
54+
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
6055
algorithm
6156
}
6257
};

sdk/security_keyvault/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! information on the project, and an overview of other crates, please refer to
55
//! [our GitHub repository](https://github.com/azure/azure-sdk-for-rust).
66
7+
#![deny(clippy::unwrap_used, clippy::expect_used)]
8+
79
#[macro_use]
810
extern crate azure_core;
911

0 commit comments

Comments
 (0)