使用google_application_credentials env var with github Actions Secret

发布于 2025-02-09 05:38:38 字数 368 浏览 2 评论 0原文

我的问题很简单:我想运行取决于google_application_credentials从github操作秘密正确设置的代码。

问题是Google_application_credentials期望包含通往服务帐户文件的路径,无论秘密是否包含实际的服务帐户文件内容。

最好的做法是什么?


编辑

本质上,我运行了一个NodeJS脚本,该脚本使用客户端库(PubSub,BigQuery等)连接到多个GCP资源。据我了解,如果google_application_credentials env var是正确定义的,它们最容易工作。

My problem is simple: I want to run code that depends on GOOGLE_APPLICATION_CREDENTIALS being set up correctly from a GitHub Actions secret.

The problem is that GOOGLE_APPLICATION_CREDENTIALS is expect to contain a path to a service account file, whether the secret contain the actual service account file content.

What's the best practice for that?


EDIT

Essentially I run a NodeJS script which connect to multiple GCP resources using client libraries (PubSub, BigQuery, etc). To my understanding, they can most easily work if GOOGLE_APPLICATION_CREDENTIALS env var is correctly defined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2025-02-16 05:38:38

问题是Google_application_credentials期望
是否包含通往服务帐户文件的路径,是否包含秘密
实际的服务帐户文件内容。

没有一个。环境变量必须指向一个文件,该文件是服务帐户JSON密钥的位置。您可以将内容写入工件,但这很危险。还有其他方法,但是您的问题没有提供有关您的GitHub操作或运行命令的详细信息。但是,我仍然不会使用这些方法。

正确的解决方案是使用Google Cloud Workload Identity Federation。这既是解决方案又是最佳实践解决方案。

Github提供了OAuth身份提供商。 Google支持从一个OAuth提供商到另一个OAuth提供商的凭据。

从GitHub Actions

google-github-actions/auth

The problem is that GOOGLE_APPLICATION_CREDENTIALS is expect to
contain a path to a service account file, whether the secret contain
the actual service account file content.

There isn't one. The environment variable must point to a file which is the location for a service account JSON key. You could write the contents to an artifact, but that is dangerous. There are other methods, but your question does not provide details on what your GitHub action does or the commands it runs. However, I still would not use those methods.

The correct solution is to use Google Cloud Workload Identity Federation. That is both the solution and the best practice solution.

GitHub provides an OAuth identity provider. Google supports federating credentials from one OAuth provider to another.

Enabling keyless authentication from GitHub Actions

google-github-actions/auth

静谧 2025-02-16 05:38:38

您可以使用 this github操作要登录。几乎您必须使用服务帐户的内容创建一个秘密,并将其指定为输入:

    - id: 'auth'
      name: 'Authenticate to Google Cloud'
      uses: 'google-github-actions/auth@v0'
      with:
        credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'

You can use this GitHub action to log in. Pretty much you have to create a secret with the content of the service account and specified it as input:

    - id: 'auth'
      name: 'Authenticate to Google Cloud'
      uses: 'google-github-actions/auth@v0'
      with:
        credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
怀里藏娇 2025-02-16 05:38:38

这是Python中可能的解决方法:

def get_credentials_from_env_variables():
    creds_list = [
        "TYPE",
        "PROJECT_ID",
        "PRIVATE_KEY_ID",
        "PRIVATE_KEY",
        "CLIENT_EMAIL",
        "CLIENT_ID",
        "AUTH_URI",
        "TOKEN_URI",
        "AUTH_PROVIDER_X509_CERT_URL",
        "CLIENT_X509_CERT_URL",
        "UNIVERSE_DOMAIN",
    ]

    creds_dict = {}
    for cred in creds_list:
        loaded_cred = os.getenv(cred)
        creds_dict[cred.lower()] = loaded_cred.encode().decode("unicode_escape")

    # Create a temporary file
    with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp_file:
        temp_filename = temp_file.name
        json.dump(creds_dict, temp_file)

    # Load credentials from the temporary file
    credentials = service_account.Credentials.from_service_account_file(temp_filename)

    # Remove the temporary file
    os.remove(temp_filename)

    return credentials

简而言之

Here's a possible workaround in Python:

def get_credentials_from_env_variables():
    creds_list = [
        "TYPE",
        "PROJECT_ID",
        "PRIVATE_KEY_ID",
        "PRIVATE_KEY",
        "CLIENT_EMAIL",
        "CLIENT_ID",
        "AUTH_URI",
        "TOKEN_URI",
        "AUTH_PROVIDER_X509_CERT_URL",
        "CLIENT_X509_CERT_URL",
        "UNIVERSE_DOMAIN",
    ]

    creds_dict = {}
    for cred in creds_list:
        loaded_cred = os.getenv(cred)
        creds_dict[cred.lower()] = loaded_cred.encode().decode("unicode_escape")

    # Create a temporary file
    with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp_file:
        temp_filename = temp_file.name
        json.dump(creds_dict, temp_file)

    # Load credentials from the temporary file
    credentials = service_account.Credentials.from_service_account_file(temp_filename)

    # Remove the temporary file
    os.remove(temp_filename)

    return credentials

In short, use env variables to create temp credentials in JSON

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文