|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage.spi.v1; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.api.client.http.HttpTransport; |
| 24 | +import com.google.api.client.http.LowLevelHttpRequest; |
| 25 | +import com.google.api.client.http.LowLevelHttpResponse; |
| 26 | +import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
| 27 | +import com.google.cloud.TransportOptions; |
| 28 | +import com.google.cloud.Tuple; |
| 29 | +import com.google.cloud.WriteChannel; |
| 30 | +import com.google.cloud.http.HttpTransportOptions; |
| 31 | +import com.google.cloud.storage.Storage; |
| 32 | +import com.google.cloud.storage.StorageOptions; |
| 33 | +import com.google.common.collect.ImmutableList; |
| 34 | +import java.io.IOException; |
| 35 | +import java.net.URL; |
| 36 | +import java.nio.ByteBuffer; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Optional; |
| 42 | +import java.util.UUID; |
| 43 | +import org.junit.Test; |
| 44 | + |
| 45 | +public class HttpRpcContextTest { |
| 46 | + @Test |
| 47 | + public void testNewInvocationId() { |
| 48 | + UUID uuid = UUID.fromString("28220dff-1e8b-4770-9e10-022c2a99d8f3"); |
| 49 | + HttpRpcContext testContext = new HttpRpcContext(() -> uuid); |
| 50 | + |
| 51 | + assertThat(testContext.newInvocationId()).isEqualTo(uuid); |
| 52 | + assertThat(testContext.getInvocationId()).isEqualTo(uuid); |
| 53 | + // call again to ensure the id is consistent with our supplier |
| 54 | + assertThat(testContext.newInvocationId()).isEqualTo(uuid); |
| 55 | + assertThat(testContext.getInvocationId()).isEqualTo(uuid); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testInvocationIdIsPassedThrough() { |
| 60 | + MockLowLevelHttpResponse response = |
| 61 | + new MockLowLevelHttpResponse() |
| 62 | + .setContentType("application/json") |
| 63 | + .setContent( |
| 64 | + "{\n" |
| 65 | + + " \"kind\": \"storage#serviceAccount\",\n" |
| 66 | + + " \"email_address\": \"service-234234@gs-project-accounts.iam.gserviceaccount.com\"\n" |
| 67 | + + "}\n") |
| 68 | + .setStatusCode(200); |
| 69 | + AuditingHttpTransport transport = new AuditingHttpTransport(response); |
| 70 | + TransportOptions transportOptions = |
| 71 | + HttpTransportOptions.newBuilder().setHttpTransportFactory(() -> transport).build(); |
| 72 | + Storage service = |
| 73 | + StorageOptions.getDefaultInstance() |
| 74 | + .toBuilder() |
| 75 | + .setTransportOptions(transportOptions) |
| 76 | + .build() |
| 77 | + .getService(); |
| 78 | + service.getServiceAccount("test-project"); |
| 79 | + Optional<Tuple<String, String>> anyXGoogApiClientWithGcclInvocationId = |
| 80 | + transport.getAddHeaderCalls().stream() |
| 81 | + .filter(t -> "x-goog-api-client".equals(t.x()) && t.y().contains("gccl-invocation-id/")) |
| 82 | + .findFirst(); |
| 83 | + assertTrue(anyXGoogApiClientWithGcclInvocationId.isPresent()); |
| 84 | + assertThat(transport.getBuildRequestCalls()).hasSize(1); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testInvocationIdIsNotPassedThroughWhenDisabled() { |
| 89 | + MockLowLevelHttpResponse response = |
| 90 | + new MockLowLevelHttpResponse() |
| 91 | + .setContentType("application/json") |
| 92 | + .setContent( |
| 93 | + "{\n" |
| 94 | + + " \"kind\": \"storage#serviceAccount\",\n" |
| 95 | + + " \"email_address\": \"service-234234@gs-project-accounts.iam.gserviceaccount.com\"\n" |
| 96 | + + "}\n") |
| 97 | + .setStatusCode(200); |
| 98 | + AuditingHttpTransport transport = new AuditingHttpTransport(response); |
| 99 | + TransportOptions transportOptions = |
| 100 | + HttpTransportOptions.newBuilder().setHttpTransportFactory(() -> transport).build(); |
| 101 | + Storage service = |
| 102 | + StorageOptions.getDefaultInstance() |
| 103 | + .toBuilder() |
| 104 | + .setTransportOptions(transportOptions) |
| 105 | + .setIncludeInvocationId(false) |
| 106 | + .build() |
| 107 | + .getService(); |
| 108 | + service.getServiceAccount("test-project"); |
| 109 | + Optional<Tuple<String, String>> anyXGoogApiClientWithGcclInvocationId = |
| 110 | + transport.getAddHeaderCalls().stream() |
| 111 | + .filter(t -> "x-goog-api-client".equals(t.x()) && t.y().contains("gccl-invocation-id/")) |
| 112 | + .findFirst(); |
| 113 | + |
| 114 | + assertFalse(anyXGoogApiClientWithGcclInvocationId.isPresent()); |
| 115 | + assertThat(transport.getBuildRequestCalls()).hasSize(1); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testInvocationIdNotInSignedURL_v2() throws IOException { |
| 120 | + URL signedUrlV2 = |
| 121 | + new URL( |
| 122 | + "http://www.test.com/test-bucket/test1.txt?GoogleAccessId=testClient-test@test.com&Expires=1553839761&Signature=MJUBXAZ7"); |
| 123 | + doTestInvocationIdNotInSignedURL(signedUrlV2); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testInvocationIdNotInSignedURL_v4() throws IOException { |
| 128 | + URL signedUrlV4 = |
| 129 | + new URL( |
| 130 | + "http://www.test.com/test-bucket/test1.txt?X-Goog-Algorithm=&X-Goog-Credential=&X-Goog-Date=&X-Goog-Expires=&X-Goog-SignedHeaders=&X-Goog-Signature=MJUBXAZ7"); |
| 131 | + doTestInvocationIdNotInSignedURL(signedUrlV4); |
| 132 | + } |
| 133 | + |
| 134 | + private void doTestInvocationIdNotInSignedURL(URL signedUrl) throws IOException { |
| 135 | + MockLowLevelHttpResponse response = |
| 136 | + new MockLowLevelHttpResponse() |
| 137 | + .setContentType("text/plain") |
| 138 | + .setHeaderNames(ImmutableList.of("Location")) |
| 139 | + .setHeaderValues(ImmutableList.of("http://test")) |
| 140 | + .setStatusCode(201); |
| 141 | + AuditingHttpTransport transport = new AuditingHttpTransport(response); |
| 142 | + TransportOptions transportOptions = |
| 143 | + HttpTransportOptions.newBuilder().setHttpTransportFactory(() -> transport).build(); |
| 144 | + Storage service = |
| 145 | + StorageOptions.getDefaultInstance() |
| 146 | + .toBuilder() |
| 147 | + .setTransportOptions(transportOptions) |
| 148 | + .build() |
| 149 | + .getService(); |
| 150 | + WriteChannel writer = service.writer(signedUrl); |
| 151 | + writer.write(ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); |
| 152 | + Optional<Tuple<String, String>> anyXGoogApiClientWithGcclInvocationId = |
| 153 | + transport.getAddHeaderCalls().stream() |
| 154 | + .filter(t -> "x-goog-api-client".equals(t.x()) && t.y().contains("gccl-invocation-id/")) |
| 155 | + .findFirst(); |
| 156 | + assertFalse(anyXGoogApiClientWithGcclInvocationId.isPresent()); |
| 157 | + assertThat(transport.getBuildRequestCalls()).hasSize(1); |
| 158 | + } |
| 159 | + |
| 160 | + private static final class AuditingHttpTransport extends HttpTransport { |
| 161 | + private final LowLevelHttpResponse response; |
| 162 | + private final List<Tuple<String, String>> buildRequestCalls; |
| 163 | + private final List<Tuple<String, String>> addHeaderCalls; |
| 164 | + |
| 165 | + private AuditingHttpTransport(LowLevelHttpResponse response) { |
| 166 | + this.response = response; |
| 167 | + this.buildRequestCalls = Collections.synchronizedList(new ArrayList<>()); |
| 168 | + this.addHeaderCalls = Collections.synchronizedList(new ArrayList<>()); |
| 169 | + } |
| 170 | + |
| 171 | + public List<Tuple<String, String>> getBuildRequestCalls() { |
| 172 | + return ImmutableList.copyOf(buildRequestCalls); |
| 173 | + } |
| 174 | + |
| 175 | + public List<Tuple<String, String>> getAddHeaderCalls() { |
| 176 | + return ImmutableList.copyOf(addHeaderCalls); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + protected LowLevelHttpRequest buildRequest(String method, String url) { |
| 181 | + buildRequestCalls.add(Tuple.of(method, url)); |
| 182 | + return new LowLevelHttpRequest() { |
| 183 | + @Override |
| 184 | + public void addHeader(String name, String value) { |
| 185 | + addHeaderCalls.add(Tuple.of(name, value)); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public LowLevelHttpResponse execute() { |
| 190 | + return response; |
| 191 | + } |
| 192 | + }; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments