|
63 | 63 | */ |
64 | 64 | public final class CoreSocketFactory { |
65 | 65 | public static final String CLOUD_SQL_INSTANCE_PROPERTY = "cloudSqlInstance"; |
66 | | - public static final String MYSQL_SOCKET_FILE_FORMAT = "/cloudsql/%s"; |
67 | | - public static final String POSTGRES_SOCKET_FILE_FORMAT = "/cloudsql/%s/.s.PGSQL.5432"; |
| 66 | + private static final String UNIX_SOCKET_PROPERTY = "unixSocketPath"; |
68 | 67 |
|
69 | 68 | /** |
70 | 69 | * Property used to set the application name for the underlying SQLAdmin client. |
@@ -149,62 +148,74 @@ static ListeningScheduledExecutorService getDefaultExecutor() { |
149 | 148 | MoreExecutors.getExitingScheduledExecutorService(executor)); |
150 | 149 | } |
151 | 150 |
|
| 151 | + /** Extracts the Unix socket argument from specified properties object. If unset, returns null. */ |
| 152 | + private static String getUnixSocketArg(Properties props) { |
| 153 | + String unixSocketPath = props.getProperty(UNIX_SOCKET_PROPERTY); |
| 154 | + if (unixSocketPath != null) { |
| 155 | + // Get the Unix socket file path from the properties object |
| 156 | + return unixSocketPath; |
| 157 | + } else if (System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null) { |
| 158 | + // If the deprecated env var is set, warn and use `/cloudsql/INSTANCE_CONNECTION_NAME` |
| 159 | + // A socket factory is provided at this path for GAE, GCF, and Cloud Run |
| 160 | + logger.warning( |
| 161 | + String.format( |
| 162 | + "\"CLOUD_SQL_FORCE_UNIX_SOCKET\" env var has been deprecated. Please use" |
| 163 | + + " '%s=\"/cloudsql/INSTANCE_CONNECTION_NAME\"' property in your JDBC url" |
| 164 | + + " instead.", |
| 165 | + UNIX_SOCKET_PROPERTY)); |
| 166 | + return "/cloudsql/" + props.getProperty(CLOUD_SQL_INSTANCE_PROPERTY); |
| 167 | + } |
| 168 | + return null; // if unset, default to null |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Creates a socket representing a connection to a Cloud SQL instance. |
| 173 | + */ |
| 174 | + public static Socket connect(Properties props) throws IOException { |
| 175 | + return connect(props, null); |
| 176 | + } |
| 177 | + |
152 | 178 | /** |
153 | 179 | * Creates a socket representing a connection to a Cloud SQL instance. |
154 | 180 | * |
155 | | - * <p>Depending on the environment, it may return either a SSL Socket or a Unix Socket. |
| 181 | + * <p>Depending on the given properties, it may return either a SSL Socket or a Unix Socket. |
156 | 182 | * |
157 | 183 | * @param props Properties used to configure the connection. |
| 184 | + * @param unixPathSuffix suffix to add the the Unix socket path. Unused if null. |
158 | 185 | * @return the newly created Socket. |
159 | 186 | * @throws IOException if error occurs during socket creation. |
160 | 187 | */ |
161 | | - public static Socket connect(Properties props, String socketPathFormat) throws IOException { |
| 188 | + public static Socket connect(Properties props, String unixPathSuffix) throws IOException { |
162 | 189 | // Gather parameters |
163 | 190 | final String csqlInstanceName = props.getProperty(CLOUD_SQL_INSTANCE_PROPERTY); |
164 | | - final List<String> ipTypes = listIpTypes(props.getProperty("ipTypes", DEFAULT_IP_TYPES)); |
165 | | - final boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null; |
166 | 191 |
|
167 | 192 | // Validate parameters |
168 | 193 | Preconditions.checkArgument( |
169 | 194 | csqlInstanceName != null, |
170 | 195 | "cloudSqlInstance property not set. Please specify this property in the JDBC URL or the " |
171 | 196 | + "connection Properties with value in form \"project:region:instance\""); |
172 | 197 |
|
173 | | - // GAE Standard + GCF provide a connection path at "/cloudsql/<CONNECTION_NAME>" |
174 | | - if (forceUnixSocket || runningOnGaeStandard() || runningOnGoogleCloudFunctions()) { |
| 198 | + // Connect using the specified Unix socket |
| 199 | + String unixSocket = getUnixSocketArg(props); |
| 200 | + if (unixSocket != null) { |
| 201 | + // Verify it ends with the correct suffix |
| 202 | + if (unixPathSuffix != null && !unixSocket.endsWith(unixPathSuffix)) { |
| 203 | + unixSocket = unixSocket + unixPathSuffix; |
| 204 | + } |
175 | 205 | logger.info( |
176 | 206 | String.format( |
177 | | - "Connecting to Cloud SQL instance [%s] via unix socket.", csqlInstanceName)); |
178 | | - UnixSocketAddress socketAddress = |
179 | | - new UnixSocketAddress(new File(String.format(socketPathFormat, csqlInstanceName))); |
| 207 | + "Connecting to Cloud SQL instance [%s] via unix socket at %s.", |
| 208 | + csqlInstanceName, unixSocket)); |
| 209 | + UnixSocketAddress socketAddress = new UnixSocketAddress(new File(unixSocket)); |
180 | 210 | return UnixSocketChannel.open(socketAddress).socket(); |
181 | 211 | } |
182 | 212 |
|
| 213 | + final List<String> ipTypes = listIpTypes(props.getProperty("ipTypes", DEFAULT_IP_TYPES)); |
183 | 214 | logger.info( |
184 | 215 | String.format("Connecting to Cloud SQL instance [%s] via SSL socket.", csqlInstanceName)); |
185 | 216 | return getInstance().createSslSocket(csqlInstanceName, ipTypes); |
186 | 217 | } |
187 | 218 |
|
188 | | - /** Returns {@code true} if running in a Google App Engine Standard runtime. */ |
189 | | - private static boolean runningOnGaeStandard() { |
190 | | - // gaeEnv="standard" indicates standard instances |
191 | | - String gaeEnv = System.getenv("GAE_ENV"); |
192 | | - // runEnv="Production" requires to rule out Java 8 emulated environments |
193 | | - String runEnv = System.getProperty("com.google.appengine.runtime.environment"); |
194 | | - // gaeRuntime="java11" in Java 11 environments (no emulated environments) |
195 | | - String gaeRuntime = System.getenv("GAE_RUNTIME"); |
196 | | - |
197 | | - return "standard".equals(gaeEnv) |
198 | | - && ("Production".equals(runEnv) || "java11".equals(gaeRuntime)); |
199 | | - } |
200 | | - |
201 | | - /** Returns {@code true} if running in a Google Cloud Functions runtime. */ |
202 | | - private static boolean runningOnGoogleCloudFunctions() { |
203 | | - // Functions automatically sets a few variables we can use to guess the env: |
204 | | - // See https://cloud.google.com/functions/docs/env-var#nodejs_10_and_subsequent_runtimes |
205 | | - return System.getenv("K_SERVICE") != null && System.getenv("K_REVISION") != null; |
206 | | - } |
207 | | - |
208 | 219 | /** |
209 | 220 | * Creates a secure socket representing a connection to a Cloud SQL instance. |
210 | 221 | * |
|
0 commit comments