-
Notifications
You must be signed in to change notification settings - Fork 0
pyJWT + urllib instrumentations #43
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b803ab
initial pyJWT instrumentation
sohankshirsagar 3303afe
urllib instrumentation + span stauts error updates
sohankshirsagar 63b20fa
Fix HTTP 404 error handling in urllib instrumentation REPLAY mode
sohankshirsagar 570059a
Fix HTTP redirect handling in urllib instrumentation REPLAY mode
sohankshirsagar 9368a57
Refactor urllib instrumentation to reduce code duplication
sohankshirsagar 83ca088
more tests
sohankshirsagar 54c0045
fix failing urllib3 test
sohankshirsagar 39b4bef
update readme
sohankshirsagar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # BUGBOT Notes | ||
|
|
||
| ## Instrumentation Guidelines | ||
|
|
||
| - When adding a new instrumentation, the README must be updated to document the new instrumentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """PyJWT instrumentation for REPLAY mode.""" | ||
|
|
||
| from .instrumentation import PyJWTInstrumentation | ||
|
|
||
| __all__ = ["PyJWTInstrumentation"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """PyJWT instrumentation for REPLAY mode. | ||
|
|
||
| Patches PyJWT to disable all verification during test replay: | ||
| 1. _merge_options - returns all verification options as False | ||
| 2. _verify_signature - no-op (defense in depth) | ||
| 3. _validate_claims - no-op (defense in depth) | ||
|
|
||
| Only active in REPLAY mode. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from types import ModuleType | ||
|
|
||
| from ...core.types import TuskDriftMode | ||
| from ..base import InstrumentationBase | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PyJWTInstrumentation(InstrumentationBase): | ||
| """Patches PyJWT to disable verification in REPLAY mode.""" | ||
|
|
||
| def __init__(self, mode: TuskDriftMode = TuskDriftMode.DISABLED, enabled: bool = True) -> None: | ||
| self.mode = mode | ||
| should_enable = enabled and mode == TuskDriftMode.REPLAY | ||
|
|
||
| super().__init__( | ||
| name="PyJWTInstrumentation", | ||
| module_name="jwt", | ||
| supported_versions="*", | ||
| enabled=should_enable, | ||
| ) | ||
|
|
||
| def patch(self, module: ModuleType) -> None: | ||
| if self.mode != TuskDriftMode.REPLAY: | ||
| return | ||
|
|
||
| self._patch_merge_options() | ||
| self._patch_signature_verification() | ||
| self._patch_claim_validation() | ||
| logger.debug("[PyJWTInstrumentation] All patches applied") | ||
|
|
||
| def _patch_signature_verification(self) -> None: | ||
| """No-op signature verification.""" | ||
| try: | ||
| from jwt import api_jws | ||
|
|
||
| def patched_verify_signature(self, *args, **kwargs): | ||
| logger.debug("[PyJWTInstrumentation] _verify_signature called - skipping verification") | ||
| return None | ||
|
|
||
| api_jws.PyJWS._verify_signature = patched_verify_signature | ||
| logger.debug("[PyJWTInstrumentation] Patched PyJWS._verify_signature") | ||
| except Exception as e: | ||
| logger.warning(f"[PyJWTInstrumentation] Failed to patch _verify_signature: {e}") | ||
|
|
||
| def _patch_claim_validation(self) -> None: | ||
| """No-op claim validation.""" | ||
| try: | ||
| from jwt import api_jwt | ||
|
|
||
| def patched_validate_claims(self, *args, **kwargs): | ||
| logger.debug("[PyJWTInstrumentation] _validate_claims called - skipping validation") | ||
| return None | ||
|
|
||
| api_jwt.PyJWT._validate_claims = patched_validate_claims | ||
| logger.debug("[PyJWTInstrumentation] Patched PyJWT._validate_claims") | ||
| except Exception as e: | ||
| logger.warning(f"[PyJWTInstrumentation] Failed to patch _validate_claims: {e}") | ||
|
|
||
| def _patch_merge_options(self) -> None: | ||
| """Patch _merge_options to always return disabled verification options.""" | ||
| try: | ||
| from jwt import api_jwt | ||
|
|
||
| disabled_options = { | ||
| "verify_signature": False, | ||
| "verify_exp": False, | ||
| "verify_nbf": False, | ||
| "verify_iat": False, | ||
| "verify_aud": False, | ||
| "verify_iss": False, | ||
| "verify_sub": False, | ||
| "verify_jti": False, | ||
| "require": [], | ||
| "strict_aud": False, | ||
| } | ||
|
|
||
| def patched_merge_options(self, options=None): | ||
| logger.debug("[PyJWTInstrumentation] _merge_options called - returning disabled options") | ||
| return disabled_options | ||
|
|
||
| api_jwt.PyJWT._merge_options = patched_merge_options | ||
| logger.debug("[PyJWTInstrumentation] Patched PyJWT._merge_options") | ||
| except Exception as e: | ||
| logger.warning(f"[PyJWTInstrumentation] Failed to patch _merge_options: {e}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """urllib.request instrumentation module.""" | ||
|
|
||
| from .instrumentation import UrllibInstrumentation | ||
|
|
||
| __all__ = ["UrllibInstrumentation"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| version: 1 | ||
|
|
||
| service: | ||
| id: "urllib-e2e-test-id" | ||
| name: "urllib-e2e-test" | ||
| port: 8000 | ||
| start: | ||
| command: "python src/app.py" | ||
| readiness_check: | ||
| command: "curl -f http://localhost:8000/health" | ||
| timeout: 45s | ||
| interval: 5s | ||
|
|
||
| tusk_api: | ||
| url: "http://localhost:8000" | ||
|
|
||
| test_execution: | ||
| concurrent_limit: 10 | ||
| batch_size: 10 | ||
| timeout: 30s | ||
|
|
||
| recording: | ||
| sampling_rate: 1.0 | ||
| export_spans: false | ||
|
|
||
| replay: | ||
| enable_telemetry: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| FROM python-e2e-base:latest | ||
|
|
||
| # Copy SDK source for editable install | ||
| COPY . /sdk | ||
|
|
||
| # Copy test files | ||
| COPY drift/instrumentation/urllib/e2e-tests /app | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Install dependencies (requirements.txt uses -e /sdk for SDK) | ||
| RUN pip install -q -r requirements.txt | ||
|
|
||
| # Make entrypoint executable | ||
| RUN chmod +x entrypoint.py | ||
|
|
||
| # Create .tusk directories | ||
| RUN mkdir -p /app/.tusk/traces /app/.tusk/logs | ||
|
|
||
| # Run entrypoint | ||
| ENTRYPOINT ["python", "entrypoint.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| services: | ||
| app: | ||
| build: | ||
| context: ../../../.. | ||
| dockerfile: drift/instrumentation/urllib/e2e-tests/Dockerfile | ||
| args: | ||
| - TUSK_CLI_VERSION=${TUSK_CLI_VERSION:-latest} | ||
| environment: | ||
| - PORT=8000 | ||
| - TUSK_ANALYTICS_DISABLED=1 | ||
| - PYTHONUNBUFFERED=1 | ||
| working_dir: /app | ||
| volumes: | ||
| # Mount SDK source for hot reload (no rebuild needed for SDK changes) | ||
| - ../../../..:/sdk | ||
| # Mount app source for development | ||
| - ./src:/app/src | ||
| # Mount .tusk folder to persist traces | ||
| - ./.tusk:/app/.tusk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| E2E Test Entrypoint for Urllib Instrumentation | ||
|
|
||
| This script orchestrates the full e2e test lifecycle: | ||
| 1. Setup: Install dependencies | ||
| 2. Record: Start app in RECORD mode, execute requests | ||
| 3. Test: Run Tusk CLI tests | ||
| 4. Teardown: Cleanup and return exit code | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Add SDK to path for imports | ||
| sys.path.insert(0, "/sdk") | ||
|
|
||
| from drift.instrumentation.e2e_common.base_runner import E2ETestRunnerBase | ||
|
|
||
|
|
||
| class UrllibE2ETestRunner(E2ETestRunnerBase): | ||
| """E2E test runner for Urllib instrumentation.""" | ||
|
|
||
| def __init__(self): | ||
| import os | ||
|
|
||
| port = int(os.getenv("PORT", "8000")) | ||
| super().__init__(app_port=port) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| runner = UrllibE2ETestRunner() | ||
| exit_code = runner.run() | ||
| sys.exit(exit_code) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -e /sdk | ||
| Flask>=3.1.2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.