【发布时间】:2020-05-18 06:29:36
【问题描述】:
可以为 Google API 凭据设置限制,以限制 API 密钥或令牌对特定电子表格或文件夹的访问,因此这些凭据无法访问帐户的所有信息,而只能访问指定文件。
【问题讨论】:
标签: security google-cloud-platform google-drive-api
可以为 Google API 凭据设置限制,以限制 API 密钥或令牌对特定电子表格或文件夹的访问,因此这些凭据无法访问帐户的所有信息,而只能访问指定文件。
【问题讨论】:
标签: security google-cloud-platform google-drive-api
当然!
Google Cloud Platform 拥有强大的工具来管理对各种事物的访问,包括 API 凭据访问。
您可以创建云服务以使用您的密钥发送响应,仅授权某些服务接收/请求密钥。
这是GCP IAM Documentation。通过前端云控制台或命令行工具按照他们的说明为您的 api 密钥服务设置策略。
以下是您将为 IAM 做的事情的要点:
my-api-key@see-the-docs-for-google-service-domain
my-app@see-the-docs-for...
对于存储在磁盘上的凭据,最好在应用程序中按需对其进行加密/解密。
看到这个SO answer。如果您加密您的密钥,请继续并添加到版本控制。否则,请避免。
但是,我建议您使用开源 Berglas 工具或 Google 托管的 Secrets 产品。您基本上将把您的 api 密钥提供给秘密管理器,存储它,然后在必要时在应用内或加载时获取它。
改编自 Google Cloud 文档,几乎一字不差:
# Import the Secret Manager client library.
from google.cloud import secretmanager_v1beta1 as sm
# GCP project in which to store secrets in Secret Manager.
project_id = 'YOUR_PROJECT_ID'
# ID of the secret to create.
secret_id = 'YOUR_SECRET_ID'
# Create the Secret Manager client.
client = sm.SecretManagerServiceClient()
# Build the parent name from the project.
parent = client.project_path(project_id)
# Create the parent secret
secret = client.create_secret(parent, secret_id, {
'replication': {
'automatic': {},
},
})
# Add the api key
version = client.add_secret_version(secret.name, {'data': b'my-google-api-credentials'})
# Access the api key
response = client.access_secret_version(version.name)
# Now you have your decoded api credentials you can use for authentication
payload = response.payload.data.decode('UTF-8')
我在上面更改了一些 cmets,但请务必查看 Google 的文档及其github examples。
如果您更喜欢冒险,Berglas 库非常棒,我通过其本地 Go 客户端和部署服务中的 docker 映像直接在多个项目中使用它。
【讨论】: