Skip to content

Commit

Permalink
Revert 3.0.1 to 3.0.0. Function code refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
eset-enterpise-integration committed Nov 22, 2024
1 parent 2911ab0 commit e29f7e2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def __init__(self) -> None:
self.token_provider = TokenProvider(TokenStorage(), self.request_sender, self.env_vars, self.config.buffer)
self.transformer_detections = TransformerDetections(self.env_vars)
self._session: ClientSession | None = None
self.lock = asyncio.Lock()
self._lock = asyncio.Lock()

async def close(self) -> None:
if self._session and not self._session.closed:
await self._session.close()

async def run(self) -> None:
self._session = ClientSession()
self._session = ClientSession(raise_for_status=True)
start_time = time.time()
try:
await asyncio.gather(
Expand Down Expand Up @@ -108,7 +108,8 @@ async def _call_service(

if not self.token_provider.token.access_token or datetime.now(timezone.utc) > self.token_provider.token.expiration_time: # type: ignore
assert self._session
await self.token_provider.get_token(self._session, self.lock)
async with self._lock:
await self.token_provider.get_token(self._session)

try:
if (
Expand All @@ -117,7 +118,7 @@ async def _call_service(
):
data = await self.request_sender.send_request(
self.request_sender.send_request_get,
self._session, # type: ignore
self._session, # type: ignore
{
"Authorization": f"Bearer {self.token_provider.token.access_token}",
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async def send_request_post(
data=urllib.parse.quote(f"grant_type={grant_type}", safe="=&/"),
timeout=self.config.requests_timeout,
) as response:
response.raise_for_status()
return await response.json()

async def send_request_get(
Expand All @@ -92,7 +91,6 @@ async def send_request_get(
headers=headers,
params=self._prepare_get_request_params(last_detection_time, next_page_token, page_size),
) as response:
response.raise_for_status()
return await response.json()

def _prepare_get_request_params(
Expand Down Expand Up @@ -133,38 +131,37 @@ def get_token_params_from_storage(self) -> None:
value = ""
setattr(self.token, token_param, value)

async def get_token(self, session: ClientSession, lock: asyncio.Lock) -> None:
async with lock:
async def get_token(self, session: ClientSession) -> None:

if not self.token.access_token or datetime.now(timezone.utc) > self.token.expiration_time: # type: ignore
logging.info(f"Getting token")
if not self.token.access_token or datetime.now(timezone.utc) > self.token.expiration_time: # type: ignore
logging.info("Getting token")

if not self.token.access_token and (not self.env_vars.username or not self.env_vars.password):
raise MissingCredentialsException()
if not self.token.access_token and (not self.env_vars.username or not self.env_vars.password):
raise MissingCredentialsException()

grant_type = (
f"refresh_token&refresh_token={self.token.refresh_token}"
if self.token.access_token
else f"password&username={self.env_vars.username}&password={self.env_vars.password}"
)
grant_type = (
f"refresh_token&refresh_token={self.token.refresh_token}"
if self.token.access_token
else f"password&username={self.env_vars.username}&password={self.env_vars.password}"
)

try:
response = await self.requests_sender.send_request(
self.requests_sender.send_request_post,
session,
{"Content-type": "application/x-www-form-urlencoded", "3rd-integration": "MS-Sentinel"},
grant_type,
)
except AuthenticationException as e:
if not self.token.access_token:
raise InvalidCredentialsException(e)
else:
self.storage_table_handler.input_entity({k: "" for k in self.token.to_dict()}) # type: ignore[call-arg]
raise TokenRefreshException(e)

if response:
self.set_token_params_locally_and_in_storage(response)
logging.info("Token obtained successfully")
try:
response = await self.requests_sender.send_request(
self.requests_sender.send_request_post,
session,
{"Content-type": "application/x-www-form-urlencoded", "3rd-integration": "MS-Sentinel"},
grant_type,
)
except AuthenticationException as e:
if not self.token.access_token:
raise InvalidCredentialsException(e)
else:
self.storage_table_handler.input_entity({k: "" for k in self.token.to_dict()}) # type: ignore[call-arg]
raise TokenRefreshException(e)

if response:
self.set_token_params_locally_and_in_storage(response)
logging.info("Token obtained successfully")

def set_token_params_locally_and_in_storage(self, response: t.Dict[str, str | int]) -> None:
self.token.access_token = t.cast(str, response["access_token"])
Expand Down
Binary file modified Solutions/ESET Protect Platform/Package/3.0.0.zip
Binary file not shown.
Binary file removed Solutions/ESET Protect Platform/Package/3.0.1.zip
Binary file not shown.
8 changes: 4 additions & 4 deletions Solutions/ESET Protect Platform/Package/mainTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"variables": {
"_solutionName": "ESET Protect Platform",
"_solutionVersion": "3.0.1",
"_solutionVersion": "3.0.0",
"solutionId": "eset.eset-protect-platform-solution",
"_solutionId": "[variables('solutionId')]",
"uiConfigId1": "ESETProtectPlatform",
Expand Down Expand Up @@ -62,7 +62,7 @@
"[extensionResourceId(resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspace')), 'Microsoft.SecurityInsights/contentPackages', variables('_solutionId'))]"
],
"properties": {
"description": "ESET Protect Platform data connector with template version 3.0.1",
"description": "ESET Protect Platform data connector with template version 3.0.0",
"mainTemplate": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "[variables('dataConnectorVersion1')]",
Expand Down Expand Up @@ -349,7 +349,7 @@
"[extensionResourceId(resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspace')), 'Microsoft.SecurityInsights/contentPackages', variables('_solutionId'))]"
],
"properties": {
"description": "ESETProtectPlatform Data Parser with template version 3.0.1",
"description": "ESETProtectPlatform Data Parser with template version 3.0.0",
"mainTemplate": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "[variables('parserObject1').parserVersion1]",
Expand Down Expand Up @@ -475,7 +475,7 @@
"apiVersion": "2023-04-01-preview",
"location": "[parameters('workspace-location')]",
"properties": {
"version": "3.0.1",
"version": "3.0.0",
"kind": "Solution",
"contentSchemaVersion": "3.0.0",
"displayName": "ESET Protect Platform",
Expand Down

0 comments on commit e29f7e2

Please sign in to comment.