Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit f42a707

Browse files
authored
feat: support downloading an emulator from an access controlled URL (#513)
* feat: support downloading an emulator from an access controlled URL fixes googleapis/java-datastore#376 used in googleapis/java-datastore#492 * fix format
1 parent 4edf733 commit f42a707

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

google-cloud-core/src/main/java/com/google/cloud/testing/BaseEmulatorHelper.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.net.HttpURLConnection;
3939
import java.net.ServerSocket;
4040
import java.net.URL;
41+
import java.net.URLConnection;
4142
import java.nio.channels.Channels;
4243
import java.nio.channels.ReadableByteChannel;
4344
import java.nio.file.Files;
@@ -316,6 +317,7 @@ protected static class DownloadableEmulatorRunner implements EmulatorRunner {
316317
private final String md5CheckSum;
317318
private final URL downloadUrl;
318319
private final String fileName;
320+
private String accessToken;
319321
private Process process;
320322
private static final Logger log = Logger.getLogger(DownloadableEmulatorRunner.class.getName());
321323

@@ -328,6 +330,12 @@ public DownloadableEmulatorRunner(
328330
this.fileName = splitUrl[splitUrl.length - 1];
329331
}
330332

333+
public DownloadableEmulatorRunner(
334+
List<String> commandText, URL downloadUrl, String md5CheckSum, String accessToken) {
335+
this(commandText, downloadUrl, md5CheckSum);
336+
this.accessToken = accessToken;
337+
}
338+
331339
@Override
332340
public boolean isAvailable() {
333341
try {
@@ -420,7 +428,11 @@ private File downloadZipFile() throws IOException {
420428
if (log.isLoggable(Level.FINE)) {
421429
log.fine("Fetching emulator");
422430
}
423-
ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream());
431+
URLConnection urlConnection = downloadUrl.openConnection();
432+
if (accessToken != null) {
433+
urlConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
434+
}
435+
ReadableByteChannel rbc = Channels.newChannel(urlConnection.getInputStream());
424436
try (FileOutputStream fos = new FileOutputStream(zipFile)) {
425437
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
426438
}

google-cloud-core/src/test/java/com/google/cloud/testing/BaseEmulatorHelperTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public void testEmulatorHelperDownloadWithRetries()
108108
String mockInputStream = "mockInputStream";
109109
String mockProtocol = "mockProtocol";
110110
String mockFile = "mockFile";
111+
String mockAccessToken = "mockAccessToken";
111112
String mockCommandText = "mockCommandText";
112113

113114
MockURLStreamHandler mockURLStreamHandler = EasyMock.createMock(MockURLStreamHandler.class);
@@ -119,6 +120,7 @@ public void testEmulatorHelperDownloadWithRetries()
119120
EasyMock.expect(mockURLConnection.getInputStream())
120121
.andReturn(new ByteArrayInputStream(mockInputStream.getBytes()))
121122
.anyTimes();
123+
mockURLConnection.setRequestProperty("Authorization", "Bearer " + mockAccessToken);
122124
EasyMock.expect(mockURLStreamHandler.openConnection(EasyMock.anyObject(URL.class)))
123125
.andThrow(new EOFException())
124126
.times(1);
@@ -130,7 +132,7 @@ public void testEmulatorHelperDownloadWithRetries()
130132
URL url = new URL(mockProtocol, null, 0, mockFile, mockURLStreamHandler);
131133
BaseEmulatorHelper.DownloadableEmulatorRunner runner =
132134
new BaseEmulatorHelper.DownloadableEmulatorRunner(
133-
ImmutableList.of(mockCommandText), url, null);
135+
ImmutableList.of(mockCommandText), url, null, mockAccessToken);
134136

135137
File cachedFile = new File(System.getProperty("java.io.tmpdir"), mockExternalForm);
136138
cachedFile.delete(); // Clear the cached version so we're always testing the download

0 commit comments

Comments
 (0)