[英]Can't access secret stored in Secrets Manager from Google Cloud Function
[英]Google Cloud Secrets Manager denies access to App Engine application but works with Google Cloud Function
我正在嘗試連接以從 google 的機密管理器獲取機密,並且相同的代碼適用於 Cloud Functions,但不適用於 App Engine。
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManagerServiceClient = new SecretManagerServiceClient();
const name = 'projects/000000000000/secrets/database/versions/latest';
exports.testSecretManager = async (req, res) => {
const [version] = await secretManagerServiceClient.accessSecretVersion({ name });
const payload = version.payload.data.toString();
console.debug(`Payload: ${payload}`);
res.sendStatus(200);
};
當我將它部署為 function 時,相同的代碼工作正常。
但是當我運行相同的代碼作為 App Engine 應用程序的一部分時。 它失敗並出現此錯誤:
Error: 16 UNAUTHENTICATED: Failed to retrieve auth metadata with error: Could not refresh access token: Unsuccessful response status code. Request failed with status code 500
at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:180:52)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:365:141)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:328:181)
at /workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:182:78
at processTicksAndRejections (node:internal/process/task_queues:78:11)
我相信 Cloud Functions 和 App Engine 都由同一個服務帳戶“App Engine 默認服務帳戶”管理。 它有權利。
環境中似乎缺少GOOGLE_APPLICATION_CREDENTIALS
。
console.log(process.env.GOOGLE_APPLICATION_CREDENTIALS);
給我不確定。 這能成為理由嗎? 那么我該如何將這個環境傳遞給應用引擎呢?
我怎樣才能更深入地調試這個?
確保從 IAM 為 App Engine 的服務賬戶授予Secret Manager Secret Accessor
角色。 您需要添加的確切角色是:roles/secretmanager.secretAccessor。 您可以參考這里了解更多詳情。
另外,看看這個Stackoverflow 案例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.