【问题标题】:How to authorize to GCR using NodeJS如何使用 NodeJS 授权 GCR
【发布时间】:2019-07-24 12:43:31
【问题描述】:

我正在尝试将 Docker Registry V2 API 与 Google Container Registry 一起使用,特别是这个端点:

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

但我想用代码而不是通过 CLI 而不是使用 gcloud 来完成。 我有一个服务帐户和一个 json 密钥文件。有可能吗?

这是一个相关问题Get service account auth token without gcloud? 但是所选答案中附加的链接对这种需求没有帮助。

【问题讨论】:

    标签: node.js authorization google-container-registry


    【解决方案1】:

    您正在查看的内容记录在此处 [1]。它解释了如何使用 Node.Js 通过服务帐户授权请求。

    这里还介绍了如何向应用程序提供凭据(也使用 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

    【讨论】:

      【解决方案2】:

      以下代码工作(灵感来自this question),使用google-auto-auth。请注意,您可能需要在您的环境中定义 GOOGLE_APPLICATION_CREDENTIALS

      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}`
        }
      
      });
      

      【讨论】:

        猜你喜欢
        • 2019-04-29
        • 2018-12-28
        • 2016-04-07
        • 2019-04-17
        • 2021-08-17
        • 2021-03-20
        • 2014-12-01
        • 2021-04-15
        • 2019-02-26
        相关资源
        最近更新 更多