Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i handle secrets in Google Cloud Functions?

What is the common practice here? There seems to be no tools provided by gcloud. I'm deploying functions from local machine for now, so I can hardcode secrets, but this seems inappropriate. Also, what about CI/CD? I would need to pass secrets as environment variables in this case. Is this even possible atm?

like image 838
stkvtflw Avatar asked Dec 06 '17 05:12

stkvtflw


People also ask

How do I manage cloud secrets?

Go to the Secret Manager page in the Google Cloud console. Select the secret and in the right side permissions tab, click Add Principal. In the New principals textbox, enter the service account email for your Cloud Run service. Grant it the role Secret Manager Secret Accessor.

How do I access the secret in GCP?

admin ) on the project, folder, or organization. Go to the Secret Manager page in the Google Cloud console. On the Secret Manager page, click Create Secret. On the Create secret page, under Name, enter a name for the secret (e.g. my-secret ).

What is secret Manager in Google Cloud?

Secret Manager is a secure and convenient storage system for API keys, passwords, certificates, and other sensitive data. Secret Manager provides a central place and single source of truth to manage, access, and audit secrets across Google Cloud.


3 Answers

You can use the Secret Manager for this. Follow the instructions on the link to add a secret.

The only GOTCHA I found is that by default the service account doesn't have read-access to the secrets, you've got to manually grant permissions, like so:

Add secret permissions

like image 73
Matthew Avatar answered Oct 17 '22 05:10

Matthew


Since making my comment, I've found a relatively simple way to do this - provide a config .json file. Here's an example I hacked together based on their Slack function example:

config.json file in the same directory as index.js:

{
  "foo": "bar"
}

index.js

const config = require('./config.json');

exports.envTest = (req, res) => {
  res.status(200).send(config.foo);
};

When you deploy the function and go to the URL, you should get the response bar.

Pros and cons:

Pros:

  1. Easy to set up and configure right in your IDE
  2. Config file can be put into .gitignore to ensure your secrets don't end up the repo
  3. File itself can be stored in a secure location and only given to individual responsible for deploying the functions

Cons:

  1. Clunky in comparison to proper secret management
  2. Requires attention to ensure the file doesn't fall into the wrong hands
  3. File can be read in plaintext in the Google Cloud console by looking at the function source

On the whole, it's a far cry from a real secrets management system, but it's workable enough to hold me over until this feature eventually makes it into the Cloud Functions core.

like image 4
Artem Zakharov Avatar answered Oct 17 '22 07:10

Artem Zakharov


You should use Cloud Key Management Service(KMS).
Don't push pure secrets to Cloud Functions with files or environment variables.

One solution is followings:

  1. Create key on Cloud KMS
  2. Encrypt secret file with that key
  3. Upload encrypted secret file to Google Cloud Storage(GCS) (Accessible by specified user)
  4. In Cloud Function Execution, get uploaded secret file from GCS, decrypt, and use it

[Ref] Secret management using the Google Cloud Platform

like image 3
R.Shindo Avatar answered Oct 17 '22 05:10

R.Shindo