Skip to content

Commit

Permalink
Merge pull request #50086 from twangboy/fix_win_groupadd
Browse files Browse the repository at this point in the history
Make win_groupadd module behave like other modules
  • Loading branch information
Mike Place authored Dec 10, 2018
2 parents 93d1a75 + fd54884 commit 241741a
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 323 deletions.
264 changes: 102 additions & 162 deletions salt/modules/win_groupadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@
<module-provider-override>`.
'''
from __future__ import absolute_import, unicode_literals, print_function
import logging

# Import Salt libs
import salt.utils.platform
import salt.utils.win_functions


try:
import win32api
import win32com.client
import pythoncom
import pywintypes
HAS_DEPENDENCIES = True
except ImportError:
HAS_DEPENDENCIES = False

log = logging.getLogger(__name__)

# Define the module's virtual name
__virtualname__ = 'group'

Expand Down Expand Up @@ -100,36 +103,29 @@ def add(name, **kwargs):
The name of the group to add
Returns:
dict: A dictionary of results
bool: ``True`` if successful, otherwise ``False``
CLI Example:
.. code-block:: bash
salt '*' group.add foo
'''
ret = {'name': name,
'result': True,
'changes': [],
'comment': ''}

if not info(name):
compObj = _get_computer_object()
comp_obj = _get_computer_object()
try:
newGroup = compObj.Create('group', name)
newGroup.SetInfo()
ret['changes'].append('Successfully created group {0}'.format(name))
except pywintypes.com_error as com_err:
ret['result'] = False
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['comment'] = 'Failed to create group {0}. {1}' \
''.format(name, friendly_error)
new_group = comp_obj.Create('group', name)
new_group.SetInfo()
log.info('Successfully created group {0}'.format(name))
except pywintypes.com_error as exc:
msg = 'Failed to create group {0}. {1}'.format(
name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False
else:
ret['result'] = None
ret['comment'] = 'The group {0} already exists.'.format(name)
log.warning('The group {0} already exists.'.format(name))

return ret
return True


def delete(name, **kwargs):
Expand All @@ -142,38 +138,28 @@ def delete(name, **kwargs):
The name of the group to remove
Returns:
dict: A dictionary of results
bool: ``True`` if successful, otherwise ``False``
CLI Example:
.. code-block:: bash
salt '*' group.delete foo
'''
ret = {'name': name,
'result': True,
'changes': [],
'comment': ''}

if info(name):
compObj = _get_computer_object()
comp_obj = _get_computer_object()
try:
compObj.Delete('group', name)
ret['changes'].append(('Successfully removed group {0}').format(name))
except pywintypes.com_error as com_err:
ret['result'] = False
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['comment'] = (
'Failed to remove group {0}. {1}'
).format(name, friendly_error)
comp_obj.Delete('group', name)
log.info('Successfully removed group {0}'.format(name))
except pywintypes.com_error as exc:
msg = 'Failed to remove group {0}. {1}'.format(
name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False
else:
ret['result'] = None
ret['comment'] = (
'The group {0} does not exists.'
).format(name)
log.warning('The group {0} does not exists.'.format(name))

return ret
return True


def info(name):
Expand All @@ -198,7 +184,10 @@ def info(name):
groupObj = _get_group_object(name)
gr_name = groupObj.Name
gr_mem = [_get_username(x) for x in groupObj.members()]
except pywintypes.com_error:
except pywintypes.com_error as exc:
msg = 'Failed to access group {0}. {1}'.format(
name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False

if not gr_name:
Expand Down Expand Up @@ -261,54 +250,38 @@ def adduser(name, username, **kwargs):
The name of the user to add to the group
Returns:
dict: A dictionary of results
bool: ``True`` if successful, otherwise ``False``
CLI Example:
.. code-block:: bash
salt '*' group.adduser foo username
'''

ret = {'name': name,
'result': True,
'changes': {'Users Added': []},
'comment': ''}

try:
groupObj = _get_group_object(name)
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['result'] = False
ret['comment'] = 'Failure accessing group {0}. {1}' \
''.format(name, friendly_error)
return ret

existingMembers = [_get_username(x) for x in groupObj.members()]
group_obj = _get_group_object(name)
except pywintypes.com_error as exc:
msg = 'Failed to access group {0}. {1}'.format(
name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False

existing_members = [_get_username(x) for x in group_obj.members()]
username = salt.utils.win_functions.get_sam_name(username)

try:
if username not in existingMembers:
if not __opts__['test']:
groupObj.Add('WinNT://' + username.replace('\\', '/'))

ret['changes']['Users Added'].append(username)
if username not in existing_members:
group_obj.Add('WinNT://' + username.replace('\\', '/'))
log.info('Added user {0}'.format(username))
else:
ret['comment'] = (
'User {0} is already a member of {1}'
).format(username, name)
ret['result'] = None
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['comment'] = (
'Failed to add {0} to group {1}. {2}'
).format(username, name, friendly_error)
ret['result'] = False
return ret
log.warning('User {0} is already a member of {1}'.format(username, name))
except pywintypes.com_error as exc:
msg = 'Failed to add {0} to group {1}. {2}'.format(
username, name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False

return ret
return True


def deluser(name, username, **kwargs):
Expand All @@ -324,53 +297,36 @@ def deluser(name, username, **kwargs):
The name of the user to remove from the group
Returns:
dict: A dictionary of results
bool: ``True`` if successful, otherwise ``False``
CLI Example:
.. code-block:: bash
salt '*' group.deluser foo username
'''

ret = {'name': name,
'result': True,
'changes': {'Users Removed': []},
'comment': ''}

try:
groupObj = _get_group_object(name)
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['result'] = False
ret['comment'] = 'Failure accessing group {0}. {1}' \
''.format(name, friendly_error)
return ret
group_obj = _get_group_object(name)
except pywintypes.com_error as exc:
msg = 'Failed to access group {0}. {1}'.format(
name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False

existingMembers = [_get_username(x) for x in groupObj.members()]
existing_members = [_get_username(x) for x in group_obj.members()]

try:
if salt.utils.win_functions.get_sam_name(username) in existingMembers:
if not __opts__['test']:
groupObj.Remove('WinNT://' + username.replace('\\', '/'))

ret['changes']['Users Removed'].append(username)
if salt.utils.win_functions.get_sam_name(username) in existing_members:
group_obj.Remove('WinNT://' + username.replace('\\', '/'))
log.info('Removed user {0}'.format(username))
else:
ret['comment'] = (
'User {0} is not a member of {1}'
).format(username, name)
ret['result'] = None
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['comment'] = (
'Failed to remove {0} from group {1}. {2}'
).format(username, name, friendly_error)
ret['result'] = False
return ret
log.warning('User {0} is not a member of {1}'.format(username, name))
except pywintypes.com_error as exc:
msg = 'Failed to remove {0} from group {1}. {2}'.format(
username, name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)

return ret
return True


def members(name, members_list, **kwargs):
Expand All @@ -387,78 +343,62 @@ def members(name, members_list, **kwargs):
contain only the users specified in this list.
Returns:
dict: A dictionary of results
bool: ``True`` if successful, otherwise ``False``
CLI Example:
.. code-block:: bash
salt '*' group.members foo 'user1,user2,user3'
'''

ret = {'name': name,
'result': True,
'changes': {'Users Added': [], 'Users Removed': []},
'comment': []}

members_list = [salt.utils.win_functions.get_sam_name(m) for m in members_list.split(",")]
if not isinstance(members_list, list):
ret['result'] = False
ret['comment'].append('Members is not a list object')
return ret
log.debug('member_list is not a list')
return False

try:
groupObj = _get_group_object(name)
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['result'] = False
ret['comment'].append((
'Failure accessing group {0}. {1}'
).format(name, friendly_error))
return ret
existingMembers = [_get_username(x) for x in groupObj.members()]
existingMembers.sort()
obj_group = _get_group_object(name)
except pywintypes.com_error as exc:
# Group probably doesn't exist, but we'll log the error
msg = 'Failed to access group {0}. {1}'.format(
name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
return False

existing_members = [_get_username(x) for x in obj_group.members()]
existing_members.sort()
members_list.sort()

if existingMembers == members_list:
ret['result'] = None
ret['comment'].append(('{0} membership is correct').format(name))
return ret
if existing_members == members_list:
log.info('{0} membership is correct'.format(name))
return True

# add users
success = True
for member in members_list:
if member not in existingMembers:
if member not in existing_members:
try:
if not __opts__['test']:
groupObj.Add('WinNT://' + member.replace('\\', '/'))
ret['changes']['Users Added'].append(member)
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['result'] = False
ret['comment'].append((
'Failed to add {0} to {1}. {2}'
).format(member, name, friendly_error))
#return ret
obj_group.Add('WinNT://' + member.replace('\\', '/'))
log.info('User added: {0}'.format(member))
except pywintypes.com_error as exc:
msg = 'Failed to add {0} to {1}. {2}'.format(
member, name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
success = False

# remove users not in members_list
for member in existingMembers:
for member in existing_members:
if member not in members_list:
try:
if not __opts__['test']:
groupObj.Remove('WinNT://' + member.replace('\\', '/'))
ret['changes']['Users Removed'].append(member)
except pywintypes.com_error as com_err:
if len(com_err.excepinfo) >= 2:
friendly_error = com_err.excepinfo[2].rstrip('\r\n')
ret['result'] = False
ret['comment'].append((
'Failed to remove {0} from {1}. {2}'
).format(member, name, friendly_error))
#return ret

return ret
obj_group.Remove('WinNT://' + member.replace('\\', '/'))
log.info('User removed: {0}'.format(member))
except pywintypes.com_error as exc:
msg = 'Failed to remove {0} from {1}. {2}'.format(
member, name, win32api.FormatMessage(exc.excepinfo[5]))
log.error(msg)
success = False

return success


def list_groups(refresh=False):
Expand Down
Loading

0 comments on commit 241741a

Please sign in to comment.