简体   繁体   English

我如何处理 Google Cloud Functions 中的机密?

[英]How do i handle secrets in Google Cloud Functions?

What is the common practice here?这里的常见做法是什么? There seems to be no tools provided by gcloud . gcloud好像没有提供任何工具。 I'm deploying functions from local machine for now, so I can hardcode secrets, but this seems inappropriate.我现在正在从本地机器部署功能,所以我可以对秘密进行硬编码,但这似乎不合适。 Also, what about CI/CD?另外,CI/CD 呢? I would need to pass secrets as environment variables in this case.在这种情况下,我需要将机密作为环境变量传递。 Is this even possible atm?这甚至可能吗?

You can use the Secret Manager for this.您可以为此使用Secret Manager Follow the instructions on the link to add a secret.按照链接上的说明添加密钥。

The only GOTCHA I found is that by default the service account doesn't have read-access to the secrets, you've got to manually grant permissions, like so:我发现的唯一 GOTCHA 是默认情况下服务帐户没有对机密的读取访问权限,您必须手动授予权限,如下所示:

添加秘密权限

Since making my comment, I've found a relatively simple way to do this - provide a config .json file.自从发表评论以来,我找到了一种相对简单的方法 - 提供一个 config .json文件。 Here's an example I hacked together based on their Slack function example:这是我根据他们的 Slack 函数示例编写的示例:

config.json file in the same directory as index.js : config.json文件在与index.js相同的目录中:

{
  "foo": "bar"
}

index.js索引.js

const config = require('./config.json');

exports.envTest = (req, res) => {
  res.status(200).send(config.foo);
};

When you deploy the function and go to the URL, you should get the response bar .当您部署函数并转到 URL 时,您应该会看到响应bar

Pros and cons:优缺点:

Pros:优点:

  1. Easy to set up and configure right in your IDE在您的 IDE 中轻松设置和配置
  2. Config file can be put into .gitignore to ensure your secrets don't end up the repo可以将配置文件放入.gitignore以确保您的机密不会最终出现在 repo 中
  3. File itself can be stored in a secure location and only given to individual responsible for deploying the functions文件本身可以存储在一个安全的位置,并且只提供给负责部署功能的个人

Cons:缺点:

  1. Clunky in comparison to proper secret management与适当的秘密管理相比笨重
  2. Requires attention to ensure the file doesn't fall into the wrong hands需要注意确保文件不会落入坏人之手
  3. File can be read in plaintext in the Google Cloud console by looking at the function source在谷歌云控制台中查看函数源码可以明文读取文件

On the whole, it's a far cry from a real secrets management system, but it's workable enough to hold me over until this feature eventually makes it into the Cloud Functions core.总的来说,它与真正的机密管理系统相去甚远,但它足以让我坚持下去,直到此功能最终成为 Cloud Functions 核心。

You should use Cloud Key Management Service(KMS) .您应该使用Cloud Key Management Service(KMS)
Don't push pure secrets to Cloud Functions with files or environment variables.不要将带有文件或环境变量的纯机密推送到 Cloud Functions。

One solution is followings:一种解决方案如下:

  1. Create key on Cloud KMS在 Cloud KMS 上创建密钥
  2. Encrypt secret file with that key使用该密钥加密秘密文件
  3. Upload encrypted secret file to Google Cloud Storage(GCS) (Accessible by specified user)将加密的机密文件上传到 Google Cloud Storage(GCS)(可由指定用户访问)
  4. In Cloud Function Execution, get uploaded secret file from GCS, decrypt, and use it在 Cloud Function Execution 中,从 GCS 获取上传的机密文件,解密并使用它

[Ref] Secret management using the Google Cloud Platform [Ref] 使用 Google Cloud Platform 进行机密管理

As of 2021-08-25, the preferred way to handle secrets in Google Cloud Functions is with the native Secret Manager integration .截至 2021-08-25,在 Google Cloud Functions 中处理机密的首选方式是使用本机 Secret Manager 集成 This integration enables you to mount secrets in Google Secret Manager via environment variables or the filesystem.此集成使您能够通过环境变量或文件系统在 Google Secret Manager 中装载机密。

  1. Create or migrate your secret to Google Secret Manager (there's a generous always-free tier):创建您的秘密或将您的秘密迁移到 Google Secret Manager(有一个慷慨的永远免费的层):

     $ gcloud secrets create "my-secret" --replication-policy="automatic" --data=...
  2. Update your Cloud Function deployment to reference the secret:更新您的云 Function 部署以引用秘密:

    As an environment variable:作为环境变量:

     $ gcloud beta functions deploy "my-function" --set-secrets "MY_SECRET=my-secret:latest"

    Via the filesystem:通过文件系统:

     $ gcloud beta functions deploy "my-function" --set-secrets "/path/to/my/secret=my-secret:latest"

To practice the principle of least privilege, it's recommended that each Cloud Function be deployed as a dedicated service account.为了实践最小权限原则,建议将每个 Cloud Function 部署为专用服务帐户。 You need to grant that service account access to the secrets your application requires.您需要授予该服务帐户访问您的应用程序所需机密的权限。

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

相关问题 如何在本地处理运行 Google Cloud API Gateway 之类的东西? - How do I handle running things like Google Cloud API Gateway locally? 如何在忽略现有功能的情况下部署 Cloud Functions? - How do I deploy Cloud Functions while ignoring existing functions? 如何在 Google Cloud Functions 中获取当前的 projectId? - How can I get current projectId inside Google Cloud Functions? 谷歌云函数的IP怎么改? - How can I change the IP of google cloud functions? 如何在 Firebase Cloud Functions 中对 npm 模块使用 `import`? - How do I use `import` for npm modules in Firebase Cloud Functions? 如何在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase? 我如何在 Python 中使用我的谷歌云 API 密钥在云功能中 - How to i user my google cloud API key in cloud functions in Python 如何将数据集从 Huggingface 移动到 Google Cloud? - How do I move a dataset from Huggingface to Google Cloud? 如何删除我的 Google Cloud Platform 帐户? - How do I delete my Google Cloud Platform Account? 如何读取和写入谷歌云数据存储? - How do I read and write to the google cloud datastore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM