lomas_server package

Subpackages

Submodules

lomas_server.app module

lomas_server.app.lifespan(app: FastAPI) AsyncGenerator[source]

Lifespan function for the server.

This function is executed once on server startup, yields and finishes running at server shutdown.

Server initialization is performed (config loading, etc.) and the server state is updated accordingly. This can have potential side effects on the return values of the “depends” functions, which check the server state.

async lomas_server.app.middleware(request: Request, call_next: Callable[[Request], Response]) Response[source]

Adds delays to requests response to protect against timing attack

lomas_server.constants module

class lomas_server.constants.AdminDBType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Types of administration databases

MONGODB: str = 'mongodb'
YAML: str = 'yaml'
class lomas_server.constants.ConfigKeys(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Keys of the configuration file

DB: str = 'admin_database'
DB_TYPE: str = 'db_type'
DB_TYPE_MONGODB: str = 'mongodb'
DEVELOP_MODE: str = 'develop_mode'
DP_LIBRARY: str = 'dp_libraries'
MONGODB_ADDR: str = 'address'
MONGODB_PORT: str = 'port'
RUNTIME_ARGS: str = 'runtime_args'
SERVER: str = 'server'
SETTINGS: str = 'settings'
SUBMIT_LIMIT: str = 'submit_limit'
TIME_ATTACK: str = 'time_attack'
class lomas_server.constants.DPLibraries(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Name of DP Library used in the query

DIFFPRIVLIB = 'diffprivlib'
OPENDP = 'opendp'
SMARTNOISE_SQL = 'smartnoise_sql'
SMARTNOISE_SYNTH = 'smartnoise_synth'
class lomas_server.constants.OpenDPDatasetInputMetric(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of opendp input metric for datasets see https://docs.opendp.org/en/stable/api/python/opendp.metrics.html see https://github.com/opendp/opendp/blob/main/rust/src/metrics/mod.rs

CHANGE_ONE_DISTANCE = 'ChangeOneDistance'
HAMMING_DISTANCE = 'HammingDistance'
INSERT_DELETE_DISTANCE = 'InsertDeleteDistance'
INT_DISTANCE = 'u32'
SYMMETRIC_DISTANCE = 'SymmetricDistance'
class lomas_server.constants.OpenDPMeasurement(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of divergence for opendp measurement see https://docs.opendp.org/en/stable/api/python/opendp.measurements.html

FIXED_SMOOTHED_MAX_DIVERGENCE = 'fixed_smoothed_max_divergence'
MAX_DIVERGENCE = 'max_divergence'
SMOOTHED_MAX_DIVERGENCE = 'smoothed_max_divergence'
ZERO_CONCENTRATED_DIVERGENCE = 'zero_concentrated_divergence'
class lomas_server.constants.PrivateDatabaseType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of Private Database for the private data

PATH = 'PATH_DB'
S3 = 'S3_DB'
class lomas_server.constants.SSynthColumnType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Type of columns for SmartnoiseSynth transformer pre-processing

CATEGORICAL = 'categorical'
CONTINUOUS = 'continuous'
DATETIME = 'datetime'
PRIVATE_ID = 'private_id'
class lomas_server.constants.SSynthGanSynthesizer(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

GAN Synthesizer models for smartnoise synth

DP_CTGAN = 'dpctgan'
DP_GAN = 'dpgan'
PATE_CTGAN = 'patectgan'
PATE_GAN = 'pategan'
class lomas_server.constants.SSynthMarginalSynthesizer(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Marginal Synthesizer models for smartnoise synth

AIM = 'aim'
MST = 'mst'
MWEM = 'mwem'
PAC_SYNTH = 'pacsynth'
class lomas_server.constants.SSynthTableTransStyle(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Transformer style for smartnoise synth

CUBE = 'cube'
GAN = 'gan'
class lomas_server.constants.TimeAttackMethod(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Possible methods against timing attacks

JITTER = 'jitter'
STALL = 'stall'

lomas_server.mongodb_admin module

lomas_server.mongodb_admin.add_dataset(db: Database, dataset_name: str, database_type: str, metadata_database_type: str, dataset_path: str | None = '', metadata_path: str | None = '', bucket: str | None = '', key: str | None = '', endpoint_url: str | None = '', credentials_name: str | None = '', metadata_bucket: str | None = '', metadata_key: str | None = '', metadata_endpoint_url: str | None = '', metadata_access_key_id: str | None = '', metadata_secret_access_key: str | None = '', metadata_credentials_name: str | None = '') None[source]

Set a database type to a dataset in dataset collection.

Parameters:
  • db (Database) – mongo database object

  • dataset_name (str) – Dataset name

  • database_type (str) – Type of the database

  • metadata_database_type (str) – Metadata database type

  • dataset_path (str) – Path to the dataset (for local db type)

  • metadata_path (str) – Path to metadata (for local db type)

  • bucket (str) – S3 bucket name

  • key (str) – S3 key

  • endpoint_url (str) – S3 endpoint URL

  • credentials_name (str) – The name of the credentials in the server config to retrieve the dataset from S3 storage.

  • metadata_bucket (str) – Metadata S3 bucket name

  • metadata_key (str) – Metadata S3 key

  • metadata_endpoint_url (str) – Metadata S3 endpoint URL

  • metadata_access_key_id (str) – Metadata AWS access key ID

  • metadata_secret_access_key (str) – Metadata AWS secret access key

  • metadata_credentials_name (str) – The name of the credentials in the server config for retrieving the metadata.

Raises:

ValueError – If the dataset already exists or if the database type is unknown.

Returns:

None

lomas_server.mongodb_admin.add_dataset_to_user(db: Database, user: str, dataset: str, epsilon: float, delta: float) None[source]

Add dataset with initialized budget values to list of datasets that the user has access to. Will not add if already added (no error will be raised in that case).

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to check

  • dataset (str) – name of the dataset to add to user

  • epsilon (float) – epsilon value for initial budget of user

  • delta (float) – delta value for initial budget of user

Raises:

ValueError – _description_

Returns:

None

lomas_server.mongodb_admin.add_datasets_via_yaml(db: Database, yaml_file: str | Dict, clean: bool, overwrite_datasets: bool, overwrite_metadata: bool) None[source]

Set all database types to datasets in dataset collection based on yaml file.

Parameters:
  • db (Database) – mongo database object

  • yaml_file (Union[str, Dict]) – if str: a path to the YAML file location if Dict: a dictionnary containing the collection data

  • clean (bool) – Whether to clean the collection before adding.

  • overwrite_datasets (bool) – Whether to overwrite existing datasets.

  • overwrite_metadata (bool) – Whether to overwrite existing metadata.

Raises:

ValueError – If there are errors in the YAML file format.

Returns:

None

lomas_server.mongodb_admin.add_user(db: Database, user: str) None[source]

Add new user in users collection with initial values for all fields set by default.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username to be added

Raises:
  • ValueError – If the user already exists.

  • WriteConcernError – If the result is not acknowledged.

Returns:

None

lomas_server.mongodb_admin.add_user_with_budget(db: Database, user: str, dataset: str, epsilon: float, delta: float) None[source]

Add new user in users collection with initial values for all fields set by default.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username to be added

  • dataset (str) – name of the dataset to add to user

  • epsilon (float) – epsilon value for initial budget of user

  • delta (float) – delta value for initial budget of user

Raises:

ValueError – _description_

Returns:

None

lomas_server.mongodb_admin.add_users_via_yaml(db: Database, yaml_file: str | Dict, clean: bool, overwrite: bool) None[source]

Add all users from yaml file to the user collection

Parameters:
  • db (Database) – mongo database object

  • yaml_file (Union[str, Dict]) – if str: a path to the YAML file location if Dict: a dictionnary containing the collection data

  • clean (bool) – boolean flag True if drop current user collection False if keep current user collection

  • overwrite (bool) – boolean flag True if overwrite already existing users False errors if new values for already existing users

Returns:

None

lomas_server.mongodb_admin.check_dataset_and_metadata_exist(enforce_true: bool) Callable[source]

Creates a wrapper function that raises a ValueError if the supplied user does not already exist in the user collection.

lomas_server.mongodb_admin.check_user_exists(enforce_true: bool) Callable[source]

Creates a wrapper function that raises a ValueError if the supplied user does (not) exist in the user collection depending on the enforce_true parameter.

Parameters:

enforce_true (bool) – If set to True, the wrapper will enforce the user is already in the database. If set to False, it will enforce the user is NOT in the database.

Returns:

The wrapper function that enforces user presence

(or absence) before calling the suplied function.

Return type:

Callable

lomas_server.mongodb_admin.check_user_has_dataset(enforce_true: bool) Callable[source]

Creates a wrapper function that raises a ValueError if the supplied user has access (or not) to the supplied dataset depending on the enforce_true parameter.

Parameters:

enforce_true (bool) – If set to true, the wrapper function will enforce the user has access to the dataset. If set to False, the wrapper function will enforce the user has NOT access to the specified dataset.

Returns:

The wrapper function that asserts user access (or not)

to the provided dataset.

Return type:

Callable

lomas_server.mongodb_admin.del_dataset(db: Database, dataset: str) None[source]

Delete dataset from dataset collection.

Parameters:
  • db (Database) – mongo database object

  • dataset (str) – Dataset name to be deleted

Returns:

None

lomas_server.mongodb_admin.del_dataset_to_user(db: Database, user: str, dataset: str) None[source]

Remove if exists the dataset (and all related budget info) from list of datasets that user has access to.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to which to delete a dataset

  • dataset (str) – name of the dataset to remove from user

Returns:

None

lomas_server.mongodb_admin.del_user(db: Database, user: str) None[source]

Delete all related information for user from the users collection.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username to be deleted

Returns:

None

lomas_server.mongodb_admin.drop_collection(db: Database, collection: str) None[source]

Delete collection.

Parameters:
  • db (Database) – mongo database object

  • collection (str) – Collection name to be deleted.

Returns:

None

lomas_server.mongodb_admin.get_archives_of_user(db: Database, user: str) List[dict][source]

Show all previous queries from a user

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to show archives

Returns:

list of previous queries from the user

Return type:

archives (List)

lomas_server.mongodb_admin.get_collection(db: Database, collection: str) list[source]

Show a collection

Parameters:
  • db (Database) – mongo database object

  • collection (str) – Collection name to be shown.

Returns:

None

lomas_server.mongodb_admin.get_dataset(db: Database, dataset: str) dict[source]

Show a dataset from dataset collection.

Parameters:
  • db (Database) – mongo database object

  • dataset (str) – name of the dataset to show

Returns:

informations about the dataset

Return type:

dataset_info (dict)

lomas_server.mongodb_admin.get_list_of_datasets(db: Database) list[source]

Get the list of all dataset is ‘datasets’ collection

Parameters:

db (Database) – mongo database object

Returns:

list of names of all datasets

Return type:

dataset_names (list)

lomas_server.mongodb_admin.get_list_of_datasets_from_user(db: Database, user: str) list[source]

Get the list of all datasets from the user

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to show archives

Returns:

list of names of all users

Return type:

user_datasets (list)

lomas_server.mongodb_admin.get_list_of_users(db: Database) list[source]

Get the list of all users is ‘users’ collection

Parameters:

db (Database) – mongo database object

Returns:

list of names of all users

Return type:

user_names (list)

lomas_server.mongodb_admin.get_metadata_of_dataset(db: Database, dataset: str) dict[source]

Show a metadata from metadata collection.

Parameters:
  • db (Database) – mongo database object

  • dataset (str) – name of the dataset of the metadata to show

Returns:

informations about the metadata

Return type:

metadata (dict)

lomas_server.mongodb_admin.get_user(db: Database, user: str) dict[source]

Show a user

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to show

Returns:

all information of user from ‘users’ collection

Return type:

user (dict)

lomas_server.mongodb_admin.set_budget_field(db: Database, user: str, dataset: str, field: str, value: float) None[source]

Set (for some reason) a budget field to a given value if given user exists and has access to given dataset.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to set budget to

  • dataset (str) – name of the dataset to set budget to

  • field (str) – one of ‘epsilon’ or ‘delta’

  • value (float) – value to set as epsilon or delta

Returns:

None

lomas_server.mongodb_admin.set_may_query(db: Database, user: str, value: bool) None[source]

Set (for some reason) the ‘may query’ field to a given value if given user exists.

Parameters:
  • db (Database) – mongo database object

  • user (str) – username of the user to enable/disable

  • value (bool) – may query value (True or False)

Returns:

None

lomas_server.mongodb_admin_cli module

lomas_server.uvicorn_serve module

Module contents