From 9981e452e278805fa1426e3bcfb62103040c03be Mon Sep 17 00:00:00 2001 From: BlackOrder <4302157+BlackOrder@users.noreply.github.com> Date: Sun, 1 Jan 2023 12:03:50 +0400 Subject: [PATCH 1/8] if path is url, use urllib.request to get --- jasmin/protocols/cli/mointerceptorm.py | 17 ++++++++++++++--- jasmin/protocols/cli/mtinterceptorm.py | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/jasmin/protocols/cli/mointerceptorm.py b/jasmin/protocols/cli/mointerceptorm.py index ba52a89e..2a4488b7 100644 --- a/jasmin/protocols/cli/mointerceptorm.py +++ b/jasmin/protocols/cli/mointerceptorm.py @@ -133,9 +133,20 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): stype, script_path = validate_typed_script(arg) if stype == 'python3': - # Open file and get its content - with open(script_path, 'r') as content_file: - pyCode = content_file.read() + import os + if os.path.isfile(script_path): + # Open file and get its content + with open(script_path, 'r') as content_file: + pyCode = content_file.read() + else: + # Assume it's a URL + import urllib.request + try: + with urllib.request.urlopen(script_path) as content_file: + pyCode = content_file.read().decode('utf-8') + except urllib.error.URLError as e: + # Handle errors that may occur while reading the file from a URL + return self.protocol.sendData('[URL]: %s' % str(e)) # Test compilation of the script compile(pyCode, '', 'exec') diff --git a/jasmin/protocols/cli/mtinterceptorm.py b/jasmin/protocols/cli/mtinterceptorm.py index 29a2ef28..c6e93b29 100644 --- a/jasmin/protocols/cli/mtinterceptorm.py +++ b/jasmin/protocols/cli/mtinterceptorm.py @@ -133,9 +133,20 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): stype, script_path = validate_typed_script(arg) if stype == 'python3': - # Open file and get its content - with open(script_path, 'r') as content_file: - pyCode = content_file.read() + import os + if os.path.isfile(script_path): + # Open file and get its content + with open(script_path, 'r') as content_file: + pyCode = content_file.read() + else: + # Assume it's a URL + import urllib.request + try: + with urllib.request.urlopen(script_path) as content_file: + pyCode = content_file.read().decode('utf-8') + except urllib.error.URLError as e: + # Handle errors that may occur while reading the file from a URL + return self.protocol.sendData('[URL]: %s' % str(e)) # Test compilation of the script compile(pyCode, '', 'exec') From 1fd35292359738f0c0a74bae25ef2d559823b99f Mon Sep 17 00:00:00 2001 From: BlackOrder <4302157+BlackOrder@users.noreply.github.com> Date: Sun, 1 Jan 2023 12:06:13 +0400 Subject: [PATCH 2/8] InvalidScriptSyntax message inclue url example --- jasmin/protocols/cli/mointerceptorm.py | 2 +- jasmin/protocols/cli/mtinterceptorm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jasmin/protocols/cli/mointerceptorm.py b/jasmin/protocols/cli/mointerceptorm.py index 2a4488b7..879e00de 100644 --- a/jasmin/protocols/cli/mointerceptorm.py +++ b/jasmin/protocols/cli/mointerceptorm.py @@ -22,7 +22,7 @@ def validate_typed_script(script): m = re.match(r'(python3)\((.*)\)', script, re.I) if not m: - raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script).') + raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script) or python3(https://domain.com/path/to/script).') else: language = m.group(1).lower() script_path = m.group(2) diff --git a/jasmin/protocols/cli/mtinterceptorm.py b/jasmin/protocols/cli/mtinterceptorm.py index c6e93b29..6d2eb300 100644 --- a/jasmin/protocols/cli/mtinterceptorm.py +++ b/jasmin/protocols/cli/mtinterceptorm.py @@ -22,7 +22,7 @@ def validate_typed_script(script): m = re.match(r'(python3)\((.*)\)', script, re.I) if not m: - raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script).') + raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script) or python3(https://domain.com/path/to/script).') else: language = m.group(1).lower() script_path = m.group(2) From 987be21ce7f60dcd3a066048b827ab58c4783e79 Mon Sep 17 00:00:00 2001 From: BlackOrder <4302157+BlackOrder@users.noreply.github.com> Date: Mon, 2 Jan 2023 18:36:29 +0400 Subject: [PATCH 3/8] handle only http/s, ftp, and file --- jasmin/protocols/cli/mointerceptorm.py | 7 ++++--- jasmin/protocols/cli/mtinterceptorm.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/jasmin/protocols/cli/mointerceptorm.py b/jasmin/protocols/cli/mointerceptorm.py index 879e00de..55197ee6 100644 --- a/jasmin/protocols/cli/mointerceptorm.py +++ b/jasmin/protocols/cli/mointerceptorm.py @@ -134,19 +134,20 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): if stype == 'python3': import os + import urllib if os.path.isfile(script_path): # Open file and get its content with open(script_path, 'r') as content_file: pyCode = content_file.read() - else: - # Assume it's a URL - import urllib.request + elif urllib.parse.urlparse(script_path).scheme in ['https', 'http', 'ftp', 'file']: try: with urllib.request.urlopen(script_path) as content_file: pyCode = content_file.read().decode('utf-8') except urllib.error.URLError as e: # Handle errors that may occur while reading the file from a URL return self.protocol.sendData('[URL]: %s' % str(e)) + else: + raise NotImplementedError("Not implemented yet !") # Test compilation of the script compile(pyCode, '', 'exec') diff --git a/jasmin/protocols/cli/mtinterceptorm.py b/jasmin/protocols/cli/mtinterceptorm.py index 6d2eb300..bb56126c 100644 --- a/jasmin/protocols/cli/mtinterceptorm.py +++ b/jasmin/protocols/cli/mtinterceptorm.py @@ -134,19 +134,20 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): if stype == 'python3': import os + import urllib if os.path.isfile(script_path): # Open file and get its content with open(script_path, 'r') as content_file: pyCode = content_file.read() - else: - # Assume it's a URL - import urllib.request + elif urllib.parse.urlparse(script_path).scheme in ['https', 'http', 'ftp', 'file']: try: with urllib.request.urlopen(script_path) as content_file: pyCode = content_file.read().decode('utf-8') except urllib.error.URLError as e: # Handle errors that may occur while reading the file from a URL return self.protocol.sendData('[URL]: %s' % str(e)) + else: + raise NotImplementedError("Not implemented yet !") # Test compilation of the script compile(pyCode, '', 'exec') From b620681857712d6c968f36ee26fbd2b28f11c1c7 Mon Sep 17 00:00:00 2001 From: BlackOrder <4302157+BlackOrder@users.noreply.github.com> Date: Mon, 2 Jan 2023 15:48:22 +0000 Subject: [PATCH 4/8] add note to docs about remote path --- misc/doc/sources/management/jcli/modules.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/misc/doc/sources/management/jcli/modules.rst b/misc/doc/sources/management/jcli/modules.rst index f2cf1960..67c3beda 100644 --- a/misc/doc/sources/management/jcli/modules.rst +++ b/misc/doc/sources/management/jcli/modules.rst @@ -736,6 +736,12 @@ Here's an example of adding a **DefaultInterceptor** to a python script:: .. note:: As of now, only **python3** script is permitted. +.. note:: The path to the script can be any of the fallowing: + + * **python3(/path/to/script.py)** or **python3(file://path/to/script.py)**: The path must be absolute, relative path is not supported + * **python3(https://example.com/path/to/script.py)**: The script is a remote python3 script. The script will be + downloaded and copied to Jasmin core. Accepts http, https, and ftp protocols. + .. note:: Pay attention that the given script is copied to Jasmin core, do not expect Jasmin to refresh the script code when you update it, you'll need to redefine the *mointerceptor* rule again so Jasmin will refresh the script. From 8372cc51b1098387c48c85e7682bdf6cd06b0793 Mon Sep 17 00:00:00 2001 From: BlackOrder <4302157+BlackOrder@users.noreply.github.com> Date: Mon, 2 Jan 2023 16:56:54 +0000 Subject: [PATCH 5/8] reversed error message --- jasmin/protocols/cli/mointerceptorm.py | 2 +- jasmin/protocols/cli/mtinterceptorm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jasmin/protocols/cli/mointerceptorm.py b/jasmin/protocols/cli/mointerceptorm.py index 55197ee6..cc3206c0 100644 --- a/jasmin/protocols/cli/mointerceptorm.py +++ b/jasmin/protocols/cli/mointerceptorm.py @@ -22,7 +22,7 @@ def validate_typed_script(script): m = re.match(r'(python3)\((.*)\)', script, re.I) if not m: - raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script) or python3(https://domain.com/path/to/script).') + raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script).') else: language = m.group(1).lower() script_path = m.group(2) diff --git a/jasmin/protocols/cli/mtinterceptorm.py b/jasmin/protocols/cli/mtinterceptorm.py index bb56126c..3d147289 100644 --- a/jasmin/protocols/cli/mtinterceptorm.py +++ b/jasmin/protocols/cli/mtinterceptorm.py @@ -22,7 +22,7 @@ def validate_typed_script(script): m = re.match(r'(python3)\((.*)\)', script, re.I) if not m: - raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script) or python3(https://domain.com/path/to/script).') + raise InvalidScriptSyntax('Invalid syntax for script, must be python3(/path/to/script).') else: language = m.group(1).lower() script_path = m.group(2) From 03b6a4cdeb2c770caaaa5b0c7a3ad9ad3d02e708 Mon Sep 17 00:00:00 2001 From: BlackOrder <4302157+BlackOrder@users.noreply.github.com> Date: Thu, 12 Jan 2023 09:41:53 +0000 Subject: [PATCH 6/8] detect path type with urlparse --- jasmin/protocols/cli/mointerceptorm.py | 8 ++++---- jasmin/protocols/cli/mtinterceptorm.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/jasmin/protocols/cli/mointerceptorm.py b/jasmin/protocols/cli/mointerceptorm.py index cc3206c0..2402efee 100644 --- a/jasmin/protocols/cli/mointerceptorm.py +++ b/jasmin/protocols/cli/mointerceptorm.py @@ -133,13 +133,13 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): stype, script_path = validate_typed_script(arg) if stype == 'python3': - import os - import urllib - if os.path.isfile(script_path): + import urllib.parse, urllib.request, urllib.error + pathscheme = urllib.parse.urlparse(script_path).scheme + if pathscheme == '': # Open file and get its content with open(script_path, 'r') as content_file: pyCode = content_file.read() - elif urllib.parse.urlparse(script_path).scheme in ['https', 'http', 'ftp', 'file']: + elif pathscheme in ['https', 'http', 'ftp', 'file']: try: with urllib.request.urlopen(script_path) as content_file: pyCode = content_file.read().decode('utf-8') diff --git a/jasmin/protocols/cli/mtinterceptorm.py b/jasmin/protocols/cli/mtinterceptorm.py index 3d147289..0081ffb9 100644 --- a/jasmin/protocols/cli/mtinterceptorm.py +++ b/jasmin/protocols/cli/mtinterceptorm.py @@ -133,13 +133,13 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): stype, script_path = validate_typed_script(arg) if stype == 'python3': - import os - import urllib - if os.path.isfile(script_path): + import urllib.parse, urllib.request, urllib.error + pathscheme = urllib.parse.urlparse(script_path).scheme + if pathscheme == '': # Open file and get its content with open(script_path, 'r') as content_file: pyCode = content_file.read() - elif urllib.parse.urlparse(script_path).scheme in ['https', 'http', 'ftp', 'file']: + elif pathscheme in ['https', 'http', 'ftp', 'file']: try: with urllib.request.urlopen(script_path) as content_file: pyCode = content_file.read().decode('utf-8') From b4f516e0493860f6ba163116d7e84048df248506 Mon Sep 17 00:00:00 2001 From: The Z <1020111+farirat@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:22:55 +0100 Subject: [PATCH 7/8] avoiding imports from inside code for better readability and to allow any import error to raise early. --- jasmin/protocols/cli/mointerceptorm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jasmin/protocols/cli/mointerceptorm.py b/jasmin/protocols/cli/mointerceptorm.py index 2402efee..42df11f8 100644 --- a/jasmin/protocols/cli/mointerceptorm.py +++ b/jasmin/protocols/cli/mointerceptorm.py @@ -2,6 +2,7 @@ import re import inspect import pickle +import urllib.parse, urllib.request, urllib.error from jasmin.protocols.cli.managers import PersistableManager, Session from jasmin.protocols.cli.filtersm import MOFILTERS from jasmin.routing.jasminApi import MOInterceptorScript @@ -133,7 +134,6 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): stype, script_path = validate_typed_script(arg) if stype == 'python3': - import urllib.parse, urllib.request, urllib.error pathscheme = urllib.parse.urlparse(script_path).scheme if pathscheme == '': # Open file and get its content From 8a41ef327237221f3605dd16ff91a3b36d41236f Mon Sep 17 00:00:00 2001 From: The Z <1020111+farirat@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:23:47 +0100 Subject: [PATCH 8/8] avoiding imports from inside code for better readability and to allow any import error to raise early. --- jasmin/protocols/cli/mtinterceptorm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jasmin/protocols/cli/mtinterceptorm.py b/jasmin/protocols/cli/mtinterceptorm.py index 0081ffb9..ae734351 100644 --- a/jasmin/protocols/cli/mtinterceptorm.py +++ b/jasmin/protocols/cli/mtinterceptorm.py @@ -2,6 +2,7 @@ import re import inspect import pickle +import urllib.parse, urllib.request, urllib.error from jasmin.protocols.cli.managers import PersistableManager, Session from jasmin.protocols.cli.filtersm import MTFILTERS from jasmin.routing.jasminApi import MTInterceptorScript @@ -133,7 +134,6 @@ def parse_args_and_call_with_instance(self, *args, **kwargs): stype, script_path = validate_typed_script(arg) if stype == 'python3': - import urllib.parse, urllib.request, urllib.error pathscheme = urllib.parse.urlparse(script_path).scheme if pathscheme == '': # Open file and get its content