使用用户帐户凭据访问私有 Cloud Run/Cloud Functions

2023-11-21

这是我的用例。

  • 我已经以私有模式部署了 Cloud Run 服务。 (与云功能相同的问题)
  • 我正在开发一项使用此 Cloud Run 的新服务。我使用应用程序中的默认凭据进行身份验证。它适用于 Compute Engine 和 Cloud Run,因为默认凭据是从元数据服务器获取的。

但是,在我的本地环境中,我需要使用服务帐户密钥文件来实现此目的。 (例如,当我设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量时)

然而,我无法使用我的用户帐户凭据。

Java、Node 或 Go 中的日志跟踪非常清晰:不可能在用户凭证类型上生成身份令牌。

So,

  • 为什么谷歌身份验证库不允许这样做?
  • 为什么 Google 前端仅接受 Google 签名的身份令牌?
  • 是否存在继续使用用户帐户的解决方法?

还有一段上下文:我们希望避免使用服务帐户密钥文件。今天,这是我们的重大安全漏洞(文件被复制、通过电子邮件发送、甚至公开提交到 Github 上......)。 用户帐户默认凭据适用于所有 Google Cloud API,但不适用于 IAP 和 CLoud Run/Functions。

EDIT

这是一些错误的例子。

JAVA

我这样做

        Credentials credentials = GoogleCredentials.getApplicationDefault().createScoped("https://www.googleapis.com/auth/cloud-platform");

        IdTokenCredentials idTokenCredentials = IdTokenCredentials.newBuilder()
                .setIdTokenProvider((IdTokenProvider) credentials)
                .setTargetAudience(myUri).build();

        HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(idTokenCredentials));

我的用户凭据不符合IdTokenProvider界面


Caused by: java.lang.ClassCastException: class com.google.auth.oauth2.UserCredentials cannot be cast to class com.google.auth.oauth2.IdTokenProvider (com.google.auth.oauth2.UserCredentials and com.google.auth.oauth2.IdTokenProvider are in unnamed module of loader 'app')

NODE

对于节点,此代码可与服务帐户一起使用

    const {GoogleAuth} = require('google-auth-library');
    const auth = new GoogleAuth()
    const client = await auth.getIdTokenClient(url);
    const res = await client.request({url});
    console.log(res.data);

但使用我的用户帐户时,我收到此错误

Error: Cannot fetch ID token in this environment, use GCE or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account credentials JSON file.
    at GoogleAuth.getIdTokenClient(....)

gcloud auth activate-service-account --key-file仅适用于运行 gcloud 命令的“您”,它不会被需要的“应用程序”拾取GOOGLE_APPLICATION_CREDENTIALS.

正如你可以看到的从 java 调用 Google Cloud Run or 如何从 Cloud Run/GCP 外部调用 Cloud Run?,您要么需要有服务帐户的 JSON 密钥文件,要么必须在 GCE/GKE/Cloud Run/App Engine/GCF 实例内运行。

为了使其适用于您的本地环境,我建议使用以下命令登录gcloud auth application-default login命令(此命令的作用就像您设置了GOOGLE_APPLICATION_CREDENTIALS本地)。让我知道这个是否奏效。

如果这不起作用,作为最后的手段,您可以重构代码以在本地工作时从环境变量(如果设置)中获取身份令牌,例如:

$ export ID_TOKEN="$(gcloud auth print-identity-token -q)"
$ ./your-app

Appendix

根据纪尧姆的要求“这怎么办?gcloud auth print-identity-token使用我的个人 Google 凭据”?

就是这样:

  1. gcloud是注册到 Google 的 OAuth 客户端,要获取 JWT 令牌,您需要在 GCE/GCF/GKE/GAE/CR 环境中工作或拥有 OAuth 应用程序。

  2. gcloud发出这样的请求:

    POST https://www.googleapis.com/oauth2/v4/token
    

    与身体:

    grant_type=refresh_token&client_id=REDACTED.apps.googleusercontent.com&client_secret=REDACTED&refresh_token=REDACTED
    

    最有可能的client_id and client_secret值对我们来说是相同的,因为它们是 gcloud 的。然而,它交换了你的refresh_token用一个新的令牌。

  3. The /token响应将包含一个access_token and id_token. e.g.:

    {
    "access_token": ".....",
    "id_token": "eyJhbG....."
    "expires_in": 3599,
    "scope": "https://www.googleapis.com/auth/userinfo.email 
    https://www.googleapis.com/auth/appengine.admin 
    https://www.googleapis.com/auth/compute 
    https://www.googleapis.com/auth/cloud-platform 
    https://www.googleapis.com/auth/accounts.reauth openid",
    "token_type": "Bearer",
    }
    

就是这样gcloud为您打印一个身份令牌。

如果您好奇代码如何使用 JSON 密钥文件执行此操作,您会看到非常相似的内容。例如,在 Go 中有http://google.golang.org/api/idtoken包,你会看到一个那里有类似的实施.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用用户帐户凭据访问私有 Cloud Run/Cloud Functions 的相关文章

随机推荐