From f18c7626612a126b73924396c57a1d4dce27b9ca Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sat, 2 Dec 2023 23:19:34 -0500 Subject: [PATCH 1/3] Added input validation callback functions. --- shodan/__main__.py | 5 +++-- shodan/cli/validation.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 shodan/cli/validation.py diff --git a/shodan/__main__.py b/shodan/__main__.py index 4093b94..05c2494 100644 --- a/shodan/__main__.py +++ b/shodan/__main__.py @@ -37,6 +37,7 @@ import requests import time import json +from shodan.cli.validation import check_file_format, check_input_file_type # The file converters that are used to go from .json.gz to various other formats from shodan.cli.converter import CsvConverter, KmlConverter, GeoJsonConverter, ExcelConverter, ImagesConverter @@ -93,8 +94,8 @@ def main(): @main.command() @click.option('--fields', help='List of properties to output.', default=None) -@click.argument('input', metavar='', type=click.Path(exists=True)) -@click.argument('format', metavar='', type=click.Choice(CONVERTERS.keys())) +@click.argument('input', metavar='', type=click.Path(exists=True), callback=check_input_file_type) +@click.argument('format', metavar='', type=click.Choice(CONVERTERS.keys()), callback=check_file_format) def convert(fields, input, format): """Convert the given input data file into a different format. The following file formats are supported: diff --git a/shodan/cli/validation.py b/shodan/cli/validation.py new file mode 100644 index 0000000..1888392 --- /dev/null +++ b/shodan/cli/validation.py @@ -0,0 +1,29 @@ +import click + + +def check_input_file_type(ctx, param, value): + """ + Click callback method used for file type input validation. + :param ctx: Python Click library Context object. + :param param: Python Click Context object params attribute. + :param value: Value passed in for a given command line parameter. + """ + idx = value.find(".") + + if idx == -1 or value[idx:] != ".json.gz": + raise click.BadParameter("Input file type must be '.json.gz'") + return value + +def check_file_format(ctx, param, value): + """ + Click callback method used for output file format input validation. + :param ctx: Python Click library Context object. + :param param: Python Click Context object params attribute. + :param value: Value passed in for a given command line parameter. + """ + supported_file_types = ["kml", "csv", "geo.json", "images", "xlsx"] + file_type_str = ', '.join(supported_file_types) + + if value not in supported_file_types: + raise click.BadParameter(f"Output file type must be one of the supported file extensions:\n{file_type_str}") + return value From 4aadd31a8ccf0f5882e83595878418fd7ea745c7 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sun, 3 Dec 2023 13:02:01 -0500 Subject: [PATCH 2/3] Updated implementation based on reviewer feedback. --- shodan/__main__.py | 2 +- shodan/cli/validation.py | 14 -------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/shodan/__main__.py b/shodan/__main__.py index 05c2494..5b1f824 100644 --- a/shodan/__main__.py +++ b/shodan/__main__.py @@ -95,7 +95,7 @@ def main(): @main.command() @click.option('--fields', help='List of properties to output.', default=None) @click.argument('input', metavar='', type=click.Path(exists=True), callback=check_input_file_type) -@click.argument('format', metavar='', type=click.Choice(CONVERTERS.keys()), callback=check_file_format) +@click.argument('format', metavar='', type=click.Choice(CONVERTERS.keys())) def convert(fields, input, format): """Convert the given input data file into a different format. The following file formats are supported: diff --git a/shodan/cli/validation.py b/shodan/cli/validation.py index 1888392..da53b90 100644 --- a/shodan/cli/validation.py +++ b/shodan/cli/validation.py @@ -13,17 +13,3 @@ def check_input_file_type(ctx, param, value): if idx == -1 or value[idx:] != ".json.gz": raise click.BadParameter("Input file type must be '.json.gz'") return value - -def check_file_format(ctx, param, value): - """ - Click callback method used for output file format input validation. - :param ctx: Python Click library Context object. - :param param: Python Click Context object params attribute. - :param value: Value passed in for a given command line parameter. - """ - supported_file_types = ["kml", "csv", "geo.json", "images", "xlsx"] - file_type_str = ', '.join(supported_file_types) - - if value not in supported_file_types: - raise click.BadParameter(f"Output file type must be one of the supported file extensions:\n{file_type_str}") - return value From e443d2a912987fb5edae934853123f72f4fc9735 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sun, 3 Dec 2023 15:31:08 -0500 Subject: [PATCH 3/3] Fixed import error. --- shodan/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shodan/__main__.py b/shodan/__main__.py index 5b1f824..1716e73 100644 --- a/shodan/__main__.py +++ b/shodan/__main__.py @@ -37,7 +37,7 @@ import requests import time import json -from shodan.cli.validation import check_file_format, check_input_file_type +from shodan.cli.validation import check_input_file_type # The file converters that are used to go from .json.gz to various other formats from shodan.cli.converter import CsvConverter, KmlConverter, GeoJsonConverter, ExcelConverter, ImagesConverter