Skip to content

Commit

Permalink
actively avoid panic at runtime in keyvault crate (#1312)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
demoray committed Jun 27, 2023
1 parent 6a95acf commit f247f90
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allow-unwrap-in-tests = true
allow-expect-in-tests = true
19 changes: 7 additions & 12 deletions sdk/security_keyvault/src/keys/operations/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ impl DecryptBuilder {

let algorithm = match self.decrypt_parameters.decrypt_parameters_encryption {
CryptographParamtersEncryption::Rsa(RsaEncryptionParameters { algorithm }) => {
request_body
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
algorithm
}
CryptographParamtersEncryption::AesGcm(AesGcmEncryptionParameters {
Expand All @@ -38,25 +37,21 @@ impl DecryptBuilder {
authentication_tag,
additional_authenticated_data,
}) => {
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
request_body
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
request_body.insert(
"tag".to_owned(),
serde_json::to_value(authentication_tag).unwrap(),
);
.insert("tag".to_owned(), serde_json::to_value(authentication_tag)?);
if let Some(aad) = additional_authenticated_data {
request_body.insert("aad".to_owned(), serde_json::to_value(aad).unwrap());
request_body.insert("aad".to_owned(), serde_json::to_value(aad)?);
};
algorithm
}
CryptographParamtersEncryption::AesCbc(AesCbcEncryptionParameters {
algorithm,
iv,
}) => {
request_body
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
algorithm
}
};
Expand Down
19 changes: 7 additions & 12 deletions sdk/security_keyvault/src/keys/operations/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ impl EncryptBuilder {

let algorithm = match self.encrypt_parameters.encrypt_parameters_encryption {
CryptographParamtersEncryption::Rsa(RsaEncryptionParameters { algorithm }) => {
request_body
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
algorithm
}
CryptographParamtersEncryption::AesGcm(AesGcmEncryptionParameters {
Expand All @@ -38,25 +37,21 @@ impl EncryptBuilder {
authentication_tag,
additional_authenticated_data,
}) => {
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
request_body
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
request_body.insert(
"tag".to_owned(),
serde_json::to_value(authentication_tag).unwrap(),
);
.insert("tag".to_owned(), serde_json::to_value(authentication_tag)?);
if let Some(aad) = additional_authenticated_data {
request_body.insert("aad".to_owned(), serde_json::to_value(aad).unwrap());
request_body.insert("aad".to_owned(), serde_json::to_value(aad)?);
};
algorithm
}
CryptographParamtersEncryption::AesCbc(AesCbcEncryptionParameters {
algorithm,
iv,
}) => {
request_body
.insert("alg".to_owned(), serde_json::to_value(&algorithm).unwrap());
request_body.insert("iv".to_owned(), serde_json::to_value(iv).unwrap());
request_body.insert("alg".to_owned(), serde_json::to_value(&algorithm)?);
request_body.insert("iv".to_owned(), serde_json::to_value(iv)?);
algorithm
}
};
Expand Down
2 changes: 2 additions & 0 deletions sdk/security_keyvault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! information on the project, and an overview of other crates, please refer to
//! [our GitHub repository](https://github.com/azure/azure-sdk-for-rust).

#![deny(clippy::unwrap_used, clippy::expect_used)]

#[macro_use]
extern crate azure_core;

Expand Down

0 comments on commit f247f90

Please sign in to comment.