从 Docker 中的 Python 访问 GCP Secret Manager 中的机密 - nontsop 权限被拒绝

2024-04-11

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

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

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

这是我已经尝试过的:

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

作为参考,我一直遵循以下位置的说明https://cloud.google.com/secret-manager/docs/reference/libraries#client-libraries-install-python 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 中密钥的名称。

完整的错误跟踪:

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。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Docker 中的 Python 访问 GCP Secret Manager 中的机密 - nontsop 权限被拒绝 的相关文章

随机推荐