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

Merged
merged 1 commit into from
Nov 1, 2024

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.

@@ -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 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @mieseprem for the test! I have moved it code into this PR.

.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?

…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.
Copy link

sonarcloud bot commented Oct 30, 2024

Quality Gate Failed Quality Gate failed

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

See analysis details on SonarCloud

@hessjcg hessjcg merged commit f2212d3 into main Nov 1, 2024
73 of 76 checks passed
@hessjcg hessjcg deleted the gh-3285-cloud-sql-autoconfig branch November 1, 2024 00:35
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