Skip to content
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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions secretmanager/src/main/java/secretmanager/CreateSecret.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 NumberFormatException if the input is not a valid integer. This also aligns with the Sample Format Guide, which emphasizes clear error handling.


// Build the secret to create.
Secret secret =
Secret.newBuilder()
.setReplication(
Replication.newBuilder()
.setAutomatic(Replication.Automatic.newBuilder().build())
.build())
.setTtl(ttl)
.build();

// Create the secret.
Expand Down