Skip to content

Commit 20e1047

Browse files
authored
Add support for legacy domain-scoped projects. (#209)
* Add support for legacy domain-scoped projects. * Run format. * Refactor to use regex instead.
1 parent c05641d commit 20e1047

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

core/src/main/java/com/google/cloud/sql/core/CloudSqlInstance.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.google.cloud.sql.core;
22

3+
import static com.google.common.base.Preconditions.checkArgument;
4+
35
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
46
import com.google.api.services.sqladmin.SQLAdmin;
57
import com.google.api.services.sqladmin.model.DatabaseInstance;
@@ -38,6 +40,8 @@
3840
import java.util.concurrent.atomic.AtomicInteger;
3941
import java.util.logging.Level;
4042
import java.util.logging.Logger;
43+
import java.util.regex.Matcher;
44+
import java.util.regex.Pattern;
4145
import javax.net.ssl.KeyManagerFactory;
4246
import javax.net.ssl.SSLContext;
4347
import javax.net.ssl.SSLSocket;
@@ -52,6 +56,11 @@
5256
class CloudSqlInstance {
5357
private static final Logger logger = Logger.getLogger(CloudSqlInstance.class.getName());
5458

59+
// Unique identifier for each Cloud SQL instance in the format "PROJECT:REGION:INSTANCE"
60+
// Some legacy project ids are domain-scoped (e.g. "example.com:PROJECT:REGION:INSTANCE")
61+
private static final Pattern CONNECTION_NAME =
62+
Pattern.compile("([^:]+(:[^:]+)?):([^:]+):([^:]+)");
63+
5564
private final ListeningScheduledExecutorService executor;
5665
private final SQLAdmin apiClient;
5766

@@ -85,18 +94,16 @@ class CloudSqlInstance {
8594
SQLAdmin apiClient,
8695
ListeningScheduledExecutorService executor,
8796
ListenableFuture<KeyPair> keyPair) {
88-
String[] connFields = connectionName.split(":");
89-
if (connFields.length != 3) {
90-
throw new IllegalArgumentException(
91-
String.format(
92-
"[%s] Cloud SQL connection name is invalid, expected string in the form of "
93-
+ "\"<PROJECT_ID>:<REGION_ID>:<INSTANCE_ID>\".",
94-
connectionName));
95-
}
97+
9698
this.connectionName = connectionName;
97-
this.projectId = connFields[0];
98-
this.regionId = connFields[1];
99-
this.instanceId = connFields[2];
99+
Matcher matcher = CONNECTION_NAME.matcher(connectionName);
100+
checkArgument(
101+
matcher.matches(),
102+
"[%s] Cloud SQL connection name is invalid, expected string in the form of"
103+
+ " \"<PROJECT_ID>:<REGION_ID>:<INSTANCE_ID>\".");
104+
this.projectId = matcher.group(1);
105+
this.regionId = matcher.group(3);
106+
this.instanceId = matcher.group(4);
100107

101108
this.apiClient = apiClient;
102109
this.executor = executor;

core/src/test/java/com/google/cloud/sql/core/CoreSocketFactoryTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public void setup() throws IOException, GeneralSecurityException, ExecutionExcep
153153
.thenThrow(fakeNotAuthorizedException());
154154
// Stub when correct instance
155155
when(adminApiInstances.get(eq("myProject"), eq("myInstance"))).thenReturn(adminApiInstancesGet);
156+
when(adminApiInstances.get(eq("example.com:myProject"), eq("myInstance"))).thenReturn(adminApiInstancesGet);
156157

157158
when(adminApi.sslCerts()).thenReturn(adminApiSslCerts);
158159
when(adminApiSslCerts.createEphemeral(
@@ -256,6 +257,28 @@ public void create_successfulConnection() throws IOException, InterruptedExcepti
256257
assertThat(line).isEqualTo(SERVER_MESSAGE);
257258
}
258259

260+
@Test
261+
public void create_successfulDomainScopedConnection() throws IOException, InterruptedException {
262+
FakeSslServer sslServer = new FakeSslServer();
263+
int port = sslServer.start();
264+
265+
CoreSocketFactory coreSocketFactory =
266+
new CoreSocketFactory(clientKeyPair, adminApi, port, defaultExecutor);
267+
Socket socket =
268+
coreSocketFactory.createSslSocket(
269+
"example.com:myProject:myRegion:myInstance", Arrays.asList("PRIMARY"));
270+
271+
verify(adminApiInstances).get("example.com:myProject", "myInstance");
272+
verify(adminApiSslCerts)
273+
.createEphemeral(
274+
eq("example.com:myProject"), eq("myInstance"), isA(SslCertsCreateEphemeralRequest.class));
275+
276+
BufferedReader bufferedReader =
277+
new BufferedReader(new InputStreamReader(socket.getInputStream(), UTF_8));
278+
String line = bufferedReader.readLine();
279+
assertThat(line).isEqualTo(SERVER_MESSAGE);
280+
}
281+
259282
@Test
260283
@Ignore
261284
// test disabled because the connection to the test server produces a different error than when

0 commit comments

Comments
 (0)