Skip to content

Commit

Permalink
Set up tox, stub tests, and satisfy linters
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkatronic committed Mar 5, 2022
1 parent 64b729f commit 1fc69b5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
2 changes: 2 additions & 0 deletions asherah/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Asherah application encryption library"""

from .asherah import Asherah
from .types import AsherahConfig

Expand Down
11 changes: 10 additions & 1 deletion asherah/asherah.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""Main Asherah class, for encrypting and decrypting of data"""
# pylint: disable=line-too-long, too-many-locals

import os
from cobhan import Cobhan
from datetime import datetime, timezone
from typing import ByteString, Union

from cobhan import Cobhan

from . import exceptions, types


class Asherah:
"""The main class for providing encryption and decryption functionality"""

KEY_SIZE = 64

def __init__(self):
Expand All @@ -22,6 +28,7 @@ def __init__(self):
)

def setup(self, config: types.AsherahConfig) -> None:
"""Set up/initialize the underlying encryption library."""
kms_type_buf = self.__cobhan.str_to_buf(config.kms_type)
metastore_buf = self.__cobhan.str_to_buf(config.metastore)
service_name_buf = self.__cobhan.str_to_buf(config.service_name)
Expand Down Expand Up @@ -61,6 +68,7 @@ def setup(self, config: types.AsherahConfig) -> None:
)

def encrypt(self, partition_id: str, data: Union[ByteString, str]):
"""Encrypt a chunk of data"""
if isinstance(data, str):
data = data.encode("utf-8")
# Inputs
Expand Down Expand Up @@ -108,6 +116,7 @@ def encrypt(self, partition_id: str, data: Union[ByteString, str]):
def decrypt(
self, partition_id: str, data_row_record: types.DataRowRecord
) -> bytearray:
"""Decrypt data that was previously encrypted by Asherah"""
# Inputs
partition_id_buf = self.__cobhan.str_to_buf(partition_id)
encrypted_data_buf = self.__cobhan.bytearray_to_buf(data_row_record.data)
Expand Down
3 changes: 3 additions & 0 deletions asherah/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
"""Custom exceptions for Asherah"""


class AsherahException(Exception):
"""Base exception class for any problems encountered in Asherah"""
25 changes: 18 additions & 7 deletions asherah/types.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
"""Type definitions for the Asherah library"""
# pylint: disable=too-many-instance-attributes,invalid-name

from dataclasses import dataclass
from datetime import datetime
from typing import ByteString
from typing import ByteString, Optional


@dataclass
class AsherahConfig:
"""Configuration options for Asherah setup"""

kms_type: str
metastore: str
service_name: str
product_id: str
rdbms_connection_string: str = None
dynamo_db_endpoint: str = None
dynamo_db_region: str = None
dynamo_db_table_name: str = None
rdbms_connection_string: Optional[str] = None
dynamo_db_endpoint: Optional[str] = None
dynamo_db_region: Optional[str] = None
dynamo_db_table_name: Optional[str] = None
enable_region_suffix: bool = False
preferred_region: str = None
region_map: str = None
preferred_region: Optional[str] = None
region_map: Optional[str] = None
verbose: bool = False
session_cache: bool = False
debug_output: bool = False


@dataclass
class KeyMeta:
"""Metadata about an encryption key"""

id: str
created: datetime


@dataclass
class EnvelopeKeyRecord:
"""Information about an encryption envelope"""

encrypted_key: ByteString
created: datetime
parent_key_meta: KeyMeta


@dataclass
class DataRowRecord:
"""Encrypted data and its related information"""

data: ByteString
key: EnvelopeKeyRecord
Empty file added tests/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/test_asherah.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# pylint: disable=missing-function-docstring,missing-class-docstring,missing-module-docstring

from unittest import TestCase


class AsherahTest(TestCase):
def test_fake(self):
self.assertEqual(True, True)
44 changes: 44 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[tox]
minversion = 3.7.0
toxworkdir = {env:TOX_WORK_DIR:.tox}
skip_missing_interpreters = True
envlist = py{37,38,39,310},black,mypy,pylint
parallel_show_output = True
isolated_build = True

[gh-actions]
python =
3.7: py37
3.8: py38
3.9: py39
3.10: py310

[testenv]
whitelist_externals =
poetry
pytest
setenv =
PYTHONDONTWRITEBYTECODE=1
PYTHONHASHSEED=0
PYTHONWARNINGS=ignore
commands =
poetry install --no-root -v
poetry run pytest {posargs}

[testenv:black]
basepython = python3.7
commands =
poetry install --no-root -v
poetry run black --check .

[testenv:mypy]
basepython = python3.7
commands =
poetry install --no-root -v
poetry run mypy .

[testenv:pylint]
basepython = python3.7
commands =
poetry install --no-root -v
poetry run pylint asherah/ tests/

0 comments on commit 1fc69b5

Please sign in to comment.