Skip to content

feat(secretmanager): Add Cloud SQL managed-rotation samples - #10348

Open
suvidha-malaviya wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
suvidha-malaviya:cloudsql-managed-rotation
Open

suvidha-malaviya wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
suvidha-malaviya:cloudsql-managed-rotation

Conversation

@suvidha-malaviya

@suvidha-malaviya suvidha-malaviya commented Sep 25, 2026 •

Copy link
Copy Markdown

Description

Adds samples for Secret Manager's Cloud SQL managed-rotation feature (regional secrets only — this feature isn't available for global secrets):

  1. Create a regional secret with Cloud SQL DB credentials
  2. Enable managed rotation on a regional Cloud SQL DB credentials secret
  3. Trigger an on-demand rotation
  4. Reconfigure the recurring rotation schedule on a secret that already has managed rotation enabled

Also added two global scenario with secret-type:

  1. Create a secret restricted to a given secret type (global)
  2. Get a secret's type (global and regional)

Sample List (global & regional):

  • CreateSecretWithType.java (secret-type restriction is global-only — no regional counterpart)
  • GetSecretType.java / GetRegionalSecretType.java
  • CreateRegionalSecretWithCloudSqlCredentials.java
  • EnableRegionalSecretManagedRotation.java
  • RotateRegionalSecret.java
  • UpdateRegionalSecretWithManagedRotationSchedule.java

Added required tests for all of the above in SnippetsIT.java (regional and global).

Also bumps google-cloud-secretmanager/proto-google-cloud-secretmanager-v1 from 2.66.0 → 2.98.0 (and the google-cloud-bom import from 26.62.0 → 26.89.0) — minimum version with Cloud SQL managed-rotation support.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended.
  • Tests pass: mvn clean verify required
  • Lint passes: mvn -P lint checkstyle:check required
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check advisory only

Compliance & Style

  • I have followed Sample Format Guide
  • pom.xml parent set to latest shared-configuration
  • Appropriate changes to README are included in PR
  • These samples need a new API enabled in testing projects to pass (let us know which ones) — Cloud SQL Admin API (sqladmin.googleapis.com)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones):
    - CLOUD_SQL_INSTANCE / CLOUD_SQL_USER — a pre-provisioned, long-lived Cloud SQL instance + DB user for managed-rotation tests to point at
    - The identity running these tests additionally needs resourcemanager.projects.getIamPolicy/setIamPolicy on the test project (e.g. roles/resourcemanager.projectIamAdmin)
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample

Post-Approval Actions

  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label Bot added samples Issues that are directly related to samples. api: secretmanager Issues related to the Secret Manager API. labels Sep 25, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new Java samples and integration tests for Google Cloud Secret Manager, specifically focusing on creating secrets with type restrictions and managing regional secrets with Cloud SQL managed rotation. The feedback suggests improving code maintainability by using standard protobuf utility classes (Timestamps and Durations) to build timestamp and duration objects, and enhancing test reliability by adding a backoff sleep when retrying on AbortedException during IAM policy updates.

Comment on lines +77 to +82
Timestamp nextRotationTime =
Timestamp.newBuilder()
.setSeconds(nextRotationInstant.getEpochSecond())
.setNanos(nextRotationInstant.getNano())
.build();
Duration rotationPeriod = Duration.newBuilder().setSeconds(rotationPeriodSeconds).build();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Instead of manually building the Timestamp and Duration objects, you can use the standard utility classes com.google.protobuf.util.Timestamps and com.google.protobuf.util.Durations to make the code cleaner and more maintainable.

      Timestamp nextRotationTime =
          com.google.protobuf.util.Timestamps.fromMillis(nextRotationInstant.toEpochMilli());
      Duration rotationPeriod =
          com.google.protobuf.util.Durations.fromSeconds(rotationPeriodSeconds);

Comment on lines +297 to +304
try {
projectsClient.setIamPolicy(resource, policyBuilder.build());
return;
} catch (AbortedException e) {
if (attempt >= 5) {
throw e;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

When retrying on AbortedException (which indicates concurrent modification of the IAM policy), retrying immediately in a tight loop can cause high CPU usage and increased contention. Adding a short backoff sleep (e.g., exponential backoff or a simple incremental sleep) between retry attempts is a best practice to reduce contention and improve test reliability.

        try {
          projectsClient.setIamPolicy(resource, policyBuilder.build());
          return;
        } catch (AbortedException e) {
          if (attempt >= 5) {
            throw e;
          }
          try {
            Thread.sleep(100 * (attempt + 1));
          } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
            throw new IOException("Interrupted during retry backoff", ie);
          }
        }

Comment on lines +337 to +344
try {
projectsClient.setIamPolicy(resource, policyBuilder.build());
return;
} catch (AbortedException e) {
if (attempt >= 5) {
throw e;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

When retrying on AbortedException (which indicates concurrent modification of the IAM policy), retrying immediately in a tight loop can cause high CPU usage and increased contention. Adding a short backoff sleep (e.g., exponential backoff or a simple incremental sleep) between retry attempts is a best practice to reduce contention and improve test reliability.

        try {
          projectsClient.setIamPolicy(resource, policyBuilder.build());
          return;
        } catch (AbortedException e) {
          if (attempt >= 5) {
            throw e;
          }
          try {
            Thread.sleep(100 * (attempt + 1));
          } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
            throw new IOException("Interrupted during retry backoff", ie);
          }
        }

suvidha-malaviya added a commit to suvidha-malaviya/java-docs-samples that referenced this pull request Sep 25, 2026
…latform#10348

- UpdateRegionalSecretWithManagedRotationSchedule.java: build the
  Timestamp/Duration via com.google.protobuf.util.Timestamps/Durations
  instead of manual builders.
- SnippetsIT.java: add backoff between AbortedException retries in
  grantCloudSqlRole and revokeCloudSqlRole to reduce contention.
- UpdateRegionalSecretWithManagedRotationSchedule.java: build the
  Timestamp/Duration via com.google.protobuf.util.Timestamps/Durations
  instead of manual builders.
- SnippetsIT.java: add backoff between AbortedException retries in
  grantCloudSqlRole and revokeCloudSqlRole to reduce contention.
@suvidha-malaviya
suvidha-malaviya force-pushed the cloudsql-managed-rotation branch from 22c4ce4 to 8370800 Compare September 25, 2026 12:09
@suvidha-malaviya
suvidha-malaviya marked this pull request as ready for review September 25, 2026 12:13
@snippet-bot

snippet-bot Bot commented Sep 25, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 7 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@suvidha-malaviya suvidha-malaviya changed the title Cloudsql managed rotation feat(secretmanager): Add Cloud SQL managed-rotation samples Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: secretmanager Issues related to the Secret Manager API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant