Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test helper grants and revokes roles/cloudsql.admin on the test project.

  1. Does the revoke still run if a test fails partway through?
  2. Does the samples CI service account even have permission to set IAM policy?
  3. If the managed-rotation tests need a real Cloud SQL instance and user, does the samples test project have one?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review!

  1. Yes — revoke_cloud_sql_role always runs on test failure. In each test, grant_cloud_sql_role runs first, then the sample call and assertions sit inside a begin...ensure block with revoke_cloud_sql_role in the ensure, so it fires regardless of outcome. Both helpers (in regional_helper.rb) also go through retry_cloud_sql_iam_call, which retries on transient errors instead of giving up immediately.
  2. & 3. I don't think the shared test setup has either of these yet. Looking at how Firestore handled a similar need (see google-cloud-firestore/samples/CONTRIBUTING.md), it got its own separate test project and credentials instead of relying on the shared one. I think we'd want the same here: a dedicated project (or at least a Cloud SQL instance) set up for these tests, with the right permissions already in place, rather than assuming the shared one can support it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with the Firestore-style approach. Until that exists, the enable, rotate and schedule tests skip in CI, so these three samples aren't exercised. Could you open an issue to track a dedicated test project (or Cloud SQL instance and user) with the permissions in place, so that it doesn't get forgotten?


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}",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading