mTLS host rewrite skips Agent Platform sandboxEnvironments calls ('sandbox' in url matches the path), causing 401 under Agent Identity; base_url workaround then doubles to mtls.mtls
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 78/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- api, authentication, networking
調査の方向性
Start in google/genai/_api_client.py at the BaseApiClient request, async, streaming, upload, and download URL-rewrite paths. Run the no-network reproduction in the issue and inspect how the existing urlparse/urlunparse imports can support hostname-only handling. Done means sandboxEnvironments URLs use the regional mTLS host and already-mTLS operation URLs remain unchanged across all five paths.
索引モデルが issue の本文から書いたものです。
説明
Environment
- google-genai 2.23.0 (reproduced unchanged on 2.26.0 and on
mainat 41b1ea6) - google-cloud-aiplatform 2.2.0 (
agentplatformpackage, which builds ongoogle.genai._api_client.BaseApiClient) - Python 3.13
- Runtime: Gemini Enterprise Agent Platform / Agent Runtime, BYOC container,
identity_type=AGENT_IDENTITY, us-central1. The client cert config is present at/var/run/secrets/workload-spiffe-credentials/credentials.json,mtls.should_use_client_cert()andmtls.has_default_client_cert_source()both return true, and the client uses the mTLSAuthorizedSessionpath added in #2172 (a95d08a).
Description
When mTLS is active, BaseApiClient rewrites the request host like this (google/genai/_api_client.py on main, lines ~1497, 1575, 1648, 2057 and 2431: sync, async, streaming, upload and download paths):
if self._authorized_session._is_mtls and 'googleapis.com' in url:
if 'sandbox' in url:
url = url.replace('sandbox.googleapis.com', 'mtls.sandbox.googleapis.com')
else:
url = url.replace('googleapis.com', 'mtls.googleapis.com')
The 'sandbox' in url branch looks meant for the *.sandbox.googleapis.com staging hosts (the tests use e.g. us-central1-autopush-aiplatform.sandbox.googleapis.com). But it tests the whole URL, path included. That causes two problems:
-
Agent Platform sandbox calls skip the mTLS host. Every sandbox API path has
sandboxin it (.../reasoningEngines/<id>/sandboxEnvironments..., used bysandboxes.create/get/execute/delete). So these calls take the staging branch, thereplacefinds nothing to match, and the request goes to the plain{region}-aiplatform.googleapis.comhost with a certificate-bound Agent Identity token. The server rejects it with:401 UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.This happens on every call for the life of the process. The same calls work with ordinary user ADC (no cert, no mTLS). Other calls from the same client (e.g. to the reasoning engine itself) are rewritten correctly and work.
-
The rewrite isn't idempotent, so
base_urlcan't work around it. If the caller setshttp_options.base_urltohttps://{region}-aiplatform.mtls.googleapis.com/, sandbox calls do go through. But any call whose path lackssandbox, such as polling the.../operations/<id>returned bysandboxes.create, takes theelsebranch and becomes{region}-aiplatform.mtls.mtls.googleapis.com. That fails withSSLError: [SSL: CERTIFICATE_VERIFY_FAILED] ... Hostname mismatch. No singlebase_urlworks for both kinds of call.
Reproduction (no credentials, no network)
The script below patches the two mtls checks to report a client cert. It then swaps in a fake mTLS session that records the final URL instead of sending the request.
from google.oauth2.credentials import Credentials
from google.auth.transport import mtls
from google.genai import _api_client, version
# Pretend we're on Agent Runtime with an Agent Identity client cert.
mtls.should_use_client_cert = lambda: True
mtls.has_default_client_cert_source = lambda: True
class Sent(Exception):
pass
class FakeMtlsSession:
"""Stands in for google.auth AuthorizedSession after configure_mtls_channel()."""
_is_mtls = True
def request(self, method, url, **kwargs):
raise Sent(url)
def final_url(path, base_url=None):
client = _api_client.BaseApiClient(
vertexai=True,
project='my-project',
location='us-central1',
credentials=Credentials(token='placeholder'), # never sent anywhere
http_options={'base_url': base_url} if base_url else None,
)
client._authorized_session = FakeMtlsSession()
try:
client.request('get', path, {})
except Sent as e:
return e.args[0]
print('google-genai', version.__version__)
engine = 'reasoningEngines/123'
mtls_host = 'https://us-central1-aiplatform.mtls.googleapis.com/'
cases = [
('(a) sandbox call, default base_url', f'{engine}/sandboxEnvironments', None),
(' control: non-sandbox call, default base_url', f'{engine}', None),
('(b) operation poll, base_url = mTLS host', f'{engine}/operations/456', mtls_host),
]
for label, path, base in cases:
print(f'{label}\n -> {final_url(path, base)}')
Output (2.23.0 and 2.26.0 are identical):
google-genai 2.26.0
(a) sandbox call, default base_url
-> https://us-central1-aiplatform.googleapis.com/v1beta1/projects/my-project/locations/us-central1/reasoningEngines/123/sandboxEnvironments
control: non-sandbox call, default base_url
-> https://us-central1-aiplatform.mtls.googleapis.com/v1beta1/projects/my-project/locations/us-central1/reasoningEngines/123
(b) operation poll, base_url = mTLS host
-> https://us-central1-aiplatform.mtls.mtls.googleapis.com/v1beta1/projects/my-project/locations/us-central1/reasoningEngines/123/operations/456
Expected
- (a)
https://us-central1-aiplatform.mtls.googleapis.com/.../sandboxEnvironments - (b)
https://us-central1-aiplatform.mtls.googleapis.com/.../operations/456, i.e. a host that is already mTLS is left alone.
Actual
- (a) stays on the plain host, which returns a 401 for a bound token.
- (b) gets
mtls.added a second time, which fails TLS hostname verification.
Suggested fix
Base the decision on the hostname, not the full URL string, and skip hosts that are already mTLS. One helper used by all five call sites:
def _to_mtls_url(url: str) -> str:
parsed = urlparse(url)
host = parsed.netloc
if '.mtls.' in host:
return url
if 'sandbox.googleapis.com' in host:
host = host.replace('sandbox.googleapis.com', 'mtls.sandbox.googleapis.com')
else:
host = host.replace('googleapis.com', 'mtls.googleapis.com')
return urlunparse(parsed._replace(netloc=host))
The existing 'googleapis.com' in url check at each call site still gates the call. urlparse/urlunparse are already imported in _api_client.py.
Workaround
Use two clients: one whose base_url is the regional *.mtls.googleapis.com host for sandboxEnvironments calls, and a second with the default host for polling the long-running operation that sandboxes.create returns. After making this change, creates under Agent Identity were accepted and returned an operation. That confirms the host is the cause of the 401s, not IAM.
Related: #1956, #2172.
- 主要言語
- Python
- スター
- 4k
- フォーク
- 1k
- 平均マージ
- 1日 17時間
- マージ済み PR(30日)
- 52
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
googleapis/python-genai のほかの issue
-
priority: p2 status:awaiting user response type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
googleapis/python-genai#3051 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: Unsubscripted typing.List and typing.Dict crash convert_if_exist_pydantic_model in AFC対応中かも @chauvuusvn が 3 日前に担当しました。 オープンpriority: p2 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
googleapis/python-genai#3044 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Name collision in google.genai.interactions: triggers.Interaction shadows response model Interaction in static type checkers対応中かも @Venkaiahbabuneelam が 8 日前に担当しました。 オープンpriority: p2 type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
googleapis/python-genai#3013 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 74/100
googleapis/python-genai#3056 ·
メンテナーはふだん 1 日以内に返信
-
Vertex batch embeddings (gemini-embedding-001): no way to attach billing labels; job and row labels never reach Cloud Billing対応中かも @Venkaiahbabuneelam が 1 日前に担当しました。 オープンpriority: p3 type: question
googleapis/python-genai#3053 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
googleapis/python-genai の issue をすべて見る
似ている issue
-
needs-human needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
gke-labs/kube-agents#2400 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Device Details tables: FS/SF columns contradict each other (nfet_01v8 Vt row, pfet_01v8 Idsat row)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
google/skywater-pdk#450 ·
-
Drained trajectory arrays are overwritten when the sequence buffer is reused対応中かも @sylvesterkaczmarek が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
google-deepmind/bsuite#56 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
LearningCircuit/local-deep-research#7206 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
chingu-voyages/V62-tier3-team-33#285 ·
メンテナーはふだん 1 日以内に返信