28


I have a ssh private key in a key.json file, and I want to use this credential to access a storage bucket using gsutil.

I can't seem to find anything about how to include json keys as a authentication method, only "private" and "secret" fields.

The file structure is:

{
  "private_key_id":
  "private_key": "-----BEGIN PRIVATE KEY-- ...
   "client_email":
   "client_id":
  "type": "service_account"
}

How do I use gsutil with that file?

d-_-b
  • 119
  • 7
GuySoft
  • 425
  • 1
  • 4
  • 11

2 Answers2

34

As of today, gsutil config -e is still in the doc when you do gsutil help config, but does not work. gsutil help creds tells to first do gcloud auth activate-service-account

So i did:

gcloud auth activate-service-account --key-file=mycredentialsialreadyhad.json

It filled ~/.config/gcloud/ and gsutil now works.

From the docs:

OAuth2 Service Account: This is the preferred type of credential to use when authenticating on behalf of a service or application (as opposed to a user). (...). This is the type of credential that will be configured when you run "gsutil config -e". To configure service account credentials when installed via the Cloud SDK, run "gcloud auth activate-service-account".

Gray
  • 569
  • 1
  • 5
  • 17
Alex F
  • 819
  • 1
  • 10
  • 17
  • 3
    This is not completely correct, the `gsutil config -e` command still works but only when you are running `gsutil` standalone and you don't have the google-cloud-sdk` installed. – Jonathan Liuti Dec 26 '18 at 18:51
3

The short version is to run the following command and follow instructions:

gsutil config -e

The gsutil tool has built-in help which can be consulted for all kinds of options and modes of operation. When running gsutil help creds, one of the help options recommended when running gsutil alone, we can read the section on "OAuth2 Service Account" to see the instructions for using a service account's json key file:

OAuth2 Service Account:

This is the preferred type of credential to use when authenticating on
behalf of a service or application (as opposed to a user). For example, if
you will run gsutil out of a nightly cron job to upload/download data,
using a service account allows the cron job not to depend on credentials of
an individual employee at your company. This is the type of credential that
will be configured when you run "gsutil config -e".

It is important to note that a service account is considered an Editor by
default for the purposes of API access, rather than an Owner. In particular,
the fact that Editors have OWNER access in the default object and
bucket ACLs, but the canned ACL options remove OWNER access from
Editors, can lead to unexpected results. The solution to this problem is to
ensure the service account is an Owner in the Permissions tab for your
project. To find the email address of your service account, visit the
`Google Developers Console <https://cloud.google.com/console#/project>`_,
click on the project you're using, click "APIs & auth", and click
"Credentials".

To create a service account, visit the Google Developers Console and then:

   - Click "APIs & auth" in the left sidebar.

   - Click "Credentials".

   - Click "Create New Client ID".

   - Select "Service Account" as your application type.

   - Save the JSON private key or the .p12 private key and password
     provided.

For further information about account roles, see:
  https://developers.google.com/console/help/#DifferentRoles

For more details about OAuth2 service accounts, see:
  https://developers.google.com/accounts/docs/OAuth2ServiceAccount
GuySoft
  • 425
  • 1
  • 4
  • 11
Nick
  • 194
  • 7