From b306d892e67dd67d43db0d3bd51bd440d7ed5ea0 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Tue, 29 Sep 2026 12:52:34 +0530 Subject: [PATCH 1/5] samples(secret_manager): add Cloud SQL managed rotation and secret type samples --- ..._secret_with_cloud_sql_credentials_test.rb | 33 ++++++++ .../create_secret_with_type_test.rb | 32 ++++++++ ...e_regional_secret_managed_rotation_test.rb | 46 ++++++++++++ .../get_regional_secret_type_test.rb | 30 ++++++++ .../acceptance/get_secret_type_test.rb | 33 ++++++++ .../samples/acceptance/regional_helper.rb | 71 ++++++++++++++++++ .../acceptance/rotate_regional_secret_test.rb | 47 ++++++++++++ ...ret_with_managed_rotation_schedule_test.rb | 54 +++++++++++++ ...ional_secret_with_cloud_sql_credentials.rb | 60 +++++++++++++++ .../samples/create_secret_with_type.rb | 55 ++++++++++++++ ...enable_regional_secret_managed_rotation.rb | 68 +++++++++++++++++ .../samples/get_regional_secret_type.rb | 47 ++++++++++++ .../samples/get_secret_type.rb | 41 ++++++++++ .../samples/rotate_regional_secret.rb | 51 +++++++++++++ ...l_secret_with_managed_rotation_schedule.rb | 75 +++++++++++++++++++ 15 files changed, 743 insertions(+) create mode 100644 google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb create mode 100644 google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb create mode 100644 google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb create mode 100644 google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb create mode 100644 google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb create mode 100644 google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb create mode 100644 google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb create mode 100644 google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb create mode 100644 google-cloud-secret_manager/samples/create_secret_with_type.rb create mode 100644 google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb create mode 100644 google-cloud-secret_manager/samples/get_regional_secret_type.rb create mode 100644 google-cloud-secret_manager/samples/get_secret_type.rb create mode 100644 google-cloud-secret_manager/samples/rotate_regional_secret.rb create mode 100644 google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb diff --git a/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb b/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb new file mode 100644 index 000000000000..bff323c0e2e1 --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb @@ -0,0 +1,33 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "regional_helper" + +describe "#create_regional_secret_with_cloud_sql_credentials", :regional_secret_manager_snippet do + it "creates a regional secret with the Cloud SQL DB credentials secret type" do + sample = SampleLoader.load "create_regional_secret_with_cloud_sql_credentials.rb" + + out, _err = capture_io do + sample.run project_id: project_id, location_id: location_id, secret_id: secret_id + end + secret_id_regex = Regexp.escape secret_id + assert_match %r{Created regional secret: projects/\S+locations/\S+/secrets/#{secret_id_regex}}, out + assert_match(/Grant this identity Cloud SQL IAM permissions/, out) + + fetched_secret = client.get_secret name: secret_name + assert_equal :CLOUD_SQL_DB_CREDENTIALS, fetched_secret.secret_type + end +end diff --git a/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb b/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb new file mode 100644 index 000000000000..18c8a69ba5e9 --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb @@ -0,0 +1,32 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "helper" + +describe "#create_secret_with_type", :secret_manager_snippet do + it "creates a secret with a secret type restriction" do + sample = SampleLoader.load "create_secret_with_type.rb" + + out, _err = capture_io do + sample.run project_id: project_id, secret_id: secret_id, secret_type: :ACCESS_KEY + end + secret_id_regex = Regexp.escape secret_id + assert_match %r{Created secret with secret type: projects/\S+/secrets/#{secret_id_regex}}, out + + fetched_secret = client.get_secret name: secret_name + assert_equal :ACCESS_KEY, fetched_secret.secret_type + end +end diff --git a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb new file mode 100644 index 000000000000..3646939dfe6d --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb @@ -0,0 +1,46 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "regional_helper" + +describe "#enable_regional_secret_managed_rotation", :regional_secret_manager_snippet do + it "enables managed rotation and creates the first secret version" do + sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" + + # enable_managed_rotation needs the secret's own built-in identity + # granted Cloud SQL IAM permissions first -- there's no broader grant + # that covers a secret before it exists, so every secret used here + # needs its own grant/revoke around the test that uses it. + member = cloud_sql_credentials_secret.policy_member.iam_policy_uid_principal + grant_cloud_sql_role member + + begin + out, _err = capture_io do + sample.run project_id: project_id, + location_id: location_id, + secret_id: secret_id, + instance_id: cloud_sql_instance_id, + username: cloud_sql_username + end + assert_match(/Enabled managed rotation, created secret version: /, out) + + version = client.get_secret_version name: "#{secret_name}/versions/1" + assert_equal :ENABLED, version.state + ensure + revoke_cloud_sql_role member + end + end +end diff --git a/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb b/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb new file mode 100644 index 000000000000..b81154375de0 --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb @@ -0,0 +1,30 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "regional_helper" + +describe "#get_regional_secret_type", :regional_secret_manager_snippet do + it "gets the regional secret's type" do + sample = SampleLoader.load "get_regional_secret_type.rb" + + refute_nil cloud_sql_credentials_secret + + out, _err = capture_io do + sample.run project_id: project_id, location_id: location_id, secret_id: secret_id + end + assert_match(/with secret type CLOUD_SQL_DB_CREDENTIALS/, out) + end +end diff --git a/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb b/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb new file mode 100644 index 000000000000..bf7320c3e98e --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb @@ -0,0 +1,33 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "helper" + +describe "#get_secret_type", :secret_manager_snippet do + it "gets the secret's type" do + create_sample = SampleLoader.load "create_secret_with_type.rb" + get_sample = SampleLoader.load "get_secret_type.rb" + + capture_io do + create_sample.run project_id: project_id, secret_id: secret_id, secret_type: :ACCESS_KEY + end + + out, _err = capture_io do + get_sample.run project_id: project_id, secret_id: secret_id + end + assert_match(/with secret type ACCESS_KEY/, out) + end +end diff --git a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb index 50dfdfc7fa32..6ebc26749521 100644 --- a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb +++ b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb @@ -49,6 +49,77 @@ class RegionalSecretManagerSnippetSpec < Minitest::Spec let(:secret_name) { "projects/#{project_id}/locations/#{location_id}/secrets/#{secret_id}" } let(:iam_user) { "user:sarafy@google.com" } + # Role granted to a Cloud SQL DB credentials secret's built-in identity so + # that managed rotation can update the Cloud SQL user's password. This + # grant is per-secret (the member is the secret's own generated + # principal), so it has to be made fresh for every secret managed + # rotation tests create. + CLOUD_SQL_ROLE = "roles/cloudsql.admin".freeze + + let(:cloud_sql_instance_id) { ENV["CLOUD_SQL_INSTANCE"] || raise("missing CLOUD_SQL_INSTANCE") } + let(:cloud_sql_username) { ENV["CLOUD_SQL_USER"] || raise("missing CLOUD_SQL_USER") } + let(:projects_client) { Google::Cloud::ResourceManager::V3::Projects::Client.new } + + # A Cloud SQL DB credentials secret, created at the shared secret_name so + # the existing `after` teardown below deletes it like any other secret. + let :cloud_sql_credentials_secret do + client.create_secret( + parent: "projects/#{project_id}/locations/#{location_id}", + secret_id: secret_id, + secret: { + secret_type: :CLOUD_SQL_DB_CREDENTIALS + } + ) + end + + # Grants CLOUD_SQL_ROLE to member on the project. set_iam_policy replaces + # the whole policy, so this reads the current policy, adds the member to + # the existing (or a new) binding for the role, and writes it back -- + # retrying the whole read-modify-write if another writer raced us + # (AbortedError, from an etag mismatch). + def grant_cloud_sql_role member + resource = "projects/#{project_id}" + retry_on_aborted do + policy = projects_client.get_iam_policy resource: resource + binding = policy.bindings.find { |b| b.role == CLOUD_SQL_ROLE } + if binding + binding.members << member unless binding.members.include? member + else + policy.bindings << Google::Iam::V1::Binding.new(role: CLOUD_SQL_ROLE, members: [member]) + end + projects_client.set_iam_policy resource: resource, policy: policy + end + # IAM grants are eventually consistent; give it a moment before a + # caller tries to use it for managed rotation. + sleep 10 + end + + # Removes member from CLOUD_SQL_ROLE on the project, added by grant_cloud_sql_role. + def revoke_cloud_sql_role member + resource = "projects/#{project_id}" + retry_on_aborted do + policy = projects_client.get_iam_policy resource: resource + changed = false + policy.bindings.each do |binding| + next unless binding.role == CLOUD_SQL_ROLE && binding.members.include?(member) + binding.members.delete member + changed = true + end + projects_client.set_iam_policy resource: resource, policy: policy if changed + end + end + + def retry_on_aborted max_attempts: 5 + attempts = 0 + begin + yield + rescue Google::Cloud::AbortedError + attempts += 1 + raise if attempts >= max_attempts + retry + end + end + let :secret do client.create_secret( parent: "projects/#{project_id}/locations/#{location_id}", diff --git a/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb new file mode 100644 index 000000000000..dd8d674405c6 --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb @@ -0,0 +1,47 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "regional_helper" + +describe "#rotate_regional_secret", :regional_secret_manager_snippet do + it "triggers a rotation, creating a new secret version" do + enable_sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" + rotate_sample = SampleLoader.load "rotate_regional_secret.rb" + + member = cloud_sql_credentials_secret.policy_member.iam_policy_uid_principal + grant_cloud_sql_role member + + begin + capture_io do + enable_sample.run project_id: project_id, + location_id: location_id, + secret_id: secret_id, + instance_id: cloud_sql_instance_id, + username: cloud_sql_username + end + + out, _err = capture_io do + rotate_sample.run project_id: project_id, location_id: location_id, secret_id: secret_id + end + assert_match(/Rotated secret, created secret version: /, out) + + rotated_version = client.get_secret_version name: "#{secret_name}/versions/2" + assert_equal :ENABLED, rotated_version.state + ensure + revoke_cloud_sql_role member + end + end +end diff --git a/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb b/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb new file mode 100644 index 000000000000..79da5822c689 --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb @@ -0,0 +1,54 @@ +# Copyright 2026 Google, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "uri" + +require_relative "regional_helper" + +describe "#update_regional_secret_with_managed_rotation_schedule", :regional_secret_manager_snippet do + it "reconfigures the recurring rotation schedule" do + enable_sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" + update_sample = SampleLoader.load "update_regional_secret_with_managed_rotation_schedule.rb" + + member = cloud_sql_credentials_secret.policy_member.iam_policy_uid_principal + grant_cloud_sql_role member + + begin + capture_io do + enable_sample.run project_id: project_id, + location_id: location_id, + secret_id: secret_id, + instance_id: cloud_sql_instance_id, + username: cloud_sql_username + end + + before = Time.now.to_i + rotation_period_seconds = 3600 + + out, _err = capture_io do + update_sample.run project_id: project_id, + location_id: location_id, + secret_id: secret_id, + rotation_period_seconds: rotation_period_seconds + end + assert_match(/Updated regional secret rotation schedule: /, out) + + fetched_secret = client.get_secret name: secret_name + assert fetched_secret.rotation.next_rotation_time.to_time.to_i >= before + rotation_period_seconds + assert_equal rotation_period_seconds, fetched_secret.rotation.rotation_period.seconds + ensure + revoke_cloud_sql_role member + end + end +end diff --git a/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb b/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb new file mode 100644 index 000000000000..cfc8c8beac53 --- /dev/null +++ b/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb @@ -0,0 +1,60 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_create_regional_secret_with_cloud_sql_credentials] +require "google/cloud/secret_manager" + +## +# Create a regional secret with the Cloud SQL DB credentials secret type. +# This type is required to enable Secret Manager's automatic rotation of +# Cloud SQL passwords. It can only be set when the secret is created, and +# the secret's location must match the region of the target Cloud SQL +# instance. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param location_id [String] Your Google Cloud location (e.g. "us-west1") +# @param secret_id [String] Your secret name (e.g. "my-secret") +# +def create_regional_secret_with_cloud_sql_credentials project_id:, location_id:, secret_id: + # Endpoint for the regional secret manager service. + api_endpoint = "secretmanager.#{location_id}.rep.googleapis.com" + + # Create the Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service do |config| + config.endpoint = api_endpoint + end + + # Build the resource name of the parent project. + parent = client.location_path project: project_id, location: location_id + + # Create the secret. + secret = client.create_secret( + parent: parent, + secret_id: secret_id, + secret: { + secret_type: :CLOUD_SQL_DB_CREDENTIALS + } + ) + + # Print the new secret name. + puts "Created regional secret: #{secret.name}" + + # This built-in identity is what you grant Cloud SQL IAM permissions to, + # so that Secret Manager can rotate the database password on its behalf. + puts "Grant this identity Cloud SQL IAM permissions to enable rotation: " \ + "#{secret.policy_member.iam_policy_uid_principal}" + + secret +end +# [END secretmanager_create_regional_secret_with_cloud_sql_credentials] diff --git a/google-cloud-secret_manager/samples/create_secret_with_type.rb b/google-cloud-secret_manager/samples/create_secret_with_type.rb new file mode 100644 index 000000000000..5c67d86dd474 --- /dev/null +++ b/google-cloud-secret_manager/samples/create_secret_with_type.rb @@ -0,0 +1,55 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_create_secret_with_type] +require "google/cloud/secret_manager" + +## +# Create a new secret with the given secret type restriction (e.g. +# ACCESS_KEY, CERTIFICATE, OTHER_DB_CREDENTIALS, or OTHER -- use +# CLOUD_SQL_DB_CREDENTIALS only for a regional secret that will go through +# enable_regional_secret_managed_rotation). Unlike +# CLOUD_SQL_DB_CREDENTIALS, these other secret types are plain metadata +# tags: they don't require any additional credentials payload at creation +# time. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param secret_id [String] Your secret name (e.g. "my-secret") +# @param secret_type [String, Symbol] Your secret type restriction (e.g. "ACCESS_KEY") +# +def create_secret_with_type project_id:, secret_id:, secret_type: + # Create a Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service + + # Build the resource name of the parent project. + parent = client.project_path project: project_id + + # Create the secret, with the given secret type restriction. + secret = client.create_secret( + parent: parent, + secret_id: secret_id, + secret: { + replication: { + automatic: {} + }, + secret_type: secret_type + } + ) + + # Print the new secret name. + puts "Created secret with secret type: #{secret.name}" + + secret +end +# [END secretmanager_create_secret_with_type] diff --git a/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb b/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb new file mode 100644 index 000000000000..b3de82c77e9f --- /dev/null +++ b/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb @@ -0,0 +1,68 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_enable_regional_secret_managed_rotation] +require "google/cloud/secret_manager" + +## +# Enable managed rotation for a Cloud SQL DB credentials secret. This +# links the secret to a Cloud SQL instance and database user, and can only +# be called once per secret. It adds the secret's first version and sets +# the matching password on the Cloud SQL user, taking the place of a +# manually added secret version, which this secret type doesn't support. +# Afterwards, use rotate_regional_secret to trigger further rotations. +# +# instance_id is the bare Cloud SQL instance ID (e.g. "my-instance") -- +# not a connection name. Neither the project nor the region should be +# included: passing "PROJECT_ID:INSTANCE_ID" or the full +# "PROJECT_ID:LOCATION_ID:INSTANCE_ID" connection name both fail -- the +# service already knows the project from the secret's own path, and +# prepends it internally, so a qualified value ends up double-prefixed. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param location_id [String] Your Google Cloud location (e.g. "us-west1") +# @param secret_id [String] Your Cloud SQL DB credentials secret name (e.g. "my-secret") +# @param instance_id [String] Your bare Cloud SQL instance id (e.g. "my-instance") +# @param username [String] Your Cloud SQL database username (e.g. "my-user") +# +def enable_regional_secret_managed_rotation project_id:, location_id:, secret_id:, instance_id:, username: + # Endpoint for the regional secret manager service. + api_endpoint = "secretmanager.#{location_id}.rep.googleapis.com" + + # Create the Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service do |config| + config.endpoint = api_endpoint + end + + # Build the resource name of the secret. Despite its name, `parent` here + # is the full secret resource name, not a collection parent -- the + # generated request message only defines a `parent` field. + parent = client.secret_path project: project_id, location: location_id, secret: secret_id + + # Enable managed rotation. Leaving password unset lets Secret Manager + # generate a secure password itself. + version = client.enable_managed_rotation( + parent: parent, + cloud_sql_single_user_credentials: { + instance_id: instance_id, + username: username + } + ) + + # Print the new secret version name. + puts "Enabled managed rotation, created secret version: #{version.name}" + + version +end +# [END secretmanager_enable_regional_secret_managed_rotation] diff --git a/google-cloud-secret_manager/samples/get_regional_secret_type.rb b/google-cloud-secret_manager/samples/get_regional_secret_type.rb new file mode 100644 index 000000000000..89ff1875dbc6 --- /dev/null +++ b/google-cloud-secret_manager/samples/get_regional_secret_type.rb @@ -0,0 +1,47 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_get_regional_secret_type] +require "google/cloud/secret_manager" + +## +# Get and print the secret type (e.g. CLOUD_SQL_DB_CREDENTIALS, ACCESS_KEY, +# CERTIFICATE, OTHER_DB_CREDENTIALS, OTHER, or SECRET_TYPE_UNSPECIFIED for +# a secret with no type restriction) of the given regional secret. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param location_id [String] Your Google Cloud location (e.g. "us-west1") +# @param secret_id [String] Your secret name (e.g. "my-secret") +# +def get_regional_secret_type project_id:, location_id:, secret_id: + # Endpoint for the regional secret manager service. + api_endpoint = "secretmanager.#{location_id}.rep.googleapis.com" + + # Create the Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service do |config| + config.endpoint = api_endpoint + end + + # Build the resource name of the secret. + name = client.secret_path project: project_id, location: location_id, secret: secret_id + + # Get the secret. + secret = client.get_secret name: name + + # Print the secret's type. + puts "Found regional secret #{secret.name} with secret type #{secret.secret_type}" + + secret +end +# [END secretmanager_get_regional_secret_type] diff --git a/google-cloud-secret_manager/samples/get_secret_type.rb b/google-cloud-secret_manager/samples/get_secret_type.rb new file mode 100644 index 000000000000..285d330601aa --- /dev/null +++ b/google-cloud-secret_manager/samples/get_secret_type.rb @@ -0,0 +1,41 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_get_secret_type] +require "google/cloud/secret_manager" + +## +# Get and print the secret type (e.g. CLOUD_SQL_DB_CREDENTIALS, ACCESS_KEY, +# CERTIFICATE, OTHER_DB_CREDENTIALS, OTHER, or SECRET_TYPE_UNSPECIFIED for +# a secret with no type restriction) of the given secret. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param secret_id [String] Your secret name (e.g. "my-secret") +# +def get_secret_type project_id:, secret_id: + # Create a Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service + + # Build the resource name of the secret. + name = client.secret_path project: project_id, secret: secret_id + + # Get the secret. + secret = client.get_secret name: name + + # Print the secret's type. + puts "Found secret #{secret.name} with secret type #{secret.secret_type}" + + secret +end +# [END secretmanager_get_secret_type] diff --git a/google-cloud-secret_manager/samples/rotate_regional_secret.rb b/google-cloud-secret_manager/samples/rotate_regional_secret.rb new file mode 100644 index 000000000000..27cc833d146b --- /dev/null +++ b/google-cloud-secret_manager/samples/rotate_regional_secret.rb @@ -0,0 +1,51 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_rotate_regional_secret] +require "google/cloud/secret_manager" + +## +# Trigger a managed rotation for a Cloud SQL DB credentials secret. +# Managed rotation must already be enabled on the secret (see +# enable_regional_secret_managed_rotation). Each call generates a new +# password, updates the Cloud SQL user, and adds the result as a new +# secret version. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param location_id [String] Your Google Cloud location (e.g. "us-west1") +# @param secret_id [String] Your Cloud SQL DB credentials secret name (e.g. "my-secret") +# +def rotate_regional_secret project_id:, location_id:, secret_id: + # Endpoint for the regional secret manager service. + api_endpoint = "secretmanager.#{location_id}.rep.googleapis.com" + + # Create the Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service do |config| + config.endpoint = api_endpoint + end + + # Build the resource name of the secret. Despite its name, `parent` here + # is the full secret resource name, not a collection parent -- the + # generated request message only defines a `parent` field. + parent = client.secret_path project: project_id, location: location_id, secret: secret_id + + # Rotate the secret. + version = client.rotate_secret parent: parent + + # Print the new secret version name. + puts "Rotated secret, created secret version: #{version.name}" + + version +end +# [END secretmanager_rotate_regional_secret] diff --git a/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb b/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb new file mode 100644 index 000000000000..7bccff7078b3 --- /dev/null +++ b/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb @@ -0,0 +1,75 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START secretmanager_update_regional_secret_with_managed_rotation_schedule] +require "google/cloud/secret_manager" + +## +# Reconfigure the recurring rotation schedule on a secret that already has +# Cloud SQL managed rotation enabled (see +# enable_regional_secret_managed_rotation). This only applies to regional +# secrets of the CLOUD_SQL_DB_CREDENTIALS type -- calling it on any other +# secret type, or before managed rotation has been enabled, fails. +# +# rotation_period_seconds is the interval between rotations, in whole +# seconds. The service requires it to be at least 3600 (1 hour), and the +# derived next_rotation_time (now + rotation_period_seconds) must be at +# least 300 seconds (5 minutes) in the future -- both are enforced by the +# API, not checked client-side here. +# +# @param project_id [String] Your Google Cloud project (e.g. "my-project") +# @param location_id [String] Your Google Cloud location (e.g. "us-west1") +# @param secret_id [String] Your Cloud SQL DB credentials secret name (e.g. "my-secret") +# @param rotation_period_seconds [Integer] Seconds between rotations (e.g. 3600) +# +def update_regional_secret_with_managed_rotation_schedule project_id:, location_id:, secret_id:, + rotation_period_seconds: + # Endpoint for the regional secret manager service. + api_endpoint = "secretmanager.#{location_id}.rep.googleapis.com" + + # Create the Secret Manager client. + client = Google::Cloud::SecretManager.secret_manager_service do |config| + config.endpoint = api_endpoint + end + + # Build the resource name of the secret. + name = client.secret_path project: project_id, location: location_id, secret: secret_id + + # next_rotation_time and rotation_period must be set together. + next_rotation_timestamp = Time.now.to_i + rotation_period_seconds + + # Update the secret's rotation schedule. The update mask names only the + # two subfields being set here, not the whole "rotation" submessage -- + # that would also include managed_rotation_status, which is output-only + # and rejects a whole-submessage replace with "immutable and cannot be + # updated" (confirmed empirically against a live project). + secret = client.update_secret( + secret: { + name: name, + rotation: { + next_rotation_time: { seconds: next_rotation_timestamp }, + rotation_period: { seconds: rotation_period_seconds } + } + }, + update_mask: { + paths: ["rotation.next_rotation_time", "rotation.rotation_period"] + } + ) + + # Print the updated secret name. + puts "Updated regional secret rotation schedule: #{secret.name}" + + secret +end +# [END secretmanager_update_regional_secret_with_managed_rotation_schedule] From adea0348450d0c68ec4a51f8701ece25dc3c1ec5 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Tue, 6 Oct 2026 15:35:56 +0530 Subject: [PATCH 2/5] samples(secret_manager): retry Cloud SQL IAM grant/revoke on any transient error --- .../samples/acceptance/regional_helper.rb | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb index 6ebc26749521..7c6c523ab23e 100644 --- a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb +++ b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb @@ -75,11 +75,12 @@ class RegionalSecretManagerSnippetSpec < Minitest::Spec # Grants CLOUD_SQL_ROLE to member on the project. set_iam_policy replaces # the whole policy, so this reads the current policy, adds the member to # the existing (or a new) binding for the role, and writes it back -- - # retrying the whole read-modify-write if another writer raced us - # (AbortedError, from an etag mismatch). + # retrying the whole read-modify-write if another writer raced us, or if + # a transient error (e.g. an etag conflict or a momentary Unavailable) + # got in the way. def grant_cloud_sql_role member resource = "projects/#{project_id}" - retry_on_aborted do + retry_cloud_sql_iam_call do policy = projects_client.get_iam_policy resource: resource binding = policy.bindings.find { |b| b.role == CLOUD_SQL_ROLE } if binding @@ -97,7 +98,7 @@ def grant_cloud_sql_role member # Removes member from CLOUD_SQL_ROLE on the project, added by grant_cloud_sql_role. def revoke_cloud_sql_role member resource = "projects/#{project_id}" - retry_on_aborted do + retry_cloud_sql_iam_call do policy = projects_client.get_iam_policy resource: resource changed = false policy.bindings.each do |binding| @@ -109,13 +110,23 @@ def revoke_cloud_sql_role member end end - def retry_on_aborted max_attempts: 5 - attempts = 0 + CLOUD_SQL_IAM_RETRY_INTERVAL = 15 + CLOUD_SQL_IAM_RETRY_MAX_DURATION = 60 + + # Retries a project IAM policy read-modify-write for up to + # CLOUD_SQL_IAM_RETRY_MAX_DURATION seconds, logging each attempt. Mirrors + # the time-boxed, broad-rescue pattern already used by + # cleanup_tag_value/cleanup_tag_key in create_secret_with_tags_test.rb -- + # a plain attempt-count retry limited to AbortedError missed a transient + # Google::Cloud::UnavailableError observed live against a real project. + def retry_cloud_sql_iam_call + end_time = Time.now + CLOUD_SQL_IAM_RETRY_MAX_DURATION begin yield - rescue Google::Cloud::AbortedError - attempts += 1 - raise if attempts >= max_attempts + rescue StandardError => e + raise if Time.now >= end_time + puts "An error occurred updating the Cloud SQL IAM policy: #{e.message}. Retrying." + sleep CLOUD_SQL_IAM_RETRY_INTERVAL retry end end From 714eeafc8e3d12bf92754e244683b5a65ea13d12 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Fri, 9 Oct 2026 16:13:40 +0530 Subject: [PATCH 3/5] docs(secretmanager): address review feedback on Cloud SQL managed-rotation samples Align comments with other samples, simplify descriptions and clarify rotation schedule behavior. --- ...e_regional_secret_managed_rotation_test.rb | 5 +--- ...ional_secret_with_cloud_sql_credentials.rb | 12 ++------ .../samples/create_secret_with_type.rb | 11 ++----- ...enable_regional_secret_managed_rotation.rb | 30 +++++++------------ .../samples/get_regional_secret_type.rb | 4 +-- .../samples/get_secret_type.rb | 4 +-- .../samples/rotate_regional_secret.rb | 12 ++------ ...l_secret_with_managed_rotation_schedule.rb | 26 ++++++---------- 8 files changed, 31 insertions(+), 73 deletions(-) diff --git a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb index 3646939dfe6d..b014b95fc8eb 100644 --- a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb @@ -20,10 +20,7 @@ it "enables managed rotation and creates the first secret version" do sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" - # enable_managed_rotation needs the secret's own built-in identity - # granted Cloud SQL IAM permissions first -- there's no broader grant - # that covers a secret before it exists, so every secret used here - # needs its own grant/revoke around the test that uses it. + # Grant the secret's identity Cloud SQL IAM permissions. member = cloud_sql_credentials_secret.policy_member.iam_policy_uid_principal grant_cloud_sql_role member diff --git a/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb b/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb index cfc8c8beac53..a4bc62671a66 100644 --- a/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb +++ b/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb @@ -16,11 +16,7 @@ require "google/cloud/secret_manager" ## -# Create a regional secret with the Cloud SQL DB credentials secret type. -# This type is required to enable Secret Manager's automatic rotation of -# Cloud SQL passwords. It can only be set when the secret is created, and -# the secret's location must match the region of the target Cloud SQL -# instance. +# Creates a new regional secret with type CLOUD_SQL_DB_CREDENTIALS. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param location_id [String] Your Google Cloud location (e.g. "us-west1") @@ -50,10 +46,8 @@ def create_regional_secret_with_cloud_sql_credentials project_id:, location_id:, # Print the new secret name. puts "Created regional secret: #{secret.name}" - # This built-in identity is what you grant Cloud SQL IAM permissions to, - # so that Secret Manager can rotate the database password on its behalf. - puts "Grant this identity Cloud SQL IAM permissions to enable rotation: " \ - "#{secret.policy_member.iam_policy_uid_principal}" + puts "Grant the Cloud SQL User rotate IAM permissions to enable managed " \ + "rotation to: #{secret.policy_member.iam_policy_uid_principal}" secret end diff --git a/google-cloud-secret_manager/samples/create_secret_with_type.rb b/google-cloud-secret_manager/samples/create_secret_with_type.rb index 5c67d86dd474..45b2e2099545 100644 --- a/google-cloud-secret_manager/samples/create_secret_with_type.rb +++ b/google-cloud-secret_manager/samples/create_secret_with_type.rb @@ -16,13 +16,8 @@ require "google/cloud/secret_manager" ## -# Create a new secret with the given secret type restriction (e.g. -# ACCESS_KEY, CERTIFICATE, OTHER_DB_CREDENTIALS, or OTHER -- use -# CLOUD_SQL_DB_CREDENTIALS only for a regional secret that will go through -# enable_regional_secret_managed_rotation). Unlike -# CLOUD_SQL_DB_CREDENTIALS, these other secret types are plain metadata -# tags: they don't require any additional credentials payload at creation -# time. +# Creates a new secret with the given secret type. +# Note: CLOUD_SQL_DB_CREDENTIALS is only supported in the regional secret. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param secret_id [String] Your secret name (e.g. "my-secret") @@ -35,7 +30,7 @@ def create_secret_with_type project_id:, secret_id:, secret_type: # Build the resource name of the parent project. parent = client.project_path project: project_id - # Create the secret, with the given secret type restriction. + # Create the secret. secret = client.create_secret( parent: parent, secret_id: secret_id, diff --git a/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb b/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb index b3de82c77e9f..d0da3d969e8e 100644 --- a/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb +++ b/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb @@ -16,24 +16,17 @@ require "google/cloud/secret_manager" ## -# Enable managed rotation for a Cloud SQL DB credentials secret. This -# links the secret to a Cloud SQL instance and database user, and can only -# be called once per secret. It adds the secret's first version and sets -# the matching password on the Cloud SQL user, taking the place of a -# manually added secret version, which this secret type doesn't support. -# Afterwards, use rotate_regional_secret to trigger further rotations. -# -# instance_id is the bare Cloud SQL instance ID (e.g. "my-instance") -- -# not a connection name. Neither the project nor the region should be -# included: passing "PROJECT_ID:INSTANCE_ID" or the full -# "PROJECT_ID:LOCATION_ID:INSTANCE_ID" connection name both fail -- the -# service already knows the project from the secret's own path, and -# prepends it internally, so a qualified value ends up double-prefixed. +# Enables managed rotation of a CLOUD_SQL_DB_CREDENTIALS typed secret. +# It validates and enables the rotation, adding a version and sets the +# passed password (optional). +# Note: AddSecretVersion is disabled on the CLOUD_SQL_DB_CREDENTIALS +# currently and for any necessary manual rotations please trigger +# rotate_secret. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param location_id [String] Your Google Cloud location (e.g. "us-west1") -# @param secret_id [String] Your Cloud SQL DB credentials secret name (e.g. "my-secret") -# @param instance_id [String] Your bare Cloud SQL instance id (e.g. "my-instance") +# @param secret_id [String] Your secret name (e.g. "my-secret") +# @param instance_id [String] Your Cloud SQL instance id (e.g. "my-instance") # @param username [String] Your Cloud SQL database username (e.g. "my-user") # def enable_regional_secret_managed_rotation project_id:, location_id:, secret_id:, instance_id:, username: @@ -45,13 +38,10 @@ def enable_regional_secret_managed_rotation project_id:, location_id:, secret_id config.endpoint = api_endpoint end - # Build the resource name of the secret. Despite its name, `parent` here - # is the full secret resource name, not a collection parent -- the - # generated request message only defines a `parent` field. + # Build the resource name of the secret. parent = client.secret_path project: project_id, location: location_id, secret: secret_id - # Enable managed rotation. Leaving password unset lets Secret Manager - # generate a secure password itself. + # Enable managed rotation. version = client.enable_managed_rotation( parent: parent, cloud_sql_single_user_credentials: { diff --git a/google-cloud-secret_manager/samples/get_regional_secret_type.rb b/google-cloud-secret_manager/samples/get_regional_secret_type.rb index 89ff1875dbc6..686961e0c1cf 100644 --- a/google-cloud-secret_manager/samples/get_regional_secret_type.rb +++ b/google-cloud-secret_manager/samples/get_regional_secret_type.rb @@ -16,9 +16,7 @@ require "google/cloud/secret_manager" ## -# Get and print the secret type (e.g. CLOUD_SQL_DB_CREDENTIALS, ACCESS_KEY, -# CERTIFICATE, OTHER_DB_CREDENTIALS, OTHER, or SECRET_TYPE_UNSPECIFIED for -# a secret with no type restriction) of the given regional secret. +# Gets the secret type of the given regional secret. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param location_id [String] Your Google Cloud location (e.g. "us-west1") diff --git a/google-cloud-secret_manager/samples/get_secret_type.rb b/google-cloud-secret_manager/samples/get_secret_type.rb index 285d330601aa..418e73544310 100644 --- a/google-cloud-secret_manager/samples/get_secret_type.rb +++ b/google-cloud-secret_manager/samples/get_secret_type.rb @@ -16,9 +16,7 @@ require "google/cloud/secret_manager" ## -# Get and print the secret type (e.g. CLOUD_SQL_DB_CREDENTIALS, ACCESS_KEY, -# CERTIFICATE, OTHER_DB_CREDENTIALS, OTHER, or SECRET_TYPE_UNSPECIFIED for -# a secret with no type restriction) of the given secret. +# Gets the secret type of the given secret. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param secret_id [String] Your secret name (e.g. "my-secret") diff --git a/google-cloud-secret_manager/samples/rotate_regional_secret.rb b/google-cloud-secret_manager/samples/rotate_regional_secret.rb index 27cc833d146b..520429d93845 100644 --- a/google-cloud-secret_manager/samples/rotate_regional_secret.rb +++ b/google-cloud-secret_manager/samples/rotate_regional_secret.rb @@ -16,15 +16,11 @@ require "google/cloud/secret_manager" ## -# Trigger a managed rotation for a Cloud SQL DB credentials secret. -# Managed rotation must already be enabled on the secret (see -# enable_regional_secret_managed_rotation). Each call generates a new -# password, updates the Cloud SQL user, and adds the result as a new -# secret version. +# Triggers an adhoc rotation for the managed CLOUD_SQL_DB_CREDENTIALS typed secret. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param location_id [String] Your Google Cloud location (e.g. "us-west1") -# @param secret_id [String] Your Cloud SQL DB credentials secret name (e.g. "my-secret") +# @param secret_id [String] Your secret name (e.g. "my-secret") # def rotate_regional_secret project_id:, location_id:, secret_id: # Endpoint for the regional secret manager service. @@ -35,9 +31,7 @@ def rotate_regional_secret project_id:, location_id:, secret_id: config.endpoint = api_endpoint end - # Build the resource name of the secret. Despite its name, `parent` here - # is the full secret resource name, not a collection parent -- the - # generated request message only defines a `parent` field. + # Build the resource name of the secret. parent = client.secret_path project: project_id, location: location_id, secret: secret_id # Rotate the secret. diff --git a/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb b/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb index 7bccff7078b3..a38fbbabb210 100644 --- a/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb +++ b/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb @@ -16,21 +16,11 @@ require "google/cloud/secret_manager" ## -# Reconfigure the recurring rotation schedule on a secret that already has -# Cloud SQL managed rotation enabled (see -# enable_regional_secret_managed_rotation). This only applies to regional -# secrets of the CLOUD_SQL_DB_CREDENTIALS type -- calling it on any other -# secret type, or before managed rotation has been enabled, fails. -# -# rotation_period_seconds is the interval between rotations, in whole -# seconds. The service requires it to be at least 3600 (1 hour), and the -# derived next_rotation_time (now + rotation_period_seconds) must be at -# least 300 seconds (5 minutes) in the future -- both are enforced by the -# API, not checked client-side here. +# Updates the rotation schedule of a CLOUD_SQL_DB_CREDENTIALS typed secret. # # @param project_id [String] Your Google Cloud project (e.g. "my-project") # @param location_id [String] Your Google Cloud location (e.g. "us-west1") -# @param secret_id [String] Your Cloud SQL DB credentials secret name (e.g. "my-secret") +# @param secret_id [String] Your secret name (e.g. "my-secret") # @param rotation_period_seconds [Integer] Seconds between rotations (e.g. 3600) # def update_regional_secret_with_managed_rotation_schedule project_id:, location_id:, secret_id:, @@ -46,14 +36,16 @@ def update_regional_secret_with_managed_rotation_schedule project_id:, location_ # Build the resource name of the secret. name = client.secret_path project: project_id, location: location_id, secret: secret_id + # The rotation schedule of a CLOUD_SQL_DB_CREDENTIALS secret can be set before + # or after enabling managed rotation; EnableManagedRotation does not need to + # be called first. Other secret types also support a rotation schedule, but + # only when Pub/Sub topics are configured. Pub/Sub topics are not required for + # CLOUD_SQL_DB_CREDENTIALS. # next_rotation_time and rotation_period must be set together. next_rotation_timestamp = Time.now.to_i + rotation_period_seconds - # Update the secret's rotation schedule. The update mask names only the - # two subfields being set here, not the whole "rotation" submessage -- - # that would also include managed_rotation_status, which is output-only - # and rejects a whole-submessage replace with "immutable and cannot be - # updated" (confirmed empirically against a live project). + # Update the secret's rotation schedule. Mask only the rotation subfields + # being set, not the whole "rotation" submessage. secret = client.update_secret( secret: { name: name, From eb50ee635013d1b6552ddedf87bc6692907857d9 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Fri, 13 Nov 2026 22:45:04 +0530 Subject: [PATCH 4/5] samples(secret_manager): address review feedback on Cloud SQL rotation tests --- ...reate_regional_secret_with_cloud_sql_credentials_test.rb | 6 ++---- .../samples/acceptance/create_secret_with_type_test.rb | 4 +--- .../enable_regional_secret_managed_rotation_test.rb | 4 +--- .../samples/acceptance/get_regional_secret_type_test.rb | 4 +--- .../samples/acceptance/get_secret_type_test.rb | 4 +--- .../samples/acceptance/rotate_regional_secret_test.rb | 4 +--- ...e_regional_secret_with_managed_rotation_schedule_test.rb | 4 +--- 7 files changed, 8 insertions(+), 22 deletions(-) diff --git a/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb b/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb index bff323c0e2e1..2dfa76dbab75 100644 --- a/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "regional_helper" describe "#create_regional_secret_with_cloud_sql_credentials", :regional_secret_manager_snippet do @@ -25,7 +23,7 @@ end secret_id_regex = Regexp.escape secret_id assert_match %r{Created regional secret: projects/\S+locations/\S+/secrets/#{secret_id_regex}}, out - assert_match(/Grant this identity Cloud SQL IAM permissions/, out) + assert_match(/Grant the Cloud SQL User rotate IAM permissions/, out) fetched_secret = client.get_secret name: secret_name assert_equal :CLOUD_SQL_DB_CREDENTIALS, fetched_secret.secret_type diff --git a/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb b/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb index 18c8a69ba5e9..01bbbc2bbfed 100644 --- a/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "helper" describe "#create_secret_with_type", :secret_manager_snippet do diff --git a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb index b014b95fc8eb..ac4908e1e174 100644 --- a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "regional_helper" describe "#enable_regional_secret_managed_rotation", :regional_secret_manager_snippet do diff --git a/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb b/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb index b81154375de0..761f2ed465ba 100644 --- a/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "regional_helper" describe "#get_regional_secret_type", :regional_secret_manager_snippet do diff --git a/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb b/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb index bf7320c3e98e..b180a021c7fe 100644 --- a/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "helper" describe "#get_secret_type", :secret_manager_snippet do diff --git a/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb index dd8d674405c6..c58f230dfd24 100644 --- a/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "regional_helper" describe "#rotate_regional_secret", :regional_secret_manager_snippet do diff --git a/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb b/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb index 79da5822c689..adf428ade973 100644 --- a/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb @@ -1,4 +1,4 @@ -# Copyright 2026 Google, Inc +# Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "uri" - require_relative "regional_helper" describe "#update_regional_secret_with_managed_rotation_schedule", :regional_secret_manager_snippet do From 2bbeea254caec4d9893f23d5382939345bd787db Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 18 Nov 2026 16:39:52 +0530 Subject: [PATCH 5/5] samples(secret_manager): skip Cloud SQL rotation tests when Cloud SQL is not configured --- .../enable_regional_secret_managed_rotation_test.rb | 2 ++ .../samples/acceptance/regional_helper.rb | 11 +++++++++-- .../samples/acceptance/rotate_regional_secret_test.rb | 2 ++ ...onal_secret_with_managed_rotation_schedule_test.rb | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb index ac4908e1e174..cd421ae2b70f 100644 --- a/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb @@ -16,6 +16,8 @@ describe "#enable_regional_secret_managed_rotation", :regional_secret_manager_snippet do it "enables managed rotation and creates the first secret version" do + skip_unless_cloud_sql_configured + sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" # Grant the secret's identity Cloud SQL IAM permissions. diff --git a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb index 7c6c523ab23e..c651083cf125 100644 --- a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb +++ b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb @@ -56,8 +56,15 @@ class RegionalSecretManagerSnippetSpec < Minitest::Spec # rotation tests create. CLOUD_SQL_ROLE = "roles/cloudsql.admin".freeze - let(:cloud_sql_instance_id) { ENV["CLOUD_SQL_INSTANCE"] || raise("missing CLOUD_SQL_INSTANCE") } - let(:cloud_sql_username) { ENV["CLOUD_SQL_USER"] || raise("missing CLOUD_SQL_USER") } + let(:cloud_sql_instance_id) { ENV["CLOUD_SQL_INSTANCE"] } + let(:cloud_sql_username) { ENV["CLOUD_SQL_USER"] } + + # Skips the current test unless a Cloud SQL instance and user are configured + # (CLOUD_SQL_INSTANCE and CLOUD_SQL_USER), e.g. in environments without one. + def skip_unless_cloud_sql_configured + return unless cloud_sql_instance_id.to_s.empty? || cloud_sql_username.to_s.empty? + skip "CLOUD_SQL_INSTANCE and CLOUD_SQL_USER must be set to run Cloud SQL managed rotation tests" + end let(:projects_client) { Google::Cloud::ResourceManager::V3::Projects::Client.new } # A Cloud SQL DB credentials secret, created at the shared secret_name so diff --git a/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb index c58f230dfd24..db68ba06148e 100644 --- a/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb @@ -16,6 +16,8 @@ describe "#rotate_regional_secret", :regional_secret_manager_snippet do it "triggers a rotation, creating a new secret version" do + skip_unless_cloud_sql_configured + enable_sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" rotate_sample = SampleLoader.load "rotate_regional_secret.rb" diff --git a/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb b/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb index adf428ade973..18413af9764d 100644 --- a/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb +++ b/google-cloud-secret_manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb @@ -16,6 +16,8 @@ describe "#update_regional_secret_with_managed_rotation_schedule", :regional_secret_manager_snippet do it "reconfigures the recurring rotation schedule" do + skip_unless_cloud_sql_configured + enable_sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" update_sample = SampleLoader.load "update_regional_secret_with_managed_rotation_schedule.rb"