-
Notifications
You must be signed in to change notification settings - Fork 4
Initial add of cached key decryptor #157
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
base: main
Are you sure you want to change the base?
Conversation
| private CompletableFuture<PlaintextDocument> decryptFields(Map<String, byte[]> document, | ||
| String documentEdek) { | ||
| // Check closed/expired state again before starting decryption | ||
| if (closed.get()) { | ||
| return CompletableFuture.failedFuture(new TscException( | ||
| TenantSecurityErrorCodes.DOCUMENT_DECRYPT_FAILED, "CachedKeyDecryptor has been closed")); | ||
| } | ||
| if (isExpired()) { | ||
| return CompletableFuture.failedFuture(new TscException( | ||
| TenantSecurityErrorCodes.DOCUMENT_DECRYPT_FAILED, "CachedKeyDecryptor has expired")); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-checking these feels really unnecessary to me. If you're following the calls from decrypt, there's not really any code between the check and the re-check, right? Or we could remove the checks from decrypt and only have them here
| // Parallel decrypt each field | ||
| Map<String, CompletableFuture<byte[]>> decryptOps = document.entrySet().stream() | ||
| .collect(Collectors.toMap(Map.Entry::getKey, | ||
| entry -> CompletableFuture.supplyAsync( | ||
| () -> CryptoUtils.decryptDocument(entry.getValue(), dek).join(), | ||
| encryptionExecutor))); | ||
|
|
||
| // Join all futures and build result | ||
| return CompletableFutures.tryCatchNonFatal(() -> { | ||
| Map<String, byte[]> decryptedBytes = decryptOps.entrySet().stream() | ||
| .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().join())); | ||
| return new PlaintextDocument(decryptedBytes, documentEdek); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bummer that this has to just be duplicated code
| if (closed.get()) { | ||
| return CompletableFuture.failedFuture(new TscException( | ||
| TenantSecurityErrorCodes.DOCUMENT_DECRYPT_FAILED, "CachedKeyDecryptor has been closed")); | ||
| } | ||
| if (isExpired()) { | ||
| return CompletableFuture.failedFuture(new TscException( | ||
| TenantSecurityErrorCodes.DOCUMENT_DECRYPT_FAILED, "CachedKeyDecryptor has expired")); | ||
| } | ||
|
|
||
| // Validate EDEK matches | ||
| if (!this.edek.equals(edek)) { | ||
| return CompletableFuture | ||
| .failedFuture(new TscException(TenantSecurityErrorCodes.DOCUMENT_DECRYPT_FAILED, | ||
| "Provided EDEK does not match the cached EDEK. " | ||
| + "This decryptor can only decrypt documents with matching EDEKs.")); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These checks should probably move to a function
| * This is the recommended pattern for using cached decryptors with CompletableFuture composition: | ||
| * | ||
| * <pre> | ||
| * client.withCachedDecryptor(edek, metadata, decryptor -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the > escaped here and below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you put a > inside a javadoc comment you have use the html escape for it. We've been really bad at putting examples on our other functions so you haven't seen it much.
This is a strawman implementation of the idea of caching a DEK for reuse for a short period of time.
TODO: