python - 从 Docker 中的 Python 访问 GCP secret 管理器中的 secret - nontsop 权限被拒绝

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

我正在构建一个简单的应用程序,它将 Twilio 凭据存储在 GCP Secret Manager 中,并在需要时将它们拉下来。但是,我不断收到有关项目资源的拒绝权限错误 (403):

google.api_core.exceptions.PermissionDenied:403 资源项目权限被拒绝。

我正在使用设置为包含服务帐户凭据的 JSON 文件路径的环境变量。

这是我已经尝试过的:

  • 确保在 GCP Console 中正确设置权限。服务帐户被设置为项目的所有者和项目级别的 secret 访问者,以及每个 secret 的对象级别的 secret 访问者。
  • 确保环境变量设置正确 - 我已验证 ENV 变量设置正确并且可以读取它指向的文件。我可以通过将 ENV 变量作为 JSON 文件打开来打印文件的内容。
  • 通过将我的 JSON 文件的内容与 GCP 控制台中的数据进行比较,确认身份验证信息是正确的
  • 我使用 gcloud CLI 在服务帐户下登录,然后使用 CLI 命令检索完全相同的 secret
  • 我可以成功访问并将数据推送到 GCS 存储桶,这表明凭据已从 ENV 变量
  • 正确加载
  • 我曾尝试以多种方式访问​​这些 secret 。我试过其他方法,比如列出项目中的 secret 。都返回权限错误。

  • 作为引用,我一直在遵循 https://cloud.google.com/secret-manager/docs/reference/libraries#client-libraries-install-python 中的说明。并且还一直在使用官方客户端库文档来探索还有什么可能是错误的。没有什么能真正帮助我在这里。

    我已经阅读了我能找到的所有资源,但没有任何帮助。有什么想法吗?

    谢谢!!!

    编辑:在下面添加代码:
    def access_secret(project_id, secret_id, version):
        """
        Access a secret- API token, etc- stored in Secret Manager
    
        Code from https://cloud.google.com/secret-manager/docs/creating-and-accessing-secrets#secretmanager-access-secret-version-python
        """
        client = secretmanager.SecretManagerServiceClient()
    
        # Build the resource name of the secret version
        name = client.secret_version_path(project_id, secret_id, version)
    
        # Access the secret version
        response = client.access_secret_version(name)
    
        # Return the secret payload
        payload = response.payload.data.decode('UTF-8')
    
        return payload
    

    EDIT2:这是我在其中运行此代码的 Dockerfile:
    FROM python:3.8.2-slim-buster
    
    WORKDIR /build
    
    # Copy in the requirements.txt file and service account credentials
    COPY requirements.txt <CREDENTIALS_FILENAME>.json /build/ 
    
    ENV PYTHONUNBUFFERED=1 \
        GOOGLE_APPLICATION_CREDENTIALS=/build/<CREDENTIALS_FILENAME>.json \
        VOICEMAIL_TIMEOUT=55 \
        MULTIRING_TIMEOUT=15 \
        GCS_VM_BUCKET=<MY GCS BUCKET NAME> \
        GCP_PROJECT=<MY GCP PROJECT NAME> \
        PHONE_NUMBER=<PHONE NUMBER> \
        TWILIO_ACCOUNT_SID_VERSION=1 \
        TWILIO_AUTH_TOKEN_VERSION=1
    
    # Install packages
    RUN pip install -r requirements.txt
    
    # Navigate to the directory containing the Python code
    WORKDIR /code/src/
    
    EXPOSE 5000
    
    # Run the actual Python code
    CMD ["python", "main.py"]
    

    后来,我有了调用上面函数的 Python 代码:
    GCS_VM_BUCKET = os.environ['GCS_VM_BUCKET']
    GCP_PROJECT = os.environ['GCP_PROJECT']
    
    TWILIO_SID = access_secret(GCP_PROJECT, 'TWILIO_ACCOUNT_SID', os.environ['TWILIO_ACCOUNT_SID_VERSION'])
    TWILIO_AUTH_TOKEN = access_secret(GCS_PROJECT, 'TWILIO_AUTH_TOKEN', os.environ['TWILIO_AUTH_TOKEN_VERSION'])
    

    其中 TWILIO_ACCOUNT_SID 和 TWILIO_AUTH_TOKEN 是 GCP 中的 secret 名称。

    完整的错误跟踪:
    Traceback (most recent call last):   
    
    File "/usr/local/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable                                                                                                        return callable_(*args, **kwargs)                                                                                                                                                                                     
    File "/usr/local/lib/python3.8/site-packages/grpc/_channel.py", line 826, in __call__                                                                                                                                     
    return _end_unary_response_blocking(state, call, False, None)                                                                                                                                                         
    File "/usr/local/lib/python3.8/site-packages/grpc/_channel.py", line 729, in _end_unary_response_blocking                                                                                                                 
    raise _InactiveRpcError(state)                                                                                                                                                                                      
    grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:                                                                                                                                                
    status = StatusCode.PERMISSION_DENIED                                                                                                                                                                                   
    details = "Permission denied on resource project <MY GCP PROJECT NAME>."                                                                                                                                                       
    debug_error_string = "{"created":"@1588389938.954039708","description":"Error received from peer ipv4:172.217.12.138:443","file":"src/core/lib/surface
    /call.cc","file_line":1056,"grpc_message":"Permission denied on resource 
    project <MY GCP PROJECT NAME>.","grpc_status":7}"                                                                                                                                                               >                                                                                                                                                                                                                                                                                                                                                                                                                                               
    The above exception was the direct cause of the following exception:                                                                                                                                                                                                                                                                                                                                                                            
    Traceback (most recent call last):                                                                                                                                                                                        
    File "main.py", line 7, in <module>                                                                                                                                                                                       
    from parameters import *                                                                                                                                                                                              
    File "/code/src/parameters.py", line 16, in <module>                                                                                                                                                                      
    TWILIO_SID = access_secret(GCP_PROJECT, 'TWILIO_ACCOUNT_SID', 
    os.environ['TWILIO_ACCOUNT_SID_VERSION'])                                                                                                         
    File "/code/src/utils.py", line 46, in access_secret                                                                                                                                                                      
    response = client.access_secret_version(name)                                                                                                                                                                         
    File "/usr/local/lib/python3.8/site-packages/google/cloud/secretmanager_v1
    /gapic/secret_manager_service_client.py", line 963, in access_secret_version                                                                    
    return self._inner_api_calls["access_secret_version"](                                                                                                                                                                
    File "/usr/local/lib/python3.8/site-packages/google/api_core/gapic_v1
    /method.py", line 143, in __call__                                                                                                                   
    return wrapped_func(*args, **kwargs)                                                                                                                                                                                  
    File "/usr/local/lib/python3.8/site-packages/google/api_core/retry.py", line 
    281, in retry_wrapped_func                                                                                                                   
    return retry_target(                                                                                                                                                                                                  
    File "/usr/local/lib/python3.8/site-packages/google/api_core/retry.py", line 
    184, in retry_target                                                                                                                         
    return target()                                                                                                                                                                                                       
    File "/usr/local/lib/python3.8/site-packages/google/api_core/timeout.py", 
    line 214, in func_with_timeout                                                                                                                  
    return func(*args, **kwargs)                                                                                                                                                                                          
    File "/usr/local/lib/python3.8/site-packages/google/api_core
    /grpc_helpers.py", line 59, in error_remapped_callable                                                                                                        
    six.raise_from(exceptions.from_grpc_error(exc), exc)                                                                                                                                                                  
    File "<string>", line 3, in raise_from                                                                                                                                                                                
    google.api_core.exceptions.PermissionDenied: 403 Permission denied on 
    resource project <MY GCP PROJECT NAME>.          
    

    最佳答案

    好的,原来我使用的是项目名称而不是项目 ID。

    叹。至少答案很简单。 :]

    关于python - 从 Docker 中的 Python 访问 GCP secret 管理器中的 secret - nontsop 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61552808/

    相关文章:

    python - 如何在 Windows 上使用 scapy 查看空中的所有数据包?

    bash - 了解 docker 入口点脚本

    docker - 如何使用 docker 在 Gitlab CI 中执行 gitversion?

    node.js - 在 docker : Error opening Api. yaml 中使用 pm2 运行 Node.js/Swagger 应用程序

    python - 使用 Google Storage Bucket 下载速度缓慢

    node.js - 从应用程序(IOS 或 Android)将图像上传到谷歌云存储

    python - 存储倒排索引

    python - 在构建脚本中组合 -= 和 += 修饰符

    kubernetes - 如何将数据从Pod中的容器发送到同一集群中单独Pod中的容器?

    Python、sqlalchemy 和获取具有模式的表的快速方法