Skip to content

Commit 60a9ea4

Browse files
gwhitehawkgcf-owl-bot[bot]parthea
authored
docs(samples): add checksum snippets (#255)
* docs(samples): add checksum snippets Source-Link: googleapis/synthtool@571ee2c Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:660abdf857d3ab9aabcd967c163c70e657fcc5653595c709263af5f3fa23ef67 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * lint * lint * lint * remove extra blank line * lint * Fix `google-crc32c` pin in samples/snippets/requirements.txt Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 66c8632 commit 60a9ea4

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

secretmanager/snippets/access_secret_version.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import argparse
2020

21+
import google_crc32c
22+
2123

2224
# [START secretmanager_access_secret_version]
2325
def access_secret_version(project_id, secret_id, version_id):
@@ -38,6 +40,13 @@ def access_secret_version(project_id, secret_id, version_id):
3840
# Access the secret version.
3941
response = client.access_secret_version(request={"name": name})
4042

43+
# Verify payload checksum.
44+
crc32c = google_crc32c.Checksum()
45+
crc32c.update(response.payload.data)
46+
if response.payload.data_crc32c != int(crc32c.hexdigest(), 16):
47+
print("Data corruption detected.")
48+
return response
49+
4150
# Print the secret payload.
4251
#
4352
# WARNING: Do not print the secret in a production environment - this

secretmanager/snippets/add_secret_version.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import argparse
2121

22+
import google_crc32c
23+
2224

2325
# [START secretmanager_add_secret_version]
2426
def add_secret_version(project_id, secret_id, payload):
@@ -39,9 +41,17 @@ def add_secret_version(project_id, secret_id, payload):
3941
# pass in bytes instead of a str for the payload argument.
4042
payload = payload.encode("UTF-8")
4143

44+
# Calculate payload checksum. Passing a checksum in add-version request
45+
# is optional.
46+
crc32c = google_crc32c.Checksum()
47+
crc32c.update(payload)
48+
4249
# Add the secret version.
4350
response = client.add_secret_version(
44-
request={"parent": parent, "payload": {"data": payload}}
51+
request={
52+
"parent": parent,
53+
"payload": {"data": payload, "data_crc32c": int(crc32c.hexdigest(), 16)},
54+
}
4555
)
4656

4757
# Print the new secret version name.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
google-cloud-secret-manager==2.9.1
1+
google-cloud-secret-manager==2.9.1
2+
google-crc32c==1.3.0

0 commit comments

Comments
 (0)