【问题标题】:Google Cloud Function : support for Google Cloud KMS谷歌云功能:支持谷歌云KMS
【发布时间】:2017-11-14 07:57:32
【问题描述】:

我正在使用带有 Pubsub 触发器的 Google Cloud Function (GCF),该触发器将 HTTP 请求发送到第三方 API。

GCF 接收来自不应该知道第三方 API 的服务使用的 Pubsub 主题的通知。

第三方 API 需要使用基本 HTTP 身份验证进行身份验证。

为了不必在我的源代码中硬编码密码,我每次部署我的函数时都使用 Google KMS 生成一个新的加密密钥。每次实例化函数时,我都使用 Google Cloud KMS 来解密密钥。

为了使用 KMS 解密,我必须向 NodeJS Google API 提供服务帐户的私钥。

我今天的主要问题是,如果我想让我的 GCF 正常工作,我必须将我的私钥推送到 GCloud Bucket。

是否可以使用 Runtime Configurator 或 Deployment Manager 来为 Google Cloud Function 配置机密?

谢谢。

【问题讨论】:

  • 我试图解决同样的问题。我使用 KMS 加密可部署包中的机密,然后尝试在运行时解密机密,但随后意识到您需要从云功能显式验证 - 据我所知,这意味着您需要可部署包中至少有某种形式的未加密机密。

标签: node.js google-cloud-platform google-cloud-functions google-cloud-kms google-secret-manager


【解决方案1】:

是否可以使用 Runtime Configurator 或 Deployment Manager 来为 Google Cloud Function 配置机密?

目前没有内置服务可以让您将机密配置为由 Google Cloud Functions 直接访问,因此您当前使用的方法是暂时处理 Cloud Functions 上的机密的正确方法。由于产品仍处于测试阶段,这种情况可能会发生变化。

如果您愿意,可以使用适当的 issue tracker 向 Cloud Function 团队提出功能请求。

【讨论】:

    【解决方案2】:

    最近几个月才出现的另一种解决方案是使用 Google Cloud Runtime Configuration 和 Firebase for Functions: https://firebase.google.com/docs/functions/config-env

    Firebase for Functions 似乎提供了对通过其他方式尚不可用的若干功能的访问。

    Runtime Configurator 不收取使用费,但会强制执行以下 API 限制和配额:

    • 每分钟 1200 次查询 (QPM) 用于删除、创建和更新请求
    • 600 QPM 用于观看请求。
    • 6000 QPM 用于获取和列出请求。
    • 每个用户 4MB 的数据,其中包括写入运行时配置器服务的所有数据和随附的元数据。

    https://cloud.google.com/deployment-manager/pricing-and-quotas#runtime_configurator


    顺便说一句,我发现 Firebase for Functions 中的这种冲突很可笑:

    Firebase SDK for Cloud Functions 提供了内置的环境配置,让您可以轻松地为您的项目存储和检索此类数据,而无需重新部署您的函数。

    过了一会儿:

    运行functions:config:set 后,您必须重新部署函数以使新配置可用。


    KMS 解决方案是一种可行的替代方案,但它的功能似乎成本高昂。 KMS 每个活动密钥每月收费 0.06 美元,每 10,000 次操作收费 0.03 美元。

    这将使您的 Cloud Functions 成本从每百万次调用 0.40 美元变为每百万次调用 3.40 美元。那是相当的跳跃。

    【讨论】:

    【解决方案3】:

    截至 2019 年 12 月,在 Google Cloud 上存储和管理机密的首选方式是Secret Manager

    $ echo -n "user:pass" | gcloud beta secrets create "my-basic-auth" \
      --data-file=- \
      --replication-policy "automatic"
    

    您还可以从 API 创建和管理机密:

    // Import the library
    const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
    
    // Create the client
    const client = new SecretManagerServiceClient();
    
    // Create the secret
    const [secret] = await client.createSecret({
      parent: "projects/<YOUR-PROJECT-ID>",
      secretId:"my-basic-auth",
      secret: {
        replication: {
          automatic: {},
        },
      },
    });
    
    // Add the version with your data
    const [version] = await client.addSecretVersion({
      parent: secret.name,
      payload: {
        data: Buffer.from("user:pass", "utf8"),
      },
    });
    

    然后,在您的云函数中:

    const [version] = await client.accessSecretVersion({
      name:"projects/<YOUR-PROJECT-ID>/secrets/<MY-SECRET>/versions/1",
    });
    
    const auth = version.payload.data.toString('utf-8');
    
    // auth is user:pass
    

    用于部署 Cloud Function 的服务帐号需要roles/secretmanager.secretAccessor 权限。

    【讨论】:

    • 当我提出这个问题时,我最终做了类似的事情。我创建了一个用于与 GCP KMS 交互的包装器。
    【解决方案4】:

    还有一个谷歌云密钥管理服务:Node.js Client

    cd functions
    npm install @google-cloud/kms
    

    例如:

    // Imports the Cloud KMS library
    const {KeyManagementServiceClient} = require('@google-cloud/kms');
    
    // Instantiates a client
    const client = new KeyManagementServiceClient();
    
    // Build the location name
    const locationName = client.locationPath(functions.config().firebase.projectId, functions.config().firebase.locationId);
    
    async function listKeyRings() {
      const [keyRings] = await client.listKeyRings({
        parent: locationName,
      });
      for (const keyRing of keyRings) {
        console.log(keyRing.name);
      }
      return keyRings;
    }
    
    return listKeyRings();
    

    【讨论】:

      猜你喜欢
      • 2014-11-07
      • 2018-12-11
      • 2019-01-19
      • 2019-05-14
      • 2020-09-03
      • 2021-10-12
      • 2019-12-03
      • 2014-03-17
      • 2019-06-07
      相关资源
      最近更新 更多