Skip to content

Commit

Permalink
Merge pull request #4 from godaddy/setupjson
Browse files Browse the repository at this point in the history
Switch to using SetupJson instead of Setup
  • Loading branch information
jgowdy-godaddy authored Mar 9, 2022
2 parents b4e2882 + 3ca3279 commit bda21d8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.2.0] - 2022-03-08

- Switch to using SetupJson instead of Setup for initialization

## [0.1.0] - 2022-03-04

- Initial release
39 changes: 5 additions & 34 deletions asherah/asherah.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Main Asherah class, for encrypting and decrypting of data"""
# pylint: disable=line-too-long, too-many-locals

import json
import os
from datetime import datetime, timezone
from typing import ByteString, Union
Expand All @@ -21,47 +22,17 @@ def __init__(self):
os.path.join(os.path.dirname(__file__), "libasherah"),
"libasherah",
"""
int32_t Setup(void* kmsTypePtr, void* metastorePtr, void* rdbmsConnectionStringPtr, void* dynamoDbEndpointPtr, void* dynamoDbRegionPtr, void* dynamoDbTableNamePtr, int32_t enableRegionSuffixInt, void* serviceNamePtr, void* productIdPtr, void* preferredRegionPtr, void* regionMapPtr, int32_t verboseInt, int32_t sessionCacheInt, int32_t debugOutputInt);
int32_t SetupJson(void* configJson);
int32_t Decrypt(void* partitionIdPtr, void* encryptedDataPtr, void* encryptedKeyPtr, int64_t created, void* parentKeyIdPtr, int64_t parentKeyCreated, void* outputDecryptedDataPtr);
int32_t Encrypt(void* partitionIdPtr, void* dataPtr, void* outputEncryptedDataPtr, void* outputEncryptedKeyPtr, void* outputCreatedPtr, void* outputParentKeyIdPtr, void* outputParentKeyCreatedPtr);
""",
)

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)
product_id_buf = self.__cobhan.str_to_buf(config.product_id)
rdbms_connection_string_buf = self.__cobhan.str_to_buf(
config.rdbms_connection_string
)
dynamo_db_endpoint_buf = self.__cobhan.str_to_buf(config.dynamo_db_endpoint)
dynamo_db_region_buf = self.__cobhan.str_to_buf(config.dynamo_db_region)
dynamo_db_table_name_buf = self.__cobhan.str_to_buf(config.dynamo_db_table_name)
enable_region_suffix_int = int(config.enable_region_suffix)
preferred_region_buf = self.__cobhan.str_to_buf(config.preferred_region)
region_map_buf = self.__cobhan.str_to_buf(config.region_map)
verbose_int = int(config.verbose)
session_cache_int = int(config.session_cache)
debug_output_int = int(config.debug_output)

result = self.__libasherah.Setup(
kms_type_buf,
metastore_buf,
rdbms_connection_string_buf,
dynamo_db_endpoint_buf,
dynamo_db_region_buf,
dynamo_db_table_name_buf,
enable_region_suffix_int,
service_name_buf,
product_id_buf,
preferred_region_buf,
region_map_buf,
verbose_int,
session_cache_int,
debug_output_int,
)
config_json = json.dumps(config.to_json())
config_buf = self.__cobhan.str_to_buf(config_json)
result = self.__libasherah.SetupJson(config_buf)
if result < 0:
raise exceptions.AsherahException(
f"Setup failed with error number {result}"
Expand Down
11 changes: 10 additions & 1 deletion asherah/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Type definitions for the Asherah library"""
# pylint: disable=too-many-instance-attributes,invalid-name

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

Expand All @@ -25,6 +25,15 @@ class AsherahConfig:
session_cache: bool = False
debug_output: bool = False

def to_json(self):
def camel_case(key):
"""Translate snake_case into camelCase."""
parts = key.split("_")
parts = [parts[0]] + [part.capitalize() for part in parts[1:]]
return "".join(parts)

return {camel_case(key): val for key, val in asdict(self).items()}


@dataclass
class KeyMeta:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "asherah"
version = "0.1.0"
version = "0.2.0"
description = "Asherah envelope encryption and key rotation library"
authors = [
"Jeremiah Gowdy <jeremiah@gowdy.me>",
Expand Down

0 comments on commit bda21d8

Please sign in to comment.