-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(oonifindings): findings ooniapi service Create oonifindings service with clickhouse support * refactor: switch to using pytest-docker * feat: add build files * feat: oonifindings service v1 * integrate changes from common into oonirun * migrate ooniauth to new common * code cleanup * add github action for oonifindings tests * introduce api tests for oonifindings * add tests for oonifindings router * extend router tests for oonifindings * refactor: remove null account and use client * refactor: use cliet without token * delete unrequired fixtures * test for cache headers in response
- Loading branch information
Showing
35 changed files
with
1,772 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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,25 @@ | ||
name: test ooniapi/oonifindings | ||
on: push | ||
jobs: | ||
run_tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Python 3.11 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.11 | ||
|
||
- name: Install hatch | ||
run: pip install hatch | ||
|
||
- name: Run all tests | ||
run: make test-cov | ||
working-directory: ./ooniapi/services/oonifindings/ | ||
|
||
- name: Upload coverage to codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
flags: oonifindings | ||
working-directory: ./ooniapi/services/oonifindings/ |
This file contains 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,70 @@ | ||
import hashlib | ||
from typing import Optional, Dict, Any | ||
import jwt | ||
|
||
|
||
def hash_email_address(email_address: str, key: str) -> str: | ||
em = email_address.encode() | ||
return hashlib.blake2b(em, key=key.encode("utf-8"), digest_size=16).hexdigest() | ||
|
||
|
||
def check_email_address( | ||
authorization: str, | ||
jwt_encryption_key: str, | ||
email_address: str, | ||
key: str | ||
) -> bool: | ||
account_id = get_account_id_or_raise(authorization, jwt_encryption_key=jwt_encryption_key) | ||
hashed = hash_email_address(email_address, key=key) | ||
if account_id == hashed: | ||
return True | ||
return False | ||
|
||
|
||
def decode_jwt(token: str, key: str, **kw) -> Dict[str, Any]: | ||
tok = jwt.decode(token, key, algorithms=["HS256"], **kw) | ||
return tok | ||
|
||
|
||
def create_jwt(payload: dict, key: str) -> str: | ||
token = jwt.encode(payload, key, algorithm="HS256") | ||
if isinstance(token, bytes): | ||
return token.decode() | ||
else: | ||
return token | ||
|
||
|
||
def get_client_token(authorization: str, jwt_encryption_key: str): | ||
try: | ||
assert authorization.startswith("Bearer ") | ||
token = authorization[7:] | ||
return decode_jwt(token, audience="user_auth", key=jwt_encryption_key) | ||
except: | ||
return None | ||
|
||
|
||
def get_client_role(authorization: str, jwt_encryption_key: str) -> str: | ||
"""Raise exception for unlogged users""" | ||
tok = get_client_token(authorization, jwt_encryption_key) | ||
try: | ||
assert tok | ||
return tok["role"] | ||
except: | ||
return None | ||
|
||
def get_account_id_or_none( | ||
authorization: str, jwt_encryption_key: str | ||
) -> Optional[str]: | ||
"""Returns None for unlogged users""" | ||
tok = get_client_token(authorization, jwt_encryption_key) | ||
if tok: | ||
return tok["account_id"] | ||
return None | ||
|
||
|
||
def get_account_id_or_raise(authorization: str, jwt_encryption_key: str) -> str: | ||
"""Raise exception for unlogged users""" | ||
tok = get_client_token(authorization, jwt_encryption_key) | ||
if tok: | ||
return tok["account_id"] | ||
raise Exception |
This file contains 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 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 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 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 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 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 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 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 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 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,10 @@ | ||
.DS_Store | ||
*.log | ||
*.pyc | ||
*.swp | ||
*.env | ||
.coverage | ||
coverage.xml | ||
dist/ | ||
.venv/ | ||
__pycache__/ |
This file contains 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,3 @@ | ||
/dist | ||
/coverage_html | ||
*.coverage* |
This file contains 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,29 @@ | ||
# Python builder | ||
FROM python:3.11-bookworm as builder | ||
ARG BUILD_LABEL=docker | ||
|
||
WORKDIR /build | ||
|
||
RUN python -m pip install hatch | ||
|
||
COPY . /build | ||
|
||
# When you build stuff on macOS you end up with ._ files | ||
# https://apple.stackexchange.com/questions/14980/why-are-dot-underscore-files-created-and-how-can-i-avoid-them | ||
RUN find /build -type f -name '._*' -delete | ||
|
||
RUN echo "$BUILD_LABEL" > /build/src/oonifindings/BUILD_LABEL | ||
|
||
RUN hatch build | ||
|
||
### Actual image running on the host | ||
FROM python:3.11-bookworm as runner | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /build/README.md /app/ | ||
COPY --from=builder /build/dist/*.whl /app/ | ||
RUN pip install /app/*whl && rm /app/*whl | ||
|
||
CMD ["uvicorn", "oonifindings.main:app", "--host", "0.0.0.0", "--port", "80"] | ||
EXPOSE 80 |
This file contains 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,26 @@ | ||
Copyright 2022-present Open Observatory of Network Interference Foundation (OONI) ETS | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its contributors | ||
may be used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Oops, something went wrong.