Skip to content

Commit

Permalink
chore: privatize invocation flag (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyn committed Apr 11, 2022
1 parent a4f9503 commit 0cd80e3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Builder setStorageRetryStrategy(StorageRetryStrategy storageRetryStrategy
* @param includeInvocationId a boolean to change enablement of invocation id
* @return the builder
*/
public Builder setIncludeInvocationId(boolean includeInvocationId) {
Builder setIncludeInvocationId(boolean includeInvocationId) {
this.includeInvocationId = includeInvocationId;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@

package com.google.cloud.storage;

import static com.google.cloud.storage.spi.v1.HttpRpcContextTest.*;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.cloud.TransportOptions;
import com.google.cloud.Tuple;
import com.google.cloud.http.HttpTransportOptions;
import com.google.cloud.storage.spi.v1.AuditingHttpTransport;
import java.util.Optional;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -81,4 +87,35 @@ public void testDisableInvocationId() {

assertFalse(opts1.isIncludeInvocationId());
}

@Test
public void testInvocationIdIsNotPassedThroughWhenDisabled() {
MockLowLevelHttpResponse response =
new MockLowLevelHttpResponse()
.setContentType("application/json")
.setContent(
"{\n"
+ " \"kind\": \"storage#serviceAccount\",\n"
+ " \"email_address\": \"service-234234@gs-project-accounts.iam.gserviceaccount.com\"\n"
+ "}\n")
.setStatusCode(200);
AuditingHttpTransport transport = new AuditingHttpTransport(response);
TransportOptions transportOptions =
HttpTransportOptions.newBuilder().setHttpTransportFactory(() -> transport).build();
Storage service =
StorageOptions.getDefaultInstance()
.toBuilder()
.setTransportOptions(transportOptions)
.setIncludeInvocationId(false)
.build()
.getService();
service.getServiceAccount("test-project");
Optional<Tuple<String, String>> anyXGoogApiClientWithGcclInvocationId =
transport.getAddHeaderCalls().stream()
.filter(t -> "x-goog-api-client".equals(t.x()) && t.y().contains("gccl-invocation-id/"))
.findFirst();

assertFalse(anyXGoogApiClientWithGcclInvocationId.isPresent());
assertThat(transport.getBuildRequestCalls()).hasSize(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.storage.spi.v1;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.cloud.Tuple;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class AuditingHttpTransport extends HttpTransport {
private final LowLevelHttpResponse response;
private final List<Tuple<String, String>> buildRequestCalls;
private final List<Tuple<String, String>> addHeaderCalls;

public AuditingHttpTransport(LowLevelHttpResponse response) {
this.response = response;
this.buildRequestCalls = Collections.synchronizedList(new ArrayList<>());
this.addHeaderCalls = Collections.synchronizedList(new ArrayList<>());
}

public List<Tuple<String, String>> getBuildRequestCalls() {
return ImmutableList.copyOf(buildRequestCalls);
}

public List<Tuple<String, String>> getAddHeaderCalls() {
return ImmutableList.copyOf(addHeaderCalls);
}

@Override
protected LowLevelHttpRequest buildRequest(String method, String url) {
buildRequestCalls.add(Tuple.of(method, url));
return new LowLevelHttpRequest() {
@Override
public void addHeader(String name, String value) {
addHeaderCalls.add(Tuple.of(name, value));
}

@Override
public LowLevelHttpResponse execute() {
return response;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.cloud.TransportOptions;
import com.google.cloud.Tuple;
Expand All @@ -35,9 +32,6 @@
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.Test;
Expand Down Expand Up @@ -84,37 +78,6 @@ public void testInvocationIdIsPassedThrough() {
assertThat(transport.getBuildRequestCalls()).hasSize(1);
}

@Test
public void testInvocationIdIsNotPassedThroughWhenDisabled() {
MockLowLevelHttpResponse response =
new MockLowLevelHttpResponse()
.setContentType("application/json")
.setContent(
"{\n"
+ " \"kind\": \"storage#serviceAccount\",\n"
+ " \"email_address\": \"service-234234@gs-project-accounts.iam.gserviceaccount.com\"\n"
+ "}\n")
.setStatusCode(200);
AuditingHttpTransport transport = new AuditingHttpTransport(response);
TransportOptions transportOptions =
HttpTransportOptions.newBuilder().setHttpTransportFactory(() -> transport).build();
Storage service =
StorageOptions.getDefaultInstance()
.toBuilder()
.setTransportOptions(transportOptions)
.setIncludeInvocationId(false)
.build()
.getService();
service.getServiceAccount("test-project");
Optional<Tuple<String, String>> anyXGoogApiClientWithGcclInvocationId =
transport.getAddHeaderCalls().stream()
.filter(t -> "x-goog-api-client".equals(t.x()) && t.y().contains("gccl-invocation-id/"))
.findFirst();

assertFalse(anyXGoogApiClientWithGcclInvocationId.isPresent());
assertThat(transport.getBuildRequestCalls()).hasSize(1);
}

@Test
public void testInvocationIdNotInSignedURL_v2() throws IOException {
URL signedUrlV2 =
Expand Down Expand Up @@ -156,40 +119,4 @@ private void doTestInvocationIdNotInSignedURL(URL signedUrl) throws IOException
assertFalse(anyXGoogApiClientWithGcclInvocationId.isPresent());
assertThat(transport.getBuildRequestCalls()).hasSize(1);
}

private static final class AuditingHttpTransport extends HttpTransport {
private final LowLevelHttpResponse response;
private final List<Tuple<String, String>> buildRequestCalls;
private final List<Tuple<String, String>> addHeaderCalls;

private AuditingHttpTransport(LowLevelHttpResponse response) {
this.response = response;
this.buildRequestCalls = Collections.synchronizedList(new ArrayList<>());
this.addHeaderCalls = Collections.synchronizedList(new ArrayList<>());
}

public List<Tuple<String, String>> getBuildRequestCalls() {
return ImmutableList.copyOf(buildRequestCalls);
}

public List<Tuple<String, String>> getAddHeaderCalls() {
return ImmutableList.copyOf(addHeaderCalls);
}

@Override
protected LowLevelHttpRequest buildRequest(String method, String url) {
buildRequestCalls.add(Tuple.of(method, url));
return new LowLevelHttpRequest() {
@Override
public void addHeader(String name, String value) {
addHeaderCalls.add(Tuple.of(name, value));
}

@Override
public LowLevelHttpResponse execute() {
return response;
}
};
}
}
}

0 comments on commit 0cd80e3

Please sign in to comment.