Secret Manager Usage Within Functions
Offering some possible updates for the sample code / documentation...
(A) Credentials May Need To Be Provided To Secret Manager:
It appears that for Google Functions (at least for first generation ones), the credentials must be passed to SecretManagerServiceClient() when you create the client. For instance:
secret_client = secretmanager.SecretManagerServiceClient(credentials={"client_email": function_service_acct})
If you don't pass the credentials here then when you call access_secret_version() the logs show the following 5 DEBUG messages as it goes through the flow of trying to get Default Credentials even if there is a service account attached to the Function:
(1) DEBUG - _get_explicit_environ_credentials @ line 257 - Checking None for explicit credentials as part of auth process...
(2) DEBUG - _get_gcloud_sdk_credentials @ line 230 - Checking Cloud SDK credentials as part of auth process...
(3) DEBUG - _get_gcloud_sdk_credentials @ line 236 - Cloud SDK credentials not found on disk; not using them
(4) DEBUG - call @ line 104 - Making request: GET http://169.254.169.254/
(5) DEBUG - call @ line 104 - Making request: GET http://metadata.google.internal/computeMetadata/v1/project/project-id
This above flow trying to get the Default Credentials is described here: https://google-auth.readthedocs.io/en/master/reference/google.auth.html
If you use the following command (that also generates those same 5 DEBUG statements), to examine the results of this flow to obtain Default Credentials, although the correct project is returned, the credentials that are returned are invalid, (credentials.valid = False):
credentials, project = google.auth.default()
For that flow:
- For Functions (1), (2) and (3) would not even apply.
- For first generation Functions it mentions that (4) should apply (App Engine standard environment).
- For second generation Functions it mentions that (5) should apply (Metadata).
But, since Functions are a spun up environment it appears that (at least for first generation Functions), (4) does not work since there are no valid credentials returned. Maybe if you have App Engine setup and configured for something else but in our case we have nothing setup in App Engine. This may be similar for second generation Functions as well.
Once you pass in the credentials for the call to SecretManagerServiceClient() then those 5 DEBUG statements do not appear.
(B) Timeout May Need To Be Set For access_secret_version:
Again, this may only apply for Functions (at least first generation ones). We constantly got a StatusCode.DEADLINE_EXCEEDED error for the call to access_secret_version() until we added the timeout parameter:
response = secret_client.access_secret_version(request={"name": latest_version}, timeout=540)
(C) Now we are investigating the following error for the call to access_secret_version():
failed to connect to all addresses; last error: UNKNOWN: ipv4:199.36.153.8:443: Failed to connect to remote host: FD Shutdown
UPDATE: The latest error mentioned above (FD Shutdown) was caused by no access to Private API -- needed a firewall rule change for serverless to enable Private API access.
Got past that now but getting the following error:
ERROR - __call__ @ line 98 - AuthMetadataPluginCallback "<google.auth.transport.grpc.AuthMetadataPlugin object at 0x3edb73724a90>" raised exception!
With TRACEBACK:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/grpc/_plugin_wrapping.py", line 95, in __call__ self._metadata_plugin(
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/auth/transport/grpc.py", line 101, in __call__ callback(self._get_authorization_headers(context), None)
File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/auth/transport/grpc.py", line 87, in _get_authorization_headers
self._credentials.before_request(
AttributeError: 'dict' object has no attribute 'before_request'
My call to access_secret_version is:
response = secret_client.access_secret_version(request={"name": latest_version})
Does this statement work sychronbously and wait for the secret value response or has something changed that this is now asychronous and you have to use a promise / callback?
Thanks for filing this! Maciej, can you help take a look at this or find someone who can from your team?
Hi, just want to check in if the issue still persistent! I have ask from a pressional's help and did a few checks with or without passing the credentials and it doesn't cause any issues with any GCP related API calls. If you could provide more details about the issue such as: Is it actually breaking your code/causing errors or it's just those 5 DEBUG messages?
Follow up are the explanation from an expertise, I quote" Google Cloud libraries rely on the same authentication mechanism and it's not unique to Secret Manager. If they use some other GCP library before that in their code, they should expect exact same messages, as the authentication machinery kicks in and refreshes access tokens etc."
Thank you!
No, we resolved this issue.
I can't remember exactly how but I think it was using the correct imports / versions in the requirements files which none of the documentation actually specifies so it is a lot of trial and error guessing what to import and what versions.
NOTE: We could not get the Python Connection to work and had to use a direct connection.
Reason: Have to use the 3 certs to connect to the Postgres database and the Python Connector does not allow you to pass those in or reference them.