Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for legacy domain-scoped projects. #209

Merged
merged 3 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions core/src/main/java/com/google/cloud/sql/core/CloudSqlInstance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.cloud.sql.core;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.sqladmin.SQLAdmin;
import com.google.api.services.sqladmin.model.DatabaseInstance;
Expand Down Expand Up @@ -38,6 +40,8 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
Expand All @@ -52,6 +56,11 @@
class CloudSqlInstance {
private static final Logger logger = Logger.getLogger(CloudSqlInstance.class.getName());

// Unique identifier for each Cloud SQL instance in the format "PROJECT:REGION:INSTANCE"
// Some legacy project ids are domain-scoped (e.g. "example.com:PROJECT:REGION:INSTANCE")
private static final Pattern CONNECTION_NAME =
Pattern.compile("([^:]+(:[^:]+)?):([^:]+):([^:]+)");

private final ListeningScheduledExecutorService executor;
private final SQLAdmin apiClient;

Expand Down Expand Up @@ -85,18 +94,16 @@ class CloudSqlInstance {
SQLAdmin apiClient,
ListeningScheduledExecutorService executor,
ListenableFuture<KeyPair> keyPair) {
String[] connFields = connectionName.split(":");
if (connFields.length != 3) {
throw new IllegalArgumentException(
String.format(
"[%s] Cloud SQL connection name is invalid, expected string in the form of "
+ "\"<PROJECT_ID>:<REGION_ID>:<INSTANCE_ID>\".",
connectionName));
}

this.connectionName = connectionName;
this.projectId = connFields[0];
this.regionId = connFields[1];
this.instanceId = connFields[2];
Matcher matcher = CONNECTION_NAME.matcher(connectionName);
checkArgument(
matcher.matches(),
"[%s] Cloud SQL connection name is invalid, expected string in the form of"
+ " \"<PROJECT_ID>:<REGION_ID>:<INSTANCE_ID>\".");
this.projectId = matcher.group(1);
this.regionId = matcher.group(3);
this.instanceId = matcher.group(4);

this.apiClient = apiClient;
this.executor = executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public void setup() throws IOException, GeneralSecurityException, ExecutionExcep
.thenThrow(fakeNotAuthorizedException());
// Stub when correct instance
when(adminApiInstances.get(eq("myProject"), eq("myInstance"))).thenReturn(adminApiInstancesGet);
when(adminApiInstances.get(eq("example.com:myProject"), eq("myInstance"))).thenReturn(adminApiInstancesGet);

when(adminApi.sslCerts()).thenReturn(adminApiSslCerts);
when(adminApiSslCerts.createEphemeral(
Expand Down Expand Up @@ -256,6 +257,28 @@ public void create_successfulConnection() throws IOException, InterruptedExcepti
assertThat(line).isEqualTo(SERVER_MESSAGE);
}

@Test
public void create_successfulDomainScopedConnection() throws IOException, InterruptedException {
FakeSslServer sslServer = new FakeSslServer();
int port = sslServer.start();

CoreSocketFactory coreSocketFactory =
new CoreSocketFactory(clientKeyPair, adminApi, port, defaultExecutor);
Socket socket =
coreSocketFactory.createSslSocket(
"example.com:myProject:myRegion:myInstance", Arrays.asList("PRIMARY"));

verify(adminApiInstances).get("example.com:myProject", "myInstance");
verify(adminApiSslCerts)
.createEphemeral(
eq("example.com:myProject"), eq("myInstance"), isA(SslCertsCreateEphemeralRequest.class));

BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(socket.getInputStream(), UTF_8));
String line = bufferedReader.readLine();
assertThat(line).isEqualTo(SERVER_MESSAGE);
}

@Test
@Ignore
// test disabled because the connection to the test server produces a different error than when
Expand Down