问题描述
我已经进行了2天的工作,对进度感到非常沮丧,对于我的理解/代码/方法可能有什么问题的任何指导,将不胜感激!
我正在尝试使用node.js从秘密管理员那里获取版本值,下面的脚本在GCE上运行良好,但是每当我在Cloud函数上运行它都会失败。
// 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"
}
}
}
}
以下是我的问题:
-
我注意到Cloud Function中的node.js运行时上有list个可用的系统软件包,因此我想知道是否是原因。我已经提交了将
@google-cloud/secret-manager
添加到node.js运行时的请求。但是,在Cloud Function文档中有一个示例,其中使用了escape-html
,但该列表中也没有。我的问题是,我是否应该将secret-manager程序包添加到node.js运行时? -
由于Cloud Function需要事件触发器,因此我也尝试使用一个简单的函数包装
testSecretManager
来处理http请求,并在浏览器的端点对其进行了测试。简单函数本身可以正常工作,但是只要我在该函数中插入与机密管理器相关的任何内容,函数就会失败,或者页面会显示Error: could not handle the request
。我的问题是,我是否必须使用HTTP请求或其他任何事件处理功能包装testSecretManager
才能在Cloud Function中触发我的目标功能? -
我对Cloud函数上的
package.json
文件感到非常困惑,当我在GCE中使用secret-manager时,package-lock.json
有600多行,因此我尝试将这些行与{ {1}}在Cloud Function上,但不起作用.....我的问题是,当我只需要package.json
软件包时,我应该在package.json中包括什么?
解决方法
-
您正在混淆系统软件包和Node软件包。系统软件包已安装在主机上(例如
apt-get install
)。 NPM软件包已安装到Node中(例如npm install
)。 您不应请求将机密管理员添加到系统软件包中。 -
您正在混合同步和异步。由于您的
testSecretManager
功能是同步功能,因此在await
中调用helloHttp
时需要以helloHttp
为开头。然后,您需要将package.json
标记为异步。如果这样不起作用,请复制并粘贴确切的错误消息和堆栈跟踪。 -
package-lock.json
和"dependencies": { "@google-cloud/secret-manager": "^3.1.0" },
是具有不同语法的单独文件。 您不应将数据从锁定文件复制到打包文件中。您可以在此处an example进行复制:saltinDB = parseInt(saltinDB)