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

feat: Add all additional Cloud SQL Java Connector parameters #3286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hessjcg
Copy link
Contributor

@hessjcg hessjcg commented Oct 4, 2024

This adds JDBC properties needed for lazy refresh, service account impersonation, universe domains, and alternate
SQL Admin API endpoints to the JDBC configuration for a Cloud SQL JDBC connection.

See #3285

This is related to GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#2065.

…pring configuration.

This adds JDBC needed for lazy refresh, service account impersonation, universe domains, and alternate 
SQL Admin API endpoints to the JDBC configuration for a Cloud SQL JDBC connection. 

This is related to GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#2065.
@hessjcg hessjcg force-pushed the gh-3285-cloud-sql-autoconfig branch from 14c58f5 to a82e856 Compare October 4, 2024 17:06
Copy link

sonarcloud bot commented Oct 4, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
54.8% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud

urlParams.put("cloudSqlAdminQuotaProject", properties.getAdminQuotaProject());
}
if (StringUtils.hasText(properties.getUniverseDomain())) {
urlParams.put("cloudSqlUniverseDomain", properties.getUniverseDomain());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does cloud-sql-jdbc-socket-factory also allow for customizing the full endpoint?

@@ -54,12 +58,49 @@ public String getJdbcUrl() {
this.properties.getDatabaseName(),
this.properties.getInstanceConnectionName());

// Build additional JDBC url parameters from the configuration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add testing for these new properties in

Copy link

@mieseprem mieseprem Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test method should do the trick:

  @Test
  void testCloudSqlSpringDatasourceWithAllOptions() {
    this.contextRunner
        .withPropertyValues(
            "spring.cloud.gcp.sql.admin-quota-project=someAdminQuotaProjectValue",
            "spring.cloud.gcp.sql.admin-root-url=someAdminRootUrlValue",
            "spring.cloud.gcp.sql.admin-service-path=someAdminServicePathValue",
            "spring.cloud.gcp.sql.database-name=test-database",
            "spring.cloud.gcp.sql.delegates=delegate1,delegate2",
            "spring.cloud.gcp.sql.enable-iam-auth=true",
            "spring.cloud.gcp.sql.instance-connection-name=tubular-bells:singapore:test-instance",
            "spring.cloud.gcp.sql.ip-types=PRIVATE",
            "spring.cloud.gcp.sql.refresh-strategy=lazy",
            "spring.cloud.gcp.sql.target-principal=target-principal",
            "spring.cloud.gcp.sql.universe-domain=someUniverseDomainValue",
            "spring.datasource.username=foo",
            "spring.datasource.password=bar")
        .run(
            context -> {
              HikariDataSource dataSource = (HikariDataSource) context.getBean(DataSource.class);
              assertThat(dataSource)
                  .returns("com.mysql.cj.jdbc.Driver", HikariDataSource::getDriverClassName)
                  .returns("foo", HikariDataSource::getUsername)
                  .returns("bar", HikariDataSource::getPassword)
                  .extracting(HikariDataSource::getJdbcUrl)
                  .asInstanceOf(InstanceOfAssertFactories.STRING)
                  .startsWith("jdbc:mysql://google/test-database?"
                      + "socketFactory=com.google.cloud.sql.mysql.SocketFactory")
                  .satisfies(
                      jdbcUrl -> assertThat(jdbcUrl.substring(jdbcUrl.indexOf('&') + 1).split("&"))
                          .containsExactlyInAnyOrder(
                              "cloudSqlAdminQuotaProject=someAdminQuotaProjectValue",
                              "cloudSqlAdminRootUrl=someAdminRootUrlValue",
                              "cloudSqlAdminServicePath=someAdminServicePathValue",
                              "cloudSqlDelegates=delegate1%2Cdelegate2",
                              "cloudSqlInstance=tubular-bells:singapore:test-instance",
                              "cloudSqlRefreshStrategy=lazy",
                              "cloudSqlTargetPrincipal=target-principal",
                              "cloudSqlUniverseDomain=someUniverseDomainValue",
                              "enableIamAuth=true",
                              "ipTypes=PRIVATE",
                              "sslmode=disable"));
              assertThat(getSpringDatasourceDriverClassName(context))
                  .matches("com.mysql.cj.jdbc.Driver");
            });
  }

I tried to create a PR to the existing PR, but I have no clue about your formatting rules (spotless?)
@hessjcg , maybe you accept this "PR" by just copy the code 😆

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I tried with #3313 😅

.map(
entry ->
URLEncoder.encode(entry.getKey()) + "=" + URLEncoder.encode(entry.getValue()))
.collect(Collectors.joining("&"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add at least one test that verifies the output of this logic? This would protect against future modifications.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burkedavison , would a test like the one in #3286 (comment) be enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants