How to handle keys and credentials when deploying to Google Cloud Functions?

2.9k Views Asked by At

I have several cloud functions (in Python) that require a modulair package auth in which there is a subfolder with credentials (containing mostly json files of Google Service Accounts files or Firebase configurations).

From a security perspective, I have obviously not included these files on the .git by adding the folder in the .gitignore file (auth/credentials).

However, I am now stuck with what to do when deploying the Google Cloud Function (.gcloudignore). If I deploy it with the credentials then I imagine that these keys are exposed on the server? How could I overcome this?

I have heard some speaking of environmental variables, but I am not sure if this is more secure than just deploying it?

What is the Google Way of doing it?

1

There are 1 best solutions below

1
On BEST ANSWER

You have two primary solutions available to you. The first is that the Cloud Function can run with the identity of a custom Service Account. This service account can then be associated with all the roles necessary for your logic to achieve its task. The value of this is that no credentials need be explicitly known to your logic. The environment in which your calls are being made "implicitly" has all that it needs.

See: Per-function identity

The second mechanism which is more in line with what you are currently doing uses the concept of the Compute Metadata Server. This metadata can be configured with the tokens necessary to make on-ward calls. The metadata is configured separately from your Cloud Function logic which merely retrieves the data as needed.

See: Fetching identity and access tokens.