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

pamd: fixes for multiple issues #47695

Merged
merged 17 commits into from
Oct 30, 2018
Merged

pamd: fixes for multiple issues #47695

merged 17 commits into from
Oct 30, 2018

Conversation

shepdelacreme
Copy link
Contributor

SUMMARY

Provides fixes for a multitude of issues in the pamd module. This PR supersedes #47420 and includes changes from #47178

For #47418:

  • Update the regex to account for leading dashes that are allowed in the pamd config file spec.
  • Also created a VALID_TYPES constant to use throughout the module with all allowed types and their dash permutations.
    ['account', '-account', 'auth', '-auth', 'password', '-password', 'session', '-session']

For #47083
See changes from @mskymoore to fix update_rule() idempotence issue

For #47197
See changes from #47178 for add_module_arguments() idempotence and duplicate argument issue. Also cleaned up and simplified the logic and forced args_present action to fail if a complex argument (a complex argument is defined in the spec as argument within square brackets) is passed in that we can't handle with this function (users is notified to use updated action instead.

Fixes #47418
Fixes #47083
Fixes #47197

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

pamd

ANSIBLE VERSION
ansible 2.6.5
  config file = /Users/me/.ansible.cfg
  configured module search path = ['/Users/me/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/me/.virtualenvs/ansible/lib/python3.6/site-packages/ansible
  executable location = /Users/me/.virtualenvs/ansible/bin/ansible
  python version = 3.6.6 (default, Jul 21 2018, 22:49:24) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]

Also tested with 2.7.0 and devel

ADDITIONAL INFORMATION

See #47418
See #47083
See #47197
See #47178


@ansibot
Copy link
Contributor

ansibot commented Oct 26, 2018

Hi @shepdelacreme, thank you for submitting this pull-request!

click here for bot help

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_maintainer Ansibot is unable to identify maintainers for this PR. (Check `author` in docs or BOTMETA.yml) needs_triage Needs a first human triage before being processed. python3 support:community This issue/PR relates to code supported by the Ansible community. labels Oct 26, 2018
@shepdelacreme
Copy link
Contributor Author

cc @defionscode

@juliedavila juliedavila self-assigned this Oct 27, 2018
@juliedavila
Copy link
Contributor

shipit

@juliedavila
Copy link
Contributor

@jamescassell if you have a moment to take a peek, maybe kick the tires and add a "shipit" comment, we can get this in quicker, same goes for @mskymoore

@juliedavila
Copy link
Contributor

cc @kevensen

@jamescassell
Copy link
Contributor

I'll take a good look a soon as I can...

Copy link
Contributor

@jamescassell jamescassell left a comment

Choose a reason for hiding this comment

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

Added a few comments. Would be good to see some integration tests. Perhaps wholesale lift/shift the pamd tasks from the various STIG roles into an integration test.

Also, would be easier to review if you use '--fixup' commits, then autosquash those to make each commit a logical change as shown on github.

lib/ansible/modules/system/pamd.py Show resolved Hide resolved
if isinstance(new_args, str):
new_args = new_args.replace(" = ", "=")
new_args = new_args.split(' ')
if(current_rule.rule_args is not new_args):
Copy link
Contributor

Choose a reason for hiding this comment

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

is not vs !=? I may not be polished enough on my python...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is tests identity = tests equality. These if statements would always test true because current_rule.rule_args and new_args are two different objects. Using != tests for equality of what is contained in each variable not if the two vars are pointing to the same spot in memory.

lib/ansible/modules/system/pamd.py Show resolved Hide resolved
no_eq_new_args.add(new_arg)
else:
pair = new_arg.split("=")
new_args_d[pair[0]] = pair[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this handle the case where there may be multiple instances of = within an arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code was changed in a later commit.

lib/ansible/modules/system/pamd.py Show resolved Hide resolved
lib/ansible/modules/system/pamd.py Outdated Show resolved Hide resolved
# Return empty list if we have no args to parse
if not module_arguments:
return []
elif not module_arguments[0]:
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this check for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When parse_module_arguments is used from within the PamdRule class to parse out the arguments in an existing rule in a pamd file it can potentially send nothing if there are no matches.

This checks for the cases where the passed in args are None, and empty list [] and the second one checks for a list with a single empty item. i.e [''] and then returns early.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like it checks that the first element is empty, but not that there is a single element...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first part of the if handles module_arguments is:

  • an empty string
  • an empty list
  • None

The second part of the if handles module_arguments is:

  • a list with a single empty item ['']

I can add a check like isinstance(module_arguments, list) and len(module_arguments) == 1 and not module_arguments[0] if that is preferable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, and that would narrow it down to one ifstatement.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that's probably more defensive given UserError, it could be possible, though probably not that likely that there would be some array [None,'foo', 'bar']

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it narrows it down to a single if since module_arguments can be an empty string or None and if it is we still want to return [] which the first if covers. My change for this would look like

    if not module_arguments:
        return []
    elif isinstance(module_arguments, list) and len(module_arguments) == 1 and not module_arguments[0]:
        return []

Copy link
Contributor

Choose a reason for hiding this comment

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

Ack. Looks like a good change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, you could do an or but that might break pep8 on line length, I dont care either way, the major thing for me was just being defensive about index 0 potentially being a None value AND not the only index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah my preference is for shorter/simpler if tests. I'll update this. I wonder if I can make a suggested change to myself and apply it hehe.

lib/ansible/modules/system/pamd.py Show resolved Hide resolved
jamescassell and others added 2 commits October 30, 2018 09:29
Co-Authored-By: shepdelacreme <shepdelacreme@users.noreply.github.com>
@shepdelacreme
Copy link
Contributor Author

@jamescassell Thanks for the thorough review. And yes I agree integration tests here are 💯 needed. The unit tests for it are good but they didn't catch the issues the module had and I believe integration tests would have.

@juliedavila
Copy link
Contributor

Sweet, I'll open a new issue about adding an integration test.

@juliedavila juliedavila merged commit ef690e9 into ansible:devel Oct 30, 2018
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Nov 1, 2018
@shepdelacreme shepdelacreme deleted the pamd_fixes_multiple_issues branch November 7, 2018 16:52
abadger pushed a commit that referenced this pull request Nov 13, 2018
* pamd: fixes for multiple issues (#47695)

* Providing fix for #47083 in pamd.py

* Providing fix for #47197

* Fixing pep8 errors

* update regex to account for leading dash and VALID_TYPES with dashes as well

* use a results dictionary and clean up unnecessary items

* remove unnessecary return value. action is already reported in invocation output

* make naming consistent across action returns

* fix comparison so it checks equality instead of identity and indentation in update_rule()

* make sure file always has EOF newline

* updated regex to skip spacing between path and args and add rule arg regex to capture complex args

* new module argument parsing code in function and DRY changes

* remove unused has_rule method on PamdService class

* fix error in parse_module_arguments()

* updated args_present action to make it handle key value args and fail on complex bracketed arguments

* pep8 and other fixes so units still work

* suggested change - make version removed 2.8

Co-Authored-By: shepdelacreme <shepdelacreme@users.noreply.github.com>

* add more error proof test to if statement

(cherry picked from commit ef690e9)

* add changelog fragment for backport

* add action return value back for backport
mjmayer pushed a commit to mjmayer/ansible that referenced this pull request Nov 30, 2018
Tomorrow9 pushed a commit to Tomorrow9/ansible that referenced this pull request Dec 4, 2018
* Providing fix for ansible#47083 in pamd.py

* Providing fix for ansible#47197

* Fixing pep8 errors

* update regex to account for leading dash and VALID_TYPES with dashes as well

* use a results dictionary and clean up unnecessary items

* remove unnessecary return value. action is already reported in invocation output

* make naming consistent across action returns

* fix comparison so it checks equality instead of identity and indentation in update_rule()

* make sure file always has EOF newline

* updated regex to skip spacing between path and args and add rule arg regex to capture complex args

* new module argument parsing code in function and DRY changes

* remove unused has_rule method on PamdService class

* fix error in parse_module_arguments()

* updated args_present action to make it handle key value args and fail on complex bracketed arguments

* pep8 and other fixes so units still work

* suggested change - make version removed 2.8

Co-Authored-By: shepdelacreme <shepdelacreme@users.noreply.github.com>

* add more error proof test to if statement
Tomorrow9 pushed a commit to Tomorrow9/ansible that referenced this pull request Dec 4, 2018
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_maintainer Ansibot is unable to identify maintainers for this PR. (Check `author` in docs or BOTMETA.yml) python3 support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
6 participants