2020
2121/**
2222 * Holds a cached DEK (Document Encryption Key) for repeated encrypt and decrypt operations without
23- * making additional TSP wrap/unwrap calls. All documents encrypted with this instance will share the
24- * same DEK/EDEK pair. The DEK is securely zeroed when close() is called.
23+ * making additional TSP wrap/unwrap calls. All documents encrypted with this instance will share
24+ * the same DEK/EDEK pair. The DEK is securely zeroed when close() is called.
2525 *
2626 * <p>
2727 * This class is thread-safe and can be used concurrently for multiple encrypt and decrypt
@@ -48,9 +48,8 @@ public final class CachedKey implements CachedEncryptor, CachedDecryptor {
4848 // Maximum time the cached key can be used before it expires
4949 private static final Duration TIMEOUT = Duration .ofMinutes (1 );
5050
51- private static final String EDEK_MISMATCH_MESSAGE =
52- "EDEK does not match the cached EDEK. "
53- + "This CachedKey can only decrypt documents with matching EDEKs." ;
51+ private static final String EDEK_MISMATCH_MESSAGE = "EDEK does not match the cached EDEK. "
52+ + "This CachedKey can only decrypt documents with matching EDEKs." ;
5453
5554 // The cached DEK bytes - zeroed on close()
5655 private final byte [] dek ;
@@ -186,15 +185,13 @@ public int getOperationCount() {
186185 * @param errorCode The error code to use for closed/expired failures
187186 */
188187 private <T > CompletableFuture <T > executeAndIncrement (Supplier <CompletableFuture <T >> operation ,
189- ToIntFunction <T > countOps , AtomicInteger counter ,
190- TenantSecurityErrorCodes errorCode ) {
188+ ToIntFunction <T > countOps , AtomicInteger counter , TenantSecurityErrorCodes errorCode ) {
191189 if (closed .get ()) {
192190 return CompletableFuture
193191 .failedFuture (new TscException (errorCode , "CachedKey has been closed" ));
194192 }
195193 if (isExpired ()) {
196- return CompletableFuture
197- .failedFuture (new TscException (errorCode , "CachedKey has expired" ));
194+ return CompletableFuture .failedFuture (new TscException (errorCode , "CachedKey has expired" ));
198195 }
199196 return operation .get ().thenApply (result -> {
200197 counter .addAndGet (countOps .applyAsInt (result ));
@@ -217,9 +214,8 @@ public CompletableFuture<EncryptedDocument> encrypt(Map<String, byte[]> document
217214 public CompletableFuture <StreamingResponse > encryptStream (InputStream input , OutputStream output ,
218215 DocumentMetadata metadata ) {
219216 return executeAndIncrement (
220- () -> CompletableFuture .supplyAsync (
221- () -> CryptoUtils .encryptStreamInternal (dek , metadata , input , output , secureRandom )
222- .join (),
217+ () -> CompletableFuture .supplyAsync (() -> CryptoUtils
218+ .encryptStreamInternal (dek , metadata , input , output , secureRandom ).join (),
223219 encryptionExecutor ).thenApply (v -> new StreamingResponse (edek )),
224220 result -> 1 , encryptCount , TenantSecurityErrorCodes .DOCUMENT_ENCRYPT_FAILED );
225221 }
@@ -229,9 +225,8 @@ public CompletableFuture<BatchResult<EncryptedDocument>> encryptBatch(
229225 Map <String , Map <String , byte []>> plaintextDocuments , DocumentMetadata metadata ) {
230226 return executeAndIncrement (() -> {
231227 ConcurrentMap <String , CompletableFuture <EncryptedDocument >> ops = new ConcurrentHashMap <>();
232- plaintextDocuments .forEach ((id , doc ) -> ops .put (id ,
233- DocumentCryptoOps .encryptFields (doc , metadata , dek , edek , encryptionExecutor ,
234- secureRandom )));
228+ plaintextDocuments .forEach ((id , doc ) -> ops .put (id , DocumentCryptoOps .encryptFields (doc ,
229+ metadata , dek , edek , encryptionExecutor , secureRandom )));
235230 return CompletableFuture .supplyAsync (() -> DocumentCryptoOps .cryptoOperationToBatchResult (ops ,
236231 TenantSecurityErrorCodes .DOCUMENT_ENCRYPT_FAILED ));
237232 }, result -> result .getSuccesses ().size (), encryptCount ,
@@ -243,9 +238,8 @@ public CompletableFuture<BatchResult<EncryptedDocument>> encryptBatch(
243238 private CompletableFuture <PlaintextDocument > validateEdekAndDecrypt (
244239 EncryptedDocument encryptedDocument ) {
245240 if (!edek .equals (encryptedDocument .getEdek ())) {
246- return CompletableFuture .failedFuture (
247- new TscException (TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED ,
248- EDEK_MISMATCH_MESSAGE ));
241+ return CompletableFuture .failedFuture (new TscException (
242+ TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED , EDEK_MISMATCH_MESSAGE ));
249243 }
250244 return DocumentCryptoOps .decryptFields (encryptedDocument .getEncryptedFields (), dek ,
251245 encryptedDocument .getEdek (), encryptionExecutor );
@@ -263,13 +257,12 @@ public CompletableFuture<Void> decryptStream(String edek, InputStream input, Out
263257 DocumentMetadata metadata ) {
264258 return executeAndIncrement (() -> {
265259 if (!this .edek .equals (edek )) {
266- return CompletableFuture
267- .<Void >failedFuture (new TscException (TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED ,
268- EDEK_MISMATCH_MESSAGE ));
260+ return CompletableFuture .<Void >failedFuture (new TscException (
261+ TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED , EDEK_MISMATCH_MESSAGE ));
269262 }
270- return CompletableFuture
271- . supplyAsync ( () -> CryptoUtils .decryptStreamInternal (this .dek , input , output ).join (),
272- encryptionExecutor );
263+ return CompletableFuture . supplyAsync (
264+ () -> CryptoUtils .decryptStreamInternal (this .dek , input , output ).join (),
265+ encryptionExecutor );
273266 }, result -> 1 , decryptCount , TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED );
274267 }
275268
@@ -282,9 +275,8 @@ public CompletableFuture<BatchResult<PlaintextDocument>> decryptBatch(
282275
283276 encryptedDocuments .forEach ((id , encDoc ) -> {
284277 if (!edek .equals (encDoc .getEdek ())) {
285- edekMismatches .put (id ,
286- new TscException (TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED ,
287- EDEK_MISMATCH_MESSAGE ));
278+ edekMismatches .put (id , new TscException (TenantSecurityErrorCodes .DOCUMENT_DECRYPT_FAILED ,
279+ EDEK_MISMATCH_MESSAGE ));
288280 } else {
289281 ops .put (id , DocumentCryptoOps .decryptFields (encDoc .getEncryptedFields (), dek ,
290282 encDoc .getEdek (), encryptionExecutor ));
0 commit comments