0
votes

I am new to Google Cloud. I am trying to access google buckets to upload files. I use Google Storage object for accessing the bucket programmatically in Python. I am able to authenticate the storage object with 'key.json'. But I am unsure when the application will run in cloud how will it access 'key.json' file securely ? Also is there a way to authenticate storage object using access token in python ?

Thanks in advance!

2

2 Answers

1
votes

But I am unsure when the application will run in cloud how will it access 'key.json' file securely ?

Review the details that I wrote below. Once you have selected your environment you might not need to use a service account JSON file at all because the metadata server is available to provide your code with credentials. This is the best case and secure. On my personal website, I have written many articles that show how to create, manage and store Google credentials and secrets.

Also is there a way to authenticate storage object using access token in python ?

All access is via an OAuth Access Token. The following link shows details using the metadata server which I cover in more detail below.

Authenticating applications directly with access tokens

There are three items to consider:

  1. My code is not running in Google Cloud
  2. My code is running in Google Cloud on a "compute" type of service with access to the metadata server
  3. My code is running in Google Cloud without access to the metadata server.

1) My code is not running in Google Cloud

This means your code is running on your desktop or even in another cloud such as AWS. You are responsible for providing the method of authorization. There are two primary methods: 1) Service Account JSON key file; 2) Google OAuth User Authorization.

Service Account JSON key file This is what you are using now with key.json. The credentials are stored in the file and are used to generate an OAuth Access Token. You must protect that file as it contains your Google Cloud secrets. You can specify the key.json directly in your code or via the environment variable GOOGLE_APPLICATION_CREDENTIALS

Google OAuth User Authorization This method requires the user to log in to Google Accounts requesting an OAuth scope for Cloud Storage. The end result is an OAuth Access Token (just like a Service Account) that authorizes access to Cloud Storage.

Getting Started with Authentication

2) My code is running in Google Cloud on a "compute" type of service with access to the metadata server

Notice the word "metadata" server. For Google Cloud compute services, Google provides a metadata server that provides applications running on that compute service (Compute Engine, Cloud Functions, Cloud Run, etc) with credentials. If you use Google SDK Client libraries for your code, the libraries will automatically select the credentials for you. The metadata server can be disabled (denied access through role/scope removal), so you need to evaluate what you are running on.

Storing and retrieving instance metadata

3) My code is running in Google Cloud without access to the metadata server.

This is a similar scenario to #1. However, now you are limited to only using a service account unless this is a web server type of service that can present the Google Accounts authorization service to the user.

0
votes

The best practice is to use OAuth 2.0 to access the buckets. If you are using Python program then try to use a Service Account to access the Storage API.

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