简体   繁体   English

初始化代码时使用 Google Cloud Secrets

[英]Use Google Cloud Secrets when initializing code

I have this code to retrieve the secrets:我有这个代码来检索秘密:

import {SecretManagerServiceClient} from "@google-cloud/secret-manager";

const client = new SecretManagerServiceClient();

async function getSecret(secret: String, version = "latest") {
    const projectID = process.env.GOOGLE_CLOUD_PROJECT;
    const [vs] = await client.accessSecretVersion({
        name: `projects/${projectID}/secrets/${secret}/versions/${version}`
    });
    const secretValue = JSON.parse(vs.payload.data.toString());
    return secretValue;
}

export {getSecret};

I would like to replace the process.env.SENTRY_DNS with await getSecrets("SENTRY_DNS") but I can't call a promise ( await ) outside an async function.我想用await getSecrets("SENTRY_DNS")替换process.env.SENTRY_DNS ,但我不能在async function 之外调用 promise ( await )。

Sentry.init({
    dsn: process.env.SENTRY_DNS,
    environment: Config.isBeta ? "Beta" : "Main"
});

function sentryCreateError(message, contexts) {
    Sentry.captureMessage(message, {
        level: "error", // one of 'info', 'warning', or 'error'
        contexts
    });
}

What are the best practices with Google Secrets? Google Secrets 的最佳做法是什么? Should I be loading the secrets once in a "config" file and then call the values from there?我应该在“配置”文件中加载一次秘密,然后从那里调用值吗? If so, I'm not sure how to do that, do you have an example?如果是这样,我不知道该怎么做,你有一个例子吗?

Leaving aside your code example (I don't work with JS anyway), I would think about a few different questions, answers on which may affect the design.撇开您的代码示例(无论如何我不使用 JS),我会考虑一些不同的问题,这些问题的答案可能会影响设计。 For example:例如:

  1. Where this code is executed?这段代码在哪里执行? - compute engine, app engine, cloud run, k8s, cloud function, and so on. - 计算引擎、应用引擎、云运行、k8s、云function等。 Depending on the answer - an approach to store secrets might be different.根据答案 - 存储秘密的方法可能会有所不同。

Suppose, for example, that is going to be a cloud function.例如,假设这将是一个云 function。 The next question -下一个问题——

  1. Would you prefer to store the secret values in a special environment variables, or in the secret manager?您希望将秘密值存储在特殊的环境变量中,还是存储在秘密管理器中? The first option is faster, but less secure - as, for instance, everybody, who has access tot he cloud function details in the console, might see those environment variable values.第一个选项更快,但不太安全 - 例如,每个可以在控制台中访问云 function 详细信息的人都可能会看到这些环境变量值。

  2. Load secret values into the memory on initialization?在初始化时将秘密值加载到 memory 中? Or on every invocation?还是在每次调用时? The first option is faster, but might cause some issues if the secrete values are modified (gradual replacement of old values with new, when some instances are terminated, and new instances are initialized).第一个选项更快,但如果修改秘密值可能会导致一些问题(当某些实例终止并初始化新实例时,用新值逐渐替换旧值)。

The second option may need some additional discussion.第二个选项可能需要一些额外的讨论。 It might be possible to get the values asynchronously.可能可以异步获取值。 In what circumstances it might be useful?在什么情况下可能有用? I think - only in case your code has something else to do, while waiting for the secret values, which are required to do (probably) the main job of the cloud function.我认为 - 只有在您的代码有其他事情要做的情况下,在等待秘密值时,这些值是(可能)完成云 function 的主要工作所必需的。 How much can we shave on that?我们可以剃掉多少? - probably a few milliseconds used on the Secret Manager API call. - 可能在 Secret Manager API 调用中使用了几毫秒。 Any drawbacks?有什么缺点吗? - code complexity, as somebody is to maintain the code in the future. - 代码复杂性,因为将来有人要维护代码。 Is that performance gain still overweight?这种性能提升是否仍然超重? - we probably can return to the item 2 in the list above and think about storing secrets in environment variables in that case. - 我们可能可以回到上面列表中的第 2 项,并考虑在这种情况下将秘密存储在环境变量中。

What about the first option?第一个选项呢? Again - if the performance is the priority - return back to the item 2 above, otherwise - is the code simplicity and maintainability the priority, and we don't need any asynchronous work here?再次 - 如果性能是优先级 - 回到上面的第 2 项,否则 - 代码的简单性和可维护性是优先级,我们在这里不需要任何异步工作吗? May be the answer of that question depends on skills, knowledge and a financial budget of your company/team, rather than on the technical preferences.这个问题的答案可能取决于您公司/团队的技能、知识和财务预算,而不是技术偏好。

  1. About the "config" file to store the secret values... While it is possible to store data in a pseudo "/tmp" directory (actually in the memory of a cloud function) during the cloud function execution, we should not expect that data to be preserved between cloud function invocations.关于存储秘密值的“config”文件...虽然可以在云 function 执行期间将数据存储在伪“/tmp”目录中(实际上是在云函数的 memory 中),但我们不应该期望要在云 function 调用之间保留的数据。 Thus, we come back to either environment variables (see the item 2 above), or to some other remote place with an API access.因此,我们回到环境变量(参见上面的第 2 项),或者回到具有 API 访问权限的其他远程位置。 I don't know if there are many other services with better latency than the Secret Manager, which can be used as a cache for storing secrets.不知道有没有比Secret Manager延迟更好的其他服务,可以作为存储秘密的缓存。 Suppose we find such services.假设我们找到了这样的服务。 And now we get the performance vs complexity/maintainability dilemma again...现在我们再次陷入了性能与复杂性/可维护性的困境......

Some concluding notes.一些结束语。 My context, experience, budget, requirements - may be completely different from your case.我的背景、经验、预算、要求——可能与你的情况完全不同。 My assumptions (ie the code is for a cloud function) - can be completely wrong as well... Thus, I would suggest to consider my writing with some criticism, and use ideas which are only relevant for your specific situation.我的假设(即代码用于云功能) - 也可能是完全错误的......因此,我建议以一些批评来考虑我的写作,并使用仅与您的具体情况相关的想法。

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

相关问题 Google Cloud Secrets - 重用密钥 - Google Cloud Secrets - Reusing a secret “无法加载默认凭据”谷歌云机密管理器 - 使用 CLI 授权用户 - "Could not load default credentials" google cloud secrets manager - use CLI authorized user 如何在 firebase 云函数中使用 github secrets - How to use github secrets in firebase cloud functions 在HTTP Google Cloud Function(NodeJS)中同步加载机密 - Synchronously load secrets in an HTTP Google Cloud Function (NodeJS) Google Cloud function 从 Secrets Manger 访问秘密 - Google Cloud function accessing a secret from Secrets Manger 如何为 Google Cloud Function Firestore 触发器使用可重用代码? - how to use reusable code for Google Cloud Function Firestore trigger? 结合使用Google Cloud和Nativescript - Use Google Cloud with Nativescript Google Cloud Functions:尝试使用 ES6 时部署失败 - Google Cloud Functions: Deployment failure when attempting to use ES6 如何在cloudbuild.yaml中使用带有KMS的Google Cloud Build将多个环境变量作为秘密传递? - How to pass multiple environment variables as secrets using Google Cloud Build with KMS in cloudbuild.yaml? 尝试在 Cloud Run 中使用 Google Cloud Storage 时调用者没有权限 - The caller does not have permission when attempting to use Google Cloud Storage within Cloud Run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM