From 307f6826db474d94a9be2734be80d352b000d1a9 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Fri, 12 Oct 2018 09:28:58 -0700 Subject: [PATCH 1/3] Add assumptions to storage integration tests --- .../cloud/storage/it/ITStorageTest.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 5b407b30bf32..2fe28f35ed2d 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -25,8 +25,12 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeTrue; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.gax.paging.Page; +import com.google.auth.ServiceAccountSigner; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.kms.v1.CreateCryptoKeyRequest; import com.google.cloud.kms.v1.CreateKeyRingRequest; @@ -1604,6 +1608,10 @@ public void testWriteChannelExistingBlob() throws IOException { @Test public void testGetSignedUrl() throws IOException { + if(storage.getOptions().getCredentials() != null) { + assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); + } + String blobName = "test-get-signed-url-blob/with/slashes/and?special=!#$&'()*+,:;=?@[]"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); @@ -1619,6 +1627,9 @@ public void testGetSignedUrl() throws IOException { @Test public void testPostSignedUrl() throws IOException { + if (storage.getOptions().getCredentials() != null) { + assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); + } String blobName = "test-post-signed-url-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); assertNotNull(storage.create(blob)); @@ -1657,7 +1668,14 @@ public void testDownloadPublicBlobWithoutAuthentication() { String landsatBucket = "gcp-public-data-landsat"; String landsatPrefix = "LC08/PRE/044/034/LC80440342016259LGN00/"; String landsatBlob = landsatPrefix + "LC80440342016259LGN00_MTL.txt"; - byte[] bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); + byte[] bytes; + try { + bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); + } catch (StorageException ex) { + assertTrue(ex.getCause() instanceof GoogleJsonResponseException); + assumeFalse(403 == ((GoogleJsonResponseException) ex.getCause()).getStatusCode()); + return; + } assertThat(bytes.length).isEqualTo(7903); int numBlobs = 0; Iterator blobIterator = unauthorizedStorage From 1eed09b46f3761d635609d610307fc18939751e2 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 16 Oct 2018 15:34:31 -0700 Subject: [PATCH 2/3] Make testDownloadPublicBlobWithoutAuthentication skippable with an environment variable --- .../com/google/cloud/storage/it/ITStorageTest.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 595e9b6e59bb..a467e94d86c2 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -1661,6 +1661,7 @@ public void testGetBlobs() { @Test public void testDownloadPublicBlobWithoutAuthentication() { + assumeFalse(System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true")); // create an unauthorized user Storage unauthorizedStorage = StorageOptions.getUnauthenticatedInstance().getService(); @@ -1668,13 +1669,8 @@ public void testDownloadPublicBlobWithoutAuthentication() { String landsatBucket = "gcp-public-data-landsat"; String landsatPrefix = "LC08/PRE/044/034/LC80440342016259LGN00/"; String landsatBlob = landsatPrefix + "LC80440342016259LGN00_MTL.txt"; - byte[] bytes; - try { - bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); - } catch (StorageException ex) { - assumeFalse(403 == ex.getCode()); - return; - } + byte[] bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); + assertThat(bytes.length).isEqualTo(7903); int numBlobs = 0; Iterator blobIterator = unauthorizedStorage From 35ef5f3b0802c0d08e8258849ad706054444223f Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 16 Oct 2018 15:34:31 -0700 Subject: [PATCH 3/3] Make testDownloadPublicBlobWithoutAuthentication skippable with an environment variable --- .../com/google/cloud/storage/it/ITStorageTest.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 595e9b6e59bb..b13c8ec6533c 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -28,7 +28,6 @@ import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; -import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.gax.paging.Page; import com.google.auth.ServiceAccountSigner; import com.google.auth.oauth2.GoogleCredentials; @@ -143,6 +142,7 @@ public class ITStorageTest { private static final String KMS_KEY_RING_LOCATION = "us"; private static final String KMS_KEY_ONE_NAME = "gcs_kms_key_one"; private static final String KMS_KEY_TWO_NAME = "gcs_kms_key_two"; + private static final boolean isVpcTest = System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true"); @BeforeClass public static void beforeClass() throws IOException { @@ -1661,6 +1661,7 @@ public void testGetBlobs() { @Test public void testDownloadPublicBlobWithoutAuthentication() { + assumeFalse(isVpcTest); // create an unauthorized user Storage unauthorizedStorage = StorageOptions.getUnauthenticatedInstance().getService(); @@ -1668,13 +1669,8 @@ public void testDownloadPublicBlobWithoutAuthentication() { String landsatBucket = "gcp-public-data-landsat"; String landsatPrefix = "LC08/PRE/044/034/LC80440342016259LGN00/"; String landsatBlob = landsatPrefix + "LC80440342016259LGN00_MTL.txt"; - byte[] bytes; - try { - bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); - } catch (StorageException ex) { - assumeFalse(403 == ex.getCode()); - return; - } + byte[] bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); + assertThat(bytes.length).isEqualTo(7903); int numBlobs = 0; Iterator blobIterator = unauthorizedStorage