Skip to content

Commit 15b8d11

Browse files
feat(secretmanager): add examples for creating, updating and deleting secret expiration times
1 parent c335901 commit 15b8d11

File tree

8 files changed

+547
-0
lines changed

8 files changed

+547
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_create_secret_with_expiration]
20+
import com.google.cloud.secretmanager.v1.ProjectName;
21+
import com.google.cloud.secretmanager.v1.Replication;
22+
import com.google.cloud.secretmanager.v1.Secret;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
24+
import com.google.protobuf.Timestamp;
25+
import java.io.IOException;
26+
import java.time.Instant;
27+
28+
public class CreateSecretWithExpiration {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
33+
// This is the id of the GCP project
34+
String projectId = "your-project-id";
35+
// This is the id of the secret to create
36+
String secretId = "your-secret-id";
37+
// This is the time in seconds from now when the secret will expire
38+
long expireTimeSeconds = 86400; // 24 hours
39+
createSecretWithExpiration(projectId, secretId, expireTimeSeconds);
40+
}
41+
42+
// Create a new secret with an expiration time.
43+
public static Secret createSecretWithExpiration(
44+
String projectId, String secretId, long expireTimeSeconds) throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the "close" method on the client to safely clean up any remaining background resources.
48+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
49+
// Build the parent name from the project.
50+
ProjectName projectName = ProjectName.of(projectId);
51+
52+
// Calculate the expiration time.
53+
Instant expireTime = Instant.now().plusSeconds(expireTimeSeconds);
54+
Timestamp expireTimestamp = Timestamp.newBuilder()
55+
.setSeconds(expireTime.getEpochSecond())
56+
.setNanos(expireTime.getNano())
57+
.build();
58+
59+
// Build the secret to create with expiration time.
60+
Secret secret =
61+
Secret.newBuilder()
62+
.setReplication(
63+
Replication.newBuilder()
64+
.setAutomatic(Replication.Automatic.newBuilder().build())
65+
.build())
66+
.setExpireTime(expireTimestamp)
67+
.build();
68+
69+
// Create the secret.
70+
Secret createdSecret = client.createSecret(projectName, secretId, secret);
71+
System.out.printf("Created secret %s with expire time\n", createdSecret.getName());
72+
73+
return createdSecret;
74+
}
75+
}
76+
}
77+
// [END secretmanager_create_secret_with_expiration]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_delete_secret_expiration]
20+
import com.google.cloud.secretmanager.v1.Secret;
21+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1.SecretName;
23+
import com.google.protobuf.FieldMask;
24+
import com.google.protobuf.util.FieldMaskUtil;
25+
import java.io.IOException;
26+
27+
public class DeleteSecretExpiration {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
32+
// This is the id of the GCP project
33+
String projectId = "your-project-id";
34+
// This is the id of the secret to update
35+
String secretId = "your-secret-id";
36+
deleteSecretExpiration(projectId, secretId);
37+
}
38+
39+
// Delete the expiration time from an existing secret.
40+
public static Secret deleteSecretExpiration(String projectId, String secretId)
41+
throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
46+
// Build the secret name.
47+
SecretName secretName = SecretName.of(projectId, secretId);
48+
49+
// Build the updated secret without expiration time.
50+
Secret secret =
51+
Secret.newBuilder()
52+
.setName(secretName.toString())
53+
.build();
54+
55+
// Build the field mask to clear the expiration time.
56+
FieldMask fieldMask = FieldMaskUtil.fromString("expire_time");
57+
58+
// Update the secret to remove expiration.
59+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
60+
System.out.printf("Deleted expiration from secret %s\n", updatedSecret.getName());
61+
62+
return updatedSecret;
63+
}
64+
}
65+
}
66+
// [END secretmanager_delete_secret_expiration]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_update_secret_expiration]
20+
import com.google.cloud.secretmanager.v1.Secret;
21+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1.SecretName;
23+
import com.google.protobuf.FieldMask;
24+
import com.google.protobuf.Timestamp;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
import java.time.Instant;
28+
29+
public class UpdateSecretExpiration {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
34+
// This is the id of the GCP project
35+
String projectId = "your-project-id";
36+
// This is the id of the secret to update
37+
String secretId = "your-secret-id";
38+
// This is the time in seconds from now when the secret will expire
39+
long expireTimeSeconds = 86400; // 24 hours
40+
updateSecretExpiration(projectId, secretId, expireTimeSeconds);
41+
}
42+
43+
// Update an existing secret with a new expiration time.
44+
public static Secret updateSecretExpiration(
45+
String projectId, String secretId, long expireTimeSeconds) throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
50+
// Build the secret name.
51+
SecretName secretName = SecretName.of(projectId, secretId);
52+
53+
// Calculate the expiration time.
54+
Instant expireTime = Instant.now().plusSeconds(expireTimeSeconds);
55+
Timestamp expireTimestamp = Timestamp.newBuilder()
56+
.setSeconds(expireTime.getEpochSecond())
57+
.setNanos(expireTime.getNano())
58+
.build();
59+
60+
// Build the updated secret with new expiration time.
61+
Secret secret =
62+
Secret.newBuilder()
63+
.setName(secretName.toString())
64+
.setExpireTime(expireTimestamp)
65+
.build();
66+
67+
// Build the field mask to update only the expiration time.
68+
FieldMask fieldMask = FieldMaskUtil.fromString("expire_time");
69+
70+
// Update the secret.
71+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
72+
System.out.printf("Updated secret %s with new expiration time\n", updatedSecret.getName());
73+
74+
return updatedSecret;
75+
}
76+
}
77+
}
78+
// [END secretmanager_update_secret_expiration]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager.regionalsamples;
18+
19+
// [START secretmanager_create_regional_secret_with_expiration]
20+
import com.google.cloud.secretmanager.v1.LocationName;
21+
import com.google.cloud.secretmanager.v1.Secret;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
24+
import com.google.protobuf.Timestamp;
25+
import java.io.IOException;
26+
import java.time.Instant;
27+
28+
public class CreateRegionalSecretWithExpiration {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
33+
// Your GCP project ID.
34+
String projectId = "your-project-id";
35+
// Location of the secret.
36+
String locationId = "your-location-id";
37+
// Resource ID of the secret to create.
38+
String secretId = "your-secret-id";
39+
// This is the time in seconds from now when the secret will expire.
40+
long expireTimeSeconds = 86400; // 24 hours
41+
createRegionalSecretWithExpiration(projectId, locationId, secretId, expireTimeSeconds);
42+
}
43+
44+
// Create a new regional secret with an expiration time.
45+
public static Secret createRegionalSecretWithExpiration(
46+
String projectId, String locationId, String secretId, long expireTimeSeconds)
47+
throws IOException {
48+
49+
// Endpoint to call the regional secret manager sever
50+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
51+
SecretManagerServiceSettings secretManagerServiceSettings =
52+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
53+
54+
// Initialize the client that will be used to send requests. This client only needs to be
55+
// created once, and can be reused for multiple requests.
56+
try (SecretManagerServiceClient client =
57+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
58+
// Build the parent name from the project.
59+
LocationName location = LocationName.of(projectId, locationId);
60+
61+
// Calculate the expiration time.
62+
Instant expireTime = Instant.now().plusSeconds(expireTimeSeconds);
63+
Timestamp expireTimestamp = Timestamp.newBuilder()
64+
.setSeconds(expireTime.getEpochSecond())
65+
.setNanos(expireTime.getNano())
66+
.build();
67+
68+
// Build the regional secret to create with expiration time.
69+
Secret secret =
70+
Secret.newBuilder()
71+
.setExpireTime(expireTimestamp)
72+
.build();
73+
74+
// Create the regional secret.
75+
Secret createdSecret = client.createSecret(location.toString(), secretId, secret);
76+
System.out.printf("Created secret %s with expire time\n", createdSecret.getName());
77+
78+
return createdSecret;
79+
}
80+
}
81+
}
82+
// [END secretmanager_create_regional_secret_with_expiration]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager.regionalsamples;
18+
19+
// [START secretmanager_delete_regional_secret_expiration]
20+
import com.google.cloud.secretmanager.v1.Secret;
21+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
23+
import com.google.cloud.secretmanager.v1.SecretName;
24+
import com.google.protobuf.FieldMask;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
28+
public class DeleteRegionalSecretExpiration {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
33+
// Your GCP project ID.
34+
String projectId = "your-project-id";
35+
// Location of the secret.
36+
String locationId = "your-location-id";
37+
// Resource ID of the secret to update.
38+
String secretId = "your-secret-id";
39+
deleteRegionalSecretExpiration(projectId, locationId, secretId);
40+
}
41+
42+
// Delete the expiration time from an existing regional secret.
43+
public static Secret deleteRegionalSecretExpiration(
44+
String projectId, String locationId, String secretId) throws IOException {
45+
46+
// Endpoint to call the regional secret manager sever
47+
String apiEndpoint = String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
48+
SecretManagerServiceSettings secretManagerServiceSettings =
49+
SecretManagerServiceSettings.newBuilder().setEndpoint(apiEndpoint).build();
50+
51+
// Initialize the client that will be used to send requests. This client only needs to be
52+
// created once, and can be reused for multiple requests.
53+
try (SecretManagerServiceClient client =
54+
SecretManagerServiceClient.create(secretManagerServiceSettings)) {
55+
// Build the secret name.
56+
SecretName secretName =
57+
SecretName.ofProjectLocationSecretName(projectId, locationId, secretId);
58+
59+
// Build the updated secret without expiration time.
60+
Secret secret =
61+
Secret.newBuilder()
62+
.setName(secretName.toString())
63+
.build();
64+
65+
// Build the field mask to clear the expiration time.
66+
FieldMask fieldMask = FieldMaskUtil.fromString("expire_time");
67+
68+
// Update the secret to remove expiration.
69+
Secret updatedSecret = client.updateSecret(secret, fieldMask);
70+
System.out.printf("Deleted expiration from secret %s\n",
71+
updatedSecret.getName());
72+
73+
return updatedSecret;
74+
}
75+
}
76+
}
77+
// [END secretmanager_delete_regional_secret_expiration]

0 commit comments

Comments
 (0)