Reputation: 93
I have code set up on a Google Cloud Function that accesses the secret, turns it into a json dict and then as Credentials for an OAuth. This is then refreshed to ensure validation.
All of this works well, but when I want to update the Secret I run into issues.
Here is the code, based off of Google Documentation:
### update the secret
parent = {"name": f"projects/{project_id}/secrets/{SC_secret_name}"}
# Convert the string payload into a bytes. This step can be omitted if you
# pass in bytes instead of a str for the payload argument.
updatepayload = creds.to_json()
updatepayload = updatepayload.encode("UTF-8")
# Calculate payload checksum. Passing a checksum in add-version request
# is optional.
crc32c = google_crc32c.Checksum()
crc32c.update(updatepayload)
# Add the secret version.
response = SMclient.add_secret_version(
request={
"parent": parent,
"payload": {"data": updatepayload, "data_crc32c": int(crc32c.hexdigest(), 16)},
}
)
The traceback error received is:
Error: SearchConsole_Data_Updater4islkx7kn195 Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/init.py", line 222, in view_func function(data, context) File "/workspace/main.py", line 72, in main response = SMclient.add_secret_version( File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/secretmanager_v1/services/secret_manager_service/client.py", line 640, in add_secret_version request = service.AddSecretVersionRequest(request) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/proto/message.py", line 570, in init pb_value = marshal.to_proto(pb_type, value) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/proto/marshal/marshal.py", line 209, in to_proto proto_type.DESCRIPTOR.has_options AttributeError: 'ProtoType' object has no attribute 'DESCRIPTOR'
I have tried updating the protobuf package via the reqiurments.txt for the GCF, but then it never deploys just gets hung indefinitely in deployment phase.
Not sure how to proceed, help is much appreciated, thanks!
Upvotes: 1
Views: 1192
Reputation: 93
The issue was due to a dict name recognition. Essentially my dict:
parent = {"name": f"projects/{project_id}/secrets/{SC_secret_name}"}
Is not what it was expecting. I changed to using the client.secret_path() function in the Docs.
Then I commented out the checksum parts since that threw and error during Cloud Function deployment.
Final Working Code:
### update the secret
parent = SMclient.secret_path(project_id, SC_secret_name)
# Convert the string payload into a bytes. This step can be omitted if you
# pass in bytes instead of a str for the payload argument.
updatepayload = creds.to_json()
updatepayload = updatepayload.encode("UTF-8")
# Add the secret version.
response = SMclient.add_secret_version(
request={
"parent": parent,
"payload": {"data": updatepayload},
}
)
Upvotes: 0