【发布时间】: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