Skip to content

Commit

Permalink
fix(AZURE): Refactor FalconCache to use thread-safe locks
Browse files Browse the repository at this point in the history
This commit refactors the `FalconCache` class to use thread-safe locks when accessing the `_arc_config` dictionary. Previously, multiple threads could access the dictionary simultaneously, leading to potential race conditions and data corruption.

- Add `_arc_config_lock` dictionary to store locks for each sensor ID
- Use `with` statement to acquire and release lock when accessing `_arc_config` dictionary

Fixes #160
  • Loading branch information
carlosmmatos committed Jul 26, 2023
1 parent 2751f3c commit 066c31a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/venv
/**venv
*.pyc
*.swp
*.egg
*.egg-info
/config/devel.ini
/runtest.sh
/runtest.sh
25 changes: 17 additions & 8 deletions fig/falcon_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from threading import Lock
from .falcon import Event
from .log import log

Expand All @@ -21,6 +22,7 @@ def __init__(self, falcon_api):
self._host_detail = {}
self._mdm_id = {}
self._arc_config = {}
self._arc_config_lock = {}

def device_details(self, sensor_id):
if not sensor_id:
Expand All @@ -41,14 +43,21 @@ def device_details(self, sensor_id):
def azure_arc_config(self, sensor_id):
if not sensor_id:
return EventDataError("Cannot fetch Azure Arc info. SensorId field is missing")
if sensor_id not in self._arc_config:
is_linux = self.device_details(sensor_id)['platform_name'] == 'Linux'
path = '/var/opt/azcmagent/agentconfig.json' if is_linux else 'C:\\ProgramData\\AzureConnectedMachineAgent\\Config\\agentconfig.json'
log.info('Fetching Azure Arc Config %s from the system %s', path, sensor_id)
file_bytes = self.falcon_api.rtr_fetch_file(sensor_id, path)
log.info('Fetched Azure Arc Config from the system: %s', str(file_bytes))
self._arc_config[sensor_id] = json.loads(file_bytes)
return self._arc_config[sensor_id]

with self._get_lock(sensor_id):
if sensor_id not in self._arc_config:
is_linux = self.device_details(sensor_id)['platform_name'] == 'Linux'
path = '/var/opt/azcmagent/agentconfig.json' if is_linux else 'C:\\ProgramData\\AzureConnectedMachineAgent\\Config\\agentconfig.json'
log.info('Fetching Azure Arc Config %s from the system %s', path, sensor_id)
file_bytes = self.falcon_api.rtr_fetch_file(sensor_id, path)
log.info('Fetched Azure Arc Config from the system: %s', str(file_bytes))
self._arc_config[sensor_id] = json.loads(file_bytes)
return self._arc_config[sensor_id]

def _get_lock(self, sensor_id):
if sensor_id not in self._arc_config_lock:
self._arc_config_lock[sensor_id] = Lock()
return self._arc_config_lock[sensor_id]

def mdm_identifier(self, sensor_id, event_platform):
if not sensor_id:
Expand Down

0 comments on commit 066c31a

Please sign in to comment.