Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix P-rules to avoid reporting registered vars as undefined #199

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def process(self, ctx: AnsibleRunContext):
wrong_values = []
undefined_values = []
unknown_type_values = []

registered_vars = []
for v_name in task.variable_set:
v = task.variable_set[v_name]
if v and v[-1].type == VariableType.RegisteredVars:
registered_vars.append(v_name)

if task.args.type == ArgumentsType.DICT:
for key in task.args.raw:
raw_value = task.args.raw[key]
Expand Down Expand Up @@ -133,7 +140,16 @@ def process(self, ctx: AnsibleRunContext):

sub_args = task.args.get(key)
if sub_args:
undefined_vars = [v.name for v in sub_args.vars if v and v.type == VariableType.Unknown]
undefined_vars = []
for v in sub_args.vars:
first_v_name = v.name.split(".")[0]
# skip registered vars
if first_v_name in registered_vars:
continue

if v and v.type == VariableType.Unknown:
undefined_vars.append(v.name)

if undefined_vars:
undefined_values.append({"key": key, "value": raw_value, "undefined_variables": undefined_vars})

Expand Down
12 changes: 12 additions & 0 deletions ansible_risk_insight/rules/P004_variable_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ def process(self, ctx: AnsibleRunContext):
task_arg_keys = []
if task.args.type == ArgumentsType.DICT:
task_arg_keys = list(task.args.raw.keys())

registered_vars = []
for v_name in task.variable_set:
v = task.variable_set[v_name]
if v and v[-1].type == VariableType.RegisteredVars:
registered_vars.append(v_name)

for v_name in task.variable_use:
first_v_name = v_name.split(".")[0]
# skip registered vars
if first_v_name in registered_vars:
continue

v = task.variable_use[v_name]
if v and v[-1].type == VariableType.Unknown:
if v_name not in undefined_variables:
Expand Down
Loading