Search code examples
google-cloud-platformgoogle-cloud-functionsmountgoogle-secret-manager

How can I reference mounted secrets from Secret Manager in a python Cloud Function?


I'm trying to reference a series of APIs and would like peace of mind for key security, so I am storing keys in Secret Manager. However, the documentation doesn't specify the best method of connecting to a mounted path within the Cloud Function.

enter image description here

Suppose my secret was named key6 and has a mount path of /api/secret/key6 - How would I call this in python?

I attempted this method: https://cloud.google.com/secret-manager/docs/creating-and-accessing-secrets#secretmanager-create-secret-python However, given that this didn't use the mounted path, I wanted to see if there was a better implementation.


Solution

  • The process to read the secret is via standard file operations in Python. So if the path is /api/secret/key6 , then you could do something like:

    secret_location = '/api/secret/key6'
    
    with open(secret_location) as f:
        YOUR_SECRET = f.readlines()[0]
    

    Just ensure that you have given the service account running your Cloud Functions, the necessary permissions to access the Secrets.