在Google Secret Manager中创建新的秘密版本时,如何获得新创建版本的版本号?

发布于 2025-01-24 15:21:42 字数 1152 浏览 0 评论 0原文

我正在尝试编写一个添加新的Google Secret Manager版本的函数,然后破坏以前的旧版本。

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

这些文档>通过const [version] =等待secrets.addsecretversion(),然后从中减去1的新秘密版本号。

但是打字稿抱怨版本不是一个数字:

算术操作的左侧必须是类型为'Any','','','',bigint'或枚举类型的类型。 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,
  });
};

I'm trying to write a function that adds a new Google Secret Manager version, and then destroys the previous old version.

I can add a new version easily, but to destroy the old version I need it's version number.

As per these docs I have tried to get the new secret version number via const [version] = await secrets.addSecretVersion() and then minus 1 from that.

But TypeScript is complaining that version is not a number:

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

Here is my code for adding a new version and deleteing the old version:

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,
  });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2025-01-31 15:21:43

弄清楚了。

版本是一个看起来像这样的对象:

{
   "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,
  });
};

Figured it out.

version is an object that looks like this:

{
   "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"
   }
}

So I used this to access the new version number and thus create the old version number:

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

Here is the full code:

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,
  });
};
仅此而已 2025-01-31 15:21:43

简单地使用此端点,通过通过以下方式传递您的资源路径:

在GO中,
返回fmt.sprintf(“%s/s/versions/最新”,G.AppSecretPath(name))

您方法的缺点是它不检查版本状态,这意味着如果有3个状态版本,您添加了一个新版本,例如4,第三版本已经处于残疾状态,您的代码断开,因此,您也需要处理该案例。只需确保您正在禁用该秘密的旧版本即可。虽然如果您足够的话,请随时继续前进。

Simple use this endpoint by passing your resource path like below:

In go,
return fmt.Sprintf("%s/versions/latest", g.appSecretPath(name))

The disadvantage of your approach is that it does not check for the version state, which means if there are 3 versions, you add a new version, say 4, and 3rd version is already in DISABLED state, your code breaks, so, you need to handle that case as well. Just make sure you are disabling the old ENABLED version of the secret. Although feel free to go ahead if that is sufficient for you.

顾铮苏瑾 2025-01-31 15:21:43

名称“:字符串,“ castement”:string,“ destiontime”:string,“ ca”:enum(ca),“ replicationStatus”:{Object(object(replicationStatus)},“ etag”:string“:clientspecifiedpaypayloadchecksum”:

name": string, "createTime": string, "destroyTime": string, "CA": enum (CA), "replicationStatus": { object (ReplicationStatus) }, "etag": string, "clientSpecifiedPayloadChecksum":

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文