Search code examples
google-cloud-platformgoogle-cloud-storagegoogle-cloud-sdkgoogle-iam

The environment variable "GOOGLE_APPLICATION_CREDENTIALS" in Google machines


Background

I have a virtual machine running a code using Google SDK for diffrent products (like Google PubSub). According to Google documentation, my machine should have an environment variable called GOOGLE_APPLICATION_CREDENTIALS and its values should be pointing to a clear text file that holding the service account of the application.

I have done it and it's working for me.

The Problem

It sounds like an unsafe practice to store such a key, in plain text, inside a virtual machine. If the machine has been hacked, this key will be one of the first targets of the attacker.

I was expected to find a solution to "hide" this key file or just encrypt it with a key that my application will be able to read.

I found some code examples (C#), that allow the programmer to pass the credentials manually to the SDK functions. But, it's not a standard way to do it and it's being changed from one product to another (seems impossible in some products).

What is the best practice to do it?


Solution

  • Have a good read at the following:

    https://cloud.google.com/docs/authentication/production

    This describes a concept called "Application Default Credentials". The concept here is that a Compute Engine (a virtual machine) has a default service account (that you can configure) associated with it. Applications running on the Compute Engine can thus make requests from that Compute Engine to other GCP services and the requests to those services will implicitly appear to come from the service account configured against the Compute Engine.

    The key phrase in the article is:

    If the environment variable GOOGLE_APPLICATION_CREDENTIALS isn't set, ADC uses the default service account that Compute Engine, Google Kubernetes Engine, App Engine, Cloud Run, and Cloud Functions provide.