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..2dfa76dbab75 --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb @@ -0,0 +1,31 @@ +# 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. + +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 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 + 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..01bbbc2bbfed --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb @@ -0,0 +1,30 @@ +# 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. + +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..cd421ae2b70f --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb @@ -0,0 +1,43 @@ +# 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. + +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 + skip_unless_cloud_sql_configured + + sample = SampleLoader.load "enable_regional_secret_managed_rotation.rb" + + # 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 + + 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..761f2ed465ba --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb @@ -0,0 +1,28 @@ +# 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. + +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..b180a021c7fe --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb @@ -0,0 +1,31 @@ +# 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. + +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..c651083cf125 100644 --- a/google-cloud-secret_manager/samples/acceptance/regional_helper.rb +++ b/google-cloud-secret_manager/samples/acceptance/regional_helper.rb @@ -49,6 +49,95 @@ 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"] } + 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 + # 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, 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_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 + 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_cloud_sql_iam_call 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 + + 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 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 + 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..db68ba06148e --- /dev/null +++ b/google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.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. + +require_relative "regional_helper" + +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" + + 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..18413af9764d --- /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 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. + +require_relative "regional_helper" + +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" + + 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..a4bc62671a66 --- /dev/null +++ b/google-cloud-secret_manager/samples/create_regional_secret_with_cloud_sql_credentials.rb @@ -0,0 +1,54 @@ +# 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" + +## +# 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") +# @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}" + + puts "Grant the Cloud SQL User rotate IAM permissions to enable managed " \ + "rotation to: #{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..45b2e2099545 --- /dev/null +++ b/google-cloud-secret_manager/samples/create_secret_with_type.rb @@ -0,0 +1,50 @@ +# 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" + +## +# 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") +# @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. + 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..d0da3d969e8e --- /dev/null +++ b/google-cloud-secret_manager/samples/enable_regional_secret_managed_rotation.rb @@ -0,0 +1,58 @@ +# 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" + +## +# 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 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: + # 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. + parent = client.secret_path project: project_id, location: location_id, secret: secret_id + + # Enable managed rotation. + 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..686961e0c1cf --- /dev/null +++ b/google-cloud-secret_manager/samples/get_regional_secret_type.rb @@ -0,0 +1,45 @@ +# 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" + +## +# 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") +# @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..418e73544310 --- /dev/null +++ b/google-cloud-secret_manager/samples/get_secret_type.rb @@ -0,0 +1,39 @@ +# 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" + +## +# 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") +# +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..520429d93845 --- /dev/null +++ b/google-cloud-secret_manager/samples/rotate_regional_secret.rb @@ -0,0 +1,45 @@ +# 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" + +## +# 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 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. + 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..a38fbbabb210 --- /dev/null +++ b/google-cloud-secret_manager/samples/update_regional_secret_with_managed_rotation_schedule.rb @@ -0,0 +1,67 @@ +# 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" + +## +# 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 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 + + # 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. Mask only the rotation subfields + # being set, not the whole "rotation" submessage. + 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]