Skip to content

Commit fc490ac

Browse files
[issue-9643 fix] added ttl to secrets tests and samples (GoogleCloudPlatform#11452)
* issue-9643 fix;added ttl to secrets tests and samples * PR comments fix * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix docstring for create secrets methods --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 08ef000 commit fc490ac

3 files changed

Lines changed: 77 additions & 12 deletions

File tree

secretmanager/snippets/create_secret.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,37 @@
1717
"""
1818

1919
import argparse
20+
from typing import Optional
2021

2122
from google.cloud import secretmanager
2223

2324

2425
# [START secretmanager_create_secret]
25-
def create_secret(project_id: str, secret_id: str) -> secretmanager.CreateSecretRequest:
26+
def create_secret(
27+
project_id: str, secret_id: str, ttl: Optional[str] = None
28+
) -> secretmanager.Secret:
2629
"""
2730
Create a new secret with the given name. A secret is a logical wrapper
2831
around a collection of secret versions. Secret versions hold the actual
2932
secret material.
33+
34+
Args:
35+
project_id (str): The project ID where the secret is to be created.
36+
secret_id (str): The ID to assign to the new secret. This ID must be unique within the project.
37+
ttl (Optional[str]): An optional string that specifies the secret's time-to-live in seconds with
38+
format (e.g., "900s" for 15 minutes). If specified, the secret
39+
versions will be automatically deleted upon reaching the end of the TTL period.
40+
41+
Returns:
42+
secretmanager.Secret: An object representing the newly created secret, containing details like the
43+
secret's name, replication settings, and optionally its TTL.
44+
45+
Example:
46+
# Create a secret with automatic replication and no TTL
47+
new_secret = create_secret("my-project", "my-new-secret")
48+
49+
# Create a secret with a TTL of 30 days
50+
new_secret_with_ttl = create_secret("my-project", "my-timed-secret", "7776000s")
3051
"""
3152

3253
# Import the Secret Manager client library.
@@ -43,7 +64,7 @@ def create_secret(project_id: str, secret_id: str) -> secretmanager.CreateSecret
4364
request={
4465
"parent": parent,
4566
"secret_id": secret_id,
46-
"secret": {"replication": {"automatic": {}}},
67+
"secret": {"replication": {"automatic": {}}, "ttl": ttl},
4768
}
4869
)
4970

@@ -60,6 +81,7 @@ def create_secret(project_id: str, secret_id: str) -> secretmanager.CreateSecret
6081
)
6182
parser.add_argument("project_id", help="id of the GCP project")
6283
parser.add_argument("secret_id", help="id of the secret to create")
84+
parser.add_argument("ttl", help="time to live for secrets, f.e. '600s' ")
6385
args = parser.parse_args()
6486

65-
create_secret(args.project_id, args.secret_id)
87+
create_secret(args.project_id, args.secret_id, args.ttl)

secretmanager/snippets/create_secret_with_user_managed_replication.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,40 @@
2424

2525

2626
def create_ummr_secret(
27-
project_id: str, secret_id: str, locations: typing.List[str]
28-
) -> secretmanager.CreateSecretRequest:
27+
project_id: str,
28+
secret_id: str,
29+
locations: typing.List[str],
30+
ttl: typing.Optional[str] = None,
31+
) -> secretmanager.Secret:
2932
"""
3033
Create a new secret with the given name. A secret is a logical wrapper
3134
around a collection of secret versions. Secret versions hold the actual
3235
secret material.
36+
37+
Args:
38+
project_id (str): The project ID where the secret is to be created.
39+
secret_id (str): The unique identifier for the new secret within the project.
40+
locations (List[str]): A list of Google Cloud locations where the secret should be replicated.
41+
ttl (Optional[str]): An optional string that specifies the secret's time-to-live in seconds with
42+
format (e.g., "900s" for 15 minutes). If specified, the secret versions will be
43+
automatically deleted upon reaching the end of the TTL period.
44+
45+
Returns:
46+
secretmanager.Secret: An object representing the newly created secret. This object includes information like the
47+
secret's name and its replication configuration. If TTL is provided, it also configures how long
48+
secret versions remain before being automatically deleted.
49+
50+
Example:
51+
# Create a secret with user-managed replication across two locations without TTL
52+
new_secret = create_ummr_secret("my-project", "my-new-secret", ["us-east1", "europe-west1"])
53+
54+
# Create a secret with a TTL of 30 days and user-managed replication across three locations
55+
new_secret_with_ttl = create_ummr_secret("my-project", "my-timed-secret", ["us-east1", "us-west1"], "7776000s")
56+
57+
Note:
58+
This function requires that the `secretmanager` API is enabled on the cloud project and that the caller has the
59+
necessary permissions to create secrets. Ensure that `secretmanager.SecretManagerServiceClient` and the `secretmanager`
60+
library are correctly configured and authenticated. The specified locations must be valid Google Cloud locations.
3361
"""
3462

3563
# Import the Secret Manager client library.
@@ -49,7 +77,8 @@ def create_ummr_secret(
4977
"secret": {
5078
"replication": {
5179
"user_managed": {"replicas": [{"location": x} for x in locations]}
52-
}
80+
},
81+
"ttl": ttl,
5382
},
5483
}
5584
)

secretmanager/snippets/snippets_test.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def iam_user() -> str:
6363
return "serviceAccount:" + os.environ["GCLOUD_SECRETS_SERVICE_ACCOUNT"]
6464

6565

66+
@pytest.fixture()
67+
def ttl() -> Optional[str]:
68+
return "300s"
69+
70+
6671
@retry.Retry()
6772
def retry_client_create_secret(
6873
client: secretmanager.SecretManagerServiceClient,
@@ -118,7 +123,10 @@ def secret_id(
118123

119124
@pytest.fixture()
120125
def secret(
121-
client: secretmanager.SecretManagerServiceClient, project_id: str, secret_id: str
126+
client: secretmanager.SecretManagerServiceClient,
127+
project_id: str,
128+
secret_id: str,
129+
ttl: Optional[str],
122130
) -> Iterator[Tuple[str, str, str]]:
123131
print(f"creating secret {secret_id}")
124132

@@ -129,7 +137,7 @@ def secret(
129137
request={
130138
"parent": parent,
131139
"secret_id": secret_id,
132-
"secret": {"replication": {"automatic": {}}},
140+
"secret": {"replication": {"automatic": {}}, "ttl": ttl},
133141
},
134142
)
135143

@@ -188,17 +196,23 @@ def test_add_secret_version(secret: Tuple[str, str, str]) -> None:
188196

189197

190198
def test_create_secret(
191-
client: secretmanager.SecretManagerServiceClient, project_id: str, secret_id: str
199+
client: secretmanager.SecretManagerServiceClient,
200+
project_id: str,
201+
secret_id: str,
202+
ttl: Optional[str],
192203
) -> None:
193-
secret = create_secret(project_id, secret_id)
204+
secret = create_secret(project_id, secret_id, ttl)
194205
assert secret_id in secret.name
195206

196207

197208
def test_create_secret_with_user_managed_replication(
198-
client: secretmanager.SecretManagerServiceClient, project_id: str, secret_id: str
209+
client: secretmanager.SecretManagerServiceClient,
210+
project_id: str,
211+
secret_id: str,
212+
ttl: Optional[str],
199213
) -> None:
200214
locations = ["us-east1", "us-east4", "us-west1"]
201-
secret = create_ummr_secret(project_id, secret_id, locations)
215+
secret = create_ummr_secret(project_id, secret_id, locations, ttl)
202216
assert secret_id in secret.name
203217

204218

0 commit comments

Comments
 (0)