【问题标题】:Can you limit the access for Google cloud API credentials你能限制谷歌云 API 凭证的访问吗
【发布时间】:2020-05-18 06:29:36
【问题描述】:

可以为 Google API 凭据设置限制,以限制 API 密钥或令牌对特定电子表格或文件夹的访问,因此这些凭据无法访问帐户的所有信息,而只能访问指定文件。

【问题讨论】:

    标签: security google-cloud-platform google-drive-api


    【解决方案1】:

    当然!

    Google Cloud Platform 拥有强大的工具来管理对各种事物的访问,包括 API 凭据访问。

    GCP IAM - 云权限和访问

    您可以创建云服务以使用您的密钥发送响应,仅授权某些服务接收/请求密钥。

    这是GCP IAM Documentation。通过前端云控制台或命令行工具按照他们的说明为您的 api 密钥服务设置策略。

    以下是您将为 IAM 做的事情的要点:

    1. 为您的项目授权各种 google api
    2. 创建一个服务帐户,例如my-api-key@see-the-docs-for-google-service-domain
    3. 为每个需要服务密钥的应用创建另一个服务帐户,即my-app@see-the-docs-for...
    4. 为您为 api 密钥服务创建的服务帐户提供您选择的访问级别/权限的任何应用服务帐户
      • 您正在授权每个应用访问 api-key-service
    5. 部署一个简单的 Flask 服务以使用您的 api-key-service 帐户发送您的 api-key
    6. 在您的应用程序中访问已获得自己的 IAM 权限的 api 凭证
      • 请记住,您在步骤 4 中授权了您的应用

    在磁盘上

    对于存储在磁盘上的凭据,最好在应用程序中按需对其进行加密/解密。

    看到这个SO answer。如果您加密您的密钥,请继续并添加到版本控制。否则,请避免。

    Secrets Manager 或 Berglas

    但是,我建议您使用开源 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 映像直接在多个项目中使用它。

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2018-06-22
      • 2017-08-20
      相关资源
      最近更新 更多