Skip to content

Commit cad95c9

Browse files
authored
fix: properly set token_expiry_is_time_of_expiration and mask access token when logging (#637)
1 parent 9ec30dc commit cad95c9

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,7 @@ def create_oauth_authenticator(
28012801
).eval(config),
28022802
scopes=model.scopes,
28032803
token_expiry_date_format=model.token_expiry_date_format,
2804+
token_expiry_is_time_of_expiration=bool(model.token_expiry_date_format),
28042805
message_repository=self._message_repository,
28052806
refresh_token_error_status_codes=model.refresh_token_updater.refresh_token_error_status_codes,
28062807
refresh_token_error_key=model.refresh_token_updater.refresh_token_error_key,

airbyte_cdk/sources/streams/http/requests_native_auth/abstract_oauth.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,26 @@ def _make_handled_request(self) -> Any:
217217
data=self.build_refresh_request_body(),
218218
headers=self.build_refresh_request_headers(),
219219
)
220-
# log the response even if the request failed for troubleshooting purposes
220+
221+
if not response.ok:
222+
# log the response even if the request failed for troubleshooting purposes
223+
self._log_response(response)
224+
response.raise_for_status()
225+
226+
response_json = response.json()
227+
228+
try:
229+
# extract the access token and add to secrets to avoid logging the raw value
230+
access_key = self._extract_access_token(response_json)
231+
if access_key:
232+
add_to_secrets(access_key)
233+
except ResponseKeysMaxRecurtionReached as e:
234+
# could not find the access token in the response, so do nothing
235+
pass
236+
221237
self._log_response(response)
222-
response.raise_for_status()
223-
return response.json()
238+
239+
return response_json
224240
except requests.exceptions.RequestException as e:
225241
if e.response is not None:
226242
if e.response.status_code == 429 or e.response.status_code >= 500:
@@ -240,9 +256,7 @@ def _ensure_access_token_in_response(self, response_data: Mapping[str, Any]) ->
240256
241257
This method attempts to extract the access token from the provided response data.
242258
If the access token is not found, it raises an exception indicating that the token
243-
refresh API response was missing the access token. If the access token is found,
244-
it adds the token to the list of secrets to ensure it is replaced before logging
245-
the response.
259+
refresh API response was missing the access token.
246260
247261
Args:
248262
response_data (Mapping[str, Any]): The response data from which to extract the access token.
@@ -257,9 +271,6 @@ def _ensure_access_token_in_response(self, response_data: Mapping[str, Any]) ->
257271
raise Exception(
258272
f"Token refresh API response was missing access token {self.get_access_token_name()}"
259273
)
260-
# Add the access token to the list of secrets so it is replaced before logging the response
261-
# An argument could be made to remove the prevous access key from the list of secrets, but unmasking values seems like a security incident waiting to happen...
262-
add_to_secrets(access_key)
263274
except ResponseKeysMaxRecurtionReached as e:
264275
raise e
265276

0 commit comments

Comments
 (0)