Skip to content

Commit

Permalink
fix playbook detection and module info (#167)
Browse files Browse the repository at this point in the history
Signed-off-by: hirokuni-kitahara <[email protected]>
  • Loading branch information
hirokuni-kitahara authored Aug 8, 2023
1 parent dfbfdc8 commit 18e73f5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
6 changes: 6 additions & 0 deletions ansible_risk_insight/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ def could_be_playbook_detail(body: str = "", data: list = None, fpath: str = "")
if not body:
return False

if not data:
return False

if not isinstance(data, list):
return False

if len(data) == 0:
return False

Expand Down
2 changes: 1 addition & 1 deletion ansible_risk_insight/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def load_playbooks(path, basedir="", skip_playbook_format_error=True, skip_task_
playbooks = []
playbook_names = []
for fpath in candidates:
if could_be_playbook(fpath) and could_be_playbook_detail(fpath):
if could_be_playbook(fpath=fpath) and could_be_playbook_detail(fpath=fpath):
relative_path = ""
if fpath.startswith(path):
relative_path = fpath[len(path) :]
Expand Down
12 changes: 7 additions & 5 deletions ansible_risk_insight/risk_assessment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,13 @@ def search_module(self, name, exact_match=False, max_match=-1, collection_name="
found_index = None
if short_name in self.module_index and self.module_index[short_name]:
from_indices = True
# look for the module index with FQCN
for possible_index in self.module_index[short_name]:
if possible_index["fqcn"] == name:
found_index = possible_index
break
# look for the module index with FQCN (only when `name` is FQCN)
if "." in name:
for possible_index in self.module_index[short_name]:
if possible_index["fqcn"] == name:
found_index = possible_index
break

# if any candidates don't match with FQCN, use the first index
if not found_index:
found_index = self.module_index[short_name][0]
Expand Down
18 changes: 16 additions & 2 deletions ansible_risk_insight/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,26 @@ def __post_init__(self):
if self.playbook_yaml:
self.target_playbook_name = self.name
else:
_, self.target_playbook_name = split_target_playbook_fullpath(self.name)
if self.base_dir:
basedir = self.base_dir
target_playbook_path = self.name.replace(basedir, "")
if target_playbook_path[0] == "/":
target_playbook_path = target_playbook_path[1:]
self.target_playbook_name = target_playbook_path
else:
_, self.target_playbook_name = split_target_playbook_fullpath(self.name)
elif self.type == LoadType.TASKFILE:
if self.taskfile_yaml:
self.target_taskfile_name = self.name
else:
_, self.target_taskfile_name = split_target_taskfile_fullpath(self.name)
if self.base_dir:
basedir = self.base_dir
target_taskfile_path = self.name.replace(basedir, "")
if target_taskfile_path[0] == "/":
target_taskfile_path = target_taskfile_path[1:]
self.target_taskfile_name = target_taskfile_path
else:
_, self.target_taskfile_name = split_target_taskfile_fullpath(self.name)
self.__path_mappings = {
"src": os.path.join(self.root_dir, type_root, proj_name, "src"),
"root_definitions": os.path.join(
Expand Down
3 changes: 2 additions & 1 deletion ansible_risk_insight/task_keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ throttle
timeout
until
vars
when
when
listen

0 comments on commit 18e73f5

Please sign in to comment.