-
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.
* Implement ooniauth API v2 * Add support for passing admin_emails via configuration * Get rid of the account_id hashing * Move clickhouse functionality into clickhouse_utils * Fix broken errors and assertions * Add tests for AHrefParser * Mark the single line which is not covered as no cov
- Loading branch information
Showing
13 changed files
with
603 additions
and
205 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,67 @@ | ||
import logging | ||
from typing import Dict, List, Optional, Union | ||
import clickhouse_driver | ||
import clickhouse_driver.errors | ||
|
||
from sqlalchemy.dialects import postgresql | ||
from sqlalchemy.sql.elements import TextClause | ||
from sqlalchemy.sql.selectable import Select | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
Query = Union[str, TextClause, Select] | ||
|
||
|
||
def _run_query( | ||
db: clickhouse_driver.Client, query: Query, query_params: dict, query_prio=3 | ||
): | ||
# settings = {"priority": query_prio, "max_execution_time": 28} | ||
settings = {} | ||
if isinstance(query, (Select, TextClause)): | ||
query = str(query.compile(dialect=postgresql.dialect())) | ||
try: | ||
q = db.execute(query, query_params, with_column_types=True, settings=settings) | ||
except clickhouse_driver.errors.ServerException as e: | ||
log.info(e.message) | ||
raise Exception("Database query error") | ||
|
||
rows, coldata = q # type: ignore | ||
colnames, coltypes = tuple(zip(*coldata)) | ||
return colnames, rows | ||
|
||
|
||
def query_click( | ||
db: clickhouse_driver.Client, query: Query, query_params: dict, query_prio=3 | ||
) -> List[Dict]: | ||
colnames, rows = _run_query(db, query, query_params, query_prio=query_prio) | ||
return [dict(zip(colnames, row)) for row in rows] # type: ignore | ||
|
||
|
||
def query_click_one_row( | ||
db: clickhouse_driver.Client, query: Query, query_params: dict, query_prio=3 | ||
) -> Optional[dict]: | ||
colnames, rows = _run_query(db, query, query_params, query_prio=query_prio) | ||
for row in rows: | ||
return dict(zip(colnames, row)) # type: ignore | ||
|
||
return None | ||
|
||
|
||
def insert_click(db: clickhouse_driver.Client, query: Query, rows: list) -> int: | ||
assert isinstance(rows, list) | ||
settings = {"priority": 1, "max_execution_time": 300} # query_prio | ||
return db.execute(query, rows, types_check=True, settings=settings) # type: ignore | ||
|
||
|
||
def optimize_table(db: clickhouse_driver.Client, tblname: str) -> None: | ||
settings = {"priority": 1, "max_execution_time": 300} # query_prio | ||
sql = f"OPTIMIZE TABLE {tblname} FINAL" | ||
db.execute(sql, {}, settings=settings) | ||
|
||
|
||
def raw_query( | ||
db: clickhouse_driver.Client, query: Query, query_params: dict, query_prio=1 | ||
): | ||
settings = {"priority": query_prio, "max_execution_time": 300} | ||
q = db.execute(query, query_params, with_column_types=True, settings=settings) | ||
return q |
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 |
---|---|---|
|
@@ -14,11 +14,15 @@ class Settings(BaseSettings): | |
statsd_port: int = 8125 | ||
statsd_prefix: str = "ooniapi" | ||
jwt_encryption_key: str = "CHANGEME" | ||
account_id_hashing_key: str = "CHANGEME" | ||
prometheus_metrics_password: str = "CHANGEME" | ||
session_expiry_days: int = 10 | ||
login_expiry_days: int = 10 | ||
|
||
admin_emails: List[str] = [ | ||
"[email protected]", | ||
"[email protected]", | ||
] | ||
|
||
aws_region: str = "" | ||
aws_access_key_id: str = "" | ||
aws_secret_access_key: str = "" | ||
|
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 |
---|---|---|
@@ -1,21 +1,19 @@ | ||
# ooniauth | ||
|
||
[![PyPI - Version](https://img.shields.io/pypi/v/ooniauth.svg)](https://pypi.org/project/ooniauth) | ||
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ooniauth.svg)](https://pypi.org/project/ooniauth) | ||
The OONI Auth service is designed to allow users to authenticate with their | ||
email address to OONI services. | ||
|
||
----- | ||
The basic workflow is: | ||
|
||
**Table of Contents** | ||
1. Perform a login request by providing your email address | ||
2. Check your email to retrieve the login link | ||
3. Click on the login link to generate a session token that's valid for the | ||
duration of the session | ||
|
||
- [Installation](#installation) | ||
- [License](#license) | ||
You may also want to periodically refresh the session token so that it does not | ||
expire. | ||
|
||
## Installation | ||
The tokens which are part of the system are: | ||
|
||
```console | ||
pip install ooniauth | ||
``` | ||
|
||
## License | ||
|
||
`ooniauth` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. | ||
- Login tokens, which are sent via email and are tied to an email address | ||
- Session token, which are issued by the API and are tied to a login token |
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
Oops, something went wrong.