Skip to content

Reuse known keys in XmlKeyManager #54490

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

Merged
merged 3 commits into from
Apr 11, 2024
Merged

Reuse known keys in XmlKeyManager #54490

merged 3 commits into from
Apr 11, 2024

Conversation

amcasey
Copy link
Member

@amcasey amcasey commented Mar 11, 2024

Keys read from the IXmlRepository (as opposed to created) use deferred decryption but may still be decrypted multiple times. Since they're immutable (other than revocation), keep track of the keys we've seen and reuse them next time they're returned from the IXmlRepository.

In the absence of deletion support, we don't expect the list of keys to ever shrink, so there's no reason to prune the cache.

While this is likely a perf win, it's a small perf win and only once a day, so it would probably not be worthwhile on that basis alone. However, this should also reduce the number of calls to IXmlDecryptor.Decrypt (which may, e.g., make an Azure KeyVault request). Each time the key ring is refreshed, it requests all keys from the key manager, clobbering all keys that may have already been decrypted (at least the default key will have been). In the single app-instance case, for example, there should be no need to call Decrypt at all for any key created during the current execution of the app (old keys will have to be decrypted the first time they're used).

This shouldn't affect security because (a) the key ring already stores all the IKeys all the time and (b) IKey wraps the actual data in an ISecret.

It would have been nice to pass the key ring's list of known keys to the key manager, rather than having it store its own cache, but the key ring's storage format would require abstraction violations that would work poorly with our public extension points.

amcasey added 2 commits March 11, 2024 14:01
We weren't actually using polymorphism and this makes cloning easier.
Keys read from the IXmlRepository (as opposed to created) use deferred decryption but may still be decrypted multiple times.  Since they're immutable (other than revocation), keep track of the keys we've seen and reuse them next time they're returned from the IXmlRepository.

In the absence of deletion support, we don't expect the list of keys to ever shrink, so there's no reason to prune the cache.

While this is likely a perf win, it's a small perf win and only once a day, so it would probably not be worthwhile on that basis alone.  However, this should also reduce the number of calls to `IXmlDecryptor.Decrypt` (which may, e.g., make an Azure KeyVault request).  Each time the key ring is refreshed, it requests all keys from the key manager, clobbering all keys that may have already been decrypted (at least the default key will have been).  In the single app-instance case, for example, there should be no need to call Decrypt at all for any key created during the current execution of the app (old keys will have to be decrypted the first time they're used).

This shouldn't affect security because (a) the key ring already stores all the `IKeys` all the time and (b) `IKey` wraps the actual data in an `ISecret`.

It would have been nice to pass the key ring's list of known keys to the key manager, rather than having it store its own cache, but the key ring's storage format would require abstraction violations that would work poorly with our public extension points.
@ghost ghost added the area-dataprotection Includes: DataProtection label Mar 11, 2024
@@ -3,30 +3,156 @@

using System;
using System.Collections.Generic;
using System.Xml.Linq;
Copy link
Member Author

Choose a reason for hiding this comment

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

It would have been more sensible to treat KeyBase as the diff baseline, but I don't think I can alter that. Basically, Key and DeferredKey were just constructors, so I moved those two constructors into the base and made the existing constructor private.

IsRevoked = true;
}

internal Key Clone()
Copy link
Member Author

Choose a reason for hiding this comment

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

This is new.

/// <summary>
/// The basic implementation of <see cref="IKey"/>, where the <see cref="IAuthenticatedEncryptorDescriptor"/>
/// has already been created.
/// </summary>
public Key(
Copy link
Member Author

Choose a reason for hiding this comment

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

I could be persuaded that the constructors should be private and there should be Eager and Deferred factory methods.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Mar 19, 2024
@amcasey
Copy link
Member Author

amcasey commented Apr 10, 2024

This did a very good job of mitigating simulated IXmlDecryptor (e.g. AKV) errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dataprotection Includes: DataProtection pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants