-
Notifications
You must be signed in to change notification settings - Fork 7
fix: support continuation lines in scan_sudoers #122
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,20 @@ | |||||
| alias_names: "{{ alias_keys | zip(alias_keys) | flatten | list }}" | ||||||
| alias_vals: "{{ alias_values.values() | flatten | list }}" | ||||||
| names_vals: "{{ alias_names | zip(alias_vals) | list }}" | ||||||
| # This is the format that the role expects the aliases to be in | ||||||
| # mostly have to lowercase the keys to match the format of the role | ||||||
| aliases_in_role_input_format: "{{ dict(keys | zip(vals)) }}" | ||||||
| keys: "{{ alias_values | dict2items | map(attribute='key') | map('lower') | list }}" | ||||||
| vals: "{{ alias_values | dict2items | map(attribute='value') | list }}" | ||||||
| # This is what we expect to be the output of scan_sudoers after parsing | ||||||
| # the file with the continuation lines | ||||||
| __expected_scan_sudoers_parsed_continations: | ||||||
| all_scanned_files: | ||||||
| - /etc/sudoers | ||||||
| sudoers_files: | ||||||
| - aliases: "{{ aliases_in_role_input_format }}" | ||||||
| path: /etc/sudoers | ||||||
| user_specifications: [] | ||||||
| tasks: | ||||||
| - name: Run tests | ||||||
| block: | ||||||
|
|
@@ -49,7 +63,9 @@ | |||||
| {% for alias in names_vals %} | ||||||
| {% set itemvals = alias.1.values() | list %} | ||||||
| {% set space = ("NO_SPACES" in itemvals.0) | ternary("", " ") %} | ||||||
| {{ alias.0 }} {{ itemvals.0 }}{{ space }}={{ space }}{{ itemvals.1 | join("") }} | ||||||
| {{ alias.0 }} \ | ||||||
| {{ itemvals.0 }}{{ space }}={{ space }}\ | ||||||
| {{ itemvals.1 | join("") }} | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect list concatenation in alias values. Line 68 uses The test currently passes only because all test data uses single-item lists, hiding this bug. 🔧 Proposed fix- {{ itemvals.1 | join("") }}
+ {{ itemvals.1 | join(", ") }}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is true, but in the case of the test, there is only one value, so the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Would you like me to open a follow-up issue to track making the join separator explicit ( |
||||||
| {% endfor %} | ||||||
| mode: preserve | ||||||
|
|
||||||
|
|
@@ -60,10 +76,11 @@ | |||||
| sudo_remove_unauthorized_included_files: true | ||||||
| sudo_sudoers_files: | ||||||
| - path: /etc/sudoers | ||||||
| aliases: "{{ aliases }}" | ||||||
| aliases: "{{ dict(keys | zip(vals)) }}" | ||||||
| keys: "{{ alias_values | dict2items | map(attribute='key') | map('lower') | list }}" | ||||||
| vals: "{{ alias_values | dict2items | map(attribute='value') | list }}" | ||||||
| aliases: "{{ aliases_in_role_input_format }}" | ||||||
|
|
||||||
| - name: Assert that scan_sudoers parsed the sudoers file correctly with continuation lines | ||||||
| assert: | ||||||
| that: __expected_scan_sudoers_parsed_continations == ansible_facts.sudoers | ||||||
|
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add failure-path and idempotence checks to this continuation test. This assertion only validates the happy path once. Add (1) a failure scenario (e.g., escaped trailing backslash that should not continue) and (2) a second role run with an idempotence assertion. Minimal extension sketch+ - name: Run the role a second time (idempotence check)
+ include_tasks: tasks/run_role_with_clear_facts.yml
+ vars:
+ sudo_rewrite_default_sudoers_file: true
+ sudo_remove_unauthorized_included_files: true
+ sudo_sudoers_files:
+ - path: /etc/sudoers
+ aliases: "{{ aliases_in_role_input_format }}"
+ register: __second_run
+
+ - name: Assert idempotence on second run
+ assert:
+ that:
+ - not __second_run.changedAs per coding guidelines, tests in 🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah - this parser is not ideal - I want to replace it all with cvtsudoers, but mapping the output of cvtsudoers to the format expected by the role input is pretty tricky - I really don't want to spend any more time fixing the existing parsing code than is necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If you'd like, I can open a follow-up issue to track the ✏️ Learnings added
|
||||||
|
|
||||||
| - name: Get sudoers | ||||||
| slurp: | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fold continuation lines before include discovery.
At Line 215,
get_includes(path)parses the raw file before the normalization added at Line 231. If#include/#includediruses continuation syntax, those includes are missed and child sudoers files are not scanned.Proposed fix
🧰 Tools
🪛 Ruff (0.15.13)
[warning] 226-226: Comment contains ambiguous
‘(LEFT SINGLE QUOTATION MARK). Did you mean ``` (GRAVE ACCENT)?(RUF003)
[warning] 226-226: Comment contains ambiguous
’(RIGHT SINGLE QUOTATION MARK). Did you mean ``` (GRAVE ACCENT)?(RUF003)
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah - this parser is not ideal - I want to replace it all with cvtsudoers, but mapping the output of cvtsudoers to the format expected by the role input is pretty tricky - I really don't want to spend any more time fixing the existing parsing code than is necessary - I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@richm, understood — the parser is a known limitation and the continuation-line fix you've added is sufficient for the current goal of restoring idempotence. Theget_includesedge case is an acceptable deferral given how uncommon a continuation-split#includedirective would be in practice.Would you like me to open a follow-up GitHub issue to track the planned migration from the current parser to
cvtsudoers?✏️ Learnings added