简体   繁体   English

Node.js - 在本地访问 Google Secret Manager

[英]Node.js - Access Google Secret Manager locally

I can't seem to figure it out.我似乎无法弄清楚。 All I want to do is access a test secret that's been created for me, when locally running my appliction.我要做的就是在本地运行我的应用程序时访问为我创建的测试密钥。 I was given a service-account.json that looks as follows, which I was told to use for local access, but I can't find any documentation or any other resources that show what to do with the file.我得到了一个service-account.json ,如下所示,我被告知将其用于本地访问,但我找不到任何文档或任何其他资源来显示如何处理该文件。

{
  "type": "service_account",
  "project_id": "test-dev-project",
  "private_key_id": "12cea3c45d67d0d89f0ccfca1bede23d4d56c7b8",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg1234\n-----END PRIVATE KEY-----\n",
  "client_email": "my-secrets-email@test-dev-project.iam.gserviceaccount.com",
  "client_id": "123456789012345678901",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/my-secrets-email%40test-dev-project.iam.gserviceaccount.com"
}

What little I could glean from the docs was that I should "set" its path via Powershell, but even when I do, and then run node index.js again, I still get the exact same error:我可以从文档中收集到的一点是我应该通过 Powershell“设置”它的路径,但即使我这样做了,然后再次运行node index.js ,我仍然得到完全相同的错误:

Error: 7 PERMISSION_DENIED: Secret Manager API has not been used in project 123456789012 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/secretmanager.googleapis.com/overview?project=123456789012 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

My only other experience with these kinds of service-account files is with Firebase Functions, and there you set the file path as an argument to the app initialization function, so I thought I'd have to do something similar here, but I can't find anything that indicates such.我对这类服务帐户文件的唯一其他经验是使用 Firebase 函数,在那里您将文件路径设置为应用程序初始化 function 的参数,所以我想我必须在这里做类似的事情,但我可以找不到任何表明这种情况的东西。

Here's what my Node project looks like, in its entirety:这是我的 Node 项目的整体外观:

// ./index.js
(async() => {
  const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
  const client = new SecretManagerServiceClient();
  const SOME_APIKEY = await client.accessSecretVersion({ name: 'latest' });
  console.log('key:', SOME_APIKEY);
})();

// ./package.json
{
  "name": "google-secret-attempt-17",
  "main": "index.js",
  "scripts": {},
  "dependencies": {
    "@google-cloud/secret-manager": "^3.10.1"
  }
}

// ./service-account.json (already posted its content above)

The steps that I've been trying is:我一直在尝试的步骤是:

  1. Run $env:GOOGLE_APPLICATION_CREDENTIALS="./service-account.json" in Powershell在 Powershell 中运行$env:GOOGLE_APPLICATION_CREDENTIALS="./service-account.json"
  2. Run node index.js in Powershell在 Powershell 中运行node index.js

I've also tried explicitly setting process.env.GOOGLE_APPLICATION_CREDENTIALS = './service-account.json' at the start of my index.js and then running step 2, but that resulted in exactly the same outcome, so I don't know what to do anymore.我还尝试在index.js的开头明确设置process.env.GOOGLE_APPLICATION_CREDENTIALS = './service-account.json'然后运行第 2 步,但结果完全相同,所以我没有知道该怎么做了。

Any insight into where I'm going wrong or anything else would be highly appreciated.任何对我哪里出错或其他任何事情的见解都将受到高度赞赏。

Looks like Secrets Manager api is not enabled in the concerned project.看起来相关项目中未启用Secrets Manager api。 Please ask the person who provided the the service-account.json file to visit https://console.developers.google.com/apis/api/secretmanager.googleapis.com/overview?project=123456789012 and enable the API. Please ask the person who provided the the service-account.json file to visit https://console.developers.google.com/apis/api/secretmanager.googleapis.com/overview?project=123456789012 and enable the API. Once complete you should be able to access the api.完成后,您应该能够访问 api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM