Google Cloud SDK: Using Service Account Private Key for Authentication and Authorization

Using Service Account Private Key for gcloud Commands

Question

You received a JSON file that contained a private key of a Service Account in order to get access to several resources in a Google Cloud project.

You downloaded and installed the Cloud SDK and want to use this private key for authentication and authorization when performing gcloud commands.

What should you do?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

B.

https://cloud.google.com/sdk/docs/authorizing

In order to use the private key of a service account for authentication and authorization when performing gcloud commands, you should follow these steps:

  1. Download and install the Cloud SDK: The Cloud SDK is a set of tools for developing and managing resources in Google Cloud Platform. You can download and install it by following the instructions on the Google Cloud SDK website.

  2. Download the private key JSON file: The JSON file containing the private key for the service account should be provided to you. Make sure to download this file to a secure location on your local machine.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS: To authenticate using the private key, you need to tell gcloud where to find the private key file. You can do this by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the private key file. The exact command to set this environment variable depends on your operating system:

On Linux and macOS:

javascript
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/keyfile.json"

On Windows:

swift
set GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\keyfile.json"
  1. Activate the service account: To activate the service account and use it for authentication and authorization, use the command gcloud auth activate-service-account followed by the email address of the service account and the path to the private key file. For example:
css
gcloud auth activate-service-account --key-file=/path/to/keyfile.json my-service-account@example-project.iam.gserviceaccount.com

This command sets the default authentication information for gcloud to use the specified service account.

So, the correct answer to the exam question is B. Use the command gcloud auth activate-service-account and point it to the private key.