Repository navigation
feat(secretmanager): Add Cloud SQL managed-rotation samples #2335
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
Merged
bshaffer
merged 5 commits into
GoogleCloudPlatform:main
from
suvidha-malaviya:feat/secretmanager-cloudsql-managed-rotation
Nov 6, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d655ae6
feat(secretmanager): add Cloud SQL managed-rotation and secret-type s…
suvidha-malaviya b705465
fix(secretmanager): address review comments on Cloud SQL rotation tests
suvidha-malaviya 50ba42f
docs(secretmanager): address review feedback on Cloud SQL managed-rot…
suvidha-malaviya 79dfbc8
Merge remote-tracking branch 'origin/main' into feat/secretmanager-cl…
bshaffer d72fd70
fix(secretmanager): address review feedback on Cloud SQL managed rota…
bshaffer 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
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
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "require": { | ||
| "google/cloud-secret-manager": "^2.1.0", | ||
| "google/cloud-secret-manager": "^2.4.0", | ||
| "google/cloud-resource-manager": "^1.0" | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
secretmanager/src/create_regional_secret_with_cloud_sql_credentials.php
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,74 @@ | ||
| <?php | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\SecretManager; | ||
|
|
||
| // [START secretmanager_create_regional_secret_with_cloud_sql_credentials] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1\CreateSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Secret; | ||
| use Google\Cloud\SecretManager\V1\Secret\SecretType; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
|
|
||
| /** | ||
| * Creates a new regional secret with type CLOUD_SQL_DB_CREDENTIALS. | ||
| * | ||
| * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $locationId Your secret Location (e.g. 'us-central1') | ||
| * @param string $secretId Your secret ID (e.g. 'my-secret') | ||
| */ | ||
| function create_regional_secret_with_cloud_sql_credentials(string $projectId, string $locationId, string $secretId): void | ||
| { | ||
| // Specify regional endpoint. | ||
| $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient($options); | ||
|
|
||
| // Build the resource name of the parent project. | ||
| $parent = $client->locationName($projectId, $locationId); | ||
|
|
||
| $secret = new Secret([ | ||
| 'secret_type' => SecretType::CLOUD_SQL_DB_CREDENTIALS, | ||
| ]); | ||
|
|
||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| // Create the secret. | ||
| $newSecret = $client->createSecret($request); | ||
|
|
||
| printf('Created secret: %s%s', $newSecret->getName(), PHP_EOL); | ||
|
|
||
| printf( | ||
| 'Grant the Cloud SQL User rotate IAM permissions to enable managed rotation to: %s%s', | ||
| $newSecret->getPolicyMember()->getIamPolicyUidPrincipal(), | ||
| PHP_EOL | ||
| ); | ||
| } | ||
| // [END secretmanager_create_regional_secret_with_cloud_sql_credentials] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); | ||
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,73 @@ | ||
| <?php | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\SecretManager; | ||
|
|
||
| // [START secretmanager_create_secret_with_type] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1\CreateSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Replication; | ||
| use Google\Cloud\SecretManager\V1\Replication\Automatic; | ||
| use Google\Cloud\SecretManager\V1\Secret; | ||
| use Google\Cloud\SecretManager\V1\Secret\SecretType; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
|
|
||
| /** | ||
| * Creates a new secret with the given secret type. | ||
| * Note: CLOUD_SQL_DB_CREDENTIALS is only supported in the regional secret. | ||
| * | ||
| * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $secretId Your secret ID (e.g. 'my-secret') | ||
| * @param string $secretType Your secret type (e.g. 'ACCESS_KEY') | ||
| */ | ||
| function create_secret_with_type(string $projectId, string $secretId, string $secretType): void | ||
| { | ||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| // Build the resource name of the parent project. | ||
| $parent = $client->projectName($projectId); | ||
|
|
||
| $secret = new Secret([ | ||
| 'replication' => new Replication([ | ||
| 'automatic' => new Automatic(), | ||
| ]), | ||
| 'secret_type' => SecretType::value($secretType), | ||
| ]); | ||
|
|
||
| // Build the request. | ||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| // Create the secret. | ||
| $newSecret = $client->createSecret($request); | ||
|
|
||
| // Print the new secret name. | ||
| printf('Created secret with secret type: %s%s', $newSecret->getName(), PHP_EOL); | ||
| } | ||
| // [END secretmanager_create_secret_with_type] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
77 changes: 77 additions & 0 deletions
77
secretmanager/src/enable_regional_secret_managed_rotation.php
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,77 @@ | ||
| <?php | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\SecretManager; | ||
|
|
||
| // [START secretmanager_enable_regional_secret_managed_rotation] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1\EnableManagedRotationRequest; | ||
| use Google\Cloud\SecretManager\V1\EnableManagedRotationRequest\CloudSQLSingleUserCredentials; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
|
|
||
| /** | ||
| * 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 string $projectId Your Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $locationId Your secret Location (e.g. 'us-central1') | ||
| * @param string $secretId Your secret ID (e.g. 'my-secret') | ||
| * @param string $instanceId Your Cloud SQL instance ID | ||
| * @param string $username Your Cloud SQL database username | ||
| */ | ||
| function enable_regional_secret_managed_rotation(string $projectId, string $locationId, string $secretId, string $instanceId, string $username): void | ||
| { | ||
| // Specify regional endpoint. | ||
| $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient($options); | ||
|
|
||
| // Build the resource name of the secret. | ||
| $parent = $client->projectLocationSecretName($projectId, $locationId, $secretId); | ||
|
|
||
| $credentials = new CloudSQLSingleUserCredentials([ | ||
| 'instance_id' => $instanceId, | ||
| 'username' => $username, | ||
| ]); | ||
|
|
||
| $request = (new EnableManagedRotationRequest()) | ||
| ->setParent($parent) | ||
| ->setCloudSqlSingleUserCredentials($credentials); | ||
|
|
||
| // Enable managed rotation. | ||
| $version = $client->enableManagedRotation($request); | ||
|
|
||
| printf('Enabled managed rotation, created secret version: %s%s', $version->getName(), PHP_EOL); | ||
| } | ||
| // [END secretmanager_enable_regional_secret_managed_rotation] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
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,69 @@ | ||
| <?php | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\SecretManager; | ||
|
|
||
| // [START secretmanager_get_regional_secret_type] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
| use Google\Cloud\SecretManager\V1\GetSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Secret\SecretType; | ||
|
|
||
| /** | ||
| * Gets the secret type of the given regional secret. | ||
| * | ||
| * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $locationId Location of the secret (e.g. 'us-central1') | ||
| * @param string $secretId Your secret ID (e.g. 'my-secret') | ||
| */ | ||
| function get_regional_secret_type(string $projectId, string $locationId, string $secretId): void | ||
| { | ||
| // Specify regional endpoint. | ||
| $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; | ||
|
|
||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient($options); | ||
|
|
||
| // Build the resource name of the secret. | ||
| $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); | ||
|
|
||
| // Build the request. | ||
| $request = GetSecretRequest::build($name); | ||
|
|
||
| // Get the secret. | ||
| $secret = $client->getSecret($request); | ||
|
|
||
| printf( | ||
| 'Found regional secret %s with secret type %s%s', | ||
| $secret->getName(), | ||
| SecretType::name($secret->getSecretType()), | ||
| PHP_EOL | ||
| ); | ||
| } | ||
| // [END secretmanager_get_regional_secret_type] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
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,65 @@ | ||
| <?php | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* | ||
| * For instructions on how to run the full sample: | ||
| * | ||
| * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Google\Cloud\Samples\SecretManager; | ||
|
|
||
| // [START secretmanager_get_secret_type] | ||
| // Import the Secret Manager client library. | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
| use Google\Cloud\SecretManager\V1\GetSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Secret\SecretType; | ||
|
|
||
| /** | ||
| * Gets the secret type of the given secret. | ||
| * | ||
| * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $secretId Your secret ID (e.g. 'my-secret') | ||
| */ | ||
| function get_secret_type(string $projectId, string $secretId): void | ||
| { | ||
| // Create the Secret Manager client. | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| // Build the resource name of the secret. | ||
| $name = $client->secretName($projectId, $secretId); | ||
|
|
||
| // Build the request. | ||
| $request = GetSecretRequest::build($name); | ||
|
|
||
| // Get the secret. | ||
| $secret = $client->getSecret($request); | ||
|
|
||
| printf( | ||
| 'Found secret %s with secret type %s%s', | ||
| $secret->getName(), | ||
| SecretType::name($secret->getSecretType()), | ||
| PHP_EOL | ||
| ); | ||
| } | ||
| // [END secretmanager_get_secret_type] | ||
|
|
||
| // The following 2 lines are only needed to execute the samples on the CLI | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
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.
[Nit] Add
// Import the Secret Manager client library.after// [START ...]across the 4 regional managed rotation files to matchcreate_secret_with_type.php,get_secret_type.php,get_regional_secret_type.php, and existing regional samples.