Skip to content

Commit

Permalink
Fix salt.states.selinux._refine_value()
Browse files Browse the repository at this point in the history
This function was not properly handling True/False values, causing the
state not to work when one of these values is used. This makes the
example in the docstring fail.

Also cleaned up/simplified _refine_mode.

Fixes saltstack#5912.
  • Loading branch information
terminalmage committed Jul 3, 2013
1 parent 94d5c17 commit e98d141
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions salt/states/selinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,30 @@

def _refine_mode(mode):
'''
Return a mode value that is completely predictable
Return a mode value that is predictable
'''
if any([
str(mode).startswith('e'),
str(mode) == '1',
str(mode).startswith('E'),
str(mode) == 'on']):
mode = str(mode).lower()
if any([mode.startswith('e'),
mode == '1',
mode == 'on']):
return 'Enforcing'
if any([
str(mode).startswith('p'),
str(mode) == '0',
str(mode).startswith('P'),
str(mode) == 'off']):
return 'Permissive'
if any([mode.startswith('p'),
mode == '0',
mode == 'off']):
return 'Permissive'
return 'unknown'


def _refine_value(value):
'''
Return a value that is completely predictable
Return a yes/no value, or None if the input is invalid
'''
if any([
str(value) == '1',
str(value) == 'on']):
value = str(value).lower()
if value in ('1', 'on', 'yes', 'true'):
return 'on'
if any([
str(value) == '0',
str(value) == 'off']):
if value in ('0', 'off', 'no', 'false'):
return 'off'
return None


def mode(name):
Expand Down Expand Up @@ -111,9 +106,14 @@ def boolean(name, value, persist=False):
ret['comment'] = 'Boolean {0} is not available'.format(name)
ret['result'] = False
return ret
value = _refine_value(value)
state = bools[name]['State'] == value
default = bools[name]['Default'] == value
rvalue = _refine_value(value)
if rvalue is None:
ret['comment'] = '{0} is not a valid value for the ' \
'boolean'.format(value)
ret['result'] = False
return ret
state = bools[name]['State'] == rvalue
default = bools[name]['Default'] == rvalue
if persist:
if state and default:
ret['comment'] = 'Boolean is in the correct state'
Expand All @@ -125,11 +125,11 @@ def boolean(name, value, persist=False):
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'Boolean {0} is set to be changed to {1}'.format(
name, value)
name, rvalue)
return ret

if __salt__['selinux.setsebool'](name, value, persist):
ret['comment'] = 'Boolean {0} has been set to {1}'.format(name, value)
if __salt__['selinux.setsebool'](name, rvalue, persist):
ret['comment'] = 'Boolean {0} has been set to {1}'.format(name, rvalue)
return ret
ret['comment'] = 'Failed to set the boolean {0} to {1}'.format(name, value)
ret['comment'] = 'Failed to set the boolean {0} to {1}'.format(name, rvalue)
return ret

0 comments on commit e98d141

Please sign in to comment.