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 check mode in iptables_state for incomplete iptables-save files along with integration tests #8029

Merged
merged 22 commits into from
Mar 24, 2024

Conversation

Maxopoly
Copy link
Contributor

@Maxopoly Maxopoly commented Feb 25, 2024

SUMMARY

Fixes #7463

ISSUE TYPE
  • Bugfix Pull Request
  • Test Pull Request
COMPONENT NAME

iptables_state

ADDITIONAL INFORMATION

This PR first attempted to reproduce the behavior observed in #7463 through integration tests. iptables-save doesn't work in docker, so this was tested against the azure pipeline through this PR, please excuse the commit spam.

a58bc16 was able reproduce the issue in #7463 based on incomplete iptables-save files. Initially, all tables are empty and uninitialized, which will make iptables-save return nothing for them.

For example when creating a nat rule and then deleting it, an output of iptables-save may look like this:

        "*filter",
        ":INPUT ACCEPT [0:0]",
        ":FORWARD DROP [0:0]",
        ":OUTPUT ACCEPT [0:0]",
        "COMMIT",
        "*nat",
        ":PREROUTING ACCEPT [0:0]",
        ":INPUT ACCEPT [0:0]",
        ":OUTPUT ACCEPT [0:0]",
        ":POSTROUTING ACCEPT [0:0]",
        "COMMIT",

while one on a new container/machine which has never had a nat rule will look like this:

        "*filter",
        ":INPUT ACCEPT [0:0]",
        ":FORWARD DROP [0:0]",
        ":OUTPUT ACCEPT [0:0]",
        "COMMIT",

This broke the existing check_mode checks, which did simple string comparison. Most likely there were also edge cases outside of check mode in regards to uninitialized tables of existing iptables-save dumps, which would report changes when restoring them, despite nothing changing. This is only the case for iptables-save files created through means other than this module.

To circument this I implemented parsing of iptables-save dumps into a simple data structure based on preexisting code. If a table currently has some kind of state (including rules) but is not referenced in the iptables-save dump, it will not be touched by iptables-restore and now handled properly by this module.

I also added the tables tables_after after restoring as a data structure, similar to the already existing state tables before running the module

@ansibullbot ansibullbot added bug This issue/PR relates to a bug integration tests/integration tests tests labels Feb 25, 2024
@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Feb 25, 2024
@Maxopoly
Copy link
Contributor Author

Ok, a58bc16 can reproduce the issue described. Incomplete iptables save files are not handled properly in check mode.

@ansibullbot ansibullbot removed the needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI label Feb 25, 2024
@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added module module plugins plugin (any type) labels Feb 25, 2024
@ansibullbot

This comment was marked as outdated.

@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added has_issue and removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Feb 25, 2024
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-7 backport-8 Automatically create a backport for the stable-8 branch labels Feb 25, 2024
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Mar 4, 2024
@Maxopoly
Copy link
Contributor Author

Maxopoly commented Mar 7, 2024

Fixed as suggested and removed the additional return value introduced. I'll make an additional PR against master to readd that once this PR is merged, as it depends on the changes made here.

@Maxopoly Maxopoly requested a review from felixfontein March 7, 2024 20:24
@ansibullbot ansibullbot removed the stale_ci CI is older than 7 days, rerun before merging label Mar 7, 2024
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If nobody objects, I'll merge this in ~a week.

@felixfontein felixfontein changed the title Fix checkmod in iptables_state for incomplete iptables-save files along with integration tests Fix check mode in iptables_state for incomplete iptables-save files along with integration tests Mar 17, 2024
@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Mar 17, 2024
@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Mar 24, 2024
@felixfontein felixfontein merged commit 23396e6 into ansible-collections:main Mar 24, 2024
121 checks passed
Copy link

patchback bot commented Mar 24, 2024

Backport to stable-7: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-7/23396e62dc75bb4a9a16f0d1b29d85fb66d93725/pr-8029

Backported as #8136

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein
Copy link
Collaborator

@Maxopoly thanks for your contribution!

patchback bot pushed a commit that referenced this pull request Mar 24, 2024
…long with integration tests (#8029)

* Implement integration test to reproduce #7463

* Make new iptables_state checks async

* Add missing commit to iptable_state integration test

* Remove async when using checkmode in iptables_state integration tests

* Do per table comparison in check mode for iptables_state

* Calculate changes of iptables state per table based on result

* Output target iptables state in checkmode

* Refactor calculation of invidual table states in iptables_state

* Add missing return for table calculation

* Add missing arg to regex check

* Remove leftover debug output for target iptable state

* Parse per table state from raw state string

* Join restored state for extration of table specific rules

* Switch arguments for joining restored iptable state

* Output final ip table state

* Compare content of tables

* Complete iptables partial tables test cases

* Correct order of test iptables data

* Update docu for iptables tables_after

* Add changelog fragment

* Appease the linting gods for iptables_state

* Adjust spelling and remove tables_after from return values

(cherry picked from commit 23396e6)
Copy link

patchback bot commented Mar 24, 2024

Backport to stable-8: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-8/23396e62dc75bb4a9a16f0d1b29d85fb66d93725/pr-8029

Backported as #8137

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Mar 24, 2024
…long with integration tests (#8029)

* Implement integration test to reproduce #7463

* Make new iptables_state checks async

* Add missing commit to iptable_state integration test

* Remove async when using checkmode in iptables_state integration tests

* Do per table comparison in check mode for iptables_state

* Calculate changes of iptables state per table based on result

* Output target iptables state in checkmode

* Refactor calculation of invidual table states in iptables_state

* Add missing return for table calculation

* Add missing arg to regex check

* Remove leftover debug output for target iptable state

* Parse per table state from raw state string

* Join restored state for extration of table specific rules

* Switch arguments for joining restored iptable state

* Output final ip table state

* Compare content of tables

* Complete iptables partial tables test cases

* Correct order of test iptables data

* Update docu for iptables tables_after

* Add changelog fragment

* Appease the linting gods for iptables_state

* Adjust spelling and remove tables_after from return values

(cherry picked from commit 23396e6)
felixfontein pushed a commit that referenced this pull request Mar 24, 2024
…te for incomplete iptables-save files along with integration tests (#8136)

Fix check mode in iptables_state for incomplete iptables-save files along with integration tests (#8029)

* Implement integration test to reproduce #7463

* Make new iptables_state checks async

* Add missing commit to iptable_state integration test

* Remove async when using checkmode in iptables_state integration tests

* Do per table comparison in check mode for iptables_state

* Calculate changes of iptables state per table based on result

* Output target iptables state in checkmode

* Refactor calculation of invidual table states in iptables_state

* Add missing return for table calculation

* Add missing arg to regex check

* Remove leftover debug output for target iptable state

* Parse per table state from raw state string

* Join restored state for extration of table specific rules

* Switch arguments for joining restored iptable state

* Output final ip table state

* Compare content of tables

* Complete iptables partial tables test cases

* Correct order of test iptables data

* Update docu for iptables tables_after

* Add changelog fragment

* Appease the linting gods for iptables_state

* Adjust spelling and remove tables_after from return values

(cherry picked from commit 23396e6)

Co-authored-by: Maxopoly <max@dermax.org>
felixfontein pushed a commit that referenced this pull request Mar 24, 2024
…te for incomplete iptables-save files along with integration tests (#8137)

Fix check mode in iptables_state for incomplete iptables-save files along with integration tests (#8029)

* Implement integration test to reproduce #7463

* Make new iptables_state checks async

* Add missing commit to iptable_state integration test

* Remove async when using checkmode in iptables_state integration tests

* Do per table comparison in check mode for iptables_state

* Calculate changes of iptables state per table based on result

* Output target iptables state in checkmode

* Refactor calculation of invidual table states in iptables_state

* Add missing return for table calculation

* Add missing arg to regex check

* Remove leftover debug output for target iptable state

* Parse per table state from raw state string

* Join restored state for extration of table specific rules

* Switch arguments for joining restored iptable state

* Output final ip table state

* Compare content of tables

* Complete iptables partial tables test cases

* Correct order of test iptables data

* Update docu for iptables tables_after

* Add changelog fragment

* Appease the linting gods for iptables_state

* Adjust spelling and remove tables_after from return values

(cherry picked from commit 23396e6)

Co-authored-by: Maxopoly <max@dermax.org>
Massl123 pushed a commit to Massl123/community.general that referenced this pull request Feb 7, 2025
…long with integration tests (ansible-collections#8029)

* Implement integration test to reproduce ansible-collections#7463

* Make new iptables_state checks async

* Add missing commit to iptable_state integration test

* Remove async when using checkmode in iptables_state integration tests

* Do per table comparison in check mode for iptables_state

* Calculate changes of iptables state per table based on result

* Output target iptables state in checkmode

* Refactor calculation of invidual table states in iptables_state

* Add missing return for table calculation

* Add missing arg to regex check

* Remove leftover debug output for target iptable state

* Parse per table state from raw state string

* Join restored state for extration of table specific rules

* Switch arguments for joining restored iptable state

* Output final ip table state

* Compare content of tables

* Complete iptables partial tables test cases

* Correct order of test iptables data

* Update docu for iptables tables_after

* Add changelog fragment

* Appease the linting gods for iptables_state

* Adjust spelling and remove tables_after from return values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8 Automatically create a backport for the stable-8 branch bug This issue/PR relates to a bug has_issue integration tests/integration module module plugins plugin (any type) stale_ci CI is older than 7 days, rerun before merging tests tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iptables_state restore not working when restoring rules
3 participants