in r2dbc/core/src/main/java/com/google/cloud/sql/core/GcpConnectionFactoryProvider.java [62:180]
abstract ConnectionFactory tcpSocketConnectionFactory(
ConnectionConfig config,
Builder optionBuilder,
Function<SslContextBuilder, SslContextBuilder> customizer);
/**
* Creates a ConnectionFactory that creates an SSL connection over a Unix domain socket, using
* driver-specific options.
*/
abstract ConnectionFactory unixSocketConnectionFactory(Builder optionBuilder, String socket);
/** Creates a driver-specific {@link ConnectionFactoryOptions.Builder}. */
abstract Builder createBuilder(ConnectionFactoryOptions connectionFactoryOptions);
/** Allows a particular driver to indicate if it supports a protocol. */
abstract boolean supportedProtocol(String protocol);
@Override
@NonNull
public ConnectionFactory create(ConnectionFactoryOptions connectionFactoryOptions) {
String protocol = (String) connectionFactoryOptions.getRequiredValue(PROTOCOL);
if (!supportedProtocol(protocol)) {
throw new UnsupportedOperationException(
"Cannot create ConnectionFactory: unsupported protocol (" + protocol + ")");
}
String ipTypes = ConnectionConfig.DEFAULT_IP_TYPES;
Object ipTypesObj = connectionFactoryOptions.getValue(IP_TYPES);
if (ipTypesObj != null) {
ipTypes = (String) ipTypesObj;
}
boolean enableIamAuth;
Object iamAuthObj = connectionFactoryOptions.getValue(ENABLE_IAM_AUTH);
if (iamAuthObj instanceof Boolean) {
enableIamAuth = (Boolean) iamAuthObj;
} else if (iamAuthObj instanceof String) {
enableIamAuth = Boolean.parseBoolean((String) iamAuthObj);
} else {
enableIamAuth = false;
}
final List<String> delegates;
Object delegatesObj = connectionFactoryOptions.getValue(DELEGATES);
if (delegatesObj instanceof String && !((String) delegatesObj).isEmpty()) {
delegates = Arrays.asList(((String) delegatesObj).split(","));
} else {
delegates = Collections.emptyList();
}
final String targetPrincipal = (String) connectionFactoryOptions.getValue(TARGET_PRINCIPAL);
final String namedConnector = (String) connectionFactoryOptions.getValue(NAMED_CONNECTOR);
final String adminRootUrl = (String) connectionFactoryOptions.getValue(ADMIN_ROOT_URL);
final String adminServicePath = (String) connectionFactoryOptions.getValue(ADMIN_SERVICE_PATH);
final String adminQuotaProject =
(String) connectionFactoryOptions.getValue(ADMIN_QUOTA_PROJECT);
final String googleCredentialsPath =
(String) connectionFactoryOptions.getValue(GOOGLE_CREDENTIALS_PATH);
final String universeDomain = (String) connectionFactoryOptions.getValue(UNIVERSE_DOMAIN);
final RefreshStrategy refreshStrategy =
"lazy".equalsIgnoreCase((String) connectionFactoryOptions.getValue(REFRESH_STRATEGY))
? RefreshStrategy.LAZY
: RefreshStrategy.BACKGROUND;
final String r2dbcHostname = (String) connectionFactoryOptions.getRequiredValue(HOST);
final String cloudSqlInstance;
final String domainName;
if (CloudSqlInstanceName.isValidInstanceName(r2dbcHostname)) {
cloudSqlInstance = r2dbcHostname;
domainName = null;
} else {
cloudSqlInstance = null;
domainName = r2dbcHostname;
}
Builder optionBuilder = createBuilder(connectionFactoryOptions);
ConnectionConfig config =
new ConnectionConfig.Builder()
.withCloudSqlInstance(cloudSqlInstance)
.withDomainName(domainName)
.withAuthType(enableIamAuth ? AuthType.IAM : AuthType.PASSWORD)
.withIpTypes(ipTypes)
.withNamedConnector(namedConnector)
.withConnectorConfig(
new ConnectorConfig.Builder()
.withTargetPrincipal(targetPrincipal)
.withDelegates(delegates)
.withAdminRootUrl(adminRootUrl)
.withAdminServicePath(adminServicePath)
.withAdminQuotaProject(adminQuotaProject)
.withGoogleCredentialsPath(googleCredentialsPath)
.withUniverseDomain(universeDomain)
.withRefreshStrategy(refreshStrategy)
.build())
.build();
String socket = (String) connectionFactoryOptions.getValue(UNIX_SOCKET);
if (socket != null) {
return unixSocketConnectionFactory(optionBuilder, socket);
}
Function<SslContextBuilder, SslContextBuilder> sslFunction =
sslContextBuilder -> {
// Execute in a default scheduler to prevent it from blocking event loop
ConnectionMetadata connectionMetadata =
Mono.fromSupplier(
() -> InternalConnectorRegistry.getInstance().getConnectionMetadata(config))
.subscribeOn(Schedulers.boundedElastic())
.share()
.block();
sslContextBuilder.keyManager(connectionMetadata.getKeyManagerFactory());
sslContextBuilder.trustManager(connectionMetadata.getTrustManagerFactory());
sslContextBuilder.protocols("TLSv1.2");
return sslContextBuilder;
};
return tcpSocketConnectionFactory(config, optionBuilder, sslFunction);
}