Node.js 什么';解析js普通对象的秘密负载的最佳方法是什么?
我使用google cloud web UI创建了一个秘密。我直接从Node.js 什么';解析js普通对象的秘密负载的最佳方法是什么?,node.js,google-cloud-platform,google-secret-manager,Node.js,Google Cloud Platform,Google Secret Manager,我使用google cloud web UI创建了一个秘密。我直接从.env文件导入它。现在我想在云函数中使用它们 代码如下: const{SecretManagerServiceClient}=require('@googlecloud/secretmanager'); const dotenv=require('dotenv'); const secretManagerServiceClient=新的secretManagerServiceClient(); const name='proj
.env
文件导入它。现在我想在云函数中使用它们
代码如下:
const{SecretManagerServiceClient}=require('@googlecloud/secretmanager');
const dotenv=require('dotenv');
const secretManagerServiceClient=新的secretManagerServiceClient();
const name='projects//secrets/workflow/versions/latest';
exports.testSecretManager=async(req,res)=>{
const[version]=wait secretManagerServiceClient.accessSecretVersion({name});
const payload=version.payload.data.toString();
调试(`Payload:${Payload}`);
const buf=缓冲区起始(有效载荷);
const secrets=dotenv.parse(buf);
console.debug('secrets:',secrets);
res.sendStatus(200);
};
version.payload.data.toString()
将为我提供一个原始字符串.env
文件,如下所示:
APP_PORT=3001
POSTGRES_HOST=db
我需要将它解析为一个js普通对象,这样我就可以通过对象属性使用它,比如payload.data.APP\u PORT
。现在,我使用它将原始字符串解析为js普通对象。输出如下所示:
APP_PORT=3001
POSTGRES_HOST=db
{
应用程序端口:3001,
博士后大学主持人:“127.0.0.1”
}
有更好的方法吗?google secret manager或
gcloud
cli是否支持从.env
文件创建JSON格式?因此,我可以使用JSON.parse
来解析JSON字符串,而无需使用任何第三方软件包?考虑将您的机密作为JSON字符串存储在Secret Manager中。为什么要在格式之间转换数据?@JohnHanley这应该可以,但我想继续使用.env
文件。根据您的示例,只需将=
字符上的每一行拆分并去除空白即可。这在任何语言中都很容易做到。你不需要第三方图书馆。@JohnHanley是的,它应该也能用。我的想法是这项繁琐的工作应该由secret manager客户端库来完成。也许我应该就GitHub问题提交一个功能请求。ThanksGoogle机密管理器将机密存储为不透明对象。重新格式化不是服务功能设计的一部分。