Skip to content
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

feat: separate flags for request/response E2E checksum and enable request checksum by default #1251

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions datastore-v1-proto-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.testparameterinjector</groupId>
<artifactId>test-parameter-injector</artifactId>
<version>1.14</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,37 @@ class RemoteRpc {
private final HttpRequestInitializer initializer;
private final String url;
private final AtomicInteger rpcCount = new AtomicInteger(0);
// Not final - so it can be set/reset in Unittests
private static boolean enableE2EChecksum =
Boolean.parseBoolean(System.getenv("GOOGLE_CLOUD_DATASTORE_HTTP_ENABLE_E2E_CHECKSUM"));
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
private static final String E2E_REQUEST_CHECKSUM_FLAG =
"GOOGLE_CLOUD_DATASTORE_HTTP_ENABLE_E2E_REQUEST_CHECKSUM";
private static final String E2E_RESPONSE_CHECKSUM_FLAG =
"GOOGLE_CLOUD_DATASTORE_HTTP_ENABLE_E2E_RESPONSE_CHECKSUM";
// By default request checksum is enabled.
// Not final - so it can be set/reset in Unittests.
private static boolean enableE2ERequestChecksum =
System.getenv(E2E_REQUEST_CHECKSUM_FLAG) == null
|| Boolean.parseBoolean(System.getenv(E2E_REQUEST_CHECKSUM_FLAG));

private static boolean enableE2EResponseChecksum =
Boolean.parseBoolean(System.getenv(E2E_RESPONSE_CHECKSUM_FLAG));

// Deprecated env var for enabling both request and response checksum.
private static final String E2E_CHECKSUM_FLAG_DEPRECATED =
"GOOGLE_CLOUD_DATASTORE_HTTP_ENABLE_E2E_CHECKSUM";

static {
if (System.getenv(E2E_CHECKSUM_FLAG_DEPRECATED) != null
&& System.getenv(E2E_REQUEST_CHECKSUM_FLAG) == null
&& System.getenv(E2E_RESPONSE_CHECKSUM_FLAG) == null) {
logger.warning(
String.format(
"%s environment variable is deprecated. "
+ "Please switch to using %s and/or %s to enable/disable "
+ "request and/or response checksum features.",
E2E_CHECKSUM_FLAG_DEPRECATED, E2E_REQUEST_CHECKSUM_FLAG, E2E_RESPONSE_CHECKSUM_FLAG));
enableE2ERequestChecksum = Boolean.parseBoolean(System.getenv(E2E_CHECKSUM_FLAG_DEPRECATED));
enableE2EResponseChecksum = enableE2ERequestChecksum;
}
}

RemoteRpc(HttpRequestFactory client, HttpRequestInitializer initializer, String url) {
this.client = client;
Expand Down Expand Up @@ -113,7 +141,7 @@ public InputStream call(
}
}
InputStream inputStream = httpResponse.getContent();
return enableE2EChecksum && EndToEndChecksumHandler.hasChecksumHeader(httpResponse)
return enableE2EResponseChecksum && EndToEndChecksumHandler.hasChecksumHeader(httpResponse)
? new ChecksumEnforcingInputStream(inputStream, httpResponse)
: inputStream;
} catch (SocketTimeoutException e) {
Expand All @@ -138,7 +166,7 @@ void setHeaders(
builder.append(databaseId);
}
httpRequest.getHeaders().put(X_GOOG_REQUEST_PARAMS_HEADER, builder.toString());
if (enableE2EChecksum && request != null) {
if (enableE2ERequestChecksum && request != null) {
String checksum = EndToEndChecksumHandler.computeChecksum(request.toByteArray());
if (checksum != null) {
httpRequest
Expand All @@ -154,8 +182,10 @@ HttpRequestFactory getClient() {
}

@VisibleForTesting
static void setSystemEnvE2EChecksum(boolean enableE2EChecksum) {
RemoteRpc.enableE2EChecksum = enableE2EChecksum;
static void setSystemEnvE2EChecksum(
boolean enableE2ERequestChecksum, boolean enableE2EResponseChecksum) {
RemoteRpc.enableE2ERequestChecksum = enableE2ERequestChecksum;
RemoteRpc.enableE2EResponseChecksum = enableE2EResponseChecksum;
}

void resetRpcCount() {
Expand Down
Loading