extend add_or_remove_flag() to handle str for old flags

If the old flags are given as str (or None), call split_flags() to
convert them to a list.

This allows to simplify change_profile_flags() which now doesn't need to
call split_flags() on its own.

Also add some tests with a str for the old flags

(cherry picked from commit e80caa130a +
 conflict resolution)
This commit is contained in:
Christian Boltz 2018-07-25 20:59:34 +02:00
parent 41eae89869
commit 267c18e725
Failed to generate hash of commit
3 changed files with 10 additions and 5 deletions

View file

@ -49,7 +49,7 @@ from apparmor.regex import (RE_PROFILE_START, RE_PROFILE_END, RE_PROFILE_LINK,
RE_PROFILE_UNIX, RE_RULE_HAS_COMMA, RE_HAS_COMMENT_SPLIT, RE_PROFILE_UNIX, RE_RULE_HAS_COMMA, RE_HAS_COMMENT_SPLIT,
strip_quotes, parse_profile_start_line, re_match_include ) strip_quotes, parse_profile_start_line, re_match_include )
from apparmor.profile_storage import ProfileStorage, add_or_remove_flag, split_flags, ruletypes from apparmor.profile_storage import ProfileStorage, add_or_remove_flag, ruletypes
import apparmor.rules as aarules import apparmor.rules as aarules
@ -625,9 +625,7 @@ def get_profile_flags(filename, program):
def change_profile_flags(filename, program, flag, set_flag): def change_profile_flags(filename, program, flag, set_flag):
old_flags = get_profile_flags(filename, program) old_flags = get_profile_flags(filename, program)
newflags = split_flags(old_flags) newflags = add_or_remove_flag(old_flags, flag, set_flag)
newflags = add_or_remove_flag(newflags, flag, set_flag)
newflags = ','.join(newflags) newflags = ','.join(newflags)

View file

@ -14,7 +14,7 @@
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
from apparmor.common import AppArmorBug, hasher from apparmor.common import AppArmorBug, hasher, type_is_str
from apparmor.rule.capability import CapabilityRuleset from apparmor.rule.capability import CapabilityRuleset
from apparmor.rule.change_profile import ChangeProfileRuleset from apparmor.rule.change_profile import ChangeProfileRuleset
@ -120,6 +120,9 @@ def split_flags(flags):
def add_or_remove_flag(flags, flag_to_change, set_flag): def add_or_remove_flag(flags, flag_to_change, set_flag):
'''add (if set_flag == True) or remove the given flag_to_change to flags''' '''add (if set_flag == True) or remove the given flag_to_change to flags'''
if type_is_str(flags) or flags is None:
flags = split_flags(flags)
if set_flag: if set_flag:
if flag_to_change not in flags: if flag_to_change not in flags:
flags.append(flag_to_change) flags.append(flag_to_change)

View file

@ -46,6 +46,10 @@ class AaTest_add_or_remove_flag(AATest):
([ [], 'audit', False ], [] ), ([ [], 'audit', False ], [] ),
([ ['complain'], 'audit', True ], ['audit', 'complain'] ), ([ ['complain'], 'audit', True ], ['audit', 'complain'] ),
([ ['complain'], 'audit', False ], ['complain'] ), ([ ['complain'], 'audit', False ], ['complain'] ),
([ '', 'audit', True ], ['audit'] ),
([ None, 'audit', False ], [] ),
([ 'complain', 'audit', True ], ['audit', 'complain'] ),
([ ' complain ', 'audit', False ], ['complain'] ),
] ]
def _run_test(self, params, expected): def _run_test(self, params, expected):