【问题标题】:GCP Server to Server Authentication with Service Account使用服务帐户的 GCP 服务器到服务器身份验证
【发布时间】:2021-04-27 14:38:17
【问题描述】:

我正在尝试验证从我的 Google Cloud Function 到我在 App Engine 上的 API 的请求(标​​准环境)。

我有一些工作,但我是 OAuth2 的新手,正在寻找健全性检查。

在我的 Cloud Function 中,我向我的 API 发送经过身份验证的请求,执行以下操作:

import { GoogleAuth } from 'google-auth-library';

// Send Request Code:
const auth = new GoogleAuth();
const tokenClient = await auth.getIdTokenClient(`/protectedEndpoint`);
await tokenClient.request({
    url: `https://${process.env.GCLOUD_PROJECT}.appspot.com/protectedEndpoint`,
    method: 'POST',
});

在 API(在 App Engine 上)中,我执行以下操作:

import { GoogleAuth } from 'google-auth-library';

// Handle Request Code:
const token = <Bearer token parsed from request headers>
const googleAuth = new GoogleAuth();
const tokenClient = await googleAuth.getIdTokenClient('');
const loginTicket = await tokenClient.verifyIdToken({
    idToken: token,
    audience: '/protectedEndpoint',
});

if (loginTicket.getUserId() !== process.env.SERVICE_ACCOUNT_ID)) {
    throw new Error('Unauthenticated Service Account');
}

return 'Successful Authentication'

注意:在这两种情况下,我都使用 Google 的默认应用程序凭据来初始化 GoogleAuth 客户端。 (我的默认 App Engine 服务帐户)

这一切都有效。我的函数向我的 API 发送请求,我的 API 能够解析不记名令牌并告诉我它来自我批准的服务帐户......但我不是 100% 确信这实际上是安全的。有人可以在没有凭据的情况下欺骗我的服务帐户吗?

提前致谢!

【问题讨论】:

    标签: google-app-engine google-cloud-platform oauth-2.0 google-cloud-functions google-oauth


    【解决方案1】:

    是否有可能有人欺骗我的服务帐户而没有 它的凭据?

    准确的答案需要指定时间。只要有足够的时间和处理能力,任何身份验证/授权/加密/散列/签名方法都可以被破解。

    Google 服务帐号包含 RSA 2048 位私钥。目前的猜测是 300 万亿年才能破解 RSA 2048 位加密。随着计算机的快速发展,我们假设您的数据在 RSA 被破坏时可能没有任何用处/价值。

    私钥用于签署 JWT。签名的 JWT 用于请求 OAuth 访问/身份令牌。

    欺骗需要使用相同的私钥进行签名。因此,用当今的技术进行欺骗是不可能的。

    窃取/泄露私钥或生成的 OAuth 令牌是当今唯一现实的方法。

    【讨论】:

    • 谢谢约翰!我可以肯定的是,我是否使用 Google 的 SDK 正确实现了这一点?
    • @sbjluke - 向您的 API 发送请求的代码是正确的。您验证身份令牌的代码出现(我没有验证运行时行为)是正确的。
    猜你喜欢
    • 2022-09-27
    • 1970-01-01
    • 2016-05-16
    • 2017-08-15
    • 2022-11-30
    • 2010-10-21
    • 2019-07-23
    • 1970-01-01
    • 2020-08-23
    相关资源
    最近更新 更多