【问题标题】:Permission denied for Cloud Function to decrypt with KMS keyCloud Function 使用 KMS 密钥解密的权限被拒绝
【发布时间】:2020-03-31 18:51:15
【问题描述】:

我有一个 Python 云函数,它使用 KMS 密钥解密环境中其他服务的一些身份验证令牌,如 https://dev.to/googlecloud/using-secrets-in-google-cloud-functions-5aem

每当我运行我的函数时,我都会收到 403 Permission Denied。当我在我的计算机上本地调用该函数时,它工作正常。我尝试将“Cloud KMS CryptoKey Decrypter”角色添加到默认的 Compute Engine 服务帐户,但没有成功。

还有其他想法吗?

编辑:这里有一些代码显示我在做什么。环境变量存储在environment.yaml 文件中,我在gcloud functions deploy 时指向该文件

def decrypt_secret(key: str, secret: str):
    kms_client = kms.KeyManagementServiceClient()
    decrypted = kms_client.decrypt(key, base64.b64decode(secret))
    return decrypted.plaintext.decode("ascii")

def do_kms_stuff():
    key = os.environ["KMS_RESOURCE_NAME"]
    session = boto3.Session(
        profile_name="my-profile",
        aws_access_key_id=decrypt_secret(
            key, os.environ["AWS_ACCESS_KEY_ID_ENCRYPTED"]
        ),
        aws_secret_access_key=decrypt_secret(
            key, os.environ["AWS_SECRET_ACCESS_KEY_ENCRYPTED"]
        ),
    )
    # ...

这是来自 Cloud Functions 控制台的错误:

File "<string>", line 3, in raise_from: google.api_core.exceptions.PermissionDenied: 403 Permission 'cloudkms.cryptoKeyVersions.useToDecrypt' denied on resource 'projects/my-project/locations/my-location1/keyRings/my-keyring/cryptoKeys/my-key' (or it may not exist). at error_remapped_callable (/env/local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py:59) at func_with_timeout (/env/local/lib/python3.7/site-packages/google/api_core/timeout.py:214) at retry_target (/env/local/lib/python3.7/site-packages/google/api_core/retry.py:182) at retry_wrapped_func (/env/local/lib/python3.7/site-packages/google/api_core/retry.py:277) at
__call__ (/env/local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py:143) at decrypt (/env/local/lib/python3.7/site-packages/google/cloud/kms_v1/gapic/key_management_service_client.py:1816) at decrypt_secret (/user_code/kms_stuff.py:17) at do_kms_stuff (/user_code/kms_stuff.py:48) at my_cloud_function (/user_code/main.py:46) at call_user_function (/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py:214) at invoke_user_function (/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py:217) at run_background_function (/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py:383)

【问题讨论】:

  • 请编辑问题以显示未按您期望的方式工作的代码,以及您为调用它所做的工作。如果您使用计算机上的某个工具调用它,请确保您已启用对该函数的公共访问。
  • 这样更好吗?
  • 当您执行gcloud kms keys list --location &lt;your location&gt; 时您的密钥是否存在?
  • 两个问题:(1)Cloud Functions在appspot服务账户下运行(cloud.google.com/functions/docs/securing/function-identity)不是计算引擎默认的; (2) 除非您故意混淆,否则密钥位置显示不正确:projects/my-project/locations/my-location1/keyRings/my-keyring/cryptoKeys/my-key
  • 我已经混淆了,但我看不出你的密钥和我的密钥之间的区别。无论如何,我认为这不是问题,因为当我在我的开发机器上运行该功能时它可以工作。我认为使用 appspot 服务帐户会有所作为

标签: google-cloud-platform google-cloud-functions google-cloud-iam google-cloud-kms


【解决方案1】:

正如@DazWilkin 和@pessolato 提到的,问题是我使用了错误的服务帐户。一旦我更改为使用默认 AppSpot 帐户,一切都会顺利进行。

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 2016-10-15
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2013-07-23
    相关资源
    最近更新 更多