【问题标题】:Python create secret with tags in Google Secret managerPython 在 Google 秘密管理器中使用标签创建秘密
【发布时间】:2022-11-22 06:15:49
【问题描述】:

我正在为我的应用程序使用 Google Cloud 运行。 我将所有机密存储在 Google Cloud Secret Manager 中。

要阅读秘密,我会执行以下操作:

from google.cloud import secretmanager
import hashlib


def access_secret_version(secret_id, version_id="latest"):
    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret version.
    PROJECT_ID = "xxxxx"
    name = f"projects/{PROJECT_ID}/secrets/{secret_id}/versions/{version_id}"

    # Access the secret version.
    response = client.access_secret_version(name=name)

    # Return the decoded payload.
    return response.payload.data.decode('UTF-8')


def secret_hash(secret_value):
    # return the sha224 hash of the secret value
    return hashlib.sha224(bytes(secret_value, "utf-8")).hexdigest()

写秘密:

from google.cloud import secretmanager


def create_secret(secret_id):
    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the parent project.
    PROJECT_ID = "xxxx"
    parent = f"projects/{PROJECT_ID}"

    # Build a dict of settings for the secret
    secret = {'replication': {'automatic': {}}}

    # Create the secret
    response = client.create_secret(secret_id=secret_id, parent=parent, secret=secret)

    # Print the new secret name.
    print(f'Created secret: {response.name}')

如何在 Python 中使用标签创建机密?

【问题讨论】:

    标签: python google-cloud-platform google-secret-manager


    【解决方案1】:

    确定功能是否可用的一种方法是研究 REST API。机密不支持标签。

    秘密支持标签,注释版本别名.

    根据您的用例,版本别名可能是工作而不是标签。

    可选的。从版本别名到版本名称的映射。

    版本别名是一个最大长度为 63 个字符的字符串,并且 可以包含大小写字母、数字和连字符 (-) 和下划线 ('_') 字符。别名字符串必须以 字母,不能是字符串 'latest' 或 'NEW'。不超过 50 别名可以分配给给定的秘密。

    版本别名对可以通过 secrets.get 和可修改的方式查看 通过 secrets.patch。启动时仅支持 Allias 访问 在 versions.get 和 versions.access 上。

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 2022-11-02
      • 2021-03-15
      • 2022-08-16
      • 2019-11-03
      • 2021-10-26
      • 2021-10-07
      • 2020-10-01
      • 2020-10-13
      相关资源
      最近更新 更多