Skip to content

Commit

Permalink
Merge pull request #235 from iriusrisk/feature/OPT-811
Browse files Browse the repository at this point in the history
[feature/OPT-811] to dev
  • Loading branch information
PacoCid committed May 5, 2023
2 parents f56e717 + 3bd540b commit 7d82b80
Show file tree
Hide file tree
Showing 106 changed files with 6,402 additions and 51,893 deletions.
45 changes: 20 additions & 25 deletions _sl_build/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@

ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

PROCESSORS = [
{'name': 'slp_base', 'type': 'processor',
'forbidden_dependencies': ['startleft', 'slp_cft', 'slp_tf', 'slp_visio', 'slp_mtmt']},
{'name': 'slp_cft', 'type': 'processor', 'provider_type': 'CLOUDFORMATION',
'forbidden_dependencies': ['startleft', 'slp_tf', 'slp_visio', 'slp_mtmt']},
{'name': 'slp_tf', 'type': 'processor', 'provider_type': 'TERRAFORM',
'forbidden_dependencies': ['startleft', 'slp_cft', 'slp_visio', 'slp_mtmt']},
{'name': 'slp_visio', 'type': 'processor', 'provider_type': 'VISIO',
'forbidden_dependencies': ['startleft', 'slp_cft', 'slp_tf', 'slp_mtmt']},
{'name': 'slp_visio', 'type': 'processor', 'provider_type': 'LUCID',
'forbidden_dependencies': ['startleft', 'slp_cft', 'slp_tf', 'slp_mtmt']},
{'name': 'slp_mtmt', 'type': 'processor', 'provider_type': 'MTMT',
'forbidden_dependencies': ['startleft', 'slp_cft', 'slp_tf', 'slp_visio']}
]

_general_modules_forbidden_dependencies = ['startleft'] + [processor['name'] for processor in PROCESSORS]
GENERAL_MODULES = [
{'name': 'sl_util', 'type': 'general', 'forbidden_dependencies': _general_modules_forbidden_dependencies},
{'name': 'otm', 'type': 'general', 'forbidden_dependencies': _general_modules_forbidden_dependencies}
]

STARTLEFT_MODULE = {'name': 'startleft', 'type': 'general', 'allowed_imports': ['slp_base', 'otm', 'sl_util']}
# TODO Startleft needs to depend on TF and CFT processors until a decision is token about the search function
_startleft_forbidden_dependencies = [p['name'] for p in PROCESSORS if 'provider_type' in p and p['name'] not in ['slp_cft', 'slp_tf']]
STARTLEFT_MODULE = [{'name': 'startleft', 'type': 'general', 'forbidden_dependencies': _startleft_forbidden_dependencies}]

ALL_MODULES = PROCESSORS + GENERAL_MODULES + STARTLEFT_MODULE
STARTLEFT_MODULE['allowed_imports'].extend(['slp_cft', 'slp_tf'])

# TODO Dependency between otm and sl_util must be removed
OTM_MODULE = {'name': 'otm', 'type': 'general', 'allowed_imports': ['sl_util']}

SL_UTIL_MODULE = {'name': 'sl_util', 'type': 'general', 'allowed_imports': ['otm']}

_slp_allowed_imports = ['slp_base', 'sl_util', 'otm']
PROCESSORS = [
{'name': 'slp_base', 'type': 'processor', 'allowed_imports': _slp_allowed_imports},
{'name': 'slp_cft', 'type': 'processor', 'provider_type': 'CLOUDFORMATION', 'allowed_imports': _slp_allowed_imports},
{'name': 'slp_tf', 'type': 'processor', 'provider_type': 'TERRAFORM', 'allowed_imports': _slp_allowed_imports},
{'name': 'slp_tfplan', 'type': 'processor', 'provider_type': 'TFPLAN', 'allowed_imports': _slp_allowed_imports},
{'name': 'slp_visio', 'type': 'processor', 'provider_type': 'VISIO', 'allowed_imports': _slp_allowed_imports},
{'name': 'slp_visio', 'type': 'processor', 'provider_type': 'LUCID', 'allowed_imports': _slp_allowed_imports},
{'name': 'slp_mtmt', 'type': 'processor', 'provider_type': 'MTMT', 'allowed_imports': _slp_allowed_imports}
]

"""
All the StartLeft modules are defined here, along with their dependencies. Further information is available in:
https://iriusrisk.github.io/startleft/development/Architecture
"""
ALL_MODULES = [STARTLEFT_MODULE] + [OTM_MODULE] + [SL_UTIL_MODULE] + PROCESSORS
12 changes: 4 additions & 8 deletions _sl_build/secure_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@


def _build_dependencies_map():
module_dependencies = [{module['name']: module['forbidden_dependencies']} for module in ALL_MODULES]
module_dependencies = [{module['name']: module['allowed_imports']} for module in ALL_MODULES]
return {name: dependencies for module in module_dependencies for name, dependencies in module.items()}


_module_names = [module['name'] for module in ALL_MODULES]
_forbidden_dependencies = _build_dependencies_map()
_allowed_imports = _build_dependencies_map()


def _get_base_module_name(full_name):
Expand All @@ -22,15 +22,11 @@ def _is_module_restricted(importing_module: str, imported_module: str):
base_imported_module = _get_base_module_name(imported_module)

if not base_importing_module or not base_imported_module or \
base_importing_module == base_imported_module or \
base_importing_module not in _module_names or base_imported_module not in _module_names:
return False

forbidden_dependencies = _forbidden_dependencies[base_importing_module]
for fd in forbidden_dependencies:
if fd == base_imported_module:
return True

return False
return base_imported_module not in _allowed_imports[base_importing_module]


def _secure_importer(name, globals=None, locals=None, fromlist=(), level=0):
Expand Down
11 changes: 5 additions & 6 deletions docs/development/Create-a-new-StartLeft-Processor.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ Now we can create a simple test that verifies that the conversion process is wor
create a `test_mais_processor.py` file inside the `slp_mais/slp_mais/tests/integration` folder:
```python
import os
from sl_util.sl_util.file_utils import get_data
from sl_util.sl_util.file_utils import get_byte_data
from slp_base.tests.util.otm import validate_and_compare
from slp_mais.slp_mais.mais_processor import MAISProcessor

Expand All @@ -405,10 +405,10 @@ class TestMAISProcessor:

def test_single_component_mais_file_ok(self):
# GIVEN a simple MAIS file with a single component
mais_file = get_data(f'{resources_path}/mais-sample.json')
mais_file = get_byte_data(f'{resources_path}/mais-sample.json')

# AND a MAIS mapping file that defines a mapping for that component
mapping_file = get_data(f'{resources_path}/mapping-sample.yaml')
mapping_file = get_byte_data(f'{resources_path}/mapping-sample.yaml')

# AND an expected OTM result
expected_otm = f'{resources_path}/expected-otm.otm'
Expand Down Expand Up @@ -442,14 +442,13 @@ no code is needed, and you only need to perform the configuration steps below.

### Configure the module
1. Go to the `_sl_build/modules.py` class, where you can find the modules' configuration.
2. In the `PROCESSORS` variable, add `slp_mais` as `forbidden_dependency` for all the existent SLP modules.
3. Create a new entry in the array with the configuration for our `slp_mais` module:
2. Create a new entry in the array with the configuration for our `slp_mais` module:
```python
{
'name': 'slp_mais',
'type': 'processor',
'provider_type': 'MAIS',
'forbidden_dependencies': ['startleft', 'slp_cft', 'slp_tf', 'slp_visio', 'slp_mtmt']
'allowed_imports': _slp_allowed_imports
}
```

Expand Down
Loading

0 comments on commit 7d82b80

Please sign in to comment.