|
| 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); |
0 commit comments