Skip to content

Commit d0db759

Browse files
Merge pull request #285 from iriusrisk/feature/OPT-934
[feature/OPT-934] to [feature/OPT-792]
2 parents f6d1b6e + d348d0e commit d0db759

File tree

9 files changed

+33
-44
lines changed

9 files changed

+33
-44
lines changed

startleft/startleft/cli/cli.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def cli(log_level, verbose):
7878
configure_logging(verbose, log_level)
7979

8080

81-
def parse_iac(iac_type, mapping_file, output_file, project_name, project_id, iac_files):
81+
def parse_iac(iac_type, default_mapping_file, custom_mapping_file, output_file, project_name, project_id,
82+
iac_files):
8283
"""
8384
Parses IaC source files into Open Threat Model
8485
"""
@@ -87,10 +88,13 @@ def parse_iac(iac_type, mapping_file, output_file, project_name, project_id, iac
8788
for iac_file in iac_files:
8889
iac_data.append(get_byte_data(iac_file))
8990

90-
mapping_data = [get_byte_data(mapping_file)]
91+
mapping_data_list = [get_byte_data(default_mapping_file)]
92+
93+
if custom_mapping_file:
94+
mapping_data_list.append(get_byte_data(custom_mapping_file))
9195

9296
processor = provider_resolver.get_processor(IacType(iac_type.upper()), project_id, project_name, iac_data,
93-
mapping_data)
97+
mapping_data_list)
9498
otm = processor.process()
9599

96100
get_otm_as_file(otm, output_file)
@@ -141,45 +145,38 @@ def parse_etm(etm_type, default_mapping_file, custom_mapping_file, output_file,
141145
help=IAC_TYPE_DESC,
142146
cls=Exclusion,
143147
mandatory=True,
144-
mutually_exclusion=['diagram_type', 'etm_type', 'default_mapping_file', 'custom_mapping_file'])
148+
mutually_exclusion=['diagram_type', 'etm_type'])
145149
@click.option(DIAGRAM_TYPE_NAME, DIAGRAM_TYPE_SHORTNAME,
146150
type=click.Choice(DIAGRAM_TYPE_SUPPORTED, case_sensitive=False),
147151
help=DIAGRAM_TYPE_DESC,
148152
cls=Exclusion,
149153
mandatory=True,
150-
mutually_exclusion=['iac_type', 'etm_type', 'mapping_file'])
154+
mutually_exclusion=['iac_type', 'etm_type'])
151155
@click.option(ETM_TYPE_NAME, ETM_TYPE_SHORTNAME,
152156
type=click.Choice(ETM_TYPE_SUPPORTED, case_sensitive=False),
153157
help=ETM_TYPE_DESC,
154158
cls=Exclusion,
155159
mandatory=True,
156-
mutually_exclusion=['diagram_type', 'iac_type', 'mapping_file'])
157-
@click.option(MAPPING_FILE_NAME, MAPPING_FILE_SHORTNAME,
158-
help=MAPPING_FILE_DESC,
159-
cls=Exclusion,
160-
mandatory=True,
161-
mutually_exclusion=['default_mapping_file', 'custom_mapping_file', 'diagram_type', 'etm_type'])
160+
mutually_exclusion=['diagram_type', 'iac_type'])
162161
@click.option(DEFAULT_MAPPING_FILE_NAME, DEFAULT_MAPPING_FILE_SHORTNAME,
163162
help=DEFAULT_MAPPING_FILE_DESC,
164163
cls=Exclusion,
165-
mandatory=True,
166-
mutually_exclusion=['mapping_file', 'iac_type'])
164+
required=True)
167165
@click.option(CUSTOM_MAPPING_FILE_NAME, CUSTOM_MAPPING_FILE_SHORTNAME,
168166
help=CUSTOM_MAPPING_FILE_DESC,
169-
cls=Exclusion,
170-
mutually_exclusion=['mapping_file', 'iac_type'])
167+
cls=Exclusion)
171168
@click.option(OUTPUT_FILE_NAME, OUTPUT_FILE_SHORTNAME, default=OUTPUT_FILE, help=OUTPUT_FILE_DESC)
172169
@click.option(PROJECT_NAME_NAME, PROJECT_NAME_SHORTNAME, required=True, help=PROJECT_NAME_DESC)
173170
@click.option(PROJECT_ID_NAME, PROJECT_ID_SHORTNAME, required=True, help=PROJECT_ID_DESC)
174171
@click.argument(SOURCE_FILE_NAME, required=True, nargs=-1)
175-
def parse_any(iac_type, diagram_type, etm_type, mapping_file, default_mapping_file, custom_mapping_file,
172+
def parse_any(iac_type, diagram_type, etm_type, default_mapping_file, custom_mapping_file,
176173
output_file, project_name, project_id, source_file):
177174
"""
178175
Parses source files into Open Threat Model
179176
"""
180177
logger.info("Parsing source files into OTM")
181178
if iac_type is not None:
182-
parse_iac(iac_type, mapping_file, output_file, project_name, project_id, source_file)
179+
parse_iac(iac_type, default_mapping_file, custom_mapping_file, output_file, project_name, project_id, source_file)
183180
elif diagram_type is not None:
184181
parse_diagram(diagram_type, default_mapping_file, custom_mapping_file, output_file, project_name,
185182
project_id, source_file)

startleft/startleft/messages.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
ETM_MAPPING_FILE_NAME = '--etm-mapping-file'
4242
ETM_MAPPING_FILE_DESC = 'External Threat Model mapping file to validate.'
4343

44-
MAPPING_FILE_NAME = '--mapping-file'
45-
MAPPING_FILE_SHORTNAME = '-m'
46-
MAPPING_FILE_DESC = 'Mapping file to parse the IaC file.'
47-
4844
DEFAULT_MAPPING_FILE_NAME = '--default-mapping-file'
4945
DEFAULT_MAPPING_FILE_SHORTNAME = '-d'
5046
DEFAULT_MAPPING_FILE_DESC = 'Default mapping file to parse the diagram file.'

startleft/tests/unit/cli/parse/diagram/test_unit_cli_parse_diagram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ def test_parse_diagram_mutually_exclusion_error(self):
8181
assert result.exit_code == 2
8282

8383
assert result.stdout.__contains__("Error: Invalid arguments: diagram_type is incompatible with:")
84-
assert result.stdout.__contains__("mapping_file")
84+
assert result.stdout.__contains__("etm_type")
8585
assert result.stdout.__contains__("iac_type")

startleft/tests/unit/cli/parse/diagram/test_unit_cli_parse_lucid.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def test_lucid_parse_successful_with_custom_mapping_file(self, mock, caplog, inp
9292
mock.assert_called_once()
9393

9494
@mark.parametrize('processor_type_option, processor_type_name, mapping_file_option, mapping_file', [
95-
('', '', '--mapping-file', TESTING_LUCID_DEFAULT_VALID_MAPPING_FILENAME),
9695
('--iac-type', 'TERRAFORM', '', '')
9796
])
9897
def test_lucid_parse_incompatible_parameters_error(self, processor_type_option, processor_type_name,
@@ -127,5 +126,5 @@ def test_lucid_parse_incompatible_parameters_error(self, processor_type_option,
127126
assert result.exit_code == 2
128127

129128
assert result.stdout.__contains__("Error: Invalid arguments: diagram_type is incompatible with:")
130-
assert result.stdout.__contains__("mapping_file")
129+
assert result.stdout.__contains__("etm_type")
131130
assert result.stdout.__contains__("iac_type")

startleft/tests/unit/cli/parse/etm/test_unit_cli_parse_etm.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def test_etm_parse_successful_with_custom_mapping_file(self, mock, caplog):
8989
mock.assert_called_once()
9090

9191
@mark.parametrize('processor_type_option, processor_type_name, mapping_file_option, mapping_file', [
92-
('', '', '--mapping-file', TESTING_MTMT_DEFAULT_VALID_MAPPING_FILENAME),
9392
('--diagram-type', 'VISIO', '', ''),
9493
('--iac-type', 'TERRAFORM', '', ''),
9594
])
@@ -126,7 +125,6 @@ def test_etm_parse_incompatible_parameters_error(self, processor_type_option, pr
126125
assert result.exit_code == 2
127126

128127
assert result.stdout.__contains__("Error: Invalid arguments: etm_type is incompatible with:")
129-
assert result.stdout.__contains__("mapping_file")
130128
assert result.stdout.__contains__("iac_type")
131129
assert result.stdout.__contains__("diagram_type")
132130

startleft/tests/unit/cli/parse/iac/test_unit_cli_iac_error_codes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_loadingiacfilerror_code(self, mock_load_source_data):
3131
# a valid IaC type
3232
'--iac-type', TESTING_IAC_TYPE,
3333
# and a valid mapping file
34-
'--mapping-file', TESTING_IAC_FILE,
34+
'--default-mapping-file', TESTING_IAC_FILE,
3535
# and a valid project name
3636
'--project-name', "project-name",
3737
# and a valid project id
@@ -62,7 +62,7 @@ def test_iacfilenotvaliderror_code(self, mock_load_source_data):
6262
# a valid IaC type
6363
'--iac-type', TESTING_IAC_TYPE,
6464
# and a valid mapping file
65-
'--mapping-file', TESTING_IAC_FILE,
65+
'--default-mapping-file', TESTING_IAC_FILE,
6666
# and a valid project name
6767
'--project-name', "project-name",
6868
# and a valid project id
@@ -93,7 +93,7 @@ def test_loadingmappingfileerror_code(self, mock_load_source_data):
9393
# a valid IaC type
9494
'--iac-type', TESTING_IAC_TYPE,
9595
# and a valid mapping file
96-
'--mapping-file', TESTING_IAC_FILE,
96+
'--default-mapping-file', TESTING_IAC_FILE,
9797
# and a valid project name
9898
'--project-name', "project-name",
9999
# and a valid project id
@@ -124,7 +124,7 @@ def test_otmbuildingerror_code(self, mock_load_source_data):
124124
# a valid IaC type
125125
'--iac-type', TESTING_IAC_TYPE,
126126
# and a valid mapping file
127-
'--mapping-file', TESTING_IAC_FILE,
127+
'--default-mapping-file', TESTING_IAC_FILE,
128128
# and a valid project name
129129
'--project-name', "project-name",
130130
# and a valid project id
@@ -156,7 +156,7 @@ def test_otmgenerationerror_code(self, mock_load_source_data):
156156
# a valid IaC type
157157
'--iac-type', TESTING_IAC_TYPE,
158158
# and a valid mapping file
159-
'--mapping-file', TESTING_IAC_FILE,
159+
'--default-mapping-file', TESTING_IAC_FILE,
160160
# and a valid project name
161161
'--project-name', "project-name",
162162
# and a valid project id

startleft/tests/unit/cli/parse/iac/test_unit_cli_parse_iac.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_parse_cloudformation_file_ok(self, mock, caplog):
3030
# a valid IaC type
3131
'--iac-type', TESTING_IAC_TYPE,
3232
# and a valid mapping file
33-
'--mapping-file', TESTING_MAPPING_FILE,
33+
'--default-mapping-file', TESTING_MAPPING_FILE,
3434
# and a valid project name
3535
'--project-name', "project-name",
3636
# and a valid project id
@@ -63,7 +63,7 @@ def test_parse_cloudformation_mutually_exclusion_error(self):
6363
# a valid Diagram type
6464
'--diagram-type', "VISIO",
6565
# and a valid mapping file
66-
'--mapping-file', TESTING_MAPPING_FILE,
66+
'--default-mapping-file', TESTING_MAPPING_FILE,
6767
# and a valid project name
6868
'--project-name', "project-name",
6969
# and a valid project id
@@ -80,6 +80,5 @@ def test_parse_cloudformation_mutually_exclusion_error(self):
8080
assert result.exit_code == 2
8181

8282
assert result.stdout.__contains__("Error: Invalid arguments: iac_type is incompatible with:")
83-
assert result.stdout.__contains__("default_mapping_file")
84-
assert result.stdout.__contains__("custom_mapping_file")
83+
assert result.stdout.__contains__("etm_type")
8584
assert result.stdout.__contains__("diagram_type")

tests/integration/cli/parse/iac/cloudformation/test_cli_parse_iac_cloudformation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_parse_cloudformation_file_ok(self):
3838
# a valid IaC type
3939
'--iac-type', "CLOUDFORMATION",
4040
# and a valid mapping file
41-
'--mapping-file', CLOUDFORMATION_MAPPING,
41+
'--default-mapping-file', CLOUDFORMATION_MAPPING,
4242
# and a valid project name
4343
'--project-name', "project-name",
4444
# and a valid project id
@@ -71,7 +71,7 @@ def test_parse_cloudformation_unknown_resources(self):
7171
# a valid IaC type
7272
'--iac-type', "CLOUDFORMATION",
7373
# and a valid mapping file
74-
'--mapping-file', CLOUDFORMATION_MAPPING,
74+
'--default-mapping-file', CLOUDFORMATION_MAPPING,
7575
# and a valid project name
7676
'--project-name', "project-name",
7777
# and a valid project id
@@ -105,7 +105,7 @@ def test_parse_cloudformation_invalid_file(self, filename):
105105
# a valid IaC type
106106
'--iac-type', "CLOUDFORMATION",
107107
# and a valid mapping file
108-
'--mapping-file', CLOUDFORMATION_MAPPING,
108+
'--default-mapping-file', CLOUDFORMATION_MAPPING,
109109
# and a valid project name
110110
'--project-name', "project-name",
111111
# and a valid project id
@@ -135,7 +135,7 @@ def test_parse_cloudformation_invalid_mapping_file(self, mapping_file):
135135
# a valid IaC type
136136
'--iac-type', "CLOUDFORMATION",
137137
# and a valid mapping file
138-
'--mapping-file', mapping_file,
138+
'--default-mapping-file', mapping_file,
139139
# and a valid project name
140140
'--project-name', "project-name",
141141
# and a valid project id

tests/integration/cli/parse/iac/terraform/test_cli_parse_iac_terraform.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_parse_terraform_file_ok(self):
3838
# a valid IaC type
3939
'--iac-type', "TERRAFORM",
4040
# and a valid mapping terraform file
41-
'--mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
41+
'--default-mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
4242
# and a valid project name
4343
'--project-name', "test_parse_terraform_file_ok",
4444
# and a valid project id
@@ -71,7 +71,7 @@ def test_parse_terraform_unknown_resources(self):
7171
# a valid IaC type
7272
'--iac-type', "TERRAFORM",
7373
# and a valid mapping terraform file
74-
'--mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
74+
'--default-mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
7575
# and a valid project name
7676
'--project-name', "project-name",
7777
# and a valid project id
@@ -104,7 +104,7 @@ def test_parse_terraform_unknown_module(self):
104104
# a valid IaC type
105105
'--iac-type', "TERRAFORM",
106106
# and a valid mapping terraform file
107-
'--mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
107+
'--default-mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
108108
# and a valid project name
109109
'--project-name', "project-name",
110110
# and a valid project id
@@ -138,7 +138,7 @@ def test_parse_terraform_invalid_file(self, filename):
138138
# a valid IaC type
139139
'--iac-type', "TERRAFORM",
140140
# and a valid mapping terraform file
141-
'--mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
141+
'--default-mapping-file', TERRAFORM_VALID_MAPPING_FILENAME,
142142
# and a valid project name
143143
'--project-name', "project-name",
144144
# and a valid project id
@@ -168,7 +168,7 @@ def test_parse_terraform_invalid_mapping_file(self, mapping_file):
168168
# a valid IaC type
169169
'--iac-type', "TERRAFORM",
170170
# and a valid mapping terraform file
171-
'--mapping-file', mapping_file,
171+
'--default-mapping-file', mapping_file,
172172
# and a valid project name
173173
'--project-name', "project-name",
174174
# and a valid project id

0 commit comments

Comments
 (0)