Skip to content

Commit

Permalink
fix: headings levels validation is not working properly (#1436)
Browse files Browse the repository at this point in the history
* fix: heading levels validation fixing

Signed-off-by: Alejandro Jose Leiva Palomo <alejandro.leiva.palomo@ibm.com>

* fix: correcting test cases and adding extra validation

Signed-off-by: Alejandro Jose Leiva Palomo <alejandro.leiva.palomo@ibm.com>

---------

Signed-off-by: Alejandro Jose Leiva Palomo <alejandro.leiva.palomo@ibm.com>
  • Loading branch information
AleJo2995 committed Aug 15, 2023
1 parent 419025d commit 22b65a9
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 50 deletions.
23 changes: 0 additions & 23 deletions tests/data/author/0.0.1/test_1_md_format/bad_instance_reordered.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
---

# Vulnerability Management (VULN) Defect Checks
## 0. Vulnerability Management Workflow
### 0.1 Data Sources
### 0.2 Fetchers
### 0.3 Data Store
### 0.4 Policy Engine
### 0.5 Ticketing System
## 1. Facts Data Model
### 1.1 Devices
#### Server
#### KubernetesCluster
#### ContainerImage
### 1.2 Vulnerabilities
#### ResourceScan
#### ResourceScanFinding
#### ResourceScanResult
### 1.3 Thresholds
#### CISOOverride
#### CISAKEV
### 1.4 Risks
#### VulnDeviations
### 1.5 Scanner Definition
#### ScannerConfiguration
## 2. Defect Checks
### Sub-capability: Reduce Software/ Firmware Vulnerabilities
#### Vulnerable Software/ Firmware
##### Purpose
##### Assessment Criteria
###### Inputs
###### Rules
####### vuln_prod_os_scan_duedate_check
######## Type
######## Rationale Statement
######## Impact Statement
######## Implementation Description
######## Audit Procedure(s)
######## Remediation Procedure(s)
######## Parameters
####### vuln_prod_os_scan_warning_duedate_check_warning
######## Type
######## Rationale Statement
######## Impact Statement
######## Implementation Description
######## Audit Procedure(s)
######## Remediation Procedure(s)
######## Parameters
###### Additional Outputs
##### Assessment Objectives
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
authors:
- Tim
- Jane
- Sally
owner: Joe
valid:
from: 2020-01-01
to: 2099-12-31
---

# { Security Capability Name } Defect Checks
## 1. Facts Data Model
### Sub-Capability: { _insert name of subcapability_}
## 2. Defect Checks
### Sub-capability: { _insert sub-capability name_}
#### { _insert defect check name_}
##### Assessment Criteria
###### Inputs
###### Rules
####### { Rule Name}
######## Type
######## Rationale Statement
######## Impact Statement
######## Implementation Description
######## Audit Procedure(s)
######## Remediation Procedure(s)
######## Parameters
###### Additional Outputs
##### Assessment Objectives
24 changes: 24 additions & 0 deletions tests/trestle/core/commands/author/folders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,27 @@ def test_drawio_versioning_validation(
monkeypatch.setattr(sys, 'argv', command_string_validate_content.split())
rc = trestle.cli.Trestle().run()
assert rc == 0


def test_heading_levels_hierarchy(
testdata_dir: pathlib.Path, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
) -> None:
"""Test behaviour when validating drawio instance."""
task_template_folder = tmp_trestle_dir / '.trestle/author/test_task/'
test_template_folder = testdata_dir / 'author/governed_folders/template_folder_headling_levels'
test_instances_folder = testdata_dir / 'author/governed_folders/instance_with_diff_heading_levels'
task_instance_folder = tmp_trestle_dir / 'test_task/folder_1'

hidden_file = testdata_dir / pathlib.Path(
'author/governed_folders/template_folder_with_drawio/.hidden_does_not_affect'
)
test_utils.make_file_hidden(hidden_file)

test_utils.copy_tree_or_file_with_hidden(test_template_folder, task_template_folder)

shutil.copytree(test_instances_folder, task_instance_folder)

command_string_validate_content = 'trestle author folders validate -tn test_task -hv'
monkeypatch.setattr(sys, 'argv', command_string_validate_content.split())
rc = trestle.cli.Trestle().run()
assert rc == 0
14 changes: 0 additions & 14 deletions tests/trestle/core/markdown/markdown_validator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@
False,
False
),
(
pathlib.Path('tests/data/author/0.0.1/test_1_md_format/template.md'),
pathlib.Path('tests/data/author/0.0.1/test_1_md_format/bad_instance_reordered.md'),
False,
False,
False
),
(
pathlib.Path('tests/data/author/0.0.1/test_1_md_format/template.md'),
pathlib.Path('tests/data/author/0.0.1/test_1_md_format/bad_instance_missing_heading.md'),
Expand Down Expand Up @@ -125,13 +118,6 @@
False,
False
),
(
pathlib.Path('tests/data/author/0.0.1/test_4_md_format_extras/template.md'),
pathlib.Path('tests/data/author/0.0.1/test_4_md_format_extras/bad_instance_reordered.md'),
False,
False,
False
),
(
pathlib.Path('tests/data/author/0.0.1/test_4_md_format_extras/template.md'),
pathlib.Path('tests/data/author/0.0.1/test_4_md_format_extras/bad_instance_missing_heading.md'),
Expand Down
22 changes: 9 additions & 13 deletions trestle/core/markdown/markdown_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import trestle.core.markdown.markdown_const as md_const
from trestle.common.err import TrestleError
from trestle.common.list_utils import as_list
from trestle.core.commands.author.consts import START_TEMPLATE_VERSION, TEMPLATE_VERSION_HEADER
from trestle.core.markdown.docs_markdown_node import DocsMarkdownNode

Expand Down Expand Up @@ -202,30 +203,25 @@ def _validate_headings(self, instance: pathlib.Path, template_keys: List[str], i
)
return False
template_header_pointer = 0
present_keys = []
for key in instance_keys:
if template_header_pointer >= len(template_keys):
break
if key in template_keys and key != template_keys[template_header_pointer]:
logger.warning(
f'Headings in the instance: {instance} were shuffled or modified. '
f'\nInstance does not have required template heading '
f'\"{template_keys[template_header_pointer]}\". '
f'Check if this heading was modified/present in the instance.'
f'\nPlease note that no changes to template headings are allowed, '
f'including extra spaces.'
)
return False
elif key in template_keys and key == template_keys[template_header_pointer]:
if key in template_keys and key not in present_keys:
present_keys.append(template_keys[template_keys.index(key)])
template_header_pointer += 1
elif re.search(md_const.SUBSTITUTION_REGEX, template_keys[template_header_pointer]) is not None:
present_keys.append(template_keys[template_header_pointer])
template_header_pointer += 1 # skip headers with substitutions
if template_header_pointer != len(template_keys):
diff_keys = set(template_keys) - set(present_keys)
if template_header_pointer != len(template_keys) and len(diff_keys) > 0:
logger.info(
f'Headings in the instance: {instance} were removed. '
f'Expected {len(template_keys)} headings, but found only {template_header_pointer}.'
)
for result in as_list(diff_keys):
logger.info(f'Heading {result} in the instance: {instance} was removed or not present ')
return False

return True

@classmethod
Expand Down

0 comments on commit 22b65a9

Please sign in to comment.