Skip to content

Commit

Permalink
docs(samples): add checksum snippets (#255)
Browse files Browse the repository at this point in the history
* 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 <partheniou@google.com>
  • Loading branch information
4 people authored Mar 13, 2022
1 parent 66c8632 commit 60a9ea4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions secretmanager/snippets/access_secret_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import argparse

import google_crc32c


# [START secretmanager_access_secret_version]
def access_secret_version(project_id, secret_id, version_id):
Expand All @@ -38,6 +40,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
12 changes: 11 additions & 1 deletion secretmanager/snippets/add_secret_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import argparse

import google_crc32c


# [START secretmanager_add_secret_version]
def add_secret_version(project_id, secret_id, payload):
Expand All @@ -39,9 +41,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 secretmanager/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

0 comments on commit 60a9ea4

Please sign in to comment.