auth: `impersonated_credentials` loses `subject` on copy, so `with_scopes()` and `with_quota_project()` turn off domain wide delegation

未关闭 适合新手
#18,428 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
88/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

从 impersonated_credentials.Credentials._make_copy 开始,并将其与 google/oauth2/service_account.py 中保留 subject 的实现进行比较。运行 main.py 复现代码,以观察基础凭据和复制凭据的端点。当通过 with_scopes()、with_quota_project() 和 with_trust_boundary() 创建的副本保留 subject,并使用与基础凭据相同的委派流程时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

  • I determined this is the correct repository in which to report this bug.

Context

impersonated_credentials.Credentials takes a subject so it can act as a Workspace user through domain wide delegation. _make_copy rebuilds the credential without it, so with_scopes(), with_quota_project() and the deprecated with_trust_boundary() all hand back a copy whose subject is None. Deriving a narrower scoped credential from one base credential is the normal way to use this, and a quota project is common for Workspace APIs.

Nothing raises. The copy refreshes fine, it just authenticates as somebody else: it posts to :generateAccessToken instead of :signJwt plus the OAuth2 token endpoint, so the token belongs to the service account and not to the user. Whatever you call next runs as the service account instead of the user. The copy also starts building a Regional Access Boundary lookup URL again, which #17763 skips while a subject is set. google/oauth2/service_account.py carries the same subject field and its _make_copy does pass it on.

Expected Behavior: the copy keeps subject, the same way it keeps target_principal, target_scopes, delegates, lifetime, quota_project_id, iam_endpoint_override and trust_boundary.

Actual Behavior: subject is None on the copy, and the copy runs the plain service account impersonation flow.

API client name and version

google-auth 2.58.0, packages/google-auth on main at 47b2845.

Reproduction steps: code

file: main.py

import json

from google.auth import credentials as ga_credentials
from google.auth import impersonated_credentials


class FakeSource(ga_credentials.Credentials):
    """Stands in for the source credential so this runs without a key file."""

    def refresh(self, request):
        self.token = "source-token"


class FakeResponse:
    def __init__(self, data):
        self.status = 200
        self.data = json.dumps(data).encode("utf-8")


class FakeRequest:
    """Records the endpoints refresh() posts to."""

    def __init__(self):
        self.urls = []

    def __call__(self, url=None, method=None, headers=None, body=None, **kwargs):
        self.urls.append(url)
        if ":signJwt" in url:
            return FakeResponse({"signedJwt": "a.b.c"})
        if url.endswith("oauth2.googleapis.com/token"):
            return FakeResponse({"access_token": "user-token", "expires_in": 3600})
        return FakeResponse(
            {"accessToken": "sa-token", "expireTime": "2099-01-01T00:00:00Z"}
        )


base = impersonated_credentials.Credentials(
    source_credentials=FakeSource(),
    target_principal="dwd-sa@my-project.iam.gserviceaccount.com",
    target_scopes=["https://www.googleapis.com/auth/drive.readonly"],
    subject="alice@example.com",
)
scoped = base.with_scopes(["https://www.googleapis.com/auth/drive.metadata.readonly"])

for label, cred in [("base", base), ("scoped", scoped)]:
    request = FakeRequest()
    cred.refresh(request)
    print("{} subject={!r}".format(label, cred._subject))
    for url in request.urls:
        print("  POST", url)

Reproduction steps: actual results

base subject='alice@example.com'
  POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/dwd-sa@my-project.iam.gserviceaccount.com:signJwt
  POST https://oauth2.googleapis.com/token
scoped subject=None
  POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/dwd-sa@my-project.iam.gserviceaccount.com:generateAccessToken

Reproduction steps: expected results

scoped keeps the subject and makes the same two posts as base:

scoped subject='alice@example.com'
  POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/dwd-sa@my-project.iam.gserviceaccount.com:signJwt
  POST https://oauth2.googleapis.com/token

OS & version + platform

macOS 26.6.2 on arm64

Python environment

Python 3.13.15

主要语言
Python
星标
5.4k
派生
1.8k
平均合并
2 天 22 小时
30 天内合并 PR
102

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

googleapis/google-cloud-python 的其他 Issue

查看 googleapis/google-cloud-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。