Skip to content

Commit

Permalink
Merge branch 'eset-protect-platform-solution-3.0.1' of https://github…
Browse files Browse the repository at this point in the history
  • Loading branch information
v-prasadboke committed Nov 27, 2024
2 parents d1d1000 + e29f7e2 commit a0badfc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 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.

0 comments on commit a0badfc

Please sign in to comment.