From 59eb39d4472665d620afa722d57f653cf90e00ff Mon Sep 17 00:00:00 2001 From: Alex Bezdieniezhnykh Date: Fri, 2 May 2025 09:41:04 +0300 Subject: [PATCH] switch from hardware object to string and replace mac address to disk serial number for more predictable hash key --- Azaion.Inference/api_client.pyx | 6 +-- Azaion.Inference/build.cmd | 1 - Azaion.Inference/hardware_service.pxd | 7 +--- Azaion.Inference/hardware_service.pyx | 41 ++++--------------- Azaion.Inference/security.pxd | 3 +- Azaion.Inference/security.pyx | 6 +-- .../test/test_download_large_file.py | 2 +- 7 files changed, 16 insertions(+), 50 deletions(-) diff --git a/Azaion.Inference/api_client.pyx b/Azaion.Inference/api_client.pyx index dffaf3f..146b4e9 100644 --- a/Azaion.Inference/api_client.pyx +++ b/Azaion.Inference/api_client.pyx @@ -8,7 +8,7 @@ cimport constants import yaml from cdn_manager cimport CDNManager, CDNCredentials -from hardware_service cimport HardwareService, HardwareInfo +from hardware_service cimport HardwareService from security cimport Security from user cimport User, RoleEnum @@ -85,7 +85,7 @@ cdef class ApiClient: cdef load_bytes(self, str filename, str folder): hardware_service = HardwareService() - cdef HardwareInfo hardware = hardware_service.get_hardware_info() + cdef str hardware = hardware_service.get_hardware_info() if self.token is None: self.login() @@ -98,7 +98,7 @@ cdef class ApiClient: payload = json.dumps( { "password": self.credentials.password, - "hardware": hardware.to_json_object(), + "hardware": hardware, "fileName": filename }, indent=4) response = requests.post(url, data=payload, headers=headers, stream=True) diff --git a/Azaion.Inference/build.cmd b/Azaion.Inference/build.cmd index 6b451e1..11b7b07 100644 --- a/Azaion.Inference/build.cmd +++ b/Azaion.Inference/build.cmd @@ -30,7 +30,6 @@ venv\Scripts\pyinstaller --name=azaion-inference ^ --collect-all pycuda ^ --collect-all pynvml ^ --collect-all boto3 ^ ---collect-all re ^ --collect-all jwt ^ --hidden-import constants ^ --hidden-import annotation ^ diff --git a/Azaion.Inference/hardware_service.pxd b/Azaion.Inference/hardware_service.pxd index 2a700a2..628e9e7 100644 --- a/Azaion.Inference/hardware_service.pxd +++ b/Azaion.Inference/hardware_service.pxd @@ -1,11 +1,6 @@ -cdef class HardwareInfo: - cdef str cpu, gpu, memory, mac_address - cdef to_json_object(self) - cdef class HardwareService: cdef bint is_windows - cdef get_mac_address(self, interface=*) @staticmethod cdef has_nvidia_gpu() - cdef HardwareInfo get_hardware_info(self) \ No newline at end of file + cdef str get_hardware_info(self) \ No newline at end of file diff --git a/Azaion.Inference/hardware_service.pyx b/Azaion.Inference/hardware_service.pyx index 9782b21..0ccf1f0 100644 --- a/Azaion.Inference/hardware_service.pyx +++ b/Azaion.Inference/hardware_service.pyx @@ -1,25 +1,6 @@ -import re import subprocess -import psutil import pynvml -cdef class HardwareInfo: - def __init__(self, str cpu, str gpu, str memory, str mac_address): - self.cpu = cpu - self.gpu = gpu - self.memory = memory - self.mac_address = mac_address - - cdef to_json_object(self): - return { - "CPU": self.cpu, - "GPU": self.gpu, - "MacAddress": self.mac_address, - "Memory": self.memory - } - - def __str__(self): - return f'CPU: {self.cpu}. GPU: {self.gpu}. Memory: {self.memory}. MAC Address: {self.mac_address}' cdef class HardwareService: """Handles hardware information retrieval and hash generation.""" @@ -35,15 +16,6 @@ cdef class HardwareService: print('Error during os type checking') self.is_windows = False - cdef get_mac_address(self, interface="Ethernet"): - addresses = psutil.net_if_addrs() - for interface_name, interface_info in addresses.items(): - if interface_name == interface: - for addr in interface_info: - if addr.family == psutil.AF_LINK: - return addr.address.replace('-', '') - return None - @staticmethod cdef has_nvidia_gpu(): try: @@ -67,13 +39,14 @@ cdef class HardwareService: print('Failed to shutdown pynvml cause probably no NVidia GPU') pass - cdef HardwareInfo get_hardware_info(self): + cdef str get_hardware_info(self): if self.is_windows: os_command = ( "powershell -Command \"" "Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name | Write-Output; " "Get-CimInstance -ClassName Win32_VideoController | Select-Object -ExpandProperty Name | Write-Output; " - "Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize | Write-Output" + "Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize | Write-Output; " + "(Get-Disk | Where-Object {$_.IsSystem -eq $true}).SerialNumber" "\"" ) else: @@ -81,6 +54,7 @@ cdef class HardwareService: "/bin/bash -c \" lscpu | grep 'Model name:' | cut -d':' -f2 && " "lspci | grep VGA | cut -d':' -f3 && " "free -g | grep Mem: | awk '{print $2}' && \"" + "udevadm info --query=property --name=\"/dev/$(lsblk -no pkname \"$(findmnt -n -o SOURCE --target /)\")\" | grep -E 'ID_SERIAL=|ID_SERIAL_SHORT=' | cut -d'=' -f2- | head -n1 && " ) # in case of subprocess error do: # cdef bytes os_command_bytes = os_command.encode('utf-8') @@ -90,7 +64,8 @@ cdef class HardwareService: cdef str cpu = lines[0].replace("Name=", "").replace(" ", " ") cdef str gpu = lines[1].replace("Name=", "").replace(" ", " ") - cdef str memory = lines[2].replace("TotalVisibleMemorySize=", "").replace(" ", " ") - cdef str mac_address = self.get_mac_address() + # could be multiple gpus + cdef str memory = lines[-2].replace("TotalVisibleMemorySize=", "").replace(" ", " ") + cdef str drive_serial = lines[-1] - return HardwareInfo(cpu, gpu, memory, mac_address) + return f'CPU: {cpu}. GPU: {gpu}. Memory: {memory}. Drive Serial: {drive_serial}' diff --git a/Azaion.Inference/security.pxd b/Azaion.Inference/security.pxd index ba6f4d8..ad1396f 100644 --- a/Azaion.Inference/security.pxd +++ b/Azaion.Inference/security.pxd @@ -1,5 +1,4 @@ from credentials cimport Credentials -from hardware_service cimport HardwareInfo cdef class Security: @staticmethod @@ -9,7 +8,7 @@ cdef class Security: cdef decrypt_to(input_bytes, key) @staticmethod - cdef get_hw_hash(HardwareInfo hardware) + cdef get_hw_hash(str hardware) @staticmethod cdef get_api_encryption_key(Credentials credentials, str hardware_hash) diff --git a/Azaion.Inference/security.pyx b/Azaion.Inference/security.pyx index aabc5e2..7bada8a 100644 --- a/Azaion.Inference/security.pyx +++ b/Azaion.Inference/security.pyx @@ -3,8 +3,6 @@ import hashlib import os from hashlib import sha384 from credentials cimport Credentials -from hardware_service cimport HardwareInfo - from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives import padding @@ -48,8 +46,8 @@ cdef class Security: return bytes(plaintext_bytes) @staticmethod - cdef get_hw_hash(HardwareInfo hardware): - cdef str key = f'Azaion_{hardware.mac_address}_{hardware.cpu}_{hardware.gpu}' + cdef get_hw_hash(str hardware): + cdef str key = f'Azaion_{hardware}_%$$$)0_' return Security.calc_hash(key) @staticmethod diff --git a/Azaion.Inference/test/test_download_large_file.py b/Azaion.Inference/test/test_download_large_file.py index 0b17ecf..185f357 100644 --- a/Azaion.Inference/test/test_download_large_file.py +++ b/Azaion.Inference/test/test_download_large_file.py @@ -202,7 +202,7 @@ class Api: payload = json.dumps( { "password": self.credentials.password, - "hardware": hardware.to_json_object(), + "hardware": hardware, "fileName": filename }, indent=4) response = requests.post(url, data=payload, headers=headers, stream=True, timeout=20)