Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

docs(samples): add checksum snippets #255

Merged
merged 9 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion samples/snippets/access_secret_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""

import argparse

import google_crc32c
parthea marked this conversation as resolved.
Show resolved Hide resolved

parthea marked this conversation as resolved.
Show resolved Hide resolved
# [START secretmanager_access_secret_version]
parthea marked this conversation as resolved.
Show resolved Hide resolved
parthea marked this conversation as resolved.
Show resolved Hide resolved
def access_secret_version(project_id, secret_id, version_id):
Expand All @@ -38,6 +38,13 @@ def access_secret_version(project_id, secret_id, version_id):
# Access the secret version.
response = client.access_secret_version(request={"name": name})

# Verify payload checksum.
crc32c = google_crc32c.Checksum()
crc32c.update(response.payload.data)
if response.payload.data_crc32c != int(crc32c.hexdigest(), 16):
print("Data corruption detected.")
return response

# Print the secret payload.
#
# WARNING: Do not print the secret in a production environment - this
Expand Down
11 changes: 10 additions & 1 deletion samples/snippets/add_secret_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""

import argparse
import google_crc32c
parthea marked this conversation as resolved.
Show resolved Hide resolved
parthea marked this conversation as resolved.
Show resolved Hide resolved


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

# Calculate payload checksum. Passing a checksum in add-version request
# is optional.
crc32c = google_crc32c.Checksum()
crc32c.update(payload)

# Add the secret version.
response = client.add_secret_version(
request={"parent": parent, "payload": {"data": payload}}
request={
"parent": parent,
"payload": {"data": payload, "data_crc32c": int(crc32c.hexdigest(), 16)},
}
)

# Print the new secret version name.
Expand Down
3 changes: 2 additions & 1 deletion samples/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
google-cloud-secret-manager==2.9.1
google-cloud-secret-manager==2.9.1
google-crc32c>=1.3.0
parthea marked this conversation as resolved.
Show resolved Hide resolved