Skip to content

Commit

Permalink
fix: LocalDatastoreHelper uses DATASTORE_EMULATOR_URL and ACCESS_TOKE…
Browse files Browse the repository at this point in the history
…N environment variables values if provided

Fixes googleapis#376
  • Loading branch information
aristide-n committed May 9, 2021
1 parent 1651b88 commit e739998
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 2 deletions.
3 changes: 3 additions & 0 deletions google-cloud-datastore/logging.properties
@@ -0,0 +1,3 @@
handlers = java.util.logging.ConsoleHandler
.level = FINE
java.util.logging.ConsoleHandler.level = FINE
8 changes: 8 additions & 0 deletions google-cloud-datastore/pom.xml
Expand Up @@ -17,6 +17,13 @@
<properties>
<site.installationModule>google-cloud-datastore</site.installationModule>
</properties>
<repositories>
<repository>
<id>project.local</id>
<name>project</name>
<url>file:${project.basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
Expand Down Expand Up @@ -57,6 +64,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.94.11</version>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
Expand Down
Binary file not shown.
@@ -0,0 +1 @@
e40a2fb462bb524f00c714c26e560c64
@@ -0,0 +1 @@
baba77a593a13ad92390c4c307c7f088d2531b07
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.94.11</version>
</project>
@@ -0,0 +1 @@
092229fa201870319488320a99d0e14f
@@ -0,0 +1 @@
140b944ab02619f6a42bfe0857fed565f2b227d2
Binary file not shown.
@@ -0,0 +1 @@
a57f5515c93ae5a848e05be96666eab1
@@ -0,0 +1 @@
c8981b05e1f3415429827a2002e61b40672cf44f
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.94.9</version>
</project>
@@ -0,0 +1 @@
a86a2939b39da47efb639be335f4a375
@@ -0,0 +1 @@
7ce264115cd6c8070d5cd06c943d7128b18a7234
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<versioning>
<release>1.94.11</release>
<versions>
<version>1.94.9</version>
<version>1.94.10</version>
<version>1.94.11</version>
</versions>
<lastUpdated>20210509194813</lastUpdated>
</versioning>
</metadata>
@@ -0,0 +1 @@
f49ad83269869787d14e8b418c6fb867
@@ -0,0 +1 @@
d52675b5566c14311f7a890df8862b0901556cf1
Expand Up @@ -63,6 +63,8 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
private static final String MD5_CHECKSUM = "ec2237a0f0ac54964c6bd95e12c73720";
private static final String BIN_CMD_PORT_FLAG = "--port=";
private static final URL EMULATOR_URL;
private static final String EMULATOR_URL_ENV_VAR = "DATASTORE_EMULATOR_URL";
private static final String ACCESS_TOKEN = System.getenv("ACCESS_TOKEN");

// Common settings
private static final String CONSISTENCY_FLAG = "--consistency=";
Expand All @@ -72,7 +74,11 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {

static {
try {
EMULATOR_URL = new URL("http://storage.googleapis.com/gcd/tools/" + FILENAME);
if (System.getenv(EMULATOR_URL_ENV_VAR) == null) {
EMULATOR_URL = new URL("http://storage.googleapis.com/gcd/tools/" + FILENAME);
} else {
EMULATOR_URL = new URL(System.getenv(EMULATOR_URL_ENV_VAR));
}
} catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
Expand Down Expand Up @@ -147,7 +153,7 @@ private LocalDatastoreHelper(Builder builder) {
gcloudCommand.add("--data-dir=" + getGcdPath());
}
DownloadableEmulatorRunner downloadRunner =
new DownloadableEmulatorRunner(binCommand, EMULATOR_URL, MD5_CHECKSUM);
new DownloadableEmulatorRunner(binCommand, EMULATOR_URL, ACCESS_TOKEN, MD5_CHECKSUM);
this.emulatorRunners = ImmutableList.of(gcloudRunner, downloadRunner);
}

Expand Down
@@ -0,0 +1,84 @@
/*
* Copyright 2021 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.datastore;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.logging.Logger;
import org.junit.Ignore;
import org.junit.Test;

public class EmulatorDownloadTest {
private static final Logger LOGGER = Logger.getLogger(EmulatorDownloadTest.class.getName());

@Test
public void testLDHEmulatorDownloadWithAuth() throws Exception {
assertNotNull(System.getenv("DATASTORE_EMULATOR_URL"));
LOGGER.info("DATASTORE_EMULATOR_URL = " + System.getenv("DATASTORE_EMULATOR_URL"));
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
helper.start();
}

@Ignore
@Test
public void testEmulatorDownloadWithInjectedAccessToken() throws Exception {
assertNotNull(System.getenv("FAKE_DATASTORE_EMULATOR_URL_WITH_AUTH"));
assertNotNull(System.getenv("ACCESS_TOKEN"));
URL emulatorUrl = new URL(System.getenv("FAKE_DATASTORE_EMULATOR_URL_WITH_AUTH"));
String accessToken = System.getenv("ACCESS_TOKEN");
LOGGER.info("FAKE_DATASTORE_EMULATOR_URL_WITH_AUTH = " + emulatorUrl);
LOGGER.info("ACCESS_TOKEN = " + accessToken);
File zipFile = new File(System.getProperty("java.io.tmpdir"), "datastore-emulator.zip");
downloadFile(zipFile, emulatorUrl, accessToken);
assertTrue(zipFile.exists());
}

@Ignore
@Test
public void testEmulatorDownload() throws Exception {
assertNotNull(System.getenv("FAKE_DATASTORE_EMULATOR_URL_WITH_AUTH"));
URL emulatorUrl = new URL(System.getenv("FAKE_DATASTORE_EMULATOR_URL_WITH_AUTH"));
LOGGER.info("FAKE_DATASTORE_EMULATOR_URL_WITH_AUTH = " + emulatorUrl);
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault().createScoped(
"https://www.googleapis.com/auth/devstorage.read_only");
googleCredentials.refreshIfExpired();
String accessToken = googleCredentials.getAccessToken().getTokenValue();
LOGGER.info("local accessToken = " + accessToken);
File zipFile = new File(System.getProperty("java.io.tmpdir"), "datastore-emulator.zip");
downloadFile(zipFile, emulatorUrl, accessToken);
assertTrue(zipFile.exists());
}

private void downloadFile(File file, URL fileUrl, String accessToken) throws Exception {
if (file.exists()) file.delete();
URLConnection urlConnection = fileUrl.openConnection();
urlConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
ReadableByteChannel rbc = Channels.newChannel(urlConnection.getInputStream());
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
}
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -204,6 +204,13 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down

0 comments on commit e739998

Please sign in to comment.