Skip to content

Commit f7ec619

Browse files
committed
Remove mutual exclusivity between PAT and API+App key auth
1 parent fe5c4e4 commit f7ec619

File tree

3 files changed

+17
-61
lines changed

3 files changed

+17
-61
lines changed

.generator/src/generator/templates/api_client.j2

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -893,16 +893,7 @@ class Endpoint:
893893
self.api_client.configuration.delegated_auth_org_uuid is not None
894894
)
895895

896-
# Check if bearer token (PAT) auth is configured
897-
has_bearer_token = self.api_client.configuration.access_token is not None and "bearerAuth" in self.settings["auth"]
898-
899-
if has_bearer_token:
900-
# Bearer token authentication: send ONLY Authorization: Bearer header.
901-
# This is a separate auth path — no API key or app key headers.
902-
bearer_setting = self.api_client.configuration.auth_settings().get("bearerAuth")
903-
if bearer_setting:
904-
headers[bearer_setting["key"]] = bearer_setting["value"]
905-
elif has_app_key_auth and has_delegated_auth:
896+
if has_app_key_auth and has_delegated_auth:
906897
# Use delegated token authentication
907898
self.api_client.use_delegated_token_auth(headers)
908899
else:

src/datadog_api_client/api_client.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -902,16 +902,7 @@ def update_params_for_auth(self, headers, queries) -> None:
902902
and self.api_client.configuration.delegated_auth_org_uuid is not None
903903
)
904904

905-
# Check if bearer token (PAT) auth is configured
906-
has_bearer_token = self.api_client.configuration.access_token is not None and "bearerAuth" in self.settings["auth"]
907-
908-
if has_bearer_token:
909-
# Bearer token authentication: send ONLY Authorization: Bearer header.
910-
# This is a separate auth path — no API key or app key headers.
911-
bearer_setting = self.api_client.configuration.auth_settings().get("bearerAuth")
912-
if bearer_setting:
913-
headers[bearer_setting["key"]] = bearer_setting["value"]
914-
elif has_app_key_auth and has_delegated_auth:
905+
if has_app_key_auth and has_delegated_auth:
915906
# Use delegated token authentication
916907
self.api_client.use_delegated_token_auth(headers)
917908
else:

tests/test_pat_auth.py

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_auth_settings_without_bearer(self):
5454

5555

5656
class TestUpdateParamsForAuthWithBearerToken:
57-
"""Test that update_params_for_auth uses bearer token correctly."""
57+
"""Test that update_params_for_auth sends all configured auth headers."""
5858

5959
def _make_endpoint(self, config, auth_schemes):
6060
"""Helper to create an Endpoint with given auth schemes."""
@@ -73,8 +73,8 @@ def _make_endpoint(self, config, auth_schemes):
7373
api_client=api_client,
7474
)
7575

76-
def test_bearer_sends_only_authorization_header(self):
77-
"""When access_token is set and bearerAuth is in auth, only Authorization: Bearer is sent."""
76+
def test_all_auth_headers_sent_when_all_configured(self):
77+
"""When all credentials are set, all auth headers are sent."""
7878
config = Configuration(
7979
api_key={"apiKeyAuth": "test-api-key", "appKeyAuth": "test-app-key"},
8080
access_token="ddpat_test_pat",
@@ -85,11 +85,11 @@ def test_bearer_sends_only_authorization_header(self):
8585
endpoint.update_params_for_auth(headers, queries)
8686

8787
assert headers["Authorization"] == "Bearer ddpat_test_pat"
88-
assert "DD-API-KEY" not in headers
89-
assert "DD-APPLICATION-KEY" not in headers
88+
assert headers["DD-API-KEY"] == "test-api-key"
89+
assert headers["DD-APPLICATION-KEY"] == "test-app-key"
9090

91-
def test_bearer_without_api_keys(self):
92-
"""Bearer token works even without any API keys configured."""
91+
def test_bearer_only(self):
92+
"""Bearer token works without any API keys configured."""
9393
config = Configuration(access_token="ddpat_test_pat")
9494
endpoint = self._make_endpoint(config, ["apiKeyAuth", "appKeyAuth", "bearerAuth"])
9595
headers = {}
@@ -100,21 +100,8 @@ def test_bearer_without_api_keys(self):
100100
assert "DD-API-KEY" not in headers
101101
assert "DD-APPLICATION-KEY" not in headers
102102

103-
def test_no_bearer_when_not_in_endpoint_auth(self):
104-
"""access_token set but endpoint doesn't declare bearerAuth — uses regular auth."""
105-
config = Configuration(
106-
api_key={"apiKeyAuth": "test-api-key", "appKeyAuth": "test-app-key"},
107-
access_token="ddpat_test_pat",
108-
)
109-
endpoint = self._make_endpoint(config, ["apiKeyAuth", "appKeyAuth"])
110-
headers = {}
111-
queries = []
112-
endpoint.update_params_for_auth(headers, queries)
113-
114-
assert headers["DD-API-KEY"] == "test-api-key"
115-
assert headers["DD-APPLICATION-KEY"] == "test-app-key"
116-
117-
def test_regular_auth_without_bearer(self):
103+
def test_api_keys_only(self):
104+
"""Regular auth works without bearer token."""
118105
config = Configuration(
119106
api_key={"apiKeyAuth": "test-api-key", "appKeyAuth": "test-app-key"},
120107
)
@@ -127,30 +114,17 @@ def test_regular_auth_without_bearer(self):
127114
assert headers["DD-APPLICATION-KEY"] == "test-app-key"
128115
assert "Authorization" not in headers
129116

130-
def test_bearer_takes_priority_over_delegated_auth(self):
131-
"""When both bearer token and delegated auth are configured, bearer wins."""
132-
133-
class MockProvider(DelegatedTokenProvider):
134-
def authenticate(self, config, api_config):
135-
return DelegatedTokenCredentials(
136-
org_uuid="test-org",
137-
delegated_token="delegated-token-123",
138-
delegated_proof="proof",
139-
expiration=datetime.now() + timedelta(minutes=10),
140-
)
141-
117+
def test_bearer_not_sent_when_not_in_endpoint_auth(self):
118+
"""access_token set but endpoint doesn't declare bearerAuth."""
142119
config = Configuration(
143120
api_key={"apiKeyAuth": "test-api-key", "appKeyAuth": "test-app-key"},
144121
access_token="ddpat_test_pat",
145-
delegated_auth_provider=MockProvider(),
146-
delegated_auth_org_uuid="test-org",
147122
)
148-
endpoint = self._make_endpoint(config, ["apiKeyAuth", "appKeyAuth", "bearerAuth"])
123+
endpoint = self._make_endpoint(config, ["apiKeyAuth", "appKeyAuth"])
149124
headers = {}
150125
queries = []
151126
endpoint.update_params_for_auth(headers, queries)
152127

153-
# Bearer token takes priority
154-
assert headers["Authorization"] == "Bearer ddpat_test_pat"
155-
assert "DD-API-KEY" not in headers
156-
assert "DD-APPLICATION-KEY" not in headers
128+
assert headers["DD-API-KEY"] == "test-api-key"
129+
assert headers["DD-APPLICATION-KEY"] == "test-app-key"
130+
assert "Authorization" not in headers

0 commit comments

Comments
 (0)