From 0c5686d149e472938c19a15a067e88df27930a8f Mon Sep 17 00:00:00 2001 From: Oleksandr Bezdieniezhnykh Date: Mon, 13 Apr 2026 06:47:06 +0300 Subject: [PATCH] Update project configuration and code structure - Added '*.o' to .gitignore to exclude object files from version control. - Modified Dockerfile to specify the application directory for Uvicorn. - Updated setup.py to reflect the new source directory structure for Cython extensions. - Adjusted E2E Docker Compose command to include the application directory. - Refined type hints in ApiClient and Security classes for better clarity and consistency. These changes enhance the project's organization and improve the build process. --- .gitignore | 1 + Dockerfile | 2 +- e2e/docker-compose.test.yml | 2 +- setup.py | 14 +++++++------- src/api_client.pxd | 4 ++-- src/api_client.pyx | 10 ++++------ src/security.pxd | 12 ++++++------ src/security.pyx | 12 ++++++------ 8 files changed, 28 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 48df0bc..f6aeb3f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ e2e-results/ test-results/ Logs/ *.enc +*.o diff --git a/Dockerfile b/Dockerfile index 89ef9f7..1742f7f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,4 +12,4 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . RUN python setup.py build_ext --inplace EXPOSE 8080 -CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"] +CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080", "--app-dir", "src"] diff --git a/e2e/docker-compose.test.yml b/e2e/docker-compose.test.yml index 2c09b6b..4d2e144 100644 --- a/e2e/docker-compose.test.yml +++ b/e2e/docker-compose.test.yml @@ -23,7 +23,7 @@ services: build: context: .. dockerfile: Dockerfile - command: bash -c "rm -rf /app/models/* && mkdir -p /app/models && python -m uvicorn main:app --host 0.0.0.0 --port 8080" + command: bash -c "rm -rf /app/models/* && mkdir -p /app/models && python -m uvicorn main:app --host 0.0.0.0 --port 8080 --app-dir src" ports: - "8080:8080" depends_on: diff --git a/setup.py b/setup.py index 29d8d6d..bd00db2 100644 --- a/setup.py +++ b/setup.py @@ -2,13 +2,13 @@ from setuptools import setup, Extension from Cython.Build import cythonize extensions = [ - Extension('constants', ['constants.pyx']), - Extension('credentials', ['credentials.pyx']), - Extension('user', ['user.pyx']), - Extension('security', ['security.pyx']), - Extension('hardware_service', ['hardware_service.pyx']), - Extension('cdn_manager', ['cdn_manager.pyx']), - Extension('api_client', ['api_client.pyx']), + Extension('constants', ['src/constants.pyx']), + Extension('credentials', ['src/credentials.pyx']), + Extension('user', ['src/user.pyx']), + Extension('security', ['src/security.pyx']), + Extension('hardware_service', ['src/hardware_service.pyx']), + Extension('cdn_manager', ['src/cdn_manager.pyx']), + Extension('api_client', ['src/api_client.pyx']), ] setup( diff --git a/src/api_client.pxd b/src/api_client.pxd index ea3cd53..f84ec55 100644 --- a/src/api_client.pxd +++ b/src/api_client.pxd @@ -15,8 +15,8 @@ cdef class ApiClient: cdef login(self) cdef set_token(self, str token) - cdef request(self, str method, str url, object payload, bint is_stream) - cdef load_bytes(self, str filename, str folder) + cdef request(self, str method, str url, str payload, bint is_stream) + cdef bytes load_bytes(self, str filename, str folder) cdef upload_file(self, str filename, bytes resource, str folder) cdef load_big_file_cdn(self, str folder, str big_part) cpdef load_big_small_resource(self, str resource_name, str folder) diff --git a/src/api_client.pyx b/src/api_client.pyx index 681fb5c..bacbeed 100644 --- a/src/api_client.pyx +++ b/src/api_client.pyx @@ -16,10 +16,6 @@ from user cimport User, RoleEnum cdef class ApiClient: def __init__(self, str api_url): - self.credentials = None - self.user = None - self.token = None - self.cdn_manager = None self.api_url = api_url cpdef set_credentials_from_dict(self, str email, str password): @@ -51,6 +47,8 @@ cdef class ApiClient: token = response.json()["token"] self.set_token(token) except HTTPError as e: + if response is None: + raise res = response.json() constants.logerror(str(res)) if response.status_code == HTTPStatus.CONFLICT: @@ -93,7 +91,7 @@ cdef class ApiClient: r.raise_for_status() constants.log(f"Uploaded {filename} to {self.api_url}/{folder} successfully: {r.status_code}.") - cdef load_bytes(self, str filename, str folder): + cdef bytes load_bytes(self, str filename, str folder): if self.credentials is None: raise Exception("No credentials set") cdef str hardware = HardwareService.get_hardware_info() @@ -112,7 +110,7 @@ cdef class ApiClient: constants.log(f'Downloaded file: {filename}, {len(data)} bytes') return data - cdef request(self, str method, str url, object payload, bint is_stream): + cdef request(self, str method, str url, str payload, bint is_stream): if self.token is None: self.login() headers = { diff --git a/src/security.pxd b/src/security.pxd index e0e92ed..cb0c74f 100644 --- a/src/security.pxd +++ b/src/security.pxd @@ -2,19 +2,19 @@ from credentials cimport Credentials cdef class Security: @staticmethod - cdef encrypt_to(input_stream, key) + cdef bytes encrypt_to(bytes input_bytes, str key) @staticmethod - cdef decrypt_to(input_bytes, key) + cdef bytes decrypt_to(bytes input_bytes, str key) @staticmethod - cdef get_hw_hash(str hardware) + cdef str get_hw_hash(str hardware) @staticmethod - cdef get_api_encryption_key(Credentials credentials, str hardware_hash) + cdef str get_api_encryption_key(Credentials credentials, str hardware_hash) @staticmethod - cdef get_resource_encryption_key() + cdef str get_resource_encryption_key() @staticmethod - cdef calc_hash(str key) \ No newline at end of file + cdef str calc_hash(str key) \ No newline at end of file diff --git a/src/security.pyx b/src/security.pyx index 25610f0..0260960 100644 --- a/src/security.pyx +++ b/src/security.pyx @@ -11,7 +11,7 @@ BUFFER_SIZE = 64 * 1024 # 64 KB cdef class Security: @staticmethod - cdef encrypt_to(input_bytes, key): + cdef bytes encrypt_to(bytes input_bytes, str key): cdef bytes aes_key = hashlib.sha256(key.encode('utf-8')).digest() iv = os.urandom(16) @@ -25,7 +25,7 @@ cdef class Security: return iv + ciphertext @staticmethod - cdef decrypt_to(ciphertext_with_iv_bytes, key): + cdef bytes decrypt_to(bytes ciphertext_with_iv_bytes, str key): cdef bytes aes_key = hashlib.sha256(key.encode('utf-8')).digest() iv = ciphertext_with_iv_bytes[:16] ciphertext_bytes = ciphertext_with_iv_bytes[16:] @@ -41,22 +41,22 @@ cdef class Security: return bytes(plaintext_bytes) @staticmethod - cdef get_hw_hash(str hardware): + cdef str get_hw_hash(str hardware): cdef str key = f'Azaion_{hardware}_%$$$)0_' return Security.calc_hash(key) @staticmethod - cdef get_api_encryption_key(Credentials creds, str hardware_hash): + cdef str get_api_encryption_key(Credentials creds, str hardware_hash): cdef str key = f'{creds.email}-{creds.password}-{hardware_hash}-#%@AzaionKey@%#---' return Security.calc_hash(key) @staticmethod - cdef get_resource_encryption_key(): + cdef str get_resource_encryption_key(): cdef str key = '-#%@AzaionKey@%#---234sdfklgvhjbnn' return Security.calc_hash(key) @staticmethod - cdef calc_hash(str key): + cdef str calc_hash(str key): str_bytes = key.encode('utf-8') hash_bytes = sha384(str_bytes).digest() cdef str h = base64.b64encode(hash_bytes).decode('utf-8')