|
1 | 1 | package com.google.cloud.sql.core;
|
2 | 2 |
|
| 3 | +import static com.google.common.base.Preconditions.checkArgument; |
| 4 | + |
3 | 5 | import com.google.api.client.googleapis.json.GoogleJsonResponseException;
|
4 | 6 | import com.google.api.services.sqladmin.SQLAdmin;
|
5 | 7 | import com.google.api.services.sqladmin.model.DatabaseInstance;
|
|
38 | 40 | import java.util.concurrent.atomic.AtomicInteger;
|
39 | 41 | import java.util.logging.Level;
|
40 | 42 | import java.util.logging.Logger;
|
| 43 | +import java.util.regex.Matcher; |
| 44 | +import java.util.regex.Pattern; |
41 | 45 | import javax.net.ssl.KeyManagerFactory;
|
42 | 46 | import javax.net.ssl.SSLContext;
|
43 | 47 | import javax.net.ssl.SSLSocket;
|
|
52 | 56 | class CloudSqlInstance {
|
53 | 57 | private static final Logger logger = Logger.getLogger(CloudSqlInstance.class.getName());
|
54 | 58 |
|
| 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 | + |
55 | 64 | private final ListeningScheduledExecutorService executor;
|
56 | 65 | private final SQLAdmin apiClient;
|
57 | 66 |
|
@@ -85,18 +94,16 @@ class CloudSqlInstance {
|
85 | 94 | SQLAdmin apiClient,
|
86 | 95 | ListeningScheduledExecutorService executor,
|
87 | 96 | 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 | + |
96 | 98 | 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); |
100 | 107 |
|
101 | 108 | this.apiClient = apiClient;
|
102 | 109 | this.executor = executor;
|
|
0 commit comments