chapps.adapter module

Policy Configuration Adapters

Policy-configuration source data adapters.

All of these currently interface to MariaDB as a fairly low level. A change is planned to convert all of this to use SQLAlchemy.

class chapps.adapter.PolicyConfigAdapter[source]

Bases: object

Base class for policy config access

Right now, all of the adapters use MariaDB, but this will change (as noted below). Non-database adapters could also be created, which might use a flat file, or a blockchain, or some sort of other API interaction, to be the source of policy data.

Ideally there should be a second-level base class called something like MariaDBPolicyConfigAdapter which would hold all the SQL-specific stuff, but that would make a lot more sense if

  1. there were going to be a lot of weird, different adapters. We may yet get there!

  2. there were anything else to put in the base class

For now, however, this class serves to store instructions about how to create the User table, since they are used for everything, just about.

SQL constants are deprecated.

All of the SQL statement constant strings defined in this class are deprecated. In a future version, all of this logic will be accomplished using SQLAlchemy.

user_table = 'CREATE TABLE IF NOT EXISTS users (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(127) UNIQUE NOT NULL)'

SQL query used to create the user table.

userid_query = 'SELECT id FROM users WHERE name = %(user)s'

SQL query for obtaining the ID of a user. These queries will go away when the older codebase is adapted to use SQLAlchemy

__init__(*, cfg=None, db_host=None, db_port=None, db_name=None, db_user=None, db_pass=None, autocommit=True)[source]
Parameters
  • db_host (str) – the hostname or IP address of the database server

  • db_port (int) – the port number of the database server

  • db_name (str) – the name of the database

  • db_user (str) – the username for login

  • db_pass (str) – the password for the user

  • autocommit (bool) – defaults to True

  • cfg (Optional[chapps.config.CHAPPSConfig]) –

finalize()[source]

Close the database connection.

adapter_context()[source]

Database connection context manager.

This method is meant to be used as a context manager, like so:

with someadapter.adapter_context() as cur:
    cur.execute(some_sql)

The context manager closes the cursor once the context ends.

class chapps.adapter.MariaDBQuotaAdapter[source]

Bases: chapps.adapter.PolicyConfigAdapter

An adapter for obtaining quota policy data from MariaDB

SQL constants are deprecated.

All of the SQL statement constant strings defined in this class are deprecated. In a future version, all of this logic will be accomplished using SQLAlchemy.

quota_table = 'CREATE TABLE IF NOT EXISTS quotas (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(31) UNIQUE NOT NULL,quota BIGINT UNIQUE NOT NULL)'

SQL statement for creating the Quota table.

join_table = 'CREATE TABLE IF NOT EXISTS quota_user (quota_id BIGINT NOT NULL,user_id BIGINT NOT NULL PRIMARY KEY,CONSTRAINT fk_user_quota FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT,CONSTRAINT fk_quota FOREIGN KEY (quota_id) REFERENCES quotas (id) ON DELETE CASCADE ON UPDATE CASCADE)'

SQL statement for creating the join table between Quota and User tables.

basic_quotas = "INSERT INTO quotas ( name, quota ) VALUES ('10eph', 240),('50eph', 1200),('200eph', 4800)"

Doubly deprecated insert statement for creating basic quotas.

quota_query = 'SELECT quota FROM quotas WHERE id = (SELECT quota_id FROM quota_user AS j LEFT JOIN users AS u ON j.user_id = u.id WHERE u.name = %(user)s)'

SQL query for selecting a Quota’s quota value for a named User.

quota_map_query = 'SELECT u.name AS user, p.quota FROM quotas AS p LEFT JOIN quota_user AS j ON p.id = j.quota_id LEFT JOIN users AS u ON j.user_id = u.id'

SQL query for selecting a list of users and their quotas.

quota_map_where = 'WHERE u.name IN ({srch})'

SQL for specifying which Users to returns quota values for.

quota_for_user(user)[source]

Return the quota amount for an user account

Parameters

user (str) – the user’s name

Return type

Optional[int]

quota_dict(users=None)[source]

Return a dict which maps users onto their quotas

Parameters

users (List[str]) – Optional limiting list of usernames; if empty or not provided, all users will be listed.

Return type

Dict[str, str]

quota_map(func, users=None)[source]

Map a callable over a set of users and their quotas

Parameters
  • func (callable) – A callable which will be passed (username, quota amount) for each user in the list

  • users (List[str]) – Optional limiting list of usernames; if empty or not provided, all users will be listed.

Return type

List[Any]

Provide a function to execute over each user and its quota. Use this to directly wire the database-loading logic to the Redis-population logic.

Return type

List[Any]

Parameters
class chapps.adapter.MariaDBSenderDomainAuthAdapter[source]

Bases: chapps.adapter.PolicyConfigAdapter

An adapter to obtain sender domain authorization data from MariaDB

SQL constants are deprecated.

All of the SQL statement constant strings defined in this class are deprecated. In a future version, all of this logic will be accomplished using SQLAlchemy.

domain_table = 'CREATE TABLE IF NOT EXISTS domains (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(63) UNIQUE NOT NULL,greylist TINYINT(1) NOT NULL DEFAULT 0,check_spf TINYINT(1) NOT NULL DEFAULT 0)'
domain_join_table = 'CREATE TABLE IF NOT EXISTS domain_user (domain_id BIGINT NOT NULL,user_id BIGINT NOT NULL,PRIMARY KEY (domain_id, user_id),CONSTRAINT fk_user_domain FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT,CONSTRAINT fk_domain FOREIGN KEY (domain_id) REFERENCES domains (id) ON DELETE CASCADE ON UPDATE CASCADE)'
email_table = 'CREATE TABLE IF NOT EXISTS emails (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(127) UNIQUE NOT NULL)'
email_join_table = 'CREATE TABLE IF NOT EXISTS email_user (email_id BIGINT NOT NULL,user_id BIGINT NOT NULL,PRIMARY KEY (email_id, user_id),CONSTRAINT fk_user_email FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT,CONSTRAINT fk_email FOREIGN KEY (email_id) REFERENCES emails (id) ON DELETE CASCADE ON UPDATE CASCADE)'
domain_query = 'SELECT d.name FROM domains AS d WHERE id = (SELECT domain_id FROM domain_user AS j LEFT JOIN users AS u ON j.user_id = u.id WHERE u.name = %(user)s)'
domain_map_query = 'SELECT u.name AS user, d.name AS domain FROM domains AS d LEFT JOIN domain_user AS j ON p.id = j.domain_id LEFT JOIN users AS u ON j.user_id = u.id'
domain_map_where = 'WHERE user IN ({srch})'
check_domain_query = "SELECT COUNT(d.name) FROM domains AS d LEFT JOIN domain_user AS j ON d.id = j.domain_id LEFT JOIN users AS u ON u.id = j.user_id WHERE d.name = '{domain}' AND u.name = '{user}'"
check_email_query = "SELECT COUNT(e.name) FROM emails AS e LEFT JOIN email_user AS j on e.id = j.email_id LEFT JOIN users AS u ON u.id = j.user_id WHERE e.name = '{email}' AND u.name = '{user}'"
check_domain_for_user(user, domain)[source]

Returns True if the user is authorized to send for this domain

Parameters
  • user (str) – name of user

  • domain (str) – name of domain

Return type

bool

check_email_for_user(user, email)[source]

Returns True if the user is authorized to send as this email

Parameters
  • user (str) – name of user

  • email (str) – email address

Return type

bool

class chapps.adapter.MariaDBInboundFlagsAdapter[source]

Bases: chapps.adapter.PolicyConfigAdapter

Adapter class for handling option flags for inbound mail

Generally these flags are set on a per-domain basis.

greylist_on_domain_query = "SELECT greylist FROM domains WHERE name = '{domain}' LIMIT 1"
check_spf_on_domain_query = "SELECT check_spf FROM domains WHERE name = '{domain}' LIMIT 1"
do_greylisting_on(domain)[source]
Return type

bool

Parameters

domain (str) –

check_spf_on(domain)[source]
Return type

bool

Parameters

domain (str) –