首页 > 解决方案 > 谷歌函数 HTTP 触发器 - 身份验证问题服务器到具有服务帐户的服务器

问题描述

我想要做什么:从我的服务器/机器调用谷歌函数并通过(简单)身份验证限制它的使用。

我使用的是:Node.js,用于身份验证的 google-auth-library库。

我做了什么/尝试了什么

1) 在 Google Cloud Functions 中创建了一个项目

2)创建了一个简单的谷歌函数

 exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

3)设置我的自定义服务帐户

4) 启用的 API: - Cloud Functions API - IAM 服务帐户凭据 API - Cloud Run API - 计算引擎 API - IAM 服务帐户凭据 API

5) 授予我的服务器帐户必要的授权(项目所有者、云功能管理员、IAM 项目管理员...... (需要更多?)

6) 从我的服务帐户生成密钥并以 json 格式保存

注意:拥有 allUser 权限(无需授权),我可以毫无问题地调用我的端点

7)从我的项目中,我尝试以这种方式验证我的功能

const { JWT } = require('google-auth-library');
const fetch = require('node-fetch');
const keys = require('./service-account-keys.json');


async function callFunction(text) {
  const url = `https://europe-west1-myFunction.cloudfunctions.net/test`;

  const client = new JWT({
    email: keys.client_email,
    keyFile: keys,
    key: keys.private_key,
    scopes: [
      'https://www.googleapis.com/auth/cloud-platform',
      'https://www.googleapis.com/auth/iam',
    ],
  });

  const res = await client.request({ url });
  const tokenInfo = await client.getTokenInfo(client.credentials.access_token);

  try {
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${client.credentials.access_token}`,
      },
    });
    if (response.status !== 200) {
      console.log(response);
      return {};
    }
    return response.json();
  } catch (e) {
    console.error(e);
  }
} 

ℹ️ 如果我尝试在没有函数名称的情况下传递 client.request() url ( https://europe-west1-myFunction.cloudfunctions.net ),我没有收到错误,但是当使用在 fetch 调用中获得的 JWT 令牌时,我收到同样的错误。

结果

 Error: 
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL <code>/test1</code>.</h2>
<h2></h2>
</body></html>

❓ 如何调用具有任何保护功能的 google 功能以防止任何人使用它?(我不需要特定的安全性,只是随机用户不使用它)提前感谢您的帮助

标签: google-cloud-platformgoogle-cloud-functionsgoogle-authenticationgoogle-cloud-iamserver-to-server

解决方案


当您调用私有函数(或私有 Cloud Run)时,您必须使用谷歌签名的身份令牌

在您的代码中,您使用访问令牌

      headers: {
        Authorization: `Bearer ${client.credentials.access_token}`,
      },

当您必须请求 Google Cloud API 而不是您的服务时,访问令牌工作

并且google 签名很重要,因为您可以使用 google auth lib 轻松生成自签名身份令牌,但它不起作用

您在这里有代码示例,如果您想尝试一下,我在 Go 中编写了一个工具

** 编辑 **

我研究了一个例子,即使我从不喜欢 Javascript,我也不得不承认我很嫉妒!!在 Node 中就是这么简单!!

这是我的工作示例

const {GoogleAuth} = require('google-auth-library');

async function main() {
    // Define your URL, here with Cloud Run but the security is exactly the same with Cloud Functions (same underlying infrastructure)
    const url = "https://go111-vqg64v3fcq-uc.a.run.app"
    // Here I use the default credential, not an explicit key like you
    const auth = new GoogleAuth();
    //Example with the key file, not recommended on GCP environment.
    //const auth = new GoogleAuth({keyFilename:"/path/to/key.json"})

    //Create your client with an Identity token.
    const client = await auth.getIdTokenClient(url);
    const res = await client.request({url});
    console.log(res.data);
}

main().catch(console.error);

注意:只有服务帐户可以生成和身份令牌与观众。如果您在本地计算机上,请不要将您的用户帐户与默认凭据模式一起使用。


推荐阅读