in reference-architectures/automated-password-rotation/terraform/code/main.py [0:0]
def update_secret(project_id, secret_id, new_secret_value):
"""Updates the value of a secret in Google Cloud Secret Manager.
Args:
project_id (str): Your Google Cloud Project ID.
secret_id (str): The ID of the secret to update.
new_secret_value (bytes): The new secret value as a bytes object.
"""
client = secretmanager.SecretManagerServiceClient()
# Build the secret path (required format for Secret Manager)
name = f"projects/{project_id}/secrets/{secret_id}"
# Prepare the payload with the updated secret data
payload = {"data": new_secret_value.encode("UTF-8")}
# Perform the update
try:
updated_secret = client.add_secret_version(
request={
"parent": name,
"payload": payload,
}
)
print(f"Updated secret {secret_id} to version: {updated_secret.name}")
return True
except Exception as e: # pylint: disable=broad-exception-caught
print(e)
return False