Skip to content

Commit

Permalink
setup test
Browse files Browse the repository at this point in the history
  • Loading branch information
felipao-mx committed May 29, 2024
1 parent 5dadcaa commit 5fc937e
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 9 deletions.
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pytest==6.2.*
pytest-asyncio==0.15.1
pytest-cov==2.12.*
pytest-freezegun==0.4.*
moto[server,kms]==5.0.8
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mongoengine==0.27.0
dnspython==2.1.0
blinker==1.4
blinker==1.8.2
103 changes: 95 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from functools import partial
from typing import Generator
import os
import subprocess
from typing import Generator, Tuple

import mongomock
import boto3
import pytest
from _pytest.monkeypatch import MonkeyPatch

# DATABASE_URI = 'mongodb://localhost:27017/db'
import requests
from bson import CodecOptions
from bson.binary import STANDARD
from pymongo.encryption import ClientEncryption


@pytest.fixture(scope='session')
Expand All @@ -20,7 +22,92 @@ def mongo_connection_url() -> Generator[str, None, None]:


@pytest.fixture(autouse=True)
def connect_database(mongo_connection_url: str) -> None:
def connect_database(mongo_connection_url: str):
import mongoengine

mongoengine.connect(host=mongo_connection_url)
return mongoengine.connect(host=mongo_connection_url)


@pytest.fixture
def kms_connection_url():
# process = subprocess.Popen(
# [
# 'moto_server',
# '-p',
# '4000',
# '-c',
# 'tests/kms_cert.crt',
# '-k',
# 'tests/kms_private_key.key',
# ]
# )
yield 'https://127.0.0.1:4000'
# import time
# time.sleep(5)
# process.kill()


KEY_NAMESPACE = 'encryption.__keyVault'
KEY_NAME = 'knox-card-key'
KMS_PROVIDER = dict(
aws=dict(
accessKeyId='test',
secretAccessKey='test',
)
)

@pytest.fixture
def master_key_kms(kms_connection_url):
"""
Creates new master key in the local kms, only for testing purpose.
:return: Tuple: master key ARN, kms region, kms host name
"""
kms = boto3.client('kms',
endpoint_url=kms_connection_url,
region_name='us-east-1',
aws_access_key_id='test',
aws_secret_access_key='test',
verify=False,
)
kms_key = kms.create_key()
return kms_key


@pytest.fixture
def create_data_key(master_key_kms, connect_database, kms_connection_url) -> Generator:
"""
Creates data keys for testing purpose. It is required in order to use
Explicit Client-Side Field Level Encryption (CSFLE)
:param master_key_kms: Tuple: master key ARN, kms region, kms host name
:return: None
"""
# client = connect(host=DB_URI)

key_name, key_coll = KEY_NAMESPACE.split(".", 1)

key_vault = connect_database[key_name][key_coll]
key_vault.drop()
key_vault.create_index(
"keyAltNames",
unique=True,
partialFilterExpression={"keyAltNames": {"$exists": True}},
)

with ClientEncryption(
KMS_PROVIDER,
KEY_NAMESPACE,
connect_database,
CodecOptions(uuid_representation=STANDARD),
) as client_encryption:
client_encryption.create_data_key(
'aws',
key_alt_names=[KEY_NAME],
master_key=dict(
key=master_key_kms['KeyMetadata']['Arn'],
region='us-east-1',
endpoint=kms_connection_url
),
)
yield
key_vault.drop()
21 changes: 21 additions & 0 deletions tests/kms_cert.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDdjCCAl4CCQCKGabuKmQzTzANBgkqhkiG9w0BAQsFADB9MQswCQYDVQQGEwJN
WDENMAsGA1UECAwEQ0RNWDENMAsGA1UEBwwEQ0RNWDEPMA0GA1UECgwGQ3VlbmNh
MQwwCgYDVQQLDANkZXYxEjAQBgNVBAMMCXByb3h5LWttczEdMBsGCSqGSIb3DQEJ
ARYOZGV2QGN1ZW5jYS5jb20wHhcNMjExMjI4MTgxMjQ1WhcNMzExMjI2MTgxMjQ1
WjB9MQswCQYDVQQGEwJNWDENMAsGA1UECAwEQ0RNWDENMAsGA1UEBwwEQ0RNWDEP
MA0GA1UECgwGQ3VlbmNhMQwwCgYDVQQLDANkZXYxEjAQBgNVBAMMCXByb3h5LWtt
czEdMBsGCSqGSIb3DQEJARYOZGV2QGN1ZW5jYS5jb20wggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQDhfW3INRRhu1lxRPq07jOQozvGiiZrBJjWlqScYsZU
kd17lrllTcH7deJ6LL8CPLZVpOQlvpO9vEviftehtkB1abMzEA2B/fV7Nb/VCk/k
oUjsdK4uEoLDbvlGXMo9I5GHQaq/6PhbDf91qK8dDmxSzOU8wN1EoyqxPYBYAc8A
hF49bCY/NtzfPPJDOkdR0BoNwc2Qn/6gtokURh+aqzrhTlaNHR1SiW61YWAPjuGq
657qL8z8qEXipAzywzekltCPCzFxyX1+od8PrRQMktlf/kM3kXXhOo1fT/SnhIjQ
WVZIJIA8Nc0n8bF+q8vpFow57lWyWEpAP4yRtBY6IBSJAgMBAAEwDQYJKoZIhvcN
AQELBQADggEBADLvJJuNENhAQE/C7JmnKr0dwZJc/EQvtqkwTtFkwGqiZ9BGxKtf
VQo0U2AJa8h035EnosmLZVF3dRP3y1nkOoVz4C6uGyanYn+JNSSkrw5UpGQI7Kgh
Lfmv8EPq+l38IhScKQw+tUOzNr+teT7cu2/brG1hJHo4HidDLZf9rzuKXkpLa7Bh
x3n1tZW/f+uZDeeEDwXLA0uo7EW5TvktOqw5oDIuM7Vyq5eB5LRdo99tHMVSqUXg
wy6ZTlWPN3Li+0Rp7W55MT1NW8L2gUQzJVcAWE4oqfNZZRQjowBiaIc24RKmQYzp
/35TIvlOVGl42hjE0SLvF0snFhru/Es0Yvw=
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions tests/kms_private_key.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDhfW3INRRhu1lx
RPq07jOQozvGiiZrBJjWlqScYsZUkd17lrllTcH7deJ6LL8CPLZVpOQlvpO9vEvi
ftehtkB1abMzEA2B/fV7Nb/VCk/koUjsdK4uEoLDbvlGXMo9I5GHQaq/6PhbDf91
qK8dDmxSzO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oGBALCr
5GsxtKY3owOXsnRz7SH78UKuQU9yy52SF72Ukf2I1afnFDp5/dWIapn3MUU+bhuk
G6JGs1GkfdHL6XRCHONNzvxMEDBZjZk98KbL2u51dz1uS10YcYtE7LaSgJ2fddU+
5JnAZlSkV6iS1oWe2o8x1XA29tx8rmRr0SK5NacBAoGBAJ07eTdzZy9nm5lksAI9
ZM78k48GRbKN+7RjERhsipIRFrQoBkQzIgqSxqJeirXcdjvqRv2aakUyr+LK205V
lgFc3+Xq/E4UHq5NlXo+oal2PDhrOGktiqhBg5wU2u0h21lyiobdTPByDE4zbaoi
ZkK7wKrX2bFQpdYkute41oKn
-----END PRIVATE KEY-----
Empty file added tests/types/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions tests/types/test_encrypted_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_encrypted_string(master_key_kms, create_data_key):
breakpoint()
assert master_key_kms

0 comments on commit 5fc937e

Please sign in to comment.