-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(secretmanager): add optional ttl to create secret sample #9889
Changes from all commits
8f64302
534b850
5eef983
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
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.Duration; | ||
import java.io.IOException; | ||
|
||
public class CreateSecret { | ||
|
@@ -41,13 +42,20 @@ public static void createSecret(String projectId, String secretId) throws IOExce | |
// Build the parent name from the project. | ||
ProjectName projectName = ProjectName.of(projectId); | ||
|
||
// Optionally set a TTL for the secret. This demonstrates how to configure | ||
// a secret to be automatically deleted after a certain period. The TTL is | ||
// specified in seconds (e.g., 900 for 15 minutes). This can be useful | ||
// for managing sensitive data and reducing storage costs. | ||
Duration ttl = Duration.newBuilder().setSeconds(900).build(); | ||
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. It's good practice to validate user inputs, especially when they can affect the behavior of the system. If you make the TTL configurable, add validation to ensure the provided value is within an acceptable range and is a valid integer. For example, you might want to enforce a minimum and maximum TTL and handle potential |
||
|
||
// Build the secret to create. | ||
Secret secret = | ||
Secret.newBuilder() | ||
.setReplication( | ||
Replication.newBuilder() | ||
.setAutomatic(Replication.Automatic.newBuilder().build()) | ||
.build()) | ||
.setTtl(ttl) | ||
.build(); | ||
|
||
// Create the secret. | ||
|
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.
Consider making the TTL configurable via a command-line argument or an environment variable. This would make the sample more flexible and allow users to experiment with different TTL values. This aligns with the Sample Format Guide's recommendation for user-provided parameters. For example, you could use
System.getenv("SECRET_TTL")
to read the TTL from an environment variable.