I use Google Source Repository to store my Google Cloud Functions. (Git repo hosted by Google, basically)
One of my function needs to access a private Google Sheet f
I think you don't really need to store any keyfile within your code.
Your function runs with a designated service account (usually the "App Engine default service account", but you can change in Advanced Settings). If you have specific needs, you should create a service account specific for your function and grant to it all the permission it needs.
In your function, the authentication will occur automatically using Application Default Credentials, so you don't have to care to anything (forget about environment variables, keyfiles, or anything). Just assure you're using the Google Cloud Client Libraries for your language that they'll handle everything for you implicitly.
Particularly, I avoid creative solutions as the one @slideshowp2 proposed. But I agree they have its uses (eg. suppose I need to store credentials for an external system, out of GCP scope. In this scenario, his solution may be a way to go), but to consume only Google services, let's keep it simple.
You can upload the service account file along with your functions and use it from within your code. It will remain secure there. Most developers will use a .gitignore or equivalent mechanism to keep that file from being added to source control. There is an example of loading service account credentials from Firebase samples. (If you're not using the Firebase SDK, you'll have to be mindful to convert the function definition to the Cloud style.
You could also use an env var, but you'll have to take special care in quoting and escaping the values to make sure they get to your function correctly. It's kind of a hassle, but doable.
My solution when using cloud function with a service account is:
Fetch service account credential json file from Cloud Storage and decrypt it using a Cloud KMS service account which has encrypt/decrypt permission.
Parse service account credential json file at runtime and get private_key
, client_email
and projectId
.
Pass these three secret variables to the client library
We store config variables as environment variables for cloud function, they are plain text, but it's ok. Because they are not secret things.
We must not store secret things like plain text, e.g cloud function environment variables.
As of January 2020, Google has released a Secret Manager, which is described as:
Secret Manager is a new Google Cloud service that provides a secure and convenient method for storing 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.
For Cloud Functions, there is a tutorial here on how to create a secret and then retrieve it from a cloud function.
This is how I solved this problem. First create a logic in a file keys.js to determine whether you are in development or production (and create corresponding ./dev.js and ./prod.js files, where you should include ./dev.js in .ignore file to make sure it's not uploaded to your github remote):
if (process.env.NODE_ENV === "production") {
module.exports = require("./prod");
} else {
module.exports = require("./dev");
}
Second, you require your keys.js file where the logic above resides and create a credential object based on the data received from keys.js:
const credentials = {
type: keys.googleType,
project_id: keys.googleProjectId,
private_key_id: keys.googlePrivateKeyId,
private_key: keys.googlePrivateKey,
client_email: keys.googleClientEmail,
client_id: keys.googleClientId,
auth_uri: keys.googleAuthUri,
token_uri: keys.googleTokenUri,
auth_provider_x509_cert_url: keys.googleAuthProviderX509CertUrl,
client_x509_cert_url: keys.googleClientX509CertUrl
};
Now, for every google cloud service you can use the following example patterns:
const storage = new Storage({
project_id: credentials.project_id,
credentials
});
const client = new textToSpeech.TextToSpeechClient({
project_id: credentials.project_id,
credentials
});
...
etc.
Here you could find how to provide credentials to your application, using the environment variable GOOGLE_APPLICATION_CREDENTIALS
.