这是我的用例。
- 我已经以私有模式部署了 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(....)