Skip to content

Commit

Permalink
[sane-pdf-reports] - assign markdown server port automatically (#34931)
Browse files Browse the repository at this point in the history
* [sane-pdf-reports] - assign markdown server port automatically

* bump rn

* rollback changes

* log on which port markdown server started

* pragma cover

* run time error

* pragma

* fix test
  • Loading branch information
GuyAfik committed Jun 19, 2024
1 parent 10664b5 commit 22397ad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Packs/Base/ReleaseNotes/1_34_14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Scripts

##### SanePdfReports

- Fixed an issue where the script failed when running multiple reports simultaneously.
29 changes: 25 additions & 4 deletions Packs/Base/Scripts/SanePdfReport/SanePdfReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@
import tempfile
from http.server import HTTPServer


def find_unused_port() -> int: # pragma: no cover
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('localhost', 0)) # tries to bind any available port on the os
return sock.getsockname()[1]
except Exception:
start_port, end_port = 10000, 30000
for port in range(start_port, end_port + 1):
is_connection_success = sock.connect_ex(('localhost', port))
if is_connection_success == 0:
demisto.debug(f'Port {port} is already used')
else:
demisto.debug(f'Port {port} is free')
return port
raise RuntimeError("Could not find available ports")
finally:
sock.close()


WORKING_DIR = Path("/app")
DISABLE_LOGOS = True # Bugfix before sane-reports can work with image files.
MD_IMAGE_PATH = '/markdown/image'
MD_HTTP_PORT = 10888
MD_HTTP_PORT = find_unused_port()
SERVER_OBJECT = None
MD_IMAGE_SUPPORT_MIN_VER = '6.5'
TABLE_TEXT_MAX_LENGTH_SUPPORT_MIN_VER = '7.0'
Expand All @@ -44,7 +64,7 @@ def find_zombie_processes():
return zombies, ps_out


def quit_driver_and_reap_children(killMarkdownServer):
def quit_driver_and_reap_children(killMarkdownServer): # pragma: no cover
try:
if killMarkdownServer:
# Kill Markdown artifacts server
Expand All @@ -66,7 +86,7 @@ def quit_driver_and_reap_children(killMarkdownServer):
demisto.error(f'Failed checking for zombie processes: {e}. Trace: {traceback.format_exc()}')


def startServer():
def startServer(): # pragma: no cover
class fileHandler(http.server.BaseHTTPRequestHandler):
# See: https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler.log_message
# Need to override otherwise messages are logged to stderr
Expand Down Expand Up @@ -114,10 +134,11 @@ def do_GET(self):
global SERVER_OBJECT
SERVER_OBJECT = HTTPServer(server_address=('', MD_HTTP_PORT), RequestHandlerClass=fileHandler)
# Start the web server
demisto.debug(f"starting markdown server on port {MD_HTTP_PORT}")
SERVER_OBJECT.serve_forever()


def main():
def main(): # pragma: no cover
try:
sane_json_b64 = demisto.args().get('sane_pdf_report_base64', '').encode(
'utf-8')
Expand Down
3 changes: 2 additions & 1 deletion Packs/Base/Scripts/SanePdfReport/SanePdfReport_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def test_sane_pdf_report(mocker):


def test_markdown_image_server(mocker, capfd):
from SanePdfReport import MD_HTTP_PORT
with capfd.disabled():
mocker.patch.object(demisto, 'results')
fileName = '1234-5678-9012-3456.png'
Expand All @@ -63,7 +64,7 @@ def test_markdown_image_server(mocker, capfd):
time.sleep(5)

# wrong path
conn = http.client.HTTPConnection("localhost", 10888)
conn = http.client.HTTPConnection("localhost", MD_HTTP_PORT)
conn.request("GET", "/wrong/path")
res1 = conn.getresponse()
assert res1.status == 400
Expand Down
2 changes: 1 addition & 1 deletion Packs/Base/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Base",
"description": "The base pack for Cortex XSOAR.",
"support": "xsoar",
"currentVersion": "1.34.13",
"currentVersion": "1.34.14",
"author": "Cortex XSOAR",
"serverMinVersion": "6.0.0",
"url": "https://www.paloaltonetworks.com/cortex",
Expand Down

0 comments on commit 22397ad

Please sign in to comment.