Skip to content

Commit 9e79149

Browse files
feat(secretmanager): add Cloud SQL managed-rotation and secret-type samples
Adds Cloud SQL managed-rotation samples for regional secrets (create/enable/rotate/schedule/get-type) plus secret-type samples for global secrets (create-with-type/get-type), ported from the completed Python reference implementation. Bumps google/cloud-secret-manager to ^2.4.0, the minimum version with Cloud SQL managed-rotation support.
1 parent 699cf3e commit 9e79149

12 files changed

Lines changed: 1531 additions & 1 deletion

‎secretmanager/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This simple command-line application demonstrates how to invoke
2121
- GOOGLE_CLOUD_PUBSUB_TOPIC - Full name of topic (projects/{project}/topics/{topic}).
2222
- GOOGLE_CLOUD_KMS_KEY - Full name of global KMS key (projects/{project}/locations/global/keyRings/{keyring}/cryptoKeys/{key}).
2323
- GOOGLE_CLOUD_REGIONAL_KMS_KEY - Full name of regional KMS key (projects/{project}/locations/{location}/keyRings/{keyring}/cryptoKeys/{key}).
24+
- CLOUD_SQL_INSTANCE / CLOUD_SQL_USER - Bare Cloud SQL instance ID and database username, required by the Cloud SQL managed-rotation tests. See [TESTING_cloudsql_managed_rotation.md](TESTING_cloudsql_managed_rotation.md) for setup.
2425

2526
1. **Download The Credentials** - Click "Go to credentials" after enabling the
2627
APIs. Click "New Credentials" and select "Service Account Key". Create a new

‎secretmanager/TESTING_cloudsql_managed_rotation.md‎

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

‎secretmanager/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-secret-manager": "^2.1.0",
3+
"google/cloud-secret-manager": "^2.4.0",
44
"google/cloud-resource-manager": "^1.0"
55
}
66
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_create_regional_secret_with_cloud_sql_credentials]
29+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
30+
use Google\Cloud\SecretManager\V1\Secret;
31+
use Google\Cloud\SecretManager\V1\Secret\SecretType;
32+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
33+
34+
/**
35+
* Create a new secret with the Cloud SQL DB credentials secret type. This
36+
* type is required to enable Secret Manager's automatic rotation of Cloud
37+
* SQL passwords. It can only be set when the secret is created, and the
38+
* secret's location must match the region of the target Cloud SQL instance.
39+
*
40+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
41+
* @param string $locationId Location of the secret; must match the Cloud SQL
42+
* instance's region (e.g. 'us-central1')
43+
* @param string $secretId Your secret ID (e.g. 'my-secret')
44+
*/
45+
function create_regional_secret_with_cloud_sql_credentials(string $projectId, string $locationId, string $secretId): void
46+
{
47+
// Specify regional endpoint.
48+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
49+
50+
// Create the Secret Manager client.
51+
$client = new SecretManagerServiceClient($options);
52+
53+
// Build the resource name of the parent project.
54+
$parent = $client->locationName($projectId, $locationId);
55+
56+
$secret = new Secret([
57+
'secret_type' => SecretType::CLOUD_SQL_DB_CREDENTIALS,
58+
]);
59+
60+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
61+
62+
// Create the secret.
63+
$newSecret = $client->createSecret($request);
64+
65+
printf('Created secret: %s%s', $newSecret->getName(), PHP_EOL);
66+
67+
// This built-in identity is what you grant Cloud SQL IAM permissions to,
68+
// so that Secret Manager can rotate the database password on its behalf.
69+
printf(
70+
'Grant this identity Cloud SQL IAM permissions to enable rotation: %s%s',
71+
$newSecret->getPolicyMember()->getIamPolicyUidPrincipal(),
72+
PHP_EOL
73+
);
74+
}
75+
// [END secretmanager_create_regional_secret_with_cloud_sql_credentials]
76+
77+
// The following 2 lines are only needed to execute the samples on the CLI
78+
require_once __DIR__ . '/../../testing/sample_helpers.php';
79+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_create_secret_with_type]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
31+
use Google\Cloud\SecretManager\V1\Replication;
32+
use Google\Cloud\SecretManager\V1\Replication\Automatic;
33+
use Google\Cloud\SecretManager\V1\Secret;
34+
use Google\Cloud\SecretManager\V1\Secret\SecretType;
35+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
36+
37+
/**
38+
* Create a new secret with the given secret type restriction (e.g.
39+
* ACCESS_KEY, CERTIFICATE, OTHER_DB_CREDENTIALS, or OTHER -- use
40+
* CLOUD_SQL_DB_CREDENTIALS only for a regional secret that will go through
41+
* enable_regional_secret_managed_rotation.php). Unlike
42+
* CLOUD_SQL_DB_CREDENTIALS, these other secret types are plain metadata
43+
* tags: they don't require any additional credentials payload at creation
44+
* time.
45+
*
46+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
47+
* @param string $secretId Your secret ID (e.g. 'my-secret')
48+
* @param string $secretType Secret type restriction to apply (e.g. 'ACCESS_KEY', 'CERTIFICATE', 'OTHER_DB_CREDENTIALS', 'OTHER')
49+
*/
50+
function create_secret_with_type(string $projectId, string $secretId, string $secretType): void
51+
{
52+
// Create the Secret Manager client.
53+
$client = new SecretManagerServiceClient();
54+
55+
// Build the resource name of the parent project.
56+
$parent = $client->projectName($projectId);
57+
58+
$secret = new Secret([
59+
'replication' => new Replication([
60+
'automatic' => new Automatic(),
61+
]),
62+
'secret_type' => SecretType::value($secretType),
63+
]);
64+
65+
// Build the request.
66+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
67+
68+
// Create the secret, with the given secret type restriction.
69+
$newSecret = $client->createSecret($request);
70+
71+
// Print the new secret name.
72+
printf('Created secret with secret type: %s%s', $newSecret->getName(), PHP_EOL);
73+
}
74+
// [END secretmanager_create_secret_with_type]
75+
76+
// The following 2 lines are only needed to execute the samples on the CLI
77+
require_once __DIR__ . '/../../testing/sample_helpers.php';
78+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_enable_regional_secret_managed_rotation]
29+
use Google\Cloud\SecretManager\V1\EnableManagedRotationRequest;
30+
use Google\Cloud\SecretManager\V1\EnableManagedRotationRequest\CloudSQLSingleUserCredentials;
31+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
32+
33+
/**
34+
* Enable managed rotation for a Cloud SQL DB credentials secret. This links
35+
* the secret to a Cloud SQL instance and database user, and can only be
36+
* called once per secret. It adds the secret's first version and sets the
37+
* matching password on the Cloud SQL user, taking the place of a manually
38+
* added secret version, which this secret type doesn't support. Afterwards,
39+
* use rotate_regional_secret.php to trigger further rotations.
40+
*
41+
* $instanceId is the bare Cloud SQL instance ID (e.g. "my-instance") -- not a
42+
* connection name. Neither the project nor the region should be included:
43+
* passing "PROJECT_ID:INSTANCE_ID" (as gcloud's own
44+
* `enable-managed-rotation --help` examples misleadingly show) or the full
45+
* "PROJECT_ID:LOCATION_ID:INSTANCE_ID" connection name both fail -- the
46+
* service already knows the project from the secret's own path, and prepends
47+
* it internally, so a qualified value ends up double-prefixed.
48+
*
49+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
50+
* @param string $locationId Location of the secret (e.g. 'us-central1')
51+
* @param string $secretId ID of the Cloud SQL DB credentials secret to enable rotation on
52+
* @param string $instanceId Bare ID of the Cloud SQL instance (no project or region prefix)
53+
* @param string $username Username of the Cloud SQL database user
54+
*/
55+
function enable_regional_secret_managed_rotation(string $projectId, string $locationId, string $secretId, string $instanceId, string $username): void
56+
{
57+
// Specify regional endpoint.
58+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
59+
60+
// Create the Secret Manager client.
61+
$client = new SecretManagerServiceClient($options);
62+
63+
// Build the resource name of the secret.
64+
$parent = $client->projectLocationSecretName($projectId, $locationId, $secretId);
65+
66+
$credentials = new CloudSQLSingleUserCredentials([
67+
'instance_id' => $instanceId,
68+
'username' => $username,
69+
// Leaving password unset lets Secret Manager generate a secure
70+
// password itself.
71+
]);
72+
73+
$request = (new EnableManagedRotationRequest())
74+
->setParent($parent)
75+
->setCloudSqlSingleUserCredentials($credentials);
76+
77+
// Enable managed rotation.
78+
$version = $client->enableManagedRotation($request);
79+
80+
printf('Enabled managed rotation, created secret version: %s%s', $version->getName(), PHP_EOL);
81+
}
82+
// [END secretmanager_enable_regional_secret_managed_rotation]
83+
84+
// The following 2 lines are only needed to execute the samples on the CLI
85+
require_once __DIR__ . '/../../testing/sample_helpers.php';
86+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_get_regional_secret_type]
29+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
31+
use Google\Cloud\SecretManager\V1\GetSecretRequest;
32+
use Google\Cloud\SecretManager\V1\Secret\SecretType;
33+
34+
/**
35+
* Get and print the secret type (e.g. CLOUD_SQL_DB_CREDENTIALS, ACCESS_KEY,
36+
* CERTIFICATE, OTHER_DB_CREDENTIALS, OTHER, or SECRET_TYPE_UNSPECIFIED for a
37+
* secret with no type restriction) of the given regional secret.
38+
*
39+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
40+
* @param string $locationId Location of the secret (e.g. 'us-central1')
41+
* @param string $secretId Your secret ID (e.g. 'my-secret')
42+
*/
43+
function get_regional_secret_type(string $projectId, string $locationId, string $secretId): void
44+
{
45+
// Specify regional endpoint.
46+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
47+
48+
// Create the Secret Manager client.
49+
$client = new SecretManagerServiceClient($options);
50+
51+
// Build the resource name of the secret.
52+
$name = $client->projectLocationSecretName($projectId, $locationId, $secretId);
53+
54+
// Build the request.
55+
$request = GetSecretRequest::build($name);
56+
57+
// Get the secret.
58+
$secret = $client->getSecret($request);
59+
60+
printf(
61+
'Found regional secret %s with secret type %s%s',
62+
$secret->getName(),
63+
SecretType::name($secret->getSecretType()),
64+
PHP_EOL
65+
);
66+
}
67+
// [END secretmanager_get_regional_secret_type]
68+
69+
// The following 2 lines are only needed to execute the samples on the CLI
70+
require_once __DIR__ . '/../../testing/sample_helpers.php';
71+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)