【问题标题】:When creating a new secret version in Google Secret Manager, how do I get the version number of the newly created version?在 Google Secret Manager 中创建新的 Secret 版本时,如何获取新创建的版本的版本号?
【发布时间】:2022-07-04 08:18:51
【问题描述】:

我正在尝试编写一个函数来添加一个新的 Google Secret Manager 版本,然后销毁之前的旧版本。

我可以轻松添加新版本,但要销毁旧版本,我需要它的版本号。

根据these docs,我尝试通过const [version] = await secrets.addSecretVersion() 获取新的秘密版本号,然后从中减去1。

但 TypeScript 抱怨 version 不是数字:

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)

这是我添加新版本和删除旧版本的代码:

const addSecretVersion = async (secretName: string, value: string) => {
  const parent = `projects/my-project/secrets/${secretName}`;
  const payload = Buffer.from(value, 'utf8');
  // Add the new secret
  const [version] = await secrets.addSecretVersion({
    parent: parent,
    payload: {
      data: payload,
    },
  });
  const oldVersionNumber = version - 1; //<--- TypeScript error here
  // Destroy the old secret (to avoid billing)
  const oldSecret = `projects/my-project/secrets/${secretName}/versions/${oldVersionNumber}`;
  await secrets.destroySecretVersion({
    name: oldSecret,
  });
};

【问题讨论】:

    标签: google-secret-manager


    【解决方案1】:

    想通了。

    version 是一个如下所示的对象:

    {
       "destroyTime":null,
       "state":"ENABLED",
       "etag":"\"9999999999\"",
       "createTime":{
          "seconds":"9999999999",
          "nanos":9999999999
       },
       "clientSpecifiedPayloadChecksum":false,
       "name":"projects/9999999999/secrets/secret-name/versions/109",
       "replicationStatus":{
          "automatic":{
             "customerManagedEncryption":null
          },
          "replicationStatus":"automatic"
       }
    }
    

    所以我用它来访问新版本号,从而创建旧版本号:

    const newVersionNumber = Number(newVersion.name?.split('/').pop());
    const oldVersionNumber = newVersionNumber - 1;
    

    这里是完整的代码:

    const addSecretVersion = async (secretName: string, value: string) => {
      const parent = `projects/my-projects/secrets/${secretName}`;
      const payload = Buffer.from(value, 'utf8');
      // Add the new secret
      const [newVersion] = await secrets.addSecretVersion({
        parent: parent,
        payload: {
          data: payload,
        },
      });
      const newVersionNumber = Number(newVersionName.name?.split('/').pop());
      const oldVersionNumber = newVersionNumber - 1;
      // Destroy the old secret (to avoid billing)
      const oldSecret = `projects/my-projects/secrets/${secretName}/versions/${oldVersionNumber}`;
      await secrets.destroySecretVersion({
        name: oldSecret,
      });
    };
    

    【讨论】:

      【解决方案2】:

      name”:字符串,“createTime”:字符串,“destroyTime”:字符串,“CA”:枚举(CA),“replicationStatus”:{对象(ReplicationStatus)},“etag”:字符串,“clientSpecifiedPayloadChecksum”:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多