-
Notifications
You must be signed in to change notification settings - Fork 33
add bouncer/net-tests to ooniapi under new router bouncer #1036
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
094da16
add bouncer/net-tests to ooniapi under new router bouncer
aagbsn 38e5b98
Merge remote-tracking branch 'origin/master' into add_net_tests_to_oo…
aagbsn ccd558c
fix pylint import complaints
aagbsn 2f79098
let fastapi validate input
aagbsn ad816a1
exclude unset parameters in bouncer/net-tests
aagbsn 6ac3a88
add bouncer tests from api/tests/integ/test_probe_services.py
aagbsn 713f9b1
raise HTTPException with status 400 for backwards-compatibility
aagbsn 04dee06
check status code on error is 400 for backwards compatibility with ol…
aagbsn 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
110 changes: 110 additions & 0 deletions
110
ooniapi/services/ooniprobe/src/ooniprobe/routers/bouncer.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,110 @@ | ||
| import logging | ||
| from typing import List, Optional, Any, Dict | ||
| from json.decoder import JSONDecodeError | ||
|
|
||
| from fastapi import APIRouter, HTTPException, Request, Response | ||
| from pydantic import Field, ValidationError | ||
|
|
||
| from ooniprobe.common.utils import setnocacheresponse | ||
| from ooniprobe.common.routers import BaseModel | ||
|
|
||
| router = APIRouter(prefix="/bouncer") | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TestHelperEntry(BaseModel): | ||
| address: str | ||
| type: str | ||
| front: Optional[str] = None | ||
|
|
||
|
|
||
| class CollectorEntry(BaseModel): | ||
| address: str | ||
| type: str | ||
| front: Optional[str] = None | ||
|
|
||
|
|
||
| class NetTest(BaseModel): | ||
| name: str | ||
| collector: str | ||
| altcollector: List[CollectorEntry] = Field(alias="collector-alternate") | ||
| hashes: Optional[Any] = Field(None, alias="input-hashes") | ||
| helpers: Dict[str, str] = Field(alias="test-helpers") | ||
| althelpers: Dict[str, List[TestHelperEntry]] = Field(alias="test-helpers-alternate") | ||
| version: str | ||
|
|
||
|
|
||
| class NetTestRequest(BaseModel): | ||
| name: str | ||
| version: str | ||
|
|
||
|
|
||
| class NetTestsRequest(BaseModel): | ||
| nettests: List[NetTestRequest] = Field(alias="net-tests") | ||
|
|
||
|
|
||
| class NetTestResponse(BaseModel): | ||
| nettests: List[NetTest] = Field(alias="net-tests") | ||
|
|
||
|
|
||
| @router.post("/net-tests", tags=["bouncer"], response_model=NetTestResponse, response_model_exclude_unset=True) | ||
| async def bouncer_net_tests( | ||
| response: Response, | ||
| request: Request, | ||
| ) -> Dict[str, List[NetTest]]: | ||
|
|
||
| try: | ||
| j = await request.json() | ||
| m = NetTestsRequest(**j) | ||
| except ValidationError as e: | ||
| raise HTTPException(400, detail=e.errors()) | ||
| except JSONDecodeError as e: | ||
| raise HTTPException(400, detail=str(e)) | ||
| except Exception as e: | ||
| log.warning("Unexpected Exception:" + str(e)) | ||
| raise HTTPException(400, detail=str(e)) | ||
|
|
||
| try: | ||
| name = m.nettests[0].name | ||
| version = m.nettests[0].version | ||
| except IndexError: | ||
| raise HTTPException(status_code=400, detail="invalid net-tests request") | ||
|
|
||
| # TODO: load this json from environment or filepath | ||
| j = { | ||
| "net-tests": [ | ||
| { | ||
| "collector": "httpo://guegdifjy7bjpequ.onion", | ||
| "collector-alternate": [ | ||
| {"type": "https", "address": "https://ams-pg.ooni.org"}, | ||
| { | ||
| "front": "dkyhjv0wpi2dk.cloudfront.net", | ||
| "type": "cloudfront", | ||
| "address": "https://dkyhjv0wpi2dk.cloudfront.net", | ||
| }, | ||
| ], | ||
| "input-hashes": None, | ||
| "name": name, | ||
| "test-helpers": { | ||
| "tcp-echo": "37.218.241.93", | ||
| "http-return-json-headers": "http://37.218.241.94:80", | ||
| "web-connectivity": "httpo://y3zq5fwelrzkkv3s.onion", | ||
| }, | ||
| "test-helpers-alternate": { | ||
| "web-connectivity": [ | ||
| {"type": "https", "address": "https://wcth.ooni.io"}, | ||
| { | ||
| "front": "d33d1gs9kpq1c5.cloudfront.net", | ||
| "type": "cloudfront", | ||
| "address": "https://d33d1gs9kpq1c5.cloudfront.net", | ||
| }, | ||
| ] | ||
| }, | ||
| "version": version, | ||
| } | ||
| ] | ||
| } | ||
| resp = NetTestResponse(**j) | ||
| setnocacheresponse(response) | ||
| return resp |
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,74 @@ | ||
| """ | ||
| Integration test for OONIProbe API | ||
| """ | ||
|
|
||
|
|
||
| def test_get_bouncer_nettests(client): | ||
| version = "1" | ||
|
|
||
| r = client.post("/bouncer/net-tests", json={"net-tests": [{"name": "foo", "version": version}]}) | ||
| j = r.json() | ||
| assert "net-tests" in j | ||
| for v in j["net-tests"]: | ||
| for x in ["collector", "input-hashes", "name", "test-helpers", "test-helpers-alternate", "version"]: | ||
| assert x in v | ||
|
|
||
| def test_bouncer_net_tests(client): | ||
| j = { | ||
| "net-tests": [ | ||
| { | ||
| "input-hashes": None, | ||
| "name": "web_connectivity", | ||
| "test-helpers": ["web-connectivity"], | ||
| "version": "0.0.1", | ||
| } | ||
| ] | ||
| } | ||
| c = client.post("/bouncer/net-tests", json=j) | ||
| expected = { | ||
| "net-tests": [ | ||
| { | ||
| "collector": "httpo://guegdifjy7bjpequ.onion", | ||
| "collector-alternate": [ | ||
| {"type": "https", "address": "https://ams-pg.ooni.org"}, | ||
| { | ||
| "front": "dkyhjv0wpi2dk.cloudfront.net", | ||
| "type": "cloudfront", | ||
| "address": "https://dkyhjv0wpi2dk.cloudfront.net", | ||
| }, | ||
| ], | ||
| "name": "web_connectivity", | ||
| "test-helpers": { | ||
| "tcp-echo": "37.218.241.93", | ||
| "http-return-json-headers": "http://37.218.241.94:80", | ||
| "web-connectivity": "httpo://y3zq5fwelrzkkv3s.onion", | ||
| }, | ||
| "test-helpers-alternate": { | ||
| "web-connectivity": [ | ||
| {"type": "https", "address": "https://wcth.ooni.io"}, | ||
| { | ||
| "front": "d33d1gs9kpq1c5.cloudfront.net", | ||
| "type": "cloudfront", | ||
| "address": "https://d33d1gs9kpq1c5.cloudfront.net", | ||
| }, | ||
| ] | ||
| }, | ||
| "version": "0.0.1", | ||
| "input-hashes": None, | ||
| } | ||
| ] | ||
| } | ||
| assert c.json() == expected | ||
|
|
||
|
|
||
| def test_bouncer_net_tests_bad_request1(client): | ||
| resp = client.post("/bouncer/net-tests") | ||
| # XXX: returns status code 400 for backwards compatibility | ||
| assert resp.status_code == 400 | ||
|
|
||
|
|
||
| def test_bouncer_net_tests_bad_request2(client): | ||
| j = {"net-tests": []} | ||
| resp = client.post("/bouncer/net-tests", json=j) | ||
| # XXX: returns status code 400 for backwards compatibility | ||
| assert resp.status_code == 400 | ||
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.