Repository navigation
samples(secret_manager): add Cloud SQL managed rotation and secret type samples #36611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suvidha-malaviya
wants to merge
5
commits into
googleapis:main
Choose a base branch
from
suvidha-malaviya:add-cloudsql-managed-rotation-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b306d89
samples(secret_manager): add Cloud SQL managed rotation and secret ty…
suvidha-malaviya adea034
samples(secret_manager): retry Cloud SQL IAM grant/revoke on any tran…
suvidha-malaviya 714eeaf
docs(secretmanager): address review feedback on Cloud SQL managed-rot…
suvidha-malaviya eb50ee6
samples(secret_manager): address review feedback on Cloud SQL rotatio…
suvidha-malaviya 2bbeea2
samples(secret_manager): skip Cloud SQL rotation tests when Cloud SQL…
suvidha-malaviya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...cret_manager/samples/acceptance/create_regional_secret_with_cloud_sql_credentials_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
30 changes: 30 additions & 0 deletions
30
google-cloud-secret_manager/samples/acceptance/create_secret_with_type_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
43 changes: 43 additions & 0 deletions
43
...e-cloud-secret_manager/samples/acceptance/enable_regional_secret_managed_rotation_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
28 changes: 28 additions & 0 deletions
28
google-cloud-secret_manager/samples/acceptance/get_regional_secret_type_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
31 changes: 31 additions & 0 deletions
31
google-cloud-secret_manager/samples/acceptance/get_secret_type_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
google-cloud-secret_manager/samples/acceptance/rotate_regional_secret_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
54 changes: 54 additions & 0 deletions
54
..._manager/samples/acceptance/update_regional_secret_with_managed_rotation_schedule_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
revoke_cloud_sql_rolealways runs on test failure. In each test,grant_cloud_sql_roleruns first, then the sample call and assertions sit inside abegin...ensureblock withrevoke_cloud_sql_rolein theensure, so it fires regardless of outcome. Both helpers (inregional_helper.rb) also go throughretry_cloud_sql_iam_call, which retries on transient errors instead of giving up immediately.There was a problem hiding this comment.
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?