-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(secretmanager): Added samples for creating secret with CMEK and Pub/Sub Topic #2185
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
dhavalbhensdadiya-crest
wants to merge
11
commits into
GoogleCloudPlatform:main
Choose a base branch
from
dhavalbhensdadiya-crest:samples/cmek-topic
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
11 commits
Select commit
Hold shift + click to select a range
ac81cd0
feat(secretmanager): Add samples for listing secrets and versions wit…
dhavalbhensdadiya-crest d6f1572
Adding new line at end as per gemini in filter tests
dhavalbhensdadiya-crest 598200e
feat(secretmanager): Add samples for ETag-based secret and secret ver…
dhavalbhensdadiya-crest 503860f
Add newline at end as per gemini suggestion in etag samples
dhavalbhensdadiya-crest b06787f
feat(secretmanager): Added samples for creating and managing secrets …
dhavalbhensdadiya-crest b4b4021
Resolving comments from gemini code review
dhavalbhensdadiya-crest c583f7b
Resolved gemini code review suggestions
dhavalbhensdadiya-crest 27e665d
Add newline at end as per gemini suggetions in expiration tests
dhavalbhensdadiya-crest 0d29885
feat(secretmanager): Add samples for creating secrets with CMEK and P…
dhavalbhensdadiya-crest 2b3d4b5
Resolving gemini comments
dhavalbhensdadiya-crest bc6bb4c
Added newlines at end as per gemini suggestions in cmek and topic tes…
dhavalbhensdadiya-crest 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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?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_cmek] | ||
| use Google\Cloud\SecretManager\V1\CreateSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\CustomerManagedEncryption; | ||
| use Google\Cloud\SecretManager\V1\Secret; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
|
|
||
| /** | ||
| * Create a regional secret that uses a customer-managed encryption key (CMEK). | ||
| * | ||
| * @param string $projectId Google Cloud project id (e.g. 'my-project-id') | ||
| * @param string $locationId Secret location (e.g. 'us-central1') | ||
| * @param string $secretId Id for the new secret (e.g. 'my-secret-id') | ||
| * @param string $kmsKeyName Full KMS key resource name (e.g. 'projects/my-project/locations/us-central1/keyRings/my-kr/cryptoKeys/my-key') | ||
| */ | ||
| function create_regional_secret_with_cmek(string $projectId, string $locationId, string $secretId, string $kmsKeyName): void | ||
| { | ||
| $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; | ||
| $client = new SecretManagerServiceClient($options); | ||
|
|
||
| $parent = $client->locationName($projectId, $locationId); | ||
|
|
||
| $cmek = new CustomerManagedEncryption([ | ||
| 'kms_key_name' => $kmsKeyName, | ||
| ]); | ||
|
|
||
| $secret = new Secret([ | ||
| 'customer_managed_encryption' => $cmek | ||
| ]); | ||
|
|
||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| $created = $client->createSecret($request); | ||
|
|
||
| printf('Created secret %s with CMEK %s%s', $created->getName(), $kmsKeyName, PHP_EOL); | ||
| } | ||
| // [END secretmanager_create_regional_secret_with_cmek] | ||
|
|
||
| // 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); |
69 changes: 69 additions & 0 deletions
69
secretmanager/src/create_regional_secret_with_expiration.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,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_create_regional_secret_with_expiration] | ||
| use Google\Cloud\SecretManager\V1\CreateSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Secret; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
| use Google\Protobuf\Duration; | ||
|
|
||
| /** | ||
| * Create a regional secret with expiration TTL. | ||
| * | ||
| * @param string $projectId Google Cloud project id (e.g. 'my-project') | ||
| * @param string $locationId Secret location (e.g. 'us-central1') | ||
| * @param string $secretId Id for the new secret (e.g. 'my-secret') | ||
| */ | ||
| function create_regional_secret_with_expiration(string $projectId, string $locationId, string $secretId): void | ||
| { | ||
| // Create the Secret Manager Regional client. | ||
| $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; | ||
| $client = new SecretManagerServiceClient($options); | ||
|
|
||
| // Build the resource name of the parent project. | ||
| $parent = $client->locationName($projectId, $locationId); | ||
|
|
||
| $duration = new Duration(); | ||
| $duration->setSeconds(3600); // 1 hour TTL in seconds | ||
|
|
||
| $secret = new Secret(); | ||
| $secret->setTtl($duration); | ||
|
|
||
| // Build the request. | ||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| // Create the secret. | ||
| $newSecret = $client->createSecret($request); | ||
|
|
||
| // Print the new secret name. | ||
| printf('Created secret: %s%s', $newSecret->getName(), PHP_EOL); | ||
| } | ||
| // [END secretmanager_create_regional_secret_with_expiration] | ||
|
|
||
| // 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_create_regional_secret_with_topic] | ||
| use Google\Cloud\SecretManager\V1\CreateSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Secret; | ||
| use Google\Cloud\SecretManager\V1\Topic; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
|
|
||
| /** | ||
| * Create a regional secret and associate it with a Pub/Sub topic. | ||
| * | ||
| * @param string $projectId Google Cloud project id (e.g. 'my-project') | ||
| * @param string $locationId Secret location (e.g. 'us-central1') | ||
| * @param string $secretId Id for the new secret (e.g. 'my-secret') | ||
| * @param string $topicName Full topic resource name (projects/{project}/topics/{topic}) | ||
| */ | ||
| function create_regional_secret_with_topic(string $projectId, string $locationId, string $secretId, string $topicName): void | ||
| { | ||
| $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; | ||
| $client = new SecretManagerServiceClient($options); | ||
|
|
||
| $parent = $client->locationName($projectId, $locationId); | ||
|
|
||
| $secret = new Secret([ | ||
| 'topics' => [new Topic(['name' => $topicName])], | ||
| ]); | ||
|
|
||
| // Build the request. | ||
dhavalbhensdadiya-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| // Create the secret. | ||
| $created = $client->createSecret($request); | ||
|
|
||
| printf('Created secret %s with topic %s%s', $created->getName(), $topicName, PHP_EOL); | ||
| } | ||
| // [END secretmanager_create_regional_secret_with_topic] | ||
|
|
||
| // 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,71 @@ | ||
| <?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_cmek] | ||
| use Google\Cloud\SecretManager\V1\CreateSecretRequest; | ||
| use Google\Cloud\SecretManager\V1\Replication; | ||
| use Google\Cloud\SecretManager\V1\Replication\Automatic; | ||
| use Google\Cloud\SecretManager\V1\CustomerManagedEncryption; | ||
| use Google\Cloud\SecretManager\V1\Secret; | ||
| use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; | ||
|
|
||
| /** | ||
| * Create a secret that uses a customer-managed encryption key (CMEK). | ||
| * | ||
| * @param string $projectId Google Cloud project id (e.g. 'my-project-id') | ||
| * @param string $secretId Id for the new secret (e.g. 'my-secret-id') | ||
| * @param string $kmsKeyName Full KMS key resource name (e.g. 'projects/my-project/locations/global/keyRings/my-kr/cryptoKeys/my-key') | ||
| */ | ||
| function create_secret_with_cmek(string $projectId, string $secretId, string $kmsKeyName): void | ||
| { | ||
| $client = new SecretManagerServiceClient(); | ||
|
|
||
| $parent = $client->projectName($projectId); | ||
|
|
||
| $cmek = new CustomerManagedEncryption([ | ||
| 'kms_key_name' => $kmsKeyName, | ||
| ]); | ||
|
|
||
| $secret = new Secret([ | ||
| 'replication' => new Replication([ | ||
| 'automatic' => new Automatic([ | ||
| 'customer_managed_encryption' => $cmek, | ||
| ]), | ||
| ]), | ||
| ]); | ||
|
|
||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| $created = $client->createSecret($request); | ||
|
|
||
| printf('Created secret %s with CMEK %s%s', $created->getName(), $kmsKeyName, PHP_EOL); | ||
| } | ||
| // [END secretmanager_create_secret_with_cmek] | ||
|
|
||
| // 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,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_secret_with_expiration] | ||
| 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\Client\SecretManagerServiceClient; | ||
| use Google\Protobuf\Duration; | ||
|
|
||
| /** | ||
| * Create a secret with expiration TTL. | ||
| * | ||
| * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') | ||
| * @param string $secretId Your secret ID (e.g. 'my-secret') | ||
| */ | ||
| function create_secret_with_expiration(string $projectId, string $secretId): 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(), | ||
| ]), | ||
| ]); | ||
|
|
||
| $duration = new Duration(); | ||
| $duration->setSeconds(3600); // 1 hour TTL in seconds | ||
|
|
||
| $secret->setTtl($duration); | ||
|
|
||
| // Build the request. | ||
| $request = CreateSecretRequest::build($parent, $secretId, $secret); | ||
|
|
||
| // Create the secret. | ||
| $newSecret = $client->createSecret($request); | ||
|
|
||
| // Print the new secret name. | ||
| printf('Created secret: %s%s', $newSecret->getName(), PHP_EOL); | ||
| } | ||
| // [END secretmanager_create_secret_with_expiration] | ||
|
|
||
| // 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.
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.
Uh oh!
There was an error while loading. Please reload this page.