Skip to content

Commit 3a63461

Browse files
committed
Address some feedback
1 parent 692567a commit 3a63461

4 files changed

Lines changed: 330 additions & 476 deletions

File tree

src/main/java/com/ironcorelabs/tenantsecurity/kms/v1/CachedKey.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ private <T> CompletableFuture<T> executeAndIncrement(Supplier<CompletableFuture<
199199
});
200200
}
201201

202-
// === Encrypt operations ===
203-
204202
@Override
205203
public CompletableFuture<EncryptedDocument> encrypt(Map<String, byte[]> document,
206204
DocumentMetadata metadata) {
@@ -233,8 +231,6 @@ public CompletableFuture<BatchResult<EncryptedDocument>> encryptBatch(
233231
TenantSecurityErrorCodes.DOCUMENT_ENCRYPT_FAILED);
234232
}
235233

236-
// === Decrypt operations ===
237-
238234
private CompletableFuture<PlaintextDocument> validateEdekAndDecrypt(
239235
EncryptedDocument encryptedDocument) {
240236
if (!edek.equals(encryptedDocument.getEdek())) {

src/main/java/com/ironcorelabs/tenantsecurity/kms/v1/TenantSecurityClient.java

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,6 @@ public CompletableFuture<PlaintextDocument> decrypt(EncryptedDocument encryptedD
488488
encryptedDocument.getEdek(), encryptionExecutor));
489489
}
490490

491-
// === Private helpers for creating CachedKey instances ===
492-
493491
private CompletableFuture<CachedKey> newCachedKeyFromUnwrap(String edek,
494492
DocumentMetadata metadata) {
495493
return this.encryptionService.unwrapKey(edek, metadata).thenApply(dekBytes -> {
@@ -519,8 +517,6 @@ private <K extends CachedKeyLifecycle, T> CompletableFuture<T> withCachedResourc
519517
return resource.thenCompose(k -> operation.apply(k).whenComplete((result, error) -> k.close()));
520518
}
521519

522-
// === Cached decryptor factory methods ===
523-
524520
/**
525521
* Create a CachedDecryptor for repeated decrypt operations using the same DEK. This unwraps the
526522
* EDEK once and caches the resulting DEK for subsequent decrypts.
@@ -531,22 +527,17 @@ private <K extends CachedKeyLifecycle, T> CompletableFuture<T> withCachedResourc
531527
*
532528
* <p>
533529
* The returned CachedDecryptor implements Closeable and should be used with try-with-resources to
534-
* ensure the DEK is securely zeroed when done:
535-
*
536-
* <pre>
537-
* try (CachedDecryptor decryptor = client.createCachedDecryptor(edek, metadata).get()) {
538-
* PlaintextDocument doc1 = decryptor.decrypt(encDoc1, metadata).get();
539-
* PlaintextDocument doc2 = decryptor.decrypt(encDoc2, metadata).get();
540-
* }
541-
* </pre>
530+
* ensure the DEK is securely zeroed when done.
542531
*
543532
* @param edek The encrypted document encryption key to unwrap
544533
* @param metadata Metadata for the unwrap operation
545534
* @return CompletableFuture resolving to a CachedDecryptor
546535
*/
547536
public CompletableFuture<CachedDecryptor> createCachedDecryptor(String edek,
548537
DocumentMetadata metadata) {
549-
return newCachedKeyFromUnwrap(edek, metadata).thenApply(k -> k);
538+
return newCachedKeyFromUnwrap(edek, metadata)
539+
// narrow the returned type to be a CachedDecryptor instead of a full CachedKey
540+
.thenApply(k -> k);
550541
}
551542

552543
/**
@@ -567,14 +558,6 @@ public CompletableFuture<CachedDecryptor> createCachedDecryptor(
567558
* key is automatically closed (and DEK zeroed) when the operation completes, whether successfully
568559
* or with an error.
569560
*
570-
* <p>
571-
* This is the recommended pattern for using cached decryptors with CompletableFuture composition:
572-
*
573-
* <pre>
574-
* client.withCachedDecryptor(edek, metadata, decryptor -&gt; decryptor.decrypt(encDoc1, metadata)
575-
* .thenCompose(doc1 -&gt; decryptor.decrypt(encDoc2, metadata)))
576-
* </pre>
577-
*
578561
* @param <T> The type returned by the operation
579562
* @param edek The encrypted document encryption key to unwrap
580563
* @param metadata Metadata for the unwrap operation
@@ -586,23 +569,6 @@ public <T> CompletableFuture<T> withCachedDecryptor(String edek, DocumentMetadat
586569
return withCachedResource(createCachedDecryptor(edek, metadata), operation);
587570
}
588571

589-
/**
590-
* Execute an operation using a CachedDecryptor with automatic lifecycle management. Convenience
591-
* method that extracts the EDEK from the document.
592-
*
593-
* @param <T> The type returned by the operation
594-
* @param encryptedDocument The encrypted document whose EDEK should be unwrapped
595-
* @param metadata Metadata for the unwrap operation
596-
* @param operation Function that takes the CachedDecryptor and returns a CompletableFuture
597-
* @return CompletableFuture resolving to the operation's result
598-
*/
599-
public <T> CompletableFuture<T> withCachedDecryptor(EncryptedDocument encryptedDocument,
600-
DocumentMetadata metadata, Function<CachedDecryptor, CompletableFuture<T>> operation) {
601-
return withCachedDecryptor(encryptedDocument.getEdek(), metadata, operation);
602-
}
603-
604-
// === Cached encryptor factory methods ===
605-
606572
/**
607573
* Create a CachedEncryptor for repeated encrypt operations using the same DEK. This wraps a new
608574
* key once and caches the resulting DEK/EDEK pair for subsequent encrypts. All documents
@@ -614,35 +580,22 @@ public <T> CompletableFuture<T> withCachedDecryptor(EncryptedDocument encryptedD
614580
*
615581
* <p>
616582
* The returned CachedEncryptor implements Closeable and should be used with try-with-resources to
617-
* ensure the DEK is securely zeroed when done:
618-
*
619-
* <pre>
620-
* try (CachedEncryptor encryptor = client.createCachedEncryptor(metadata).get()) {
621-
* EncryptedDocument enc1 = encryptor.encrypt(doc1, metadata).get();
622-
* EncryptedDocument enc2 = encryptor.encrypt(doc2, metadata).get();
623-
* }
624-
* </pre>
583+
* ensure the DEK is securely zeroed when done.
625584
*
626585
* @param metadata Metadata for the wrap operation
627586
* @return CompletableFuture resolving to a CachedEncryptor
628587
*/
629588
public CompletableFuture<CachedEncryptor> createCachedEncryptor(DocumentMetadata metadata) {
630-
return newCachedKeyFromWrap(metadata).thenApply(k -> k);
589+
return newCachedKeyFromWrap(metadata)
590+
// narrow the returned type to be a CachedEncryptor instead of a full CachedKey
591+
.thenApply(k -> k);
631592
}
632593

633594
/**
634595
* Execute an operation using a CachedEncryptor with automatic lifecycle management. The cached
635596
* key is automatically closed (and DEK zeroed) when the operation completes, whether successfully
636597
* or with an error.
637598
*
638-
* <p>
639-
* This is the recommended pattern for using cached encryptors with CompletableFuture composition:
640-
*
641-
* <pre>
642-
* client.withCachedEncryptor(metadata, encryptor -&gt; encryptor.encrypt(doc1, metadata)
643-
* .thenCompose(enc1 -&gt; encryptor.encrypt(doc2, metadata)))
644-
* </pre>
645-
*
646599
* @param <T> The type returned by the operation
647600
* @param metadata Metadata for the wrap operation
648601
* @param operation Function that takes the CachedEncryptor and returns a CompletableFuture
@@ -653,8 +606,6 @@ public <T> CompletableFuture<T> withCachedEncryptor(DocumentMetadata metadata,
653606
return withCachedResource(createCachedEncryptor(metadata), operation);
654607
}
655608

656-
// === CachedKey factory methods (full encrypt + decrypt access) ===
657-
658609
/**
659610
* Create a CachedKey for both encrypt and decrypt operations. Wraps a new key and caches the
660611
* resulting DEK/EDEK pair.
@@ -682,19 +633,6 @@ public CompletableFuture<CachedKey> createCachedKey(String edek, DocumentMetadat
682633
return newCachedKeyFromUnwrap(edek, metadata);
683634
}
684635

685-
/**
686-
* Create a CachedKey for both encrypt and decrypt operations from an existing EncryptedDocument.
687-
* Convenience method that extracts the EDEK from the document.
688-
*
689-
* @param encryptedDocument The encrypted document whose EDEK should be unwrapped
690-
* @param metadata Metadata for the unwrap operation
691-
* @return CompletableFuture resolving to a CachedKey
692-
*/
693-
public CompletableFuture<CachedKey> createCachedKey(EncryptedDocument encryptedDocument,
694-
DocumentMetadata metadata) {
695-
return createCachedKey(encryptedDocument.getEdek(), metadata);
696-
}
697-
698636
/**
699637
* Execute an operation using a CachedKey with automatic lifecycle management. Wraps a new key and
700638
* provides full encrypt + decrypt access.

0 commit comments

Comments
 (0)