Skip to content

Commit

Permalink
Merge pull request #56160 from twangboy/fix_reg_dword
Browse files Browse the repository at this point in the history
Fix issue with existing reg_dword entries
  • Loading branch information
dwoz authored Mar 11, 2020
2 parents 8f23706 + 80650c2 commit b33047c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
12 changes: 6 additions & 6 deletions salt/states/reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,13 @@ def present(name,
vname=vname,
use_32bit_registry=use_32bit_registry)

# Cast the vdata according to the vtype
vdata_decoded = __utils__['reg.cast_vdata'](vdata=vdata, vtype=vtype)

# Check if the key already exists
# If so, check perms
# We check `vdata` and `success` because `vdata` can be None
if vdata == reg_current['vdata'] and reg_current['success']:
if vdata_decoded == reg_current['vdata'] and reg_current['success']:
ret['comment'] = '{0} in {1} is already present' \
''.format(salt.utils.stringutils.to_unicode(vname, 'utf-8') if vname else '(Default)',
salt.utils.stringutils.to_unicode(name, 'utf-8'))
Expand All @@ -413,9 +416,6 @@ def present(name,
inheritance=win_inheritance,
reset=win_perms_reset)

# Cast the vdata according to the vtype
vdata_decoded = __utils__['reg.cast_vdata'](vdata=vdata, vtype=vtype)

add_change = {'Key': r'{0}\{1}'.format(hive, key),
'Entry': '{0}'.format(salt.utils.stringutils.to_unicode(vname, 'utf-8') if vname else '(Default)'),
'Value': vdata_decoded,
Expand All @@ -440,10 +440,10 @@ def present(name,

if not ret['result']:
ret['changes'] = {}
ret['comment'] = r'Failed to add {0} to {1}\{2}'.format(name, hive, key)
ret['comment'] = r'Failed to add {0} to {1}\{2}'.format(vname, hive, key)
else:
ret['changes'] = {'reg': {'Added': add_change}}
ret['comment'] = r'Added {0} to {1}\{2}'.format(name, hive, key)
ret['comment'] = r'Added {0} to {1}\{2}'.format(vname, hive, key)

if ret['result']:
ret = __utils__['dacl.check_perms'](
Expand Down
56 changes: 53 additions & 3 deletions tests/unit/states/test_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_present(self):
Test to set a registry entry.
'''
expected = {
'comment': 'Added {0} to {0}'.format(self.name),
'comment': 'Added {0} to {1}'.format(self.vname, self.name),
'pchanges': {},
'changes': {
'reg': {
Expand All @@ -58,15 +58,65 @@ def test_present(self):
'Perms': {
'Deny': None,
'Grant': None},
'Value': '0.15.3',
'Value': self.vdata,
'Key': self.name,
'Owner': None,
'Entry': 'version'}}},
'Entry': self.vname}}},
'name': self.name,
'result': True}
ret = reg.present(self.name, vname=self.vname, vdata=self.vdata)
self.assertDictEqual(ret, expected)

@destructiveTest
def test_present_string_dword(self):
'''
Test to set a registry entry.
'''
vname = 'dword_data'
vdata = '00000001'
vtype = 'REG_DWORD'
expected_vdata = 1
expected = {
'comment': 'Added {0} to {1}'.format(vname, self.name),
'pchanges': {},
'changes': {
'reg': {
'Added': {
'Inheritance': True,
'Perms': {
'Deny': None,
'Grant': None},
'Value': expected_vdata,
'Key': self.name,
'Owner': None,
'Entry': vname}}},
'name': self.name,
'result': True}
ret = reg.present(
self.name, vname=vname, vdata=vdata, vtype=vtype)
self.assertDictEqual(ret, expected)

@destructiveTest
def test_present_string_dword_existing(self):
'''
Test to set a registry entry.
'''
vname = 'dword_data'
vdata = '0000001'
vtype = 'REG_DWORD'
# Set it first
reg.present(
self.name, vname=vname, vdata=vdata, vtype=vtype)
expected = {
'comment': '{0} in {1} is already present'.format(vname, self.name),
'pchanges': {},
'changes': {},
'name': self.name,
'result': True}
ret = reg.present(
self.name, vname=vname, vdata=vdata, vtype=vtype)
self.assertDictEqual(ret, expected)

def test_present_test_true(self):
expected = {
'comment': '',
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/utils/test_win_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,17 @@ def test_cast_vdata_reg_dword(self):
Should always return integer
'''
vdata = 1
expected = 1
result = win_reg.cast_vdata(vdata=vdata, vtype='REG_DWORD')
self.assertTrue(isinstance(result, six.integer_types))
self.assertEqual(result, expected)

vdata = '1'
result = win_reg.cast_vdata(vdata=vdata, vtype='REG_DWORD')
self.assertTrue(isinstance(result, six.integer_types))
self.assertEqual(result, expected)

vdata = '0000001'
result = win_reg.cast_vdata(vdata=vdata, vtype='REG_DWORD')
self.assertEqual(result, expected)

def test_cast_vdata_reg_expand_sz(self):
'''
Expand Down

0 comments on commit b33047c

Please sign in to comment.