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

win_user_right: add module with tests #26276

Merged
merged 7 commits into from Aug 10, 2017

Conversation

jborean93
Copy link
Contributor

SUMMARY

Added a new module win_user_right that allows you to add/remove/set user rights for either local or domain accounts.

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

win_user_right

ANSIBLE VERSION
ansible 2.4.0 (win_user_right-module d5bfe4cf93) last updated 2017/06/30 18:20:08 (GMT +1000)
  config file = None
  configured module search path = [u'/home/jborean/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/jborean/dev/ansible/lib/ansible
  executable location = /home/jborean/dev/ansible/bin/ansible
  python version = 2.7.13 (default, Jun 28 2017, 21:13:49) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]

ADDITIONAL INFORMATION

This module is to extend #22775 where it allows you to idempotently set users/groups on user rights without knowing the SID for said account.

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_module This PR includes a new module. new_plugin This PR includes a new plugin. support:community This issue/PR relates to code supported by the Ansible community. test_pull_requests windows Windows community labels Jun 30, 2017
@ansibot
Copy link
Contributor

ansibot commented Jun 30, 2017

The test ansible-test sanity --test pep8 failed with the following error:

lib/ansible/modules/windows/win_user_right.py:99:31: W291 trailing whitespace

click here for bot help

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Jun 30, 2017
@ansibot
Copy link
Contributor

ansibot commented Jun 30, 2017

@SamLiu79 @timothyvandenbrande @ar7z1 @blakfeld @brianlloyd @chrishoffman @if-meaton @joshludwig @petemounce @schwartzmx @smadam813

As a maintainer of a module in the same namespace this new module has been submitted to, your vote counts for shipits. Please review this module and add shipit if you would like to see it merged.

click here for bot help

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 30, 2017
@s-hertel s-hertel removed the needs_triage Needs a first human triage before being processed. label Jun 30, 2017
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. new_module This PR includes a new module. labels Jul 12, 2017
Copy link
Member

@nitzmahone nitzmahone left a comment

Choose a reason for hiding this comment

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

Also looks great- a couple things that could be tightened up and a couple of potential perf issues for large policies.

foreach ($existing_user in $existing_users) {
$user_match = $true
foreach ($user in $users) {
$user_sid = Get-SID -account_name $user
Copy link
Member

Choose a reason for hiding this comment

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

This is potentially expensive if either list is large, since you have to re-lookup the specified user SIDs on each value . Might be better to do a single-pass over each upfront to build a HashSet and use set theory methods to find users to remove. Could also probably call the LINQ set-theory extension methods (they'd take care of building the underlying sets for you).


# sort the user objects for later comparison and remove duplicates
$new_users = $new_users | Sort-Object -Unique
$existing_users = $existing_users | Sort-Object -Unique
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't at least the uniqueness be handled inside the Build-XList function (if not also the sort)?

removed = @()
}

foreach ($entry in $existing_list) {
Copy link
Member

Choose a reason for hiding this comment

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

Another potentially good place for HashSet and set-theory methods (especially if they got built once outside as HashSet, since order probably doesn't matter)...

$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
$users = Get-AnsibleParam -obj $params -name "users" -type "list" -failifempty $true
$action = Get-AnsibleParam -obj $params -name "action" -type "str" -default "set" -validateset "add","remove","set"

Copy link
Member

Choose a reason for hiding this comment

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

Do we want to add diff support (esp since you've already done the work of calculating the diff)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have added diff support, I didn't remove the added/removed return items as they were used in the tests and thought they were still useful to have.

# check the return code and if the file has been populated, otherwise error out
if (($export_result.rc -ne 0) -or ((Get-Item -Path $secedit_ini_path).Length -eq 0)) {
Remove-Item -Path $secedit_ini_path # file is empty and we don't need it
Fail-Json $result "Failed to export secedit.ini file to $($secedit_ini_path).`nRC: $($export_result.rc)`nSTDOUT: $($export_result.stdout)`nSTDERR: $($export_result.stderr)`nLOG: $($export_result.log)"
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to include all that in the message, or just return discrete keys in the fail dict? Could see reasons for either...

Remove-Item -Path $secedit_ini_path # file is empty and we don't need it
Fail-Json $result "Failed to export secedit.ini file to $($secedit_ini_path).`nRC: $($export_result.rc)`nSTDOUT: $($export_result.stdout)`nSTDERR: $($export_result.stderr)`nLOG: $($export_result.log)"
}
$secedit_ini = ConvertFrom-Ini -file_path $secedit_ini_path
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't we just extract the initial change calculation into a function and re-run it here, failing if it reported changed?

options:
name:
description:
- The name of the User Right as shown by the Constant name here
Copy link
Member

Choose a reason for hiding this comment

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

s/Constant name here/C(Constant Name) value from/

that:
- add_right_on_existing_check|changed
- add_right_on_existing_check.removed == []
- add_right_on_existing_check.added == ["BUILTIN\\Users", "BUILTIN\\Guests"]
Copy link
Member

Choose a reason for hiding this comment

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

Why the mix of double-quoted/escaped and single-quoted stuff below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

\U and \Gthrows an error when using single quotes, I'll change it so they are all consistent and use double quotes.

Copy link
Contributor

@dagwieers dagwieers Jul 17, 2017

Choose a reason for hiding this comment

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

You mean single backslashes ?

We are recommending in our new documentation to use single quotes as much as possible to prevent this behavior and the need to escape backslashes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so 'BUILTIN\User' fails but "BUILTIN\\Users" is fine, note this is only for the assertions and not for the module parameters, there are test cases where there is an entry called '{{ansible_hostanme}}\Users' and that works fine.

Copy link
Contributor

@dagwieers dagwieers Jul 17, 2017

Choose a reason for hiding this comment

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

Oh right, Jinja2 and Python don't care about single or double quotes, which is making our generic advice a bit more difficult than we planned :-( We still have to document the behavior of Jinja2...

cc: @jhawkesworth

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jul 12, 2017
@dagwieers
Copy link
Contributor

+label new_module

@ansibot ansibot added new_module This PR includes a new module. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Jul 13, 2017
@jborean93
Copy link
Contributor Author

@nitzmahone, I've made the changes from your comments, let me know if there is anything I've miseed.

@ansibot ansibot added the module This issue/PR relates to a module. label Jul 25, 2017
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Jul 25, 2017
@ansibot ansibot removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Aug 8, 2017
@jborean93
Copy link
Contributor Author

@nitzmahone, just updated this PR to no longer use SecEdit.exe, makes things simpler as we don't need to parse an ini file.

@dagwieers
Copy link
Contributor

dagwieers commented Aug 8, 2017

So the module freeze deadline for v2.4 is getting closer (2017-08-29). Now is the time to finish up, get it reviewed and merged with no delay.

@jborean93 jborean93 merged commit e46adec into ansible:devel Aug 10, 2017
@jborean93 jborean93 deleted the win_user_right-module branch August 10, 2017 21:52
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 module This issue/PR relates to a module. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_module This PR includes a new module. new_plugin This PR includes a new plugin. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. windows Windows community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants