0

I'm trying to use Docker registry V2 API with Google Container Registry, specifically this endpoint:

curl -I \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://gcr.io/v2/$PROJECT/hello/blobs/$digest

But I'd like to do it with code and not via CLI and not using gcloud. I have a service account and a json key file. Is it possible?

This is a related question Get service account auth token without gcloud? But the link attached in the chosen answer doesn't help with this need.

wolf
  • 127
  • 1
  • 10

2 Answers2

0

What you are looking is documented here [1]. It explains how to authorize requests with a service account using Node.Js.

Here is also how to provide credentials to the application (also using NodeJS)[2].


[1] https://cloud.google.com/compute/docs/tutorials/nodejs-guide#authorization

[2] https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application

0

The following code works (inspired by this question), using google-auto-auth. Note that you might need to define GOOGLE_APPLICATION_CREDENTIALS in your environment:

const googleAuth = require('google-auto-auth');

const authConfig = {scopes: ['https://www.googleapis.com/auth/devstorage.read_write']};

// Create a client
const auth = googleAuth(authConfig);

auth.getToken(function (err, token) {
  if (err)
      console.log(err);
  else { 
     //  Send a request to the relevant Docker Registry V2 API endpoint
     //  with header `Authorization: Bearer ${token}`
  }

});
Yoni Rabinovitch
  • 5,171
  • 1
  • 23
  • 34