Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rabbit payload size limiter #95

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions modular_sdk/services/impl/maestro_http_transport_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import urllib.parse
import urllib.error
from typing import Any
from modular_sdk.commons import (
ModularException, generate_id, build_secure_message, build_message,
)
from modular_sdk.commons import ModularException, generate_id, build_message
from modular_sdk.commons.constants import SUCCESS_STATUS
from modular_sdk.commons.log_helper import get_logger
from modular_sdk.services.impl.maestro_signature_builder import (
Expand Down Expand Up @@ -45,10 +43,9 @@ def __init__(
self.timeout = timeout or HTTP_DEFAULT_RESPONSE_TIMEOUT

def pre_process_request(self, command_name: str, parameters: list[dict] | dict,
secure_parameters: list | None = None,
is_flat_request: bool = False,
async_request: bool = False,
compressed: bool = False, config=None
compressed: bool = False, config=None, **kwargs
) -> tuple[bytes, dict]:
request_id = generate_id()
_LOG.debug('Going to pre-process HTTP request')
Expand All @@ -59,20 +56,10 @@ def pre_process_request(self, command_name: str, parameters: list[dict] | dict,
is_flat_request=is_flat_request,
compressed=compressed,
)
secure_message = message
# todo that is strange because why uncompressed data
# should lack parameters?
if not compressed:
secure_message = build_secure_message(
command_name=command_name,
parameters_to_secure=parameters,
secure_parameters=secure_parameters,
request_id=request_id,
is_flat_request=is_flat_request,
)
_LOG.debug(
f'Prepared command: {command_name}\nCommand format: {secure_message}'
)
_LOG.info(f'Going to send rabbit '
f'request: {command_name=}, {request_id=}, '
f'{is_flat_request=}')

signer = MaestroSignatureBuilder(
access_key=config.sdk_access_key if config and config.sdk_access_key else self.access_key,
secret_key=config.sdk_secret_key if config and config.sdk_secret_key else self.secret_key,
Expand Down Expand Up @@ -114,15 +101,13 @@ def post_process_request(self, response: bytes) -> tuple[int, str, Any]:
return code, status, data

def send_sync(self, command_name: str, parameters: list[dict] | dict,
secure_parameters: list | None = None,
is_flat_request: bool = False, async_request: bool = False,
compressed: bool = False, config=None
compressed: bool = False, config=None, **kwargs
) -> tuple[int, str, Any]:
_LOG.debug('Making sync http request ')
message, headers = self.pre_process_request(
command_name=command_name,
parameters=parameters,
secure_parameters=secure_parameters,
is_flat_request=is_flat_request,
async_request=async_request,
compressed=compressed,
Expand Down
28 changes: 7 additions & 21 deletions modular_sdk/services/impl/maestro_rabbit_transport_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import binascii
import json
from typing import Any
from modular_sdk.commons import (
ModularException, generate_id, build_secure_message, build_message,
)
from modular_sdk.commons.constants import (
PLAIN_CONTENT_TYPE,
SUCCESS_STATUS,
ERROR_STATUS,
RESULTS,
DATA
) # todo remove these imports with major release. They can be used from outside
from modular_sdk.commons import ModularException, generate_id, build_message
from modular_sdk.commons.constants import SUCCESS_STATUS
from modular_sdk.commons.log_helper import get_logger
from modular_sdk.connections.rabbit_connection import RabbitMqConnection
from modular_sdk.services.impl.maestro_signature_builder import (
Expand Down Expand Up @@ -48,9 +40,9 @@ def __init__(self, rabbit_connection: RabbitMqConnection,
self.secret_key = config.sdk_secret_key
self.user = config.maestro_user

def pre_process_request(self, command_name, parameters, secure_parameters,
def pre_process_request(self, command_name, parameters,
is_flat_request, async_request, compressed=False,
config=None) -> tuple[str | bytes, dict]:
config=None, **kwargs) -> tuple[str | bytes, dict]:
request_id = generate_id()
_LOG.debug('Going to pre-process request')
message = build_message(
Expand All @@ -60,15 +52,9 @@ def pre_process_request(self, command_name, parameters, secure_parameters,
is_flat_request=is_flat_request,
compressed=compressed
)
secure_message = message
if not compressed:
secure_message = build_secure_message(
command_name=command_name,
parameters_to_secure=parameters,
secure_parameters=secure_parameters,
request_id=request_id,
is_flat_request=is_flat_request
)
_LOG.info(f'Going to send rabbit '
f'request: {command_name=}, {request_id=}, '
f'{is_flat_request=}')

signer = MaestroSignatureBuilder(
access_key=config.sdk_access_key if config and config.sdk_access_key else self.access_key,
Expand Down
10 changes: 3 additions & 7 deletions modular_sdk/services/impl/maestro_signature_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def encrypt(self, data: str | dict | list) -> bytes:
raise ValueError(str(e).replace('AESGCM key', 'Secret Key'))
encrypted_data = cipher.encrypt(
nonce=iv, data=data_in_bytes, associated_data=None)
encrypted_data_with_iv = bytes(iv) + encrypted_data
return base64.b64encode(encrypted_data_with_iv)
return base64.b64encode(iv + encrypted_data)

def get_signed_headers(self, async_request: bool = False,
compressed: bool = False) -> dict:
Expand All @@ -65,11 +64,8 @@ def get_signed_headers(self, async_request: bool = False,
"""
date = int(time.time() * 1000)
signature = hmac.new(
key=bytearray(f'{self._secret_key}{date}'.encode('utf-8')),
msg=bytearray(
f'M3-POST:{self._access_key}:{date}:{self._user}'.encode(
'utf-8')
),
key=f'{self._secret_key}{date}'.encode('utf-8'),
msg=f'M3-POST:{self._access_key}:{date}:{self._user}'.encode('utf-8'),
digestmod=hashlib.sha256
).hexdigest()
n = 2
Expand Down
8 changes: 1 addition & 7 deletions modular_sdk/services/rabbit_transport_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
from typing import TYPE_CHECKING, Any
from pika import exceptions
from modular_sdk.commons import ModularException, generate_id_hex
from modular_sdk.commons.constants import (
PLAIN_CONTENT_TYPE,
SUCCESS_STATUS,
ERROR_STATUS,
RESULTS,
DATA
) # todo remove these imports with major release. They can be used from outside
from modular_sdk.commons.constants import PLAIN_CONTENT_TYPE
from modular_sdk.commons.log_helper import get_logger

if TYPE_CHECKING:
Expand Down
Loading