Skip to content

Try-catch background process and bugfix for READ_PROPERTY #46

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

Merged
merged 1 commit into from
Aug 7, 2024
Merged
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
64 changes: 38 additions & 26 deletions aikido_firewall/background_process/aikido_background_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,45 @@
conn = listener.accept()
logger.debug("connection accepted from %s", listener.last_accepted)
while True:
data = conn.recv() # because of this no sleep needed in thread
logger.debug("Incoming data : %s", data)
if data[0] == "ATTACK":
self.queue.put(data[1])
elif data[0] == "CLOSE": # this is a kind of EOL for python IPC
conn.close()
break
elif (
data[0] == "KILL"
): # when main process quits , or during testing etc
logger.debug("Killing subprocess")
conn.close()
sys.exit(0)
elif data[0] == "READ_PROPERTY": # meant to get config props
if hasattr(self.reporter, data[1]):
conn.send(self.reporter.__dict__[data[1]])
elif data[0] == "ROUTE":
# Called every time the user visits a route
self.reporter.routes.add_route(method=data[1][0], path=data[1][1])
elif data[0] == "SHOULD_RATELIMIT":
# Called to check if the context passed along as data should be
# Rate limited
conn.send(
should_ratelimit_request(
context=data[1], reporter=self.reporter
try:
data = conn.recv() # because of this no sleep needed in thread
logger.debug("Incoming data : %s", data)
if data[0] == "ATTACK":
self.queue.put(data[1])
elif data[0] == "CLOSE": # this is a kind of EOL for python IPC
conn.close()
break
elif (

Check warning on line 55 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L47-L55

Added lines #L47 - L55 were not covered by tests
data[0] == "KILL"
): # when main process quits , or during testing etc
logger.debug("Killing subprocess")
conn.close()
sys.exit(0)
elif data[0] == "READ_PROPERTY": # meant to get config props
if hasattr(self.reporter, data[1]):
conn.send(self.reporter.__dict__[data[1]])

Check warning on line 63 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L58-L63

Added lines #L58 - L63 were not covered by tests
else:
logger.debug(

Check warning on line 65 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L65

Added line #L65 was not covered by tests
"Reporter has no attribute %s, current reporter: %s",
data[1],
self.reporter,
)
conn.send(None)
elif data[0] == "ROUTE":

Check warning on line 71 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L70-L71

Added lines #L70 - L71 were not covered by tests
# Called every time the user visits a route
self.reporter.routes.add_route(

Check warning on line 73 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L73

Added line #L73 was not covered by tests
method=data[1][0], path=data[1][1]
)
)
elif data[0] == "SHOULD_RATELIMIT":

Check warning on line 76 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L76

Added line #L76 was not covered by tests
# Called to check if the context passed along as data should be
# Rate limited
conn.send(

Check warning on line 79 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L79

Added line #L79 was not covered by tests
should_ratelimit_request(
context=data[1], reporter=self.reporter
)
)
except Exception as e:
logger.error("Exception occured in server thread : %s", e)

Check warning on line 85 in aikido_firewall/background_process/aikido_background_process.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/aikido_background_process.py#L84-L85

Added lines #L84 - L85 were not covered by tests

def reporting_thread(self):
"""Reporting thread"""
Expand Down
Loading