【问题标题】:Disable warning when using end user credentials in GCP scripts在 GCP 脚本中使用最终用户凭据时禁用警告
【发布时间】:2022-03-11 05:41:27
【问题描述】:

我正在使用 Python 客户端库在 GCP 中运行一些脚本来执行一些临时作业。
这些是临时的,因此我认为它们应该在我的最终用户凭据而不是服务帐户上运行。

我不断看到这个警告,这让我很难调试我的脚本标准输出:

UserWarning: Your application has authenticated using end user credentials from Google Cloud SDK without a quota project. You might receive a "quota exceeded" or "API not enabled" error. We recommend you rerun `gcloud auth application-default login` and make sure a quota project is added. Or you can use service accounts instead. For more information about service accounts, see https://cloud.google.com/docs/authentication/

我执行了建议的gcloud auth application-default login,但仍然重复出现相同的警告消息。

如何禁用此警告?

【问题讨论】:

  • 我不确定我是否理解您的问题。如果您想禁用所有警告,您可以随时利用--verbosity flag 并运行带有设置为error 的标志的每个命令(例如 gcloud auth init --verbosity="error")。运行 gcloud config set core/verbosity error 将更新您的 default configuration 上的值,并且您运行的所有 gcloud 命令将默认使用该详细级别。
  • 如果您认为上面的评论没有用,请发布您的特定脚本的最低限度、可重现的示例,但不要分享任何合理的数据以及对您的脚本功能的简要说明。我建议您通过 this documentation 在使用 gcloud 命令编写脚本时提供一些有用的建议。
  • 我想我应该提到,我正在使用 Python 客户端库
  • 编辑您的帖子以包含您的脚本代码的最小可重现示例可能会有用! Python client libraries 处理基于环境变量的所有低级别身份验证,或指向具有服务帐户特定凭据的文件。你在用google-auth¶
  • @DanielOcando 我的评论怎么了?嗯

标签: google-cloud-platform


【解决方案1】:

看看the lines in the library。该消息被硬编码到名为 _warn_about_problematic_credentials 的函数中(从第 59 行开始)。

如果你去deeper into the file,你可以看到只有在没有定义配额项目时才会显示警告信息。

您可以使用以下命令将配额项目添加到您的应用程序默认凭据:

gcloud auth application-default set-quota-project <YOUR PROJECT ID>

【讨论】:

  • 为了补充这个答案,这里是set-quota-project command 的相关文档。请注意,您必须首先运行 gcloud auth application-default login 才能生成应用程序默认凭据,并且现有应用程序默认凭据必须至少分配有“serviceusage.services.use”权限。
  • 这对我有用。我接受了这一点而不是我的回答,因为这比我的不那么老套。
【解决方案2】:

当我阅读here 时,我通过使用 Python 忽略警告选项设法禁用了此功能。

python3 -W ignore my_script.py

如果您想更精确并且只禁用某些警告,您可以尝试this

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2019-07-17
    • 2020-11-09
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多