-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add samples for secret expiration - create, update, and delete #10225
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
base: main
Are you sure you want to change the base?
Changes from all commits
e337c21
36203bb
0acae16
9d6e4ee
710bb85
bfbc85c
c335901
15b8d11
d744090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package secretmanager; | ||
|
|
||
| // [START secretmanager_bind_secret_tag] | ||
| import com.google.cloud.resourcemanager.v3.CreateTagBindingRequest; | ||
| import com.google.cloud.resourcemanager.v3.TagBinding; | ||
| import com.google.cloud.resourcemanager.v3.TagBindingsClient; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public class BindSecretTag { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| // TODO(developer): replace these variables before running the sample. | ||
|
|
||
| // This is the id of the GCP project | ||
| String projectId = "your-project-id"; | ||
| // This is the id of the secret to act on | ||
| String secretId = "your-secret-id"; | ||
| // Tag value to bind, e.g. "tagValues/123" | ||
| String tagValueName = "your-tag-value"; | ||
|
|
||
| bindSecretTag(projectId, secretId, tagValueName); | ||
| } | ||
|
|
||
| // Bind a TagValue to a Secret by creating a TagBinding. | ||
| public static TagBinding bindSecretTag(String projectId, String secretId, String tagValueName) | ||
| throws IOException, InterruptedException, ExecutionException { | ||
|
|
||
| String parent = String.format("//secretmanager.googleapis.com/projects/%s/secrets/%s", | ||
| projectId, secretId); | ||
|
|
||
| try (TagBindingsClient tagBindingsClient = TagBindingsClient.create()) { | ||
| TagBinding tagBinding = TagBinding.newBuilder() | ||
| .setTagValue(tagValueName) | ||
| .setParent(parent) | ||
| .build(); | ||
|
|
||
| CreateTagBindingRequest request = CreateTagBindingRequest.newBuilder() | ||
| .setTagBinding(tagBinding) | ||
| .build(); | ||
|
|
||
| TagBinding created = tagBindingsClient.createTagBindingAsync(request).get(); | ||
| System.out.printf("Created TagBinding: %s\n", created.getName()); | ||
| return created; | ||
| } | ||
| } | ||
| } | ||
| // [END secretmanager_bind_secret_tag] | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||||||||
| /* | ||||||||||||
| * Copyright 2026 Google LLC | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
| * | ||||||||||||
| * 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. | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| package secretmanager; | ||||||||||||
|
|
||||||||||||
| // [START secretmanager_create_secret_with_cmek] | ||||||||||||
| import com.google.cloud.secretmanager.v1.CustomerManagedEncryption; | ||||||||||||
| import com.google.cloud.secretmanager.v1.ProjectName; | ||||||||||||
| import com.google.cloud.secretmanager.v1.Replication; | ||||||||||||
| import com.google.cloud.secretmanager.v1.Secret; | ||||||||||||
| import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; | ||||||||||||
| import java.io.IOException; | ||||||||||||
|
|
||||||||||||
| public class CreateSecretWithCmek { | ||||||||||||
|
|
||||||||||||
| public static void main(String[] args) throws IOException { | ||||||||||||
| // TODO(developer): Replace these variables before running the sample. | ||||||||||||
|
|
||||||||||||
| // This is the id of the GCP project | ||||||||||||
| String projectId = "your-project-id"; | ||||||||||||
| // This is the id of the secret to act on | ||||||||||||
| String secretId = "your-secret-id"; | ||||||||||||
| // This is the Full kms key name to be used for Cmek. | ||||||||||||
| String kmsKeyName = "your-kms-key-name"; | ||||||||||||
| createSecretWithCmek(projectId, secretId, kmsKeyName); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Create a secret with a customer-managed encryption key (CMEK). | ||||||||||||
| public static Secret createSecretWithCmek(String projectId, String secretId, String kmsKeyName) | ||||||||||||
| throws IOException { | ||||||||||||
|
|
||||||||||||
| // Initialize client that will be used to send requests. This client only needs | ||||||||||||
| // to be created | ||||||||||||
| // once, and can be reused for multiple requests. | ||||||||||||
|
Comment on lines
+45
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This multi-line comment can be made more concise to improve readability. It's generally better to keep comments on a single line if possible.
Suggested change
|
||||||||||||
| try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { | ||||||||||||
|
|
||||||||||||
| // Build the secret name. | ||||||||||||
| ProjectName projectName = ProjectName.of(projectId); | ||||||||||||
|
|
||||||||||||
| // Build the Cmek configuration. | ||||||||||||
| CustomerManagedEncryption customerManagedEncryption = | ||||||||||||
| CustomerManagedEncryption.newBuilder().setKmsKeyName(kmsKeyName).build(); | ||||||||||||
|
|
||||||||||||
| // Build the replication using Cmek. | ||||||||||||
| Replication secretReplication = | ||||||||||||
| Replication.newBuilder() | ||||||||||||
| .setAutomatic( | ||||||||||||
| Replication.Automatic.newBuilder() | ||||||||||||
| .setCustomerManagedEncryption(customerManagedEncryption) | ||||||||||||
| .build()) | ||||||||||||
| .build(); | ||||||||||||
|
|
||||||||||||
| // Build the secret to create with the replication policy. | ||||||||||||
| Secret secret = Secret.newBuilder().setReplication(secretReplication).build(); | ||||||||||||
|
|
||||||||||||
| // Create the secret. | ||||||||||||
| Secret createdSecret = client.createSecret(projectName, secretId, secret); | ||||||||||||
| System.out.printf("Created secret %s\n", createdSecret.getName()); | ||||||||||||
| return createdSecret; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| // [END secretmanager_create_secret_with_cmek] | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package secretmanager; | ||
|
|
||
| // [START secretmanager_create_secret_with_expiration] | ||
| import com.google.cloud.secretmanager.v1.ProjectName; | ||
| import com.google.cloud.secretmanager.v1.Replication; | ||
| import com.google.cloud.secretmanager.v1.Secret; | ||
| import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; | ||
| import com.google.protobuf.Timestamp; | ||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
|
|
||
| public class CreateSecretWithExpiration { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
|
|
||
| // This is the id of the GCP project | ||
| String projectId = "your-project-id"; | ||
| // This is the id of the secret to create | ||
| String secretId = "your-secret-id"; | ||
| // This is the time in seconds from now when the secret will expire | ||
| long expireTimeSeconds = 86400; // 24 hours | ||
| createSecretWithExpiration(projectId, secretId, expireTimeSeconds); | ||
| } | ||
|
|
||
| // Create a new secret with an expiration time. | ||
| public static Secret createSecretWithExpiration( | ||
| String projectId, String secretId, long expireTimeSeconds) throws IOException { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { | ||
| // Build the parent name from the project. | ||
| ProjectName projectName = ProjectName.of(projectId); | ||
|
|
||
| // Calculate the expiration time. | ||
| Instant expireTime = Instant.now().plusSeconds(expireTimeSeconds); | ||
| Timestamp expireTimestamp = Timestamp.newBuilder() | ||
| .setSeconds(expireTime.getEpochSecond()) | ||
| .setNanos(expireTime.getNano()) | ||
| .build(); | ||
|
|
||
| // Build the secret to create with expiration time. | ||
| Secret secret = | ||
| Secret.newBuilder() | ||
| .setReplication( | ||
| Replication.newBuilder() | ||
| .setAutomatic(Replication.Automatic.newBuilder().build()) | ||
| .build()) | ||
| .setExpireTime(expireTimestamp) | ||
| .build(); | ||
|
|
||
| // Create the secret. | ||
| Secret createdSecret = client.createSecret(projectName, secretId, secret); | ||
| System.out.printf("Created secret %s with expire time\n", createdSecret.getName()); | ||
|
|
||
| return createdSecret; | ||
| } | ||
| } | ||
| } | ||
| // [END secretmanager_create_secret_with_expiration] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package secretmanager; | ||
|
|
||
| // [START secretmanager_create_secret_with_rotation] | ||
| import com.google.cloud.secretmanager.v1.ProjectName; | ||
| import com.google.cloud.secretmanager.v1.Replication; | ||
| import com.google.cloud.secretmanager.v1.Rotation; | ||
| import com.google.cloud.secretmanager.v1.Secret; | ||
| import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; | ||
| import com.google.cloud.secretmanager.v1.Topic; | ||
| import com.google.protobuf.Duration; | ||
| import com.google.protobuf.Timestamp; | ||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
|
|
||
| public class CreateSecretWithRotation { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
|
|
||
| // This is the id of the GCP project | ||
| String projectId = "your-project-id"; | ||
| // This is the id of the secret to create | ||
| String secretId = "your-secret-id"; | ||
| // This is the rotation period in seconds (e.g., 2592000 for 30 days) | ||
| long rotationPeriodSeconds = 2592000; | ||
| // This is the topic name in the format projects/PROJECT_ID/topics/TOPIC_ID | ||
| String topicName = "projects/your-project-id/topics/your-topic-id"; | ||
| createSecretWithRotation(projectId, secretId, rotationPeriodSeconds, topicName); | ||
| } | ||
|
|
||
| // Create a new secret with automatic rotation. | ||
| public static Secret createSecretWithRotation( | ||
| String projectId, String secretId, long rotationPeriodSeconds, String topicName) | ||
| throws IOException { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { | ||
| // Build the parent name from the project. | ||
| ProjectName projectName = ProjectName.of(projectId); | ||
|
|
||
| // Calculate the next rotation time. | ||
| Instant nextRotationTime = Instant.now().plusSeconds(rotationPeriodSeconds); | ||
| Timestamp nextRotationTimestamp = Timestamp.newBuilder() | ||
| .setSeconds(nextRotationTime.getEpochSecond()) | ||
| .setNanos(nextRotationTime.getNano()) | ||
| .build(); | ||
|
|
||
| // Build the rotation policy. | ||
| Rotation rotation = Rotation.newBuilder() | ||
| .setNextRotationTime(nextRotationTimestamp) | ||
| .setRotationPeriod(Duration.newBuilder().setSeconds(rotationPeriodSeconds).build()) | ||
| .build(); | ||
|
|
||
| // Build the topic for rotation notifications. | ||
| Topic topic = Topic.newBuilder() | ||
| .setName(topicName) | ||
| .build(); | ||
|
|
||
| // Build the secret to create with rotation and topic. | ||
| Secret secret = | ||
| Secret.newBuilder() | ||
| .setReplication( | ||
| Replication.newBuilder() | ||
| .setAutomatic(Replication.Automatic.newBuilder().build()) | ||
| .build()) | ||
| .setRotation(rotation) | ||
| .addTopics(topic) | ||
| .build(); | ||
|
|
||
| // Create the secret. | ||
| Secret createdSecret = client.createSecret(projectName, secretId, secret); | ||
| System.out.printf("Created secret %s with rotation\n", createdSecret.getName()); | ||
|
|
||
| return createdSecret; | ||
| } | ||
| } | ||
| } | ||
| // [END secretmanager_create_secret_with_rotation] |
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.
The copyright year
2026appears to be a typo and should be2024.