Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output response body when request fails #93

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions id/_internal/oidc/ambient.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def detect_github(audience: str) -> Optional[str]:
resp.raise_for_status()
except requests.HTTPError as http_error:
raise AmbientCredentialError(
f"GitHub: OIDC token request failed (code={resp.status_code})"
f"GitHub: OIDC token request failed (code={resp.status_code}, "
f"body={resp.content.decode()!r})"
) from http_error
except requests.Timeout:
raise AmbientCredentialError("GitHub: OIDC token request timed out")
Expand Down Expand Up @@ -128,7 +129,8 @@ def detect_gcp(audience: str) -> Optional[str]:
resp.raise_for_status()
except requests.HTTPError as http_error:
raise AmbientCredentialError(
f"GCP: access token request failed (code={resp.status_code})"
f"GCP: access token request failed (code={resp.status_code}, "
f"body={resp.content.decode()!r})"
) from http_error
except requests.Timeout:
raise AmbientCredentialError("GCP: access token request timed out")
Expand All @@ -152,7 +154,8 @@ def detect_gcp(audience: str) -> Optional[str]:
resp.raise_for_status()
except requests.HTTPError as http_error:
raise AmbientCredentialError(
f"GCP: OIDC token request failed (code={resp.status_code})"
f"GCP: OIDC token request failed (code={resp.status_code}, "
f"body={resp.content.decode()!r})"
) from http_error
except requests.Timeout:
raise AmbientCredentialError("GCP: OIDC token request timed out")
Expand Down Expand Up @@ -195,7 +198,8 @@ def detect_gcp(audience: str) -> Optional[str]:
resp.raise_for_status()
except requests.HTTPError as http_error:
raise AmbientCredentialError(
f"GCP: OIDC token request failed (code={resp.status_code})"
f"GCP: OIDC token request failed (code={resp.status_code}, "
f"body={resp.content.decode()!r})"
) from http_error
except requests.Timeout:
raise AmbientCredentialError("GCP: OIDC token request timed out")
Expand Down
30 changes: 22 additions & 8 deletions test/unit/internal/oidc/test_ambient.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,19 @@ def test_detect_github_request_fails(monkeypatch):
monkeypatch.setenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN", "faketoken")
monkeypatch.setenv("ACTIONS_ID_TOKEN_REQUEST_URL", "fakeurl")

resp = pretend.stub(raise_for_status=pretend.raiser(HTTPError), status_code=999)
resp = pretend.stub(
raise_for_status=pretend.raiser(HTTPError),
status_code=999,
content=b"something",
)
requests = pretend.stub(
get=pretend.call_recorder(lambda url, **kw: resp), HTTPError=HTTPError
)
monkeypatch.setattr(ambient, "requests", requests)

with pytest.raises(
ambient.AmbientCredentialError,
match=r"GitHub: OIDC token request failed \(code=999\)",
match=r"GitHub: OIDC token request failed \(code=999, body='something'\)",
):
ambient.detect_github("some-audience")
assert requests.get.calls == [
Expand Down Expand Up @@ -198,15 +202,19 @@ def test_gcp_impersonation_access_token_request_fail(monkeypatch):
logger = pretend.stub(debug=pretend.call_recorder(lambda s: None))
monkeypatch.setattr(ambient, "logger", logger)

resp = pretend.stub(raise_for_status=pretend.raiser(HTTPError), status_code=999)
resp = pretend.stub(
raise_for_status=pretend.raiser(HTTPError),
status_code=999,
content=b"something",
)
requests = pretend.stub(
get=pretend.call_recorder(lambda url, **kw: resp), HTTPError=HTTPError
)
monkeypatch.setattr(ambient, "requests", requests)

with pytest.raises(
ambient.AmbientCredentialError,
match=r"GCP: access token request failed \(code=999\)",
match=r"GCP: access token request failed \(code=999, body='something'\)",
):
ambient.detect_gcp("some-audience")

Expand Down Expand Up @@ -284,7 +292,9 @@ def test_gcp_impersonation_identity_token_request_fail(monkeypatch):
raise_for_status=lambda: None, json=lambda: {"access_token": access_token}
)
post_resp = pretend.stub(
raise_for_status=pretend.raiser(HTTPError), status_code=999
raise_for_status=pretend.raiser(HTTPError),
status_code=999,
content=b"something",
)
requests = pretend.stub(
get=pretend.call_recorder(lambda url, **kw: get_resp),
Expand All @@ -295,7 +305,7 @@ def test_gcp_impersonation_identity_token_request_fail(monkeypatch):

with pytest.raises(
ambient.AmbientCredentialError,
match=r"GCP: OIDC token request failed \(code=999\)",
match=r"GCP: OIDC token request failed \(code=999, body='something'\)",
):
ambient.detect_gcp("some-audience")

Expand Down Expand Up @@ -457,15 +467,19 @@ def test_detect_gcp_request_fails(monkeypatch):
)
monkeypatch.setitem(ambient.__builtins__, "open", lambda fn: stub_file) # type: ignore

resp = pretend.stub(raise_for_status=pretend.raiser(HTTPError), status_code=999)
resp = pretend.stub(
raise_for_status=pretend.raiser(HTTPError),
status_code=999,
content=b"something",
)
requests = pretend.stub(
get=pretend.call_recorder(lambda url, **kw: resp), HTTPError=HTTPError
)
monkeypatch.setattr(ambient, "requests", requests)

with pytest.raises(
ambient.AmbientCredentialError,
match=r"GCP: OIDC token request failed \(code=999\)",
match=r"GCP: OIDC token request failed \(code=999, body='something'\)",
):
ambient.detect_gcp("some-audience")
assert requests.get.calls == [
Expand Down