Google Cloud Function:支持Google Cloud KMS

Jac*_*per 5 node.js google-cloud-platform google-cloud-functions google-cloud-kms

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

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

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

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

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

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

是否可以通过使用运行时配置程序或部署管理器来配置Google Cloud Function的机密?

谢谢。

set*_*rgo 4

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

$ echo -n "user:pass" | gcloud beta secrets create "my-basic-auth" \
  --data-file=- \
  --replication-policy "automatic"
Run Code Online (Sandbox Code Playgroud)

您还可以通过 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"),
  },
});
Run Code Online (Sandbox Code Playgroud)

然后,在您的云功能中:

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
Run Code Online (Sandbox Code Playgroud)

用于部署云功能的服务帐户将需要roles/secretmanager.secretAccessor权限。