Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keyvault decrypt #333

Merged
merged 10 commits into from
Aug 12, 2021
Merged

add keyvault decrypt #333

merged 10 commits into from
Aug 12, 2021

Conversation

vincentserpoul
Copy link
Contributor

Nothing too fancy here, but I really quickly need the decrypt function for the vault

@ghost
Copy link

ghost commented Jul 27, 2021

CLA assistant check
All CLA requirements met.

Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contributoin. While we do want idiomatic code (naming conventions, usage patterns, etc.) we also want names to be consistent with existing Azure SDKs, such as the .NET SDKs I referenced a few times here, though a mix of Java and Python SDKs might be easier, but the main difference you'll see is that .NET uses PascalCase while Rust uses snake_case as you have done mostly.

#[derive(Debug, Deserialize, Getters)]
#[getset(get = "pub")]
pub struct DecryptResult {
aad: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be additional_authenticated_data. See https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/keyvault/Azure.Security.KeyVault.Keys/api/Azure.Security.KeyVault.Keys.netstandard2.0.cs for names, adjusting for idiomatic naming conventions, of course. We want Azure SDKs to be first and foremost idiomatic, but consistent second and naming is one of them.

This also shouldn't be defined on DecryptResult. It's passed into DecryptParameters (as it should be called). Specifically, see the classes at https://github.com/Azure/azure-sdk-for-net/blob/fe8e623c8ef31810a4712fb12fec4ecbc019859f/sdk/keyvault/Azure.Security.KeyVault.Keys/api/Azure.Security.KeyVault.Keys.netstandard2.0.cs#L395-L422.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at https://docs.microsoft.com/en-us/rest/api/keyvault/decrypt/decrypt#keyoperationresult
and the key operation result contain these field. I guess the azure sdk for .NET is more precise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we did change a few property names mainly to avoid abbreviations (we have guidelines against that). Abbreviations are great for a transform protocol but not always an SDK, or so the thinking goes. The SDK does also change the shape a bit for some things you haven't touched here, too (so not an issue with this PR).

#[getset(get = "pub")]
pub struct DecryptResult {
aad: Option<String>,
iv: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though aad and iv shouldn't be defined here, they are base64url-encoded and should be exposed as binary, i.e. Option<Vec<u8>>


#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EncryptionAlgorithm {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep consistent casing. If it's idiomatic for all-uppercase characters, do so throughout. See https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/keyvault/Azure.Security.KeyVault.Keys/src/Cryptography/EncryptionAlgorithm.cs for how .NET did this. IIRC, Java is all uppercase since that is idiomatic for Java statics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will follow this as well. But I was looking at the SignatureAlgorithms above, which doesn't follow the .NET.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point we're not concerned with breaking changes - in fact, there will be a lot more to come - so you could make SignatureAlgorithms match the guidance or ignore for now and I'll clean it up later.

Comment on lines 357 to 360
value: &str,
aad: Option<&str>,
iv: Option<&str>,
tag: Option<&str>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&mut self,
algorithm: EncryptionAlgorithm,
key_name: &str,
key_version: &str,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in DecryptParameters, version (not key_version) is optional despite what the swagger says (path variables can't be optional, but they handle this correctly on the server.

request_body.insert("alg".to_owned(), Value::String(algorithm.to_string()));
request_body.insert("value".to_owned(), Value::String(value.to_owned()));

if let Some(aad) = aad {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These members should be Vec<u8> (or Option<Vec<u8>>) and base64url-encoded here - don't make the user do it in their own code, which is inconsistent with our other Azure SDKs.

@vincentserpoul
Copy link
Contributor Author

I tried to look into the net, java, python, js sdk to get inspiration.
I believe some more tests on my side are in order.
I think macro could also help to be less verbose, but i am not sure you want to go down that path.

@vincentserpoul vincentserpoul requested a review from heaths August 8, 2021 09:38
Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks! Before I merge, though, I wanted to give you a chance to respond to one of my comments about simplifying.

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DecryptParametersEncryption {
Rsa15(Rsa15Parameters),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could all be simplified to families. RSA will only ever take the ciphertext and algorithm, while AES GCM will take the ciphertext, aad, and tag, and AES CBC the ciphertext and iv. Would it be possible to simplify these? Overall, though, I appreciate the attention to detail since the parameters do take different arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified but had to add an error in the process, in order to make sure the params can't be created with the wrong alg. Let me know if that's ok, or even if there is a better way

sdk/key_vault/src/key.rs Outdated Show resolved Hide resolved
@vincentserpoul
Copy link
Contributor Author

vincentserpoul commented Aug 12, 2021

updated with latest remarks, waiting for review

Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thank you. So much cleaner. Only one other change because inputs to encrypt and decrypt are different - at least for AES GCM. I'd just name all the structs *DecryptParameters. A couple may be duplicated for encryption, but should the models - or at least parameters to construct them - change in the future, we don't incur any breaking changes. I'll get it merged then.

sdk/key_vault/src/key.rs Show resolved Hide resolved
@vincentserpoul vincentserpoul requested a review from heaths August 12, 2021 16:38
Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. Thank you!

@heaths heaths merged commit dd07e20 into Azure:main Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants