谷歌云秘密管理器nodejs getsecret问题

问题描述

我需要在Cloudrun应用程序中从Google Secret Manager获得密码。 我的代码:

const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
const smClient = new SecretManagerServiceClient();
const pgpwd = 'projects/xxxxxxxx/secrets/pgpwd'; 
async function getSecret() { 
    const [version] = await smClient.accessSecretVersion({
        pgpwd: pgpwd
    });
    const pwd = version.payload.data.toString();
    return pwd;
}

const pwd = getSecret();
console.log(`out secret ${pwd}`);

在部署过程中接收...

020-09-18 11:55:12.421 IDT> node index.js
2020-09-18 11:55:12.421 IDT
2020-09-18 11:55:14.061 IDTout secret [object Promise]
2020-09-18 11:55:14.062 IDTServer running on port 8080
2020-09-18 11:55:15.451 IDT(node:14) UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT: Invalid resource field value in the request.
2020-09-18 11:55:15.451 IDT at Object.callErrorFromStatus (/usr/src/app/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
2020-09-18 11:55:15.451 IDT at Object.onReceiveStatus (/usr/src/app/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
2020-09-18 11:55:15.451 IDT at Object.onReceiveStatus (/usr/src/app/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:342:141)
2020-09-18 11:55:15.451 IDT at Object.onReceiveStatus (/usr/src/app/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:305:181)
2020-09-18 11:55:15.451 IDT at /usr/src/app/node_modules/@grpc/grpc-js/build/src/call-stream.js:124:78
2020-09-18 11:55:15.451 IDT at processTicksAndRejections (internal/process/task_queues.js:79:11)
2020-09-18 11:55:15.451 IDT(node:14) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2020-09-18 11:55:15.551 IDT(node:14) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

解决方法

const [version] = await smClient.accessSecretVersion({
  pgpwd: pgpwd
});

应为:

const [version] = await smClient.accessSecretVersion({
  name: pgpwd
});

pgpwd的格式应为:

projects/PROJECT/secrets/NAME/versions/VERSION

您可以使用版本号或魔术别名“最新”来获取最新版本。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...