如何在云函数节点.js10中访问机密管理器?



我已经为此工作了 2 天,对进度感到非常沮丧,任何关于我的理解/代码/方法可能存在问题的指导将不胜感激!

我正在尝试使用 node.js 从秘密管理器中获取版本值,下面的脚本在 GCE 上运行良好,但每当我在云功能上运行它时,它都会失败。

// My script on GCE, it works fine
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManagerServiceClient = new SecretManagerServiceClient();
const name = 'projects/moonhome/secrets/moonFirstSecret/versions/latest';
testSecretManager = async () => {
const [version] = await secretManagerServiceClient.accessSecretVersion({ name });
const payload = version.payload.data.toString();
console.debug(`Payload: ${payload}`);
};
testSecretManager();
// My index.js on Cloud Function
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManagerServiceClient = new SecretManagerServiceClient();
const name = 'projects/moonhome/secrets/moonFirstSecret/versions/latest';
testSecretManager = async () => {
const [version] = await secretManagerServiceClient.accessSecretVersion({ name });
const payload = version.payload.data.toString();
console.debug(`Payload: ${payload}`);
};
exports.helloHttp = (req, res) => {
testSecretManager();
res.send("noooo1");
};
// One of many versions of packaga.json I tried on Cloud function
{
"dependencies": {
"@google-cloud/secret-manager": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@google-cloud/secret-manager/-/secret-manager-3.1.0.tgz",
"integrity": "sha512-/9IOWEhKAz/r3kSyp16kjudELkEJSRhwFfzukKbzQehVRZ3RceNDzjn+Rti1TivODJHEEIBZVsQFsKp7cLfUgQ==",
"requires": {
"google-gax": "^2.1.0"
}
}
}
}

以下是我的问题:

  1. 我注意到 Cloud Function 中的 node.js 运行时有一个可用系统包的列表,所以我想知道这是否是原因。我已经提交了将@google-cloud/secret-manager添加到 node.js 运行时的请求。但是,云函数文档中有一个使用escape-html的示例,该列表中也没有该示例。我的问题是,我应该请求将密钥管理器包添加到节点运行时.js吗?

  2. 由于云函数需要一个事件触发器,我也尝试用一个简单的函数来包装这个testSecretManager来处理 http 请求,并在浏览器中的端点上对其进行了测试。简单函数本身工作正常,但是每当我将与机密管理器相关的任何内容插入该函数时,函数要么失败,要么页面显示它Error: could not handle the request。我的问题是,我是否必须用 HTTP 请求或任何其他事件处理函数包装testSecretManager才能在 Cloud Function 中触发我的目标函数?

  3. 我对云功能上的package.json文件非常困惑,当我在GCE中使用secret管理器时,package-lock.json有600+行,所以我尝试将这些行package.json云功能,但它不起作用.....我的问题是,当我想要的只是@google-cloud/secret-manager包时,我应该在package.json中包含什么?

  1. 你混淆了系统包和节点包。系统软件包安装在主机上(例如apt-get install)。NPM 包被安装到 Node 中(例如npm install)。不应请求将机密管理器添加到系统包中。

  2. 您的功能是混合同步和异步。由于您的testSecretManager函数是同步函数,因此在helloHttp中调用它时需要以await开头。然后,您需要将helloHttp标记为异步。如果这不起作用,请复制并粘贴确切的错误消息和堆栈跟踪。

  3. package.jsonpackage-lock.json是具有不同语法的单独文件。不应将数据从锁文件复制到包文件中。下面是可以复制的示例:

    "dependencies": {
    "@google-cloud/secret-manager": "^3.1.0"
    },
    

最新更新