Skip to content

Commit a69d8ae

Browse files
feat(secretmanager): Add Cloud SQL managed-rotation samples (#2335)
* 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. * fix(secretmanager): address review comments on Cloud SQL rotation tests - Add @Depends annotations so testGetRegionalSecretType/testGetSecretType explicitly declare their dependency on the test that creates their secret, instead of relying on declaration order. - Move grantCloudSqlRole() inside the try block in the three Cloud SQL rotation tests so the secret is still cleaned up in finally if the grant itself throws. * docs(secretmanager): address review feedback on Cloud SQL managed-rotation samples Align docblocks with other samples, simplify comments and clarify rotation schedule behavior. * fix(secretmanager): address review feedback on Cloud SQL managed rotation samples --------- Co-authored-by: Brent Shaffer <betterbrent@google.com>
1 parent e58e078 commit a69d8ae

11 files changed

Lines changed: 811 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.
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/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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
31+
use Google\Cloud\SecretManager\V1\Secret;
32+
use Google\Cloud\SecretManager\V1\Secret\SecretType;
33+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
34+
35+
/**
36+
* Creates a new regional secret with type CLOUD_SQL_DB_CREDENTIALS.
37+
*
38+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
39+
* @param string $locationId Your secret Location (e.g. 'us-central1')
40+
* @param string $secretId Your secret ID (e.g. 'my-secret')
41+
*/
42+
function create_regional_secret_with_cloud_sql_credentials(string $projectId, string $locationId, string $secretId): void
43+
{
44+
// Specify regional endpoint.
45+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
46+
47+
// Create the Secret Manager client.
48+
$client = new SecretManagerServiceClient($options);
49+
50+
// Build the resource name of the parent project.
51+
$parent = $client->locationName($projectId, $locationId);
52+
53+
$secret = new Secret([
54+
'secret_type' => SecretType::CLOUD_SQL_DB_CREDENTIALS,
55+
]);
56+
57+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
58+
59+
// Create the secret.
60+
$newSecret = $client->createSecret($request);
61+
62+
printf('Created secret: %s%s', $newSecret->getName(), PHP_EOL);
63+
64+
printf(
65+
'Grant the Cloud SQL User rotate IAM permissions to enable managed rotation to: %s%s',
66+
$newSecret->getPolicyMember()->getIamPolicyUidPrincipal(),
67+
PHP_EOL
68+
);
69+
}
70+
// [END secretmanager_create_regional_secret_with_cloud_sql_credentials]
71+
72+
// The following 2 lines are only needed to execute the samples on the CLI
73+
require_once __DIR__ . '/../../testing/sample_helpers.php';
74+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
* Creates a new secret with the given secret type.
39+
* Note: CLOUD_SQL_DB_CREDENTIALS is only supported in the regional secret.
40+
*
41+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
42+
* @param string $secretId Your secret ID (e.g. 'my-secret')
43+
* @param string $secretType Your secret type (e.g. 'ACCESS_KEY')
44+
*/
45+
function create_secret_with_type(string $projectId, string $secretId, string $secretType): void
46+
{
47+
// Create the Secret Manager client.
48+
$client = new SecretManagerServiceClient();
49+
50+
// Build the resource name of the parent project.
51+
$parent = $client->projectName($projectId);
52+
53+
$secret = new Secret([
54+
'replication' => new Replication([
55+
'automatic' => new Automatic(),
56+
]),
57+
'secret_type' => SecretType::value($secretType),
58+
]);
59+
60+
// Build the request.
61+
$request = CreateSecretRequest::build($parent, $secretId, $secret);
62+
63+
// Create the secret.
64+
$newSecret = $client->createSecret($request);
65+
66+
// Print the new secret name.
67+
printf('Created secret with secret type: %s%s', $newSecret->getName(), PHP_EOL);
68+
}
69+
// [END secretmanager_create_secret_with_type]
70+
71+
// The following 2 lines are only needed to execute the samples on the CLI
72+
require_once __DIR__ . '/../../testing/sample_helpers.php';
73+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// Import the Secret Manager client library.
30+
use Google\Cloud\SecretManager\V1\EnableManagedRotationRequest;
31+
use Google\Cloud\SecretManager\V1\EnableManagedRotationRequest\CloudSQLSingleUserCredentials;
32+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
33+
34+
/**
35+
* Enables managed rotation of a CLOUD_SQL_DB_CREDENTIALS typed secret.
36+
* It validates and enables the rotation, adding a version and sets the
37+
* passed password (optional).
38+
* Note: AddSecretVersion is disabled on the CLOUD_SQL_DB_CREDENTIALS
39+
* currently and for any necessary manual rotations please trigger
40+
* rotate_secret.
41+
*
42+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
43+
* @param string $locationId Your secret Location (e.g. 'us-central1')
44+
* @param string $secretId Your secret ID (e.g. 'my-secret')
45+
* @param string $instanceId Your Cloud SQL instance ID
46+
* @param string $username Your Cloud SQL database username
47+
*/
48+
function enable_regional_secret_managed_rotation(string $projectId, string $locationId, string $secretId, string $instanceId, string $username): void
49+
{
50+
// Specify regional endpoint.
51+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
52+
53+
// Create the Secret Manager client.
54+
$client = new SecretManagerServiceClient($options);
55+
56+
// Build the resource name of the secret.
57+
$parent = $client->projectLocationSecretName($projectId, $locationId, $secretId);
58+
59+
$credentials = new CloudSQLSingleUserCredentials([
60+
'instance_id' => $instanceId,
61+
'username' => $username,
62+
]);
63+
64+
$request = (new EnableManagedRotationRequest())
65+
->setParent($parent)
66+
->setCloudSqlSingleUserCredentials($credentials);
67+
68+
// Enable managed rotation.
69+
$version = $client->enableManagedRotation($request);
70+
71+
printf('Enabled managed rotation, created secret version: %s%s', $version->getName(), PHP_EOL);
72+
}
73+
// [END secretmanager_enable_regional_secret_managed_rotation]
74+
75+
// The following 2 lines are only needed to execute the samples on the CLI
76+
require_once __DIR__ . '/../../testing/sample_helpers.php';
77+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
* Gets the secret type of the given regional secret.
36+
*
37+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
38+
* @param string $locationId Location of the secret (e.g. 'us-central1')
39+
* @param string $secretId Your secret ID (e.g. 'my-secret')
40+
*/
41+
function get_regional_secret_type(string $projectId, string $locationId, string $secretId): void
42+
{
43+
// Specify regional endpoint.
44+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
45+
46+
// Create the Secret Manager client.
47+
$client = new SecretManagerServiceClient($options);
48+
49+
// Build the resource name of the secret.
50+
$name = $client->projectLocationSecretName($projectId, $locationId, $secretId);
51+
52+
// Build the request.
53+
$request = GetSecretRequest::build($name);
54+
55+
// Get the secret.
56+
$secret = $client->getSecret($request);
57+
58+
printf(
59+
'Found regional secret %s with secret type %s%s',
60+
$secret->getName(),
61+
SecretType::name($secret->getSecretType()),
62+
PHP_EOL
63+
);
64+
}
65+
// [END secretmanager_get_regional_secret_type]
66+
67+
// The following 2 lines are only needed to execute the samples on the CLI
68+
require_once __DIR__ . '/../../testing/sample_helpers.php';
69+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_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+
* Gets the secret type of the given secret.
36+
*
37+
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
38+
* @param string $secretId Your secret ID (e.g. 'my-secret')
39+
*/
40+
function get_secret_type(string $projectId, string $secretId): void
41+
{
42+
// Create the Secret Manager client.
43+
$client = new SecretManagerServiceClient();
44+
45+
// Build the resource name of the secret.
46+
$name = $client->secretName($projectId, $secretId);
47+
48+
// Build the request.
49+
$request = GetSecretRequest::build($name);
50+
51+
// Get the secret.
52+
$secret = $client->getSecret($request);
53+
54+
printf(
55+
'Found secret %s with secret type %s%s',
56+
$secret->getName(),
57+
SecretType::name($secret->getSecretType()),
58+
PHP_EOL
59+
);
60+
}
61+
// [END secretmanager_get_secret_type]
62+
63+
// The following 2 lines are only needed to execute the samples on the CLI
64+
require_once __DIR__ . '/../../testing/sample_helpers.php';
65+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)