Skip to content

Commit e67803a

Browse files
author
Dhriti Chopra
committed
feat(storage): add full object checksum validation for bidi flow
1 parent 91e8f36 commit e67803a

3 files changed

Lines changed: 489 additions & 2 deletions

File tree

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BaseObjectReadSessionStreamRead.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ private AccumulatingRead(
150150
IOAutoCloseable onCloseCallback) {
151151
super(rangeSpec, retryContext, onCloseCallback);
152152
this.readId = readId;
153-
this.hasher = hasher;
153+
this.hasher =
154+
(rangeSpec.begin() == 0)
155+
? new CumulativeHasher(hasher, 0, rangeSpec.maxLength())
156+
: hasher;
154157
this.complete = SettableApiFuture.create();
155158
this.childRefs = Collections.synchronizedList(new ArrayList<>());
156159
}
@@ -280,7 +283,10 @@ static class StreamingRead extends BaseObjectReadSessionStreamRead<ScatteringByt
280283
IOAutoCloseable onCloseCallback) {
281284
super(rangeSpec, retryContext, onCloseCallback);
282285
this.readId = new AtomicLong(readId);
283-
this.hasher = hasher;
286+
this.hasher =
287+
(rangeSpec.begin() == 0)
288+
? new CumulativeHasher(hasher, 0, rangeSpec.maxLength())
289+
: hasher;
284290
this.closed = false;
285291
this.failFuture = SettableApiFuture.create();
286292
this.queue = new ArrayBlockingQueue<>(2);

java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/ObjectReadSessionStream.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ public void onResponse(BidiReadObjectResponse response) {
351351
executor.execute(
352352
StorageException.liftToRunnable(
353353
() -> {
354+
try {
355+
validateCumulativeChecksum(read);
356+
} catch (UncheckedCumulativeChecksumMismatchException e) {
357+
state.removeOutstandingReadOnFailure(id, read::fail).onFailure(e);
358+
return;
359+
}
354360
read.eof();
355361
// don't remove the outstanding read until the future has been resolved
356362
state.removeOutstandingRead(id);
@@ -545,6 +551,15 @@ public void onComplete() {
545551
}
546552
}
547553

554+
private void validateCumulativeChecksum(ObjectReadSessionStreamRead<?> read) {
555+
Hasher hasher = read.hasher();
556+
if (hasher instanceof CumulativeHasher) {
557+
CumulativeHasher cumulativeHasher = (CumulativeHasher) hasher;
558+
com.google.storage.v2.Object metadata = state.getMetadata();
559+
cumulativeHasher.validateCumulativeChecksum(metadata);
560+
}
561+
}
562+
548563
static ObjectReadSessionStream create(
549564
ScheduledExecutorService executor,
550565
ZeroCopyBidiStreamingCallable<BidiReadObjectRequest, BidiReadObjectResponse> callable,

0 commit comments

Comments
 (0)