utils: cleanup serialize_profile() and its callers

Merge branch 'cboltz-cleanup-serialize-profile'

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
PR: https://gitlab.com/apparmor/apparmor/merge_requests/138
This commit is contained in:
Steve Beattie 2018-06-29 15:21:24 -07:00
commit 8fc0ff7ffc
Failed to generate hash of commit
4 changed files with 16 additions and 21 deletions

View file

@ -126,8 +126,8 @@ def act(files, merging_profile):
elif ans == 'CMD_VIEW_CHANGES': elif ans == 'CMD_VIEW_CHANGES':
for program in programs: for program in programs:
apparmor.aa.original_aa[program] = apparmor.aa.deepcopy(apparmor.aa.aa[program]) apparmor.aa.original_aa[program] = apparmor.aa.deepcopy(apparmor.aa.aa[program])
#oldprofile = apparmor.serialize_profile(apparmor.original_aa[program], program, '') #oldprofile = apparmor.serialize_profile(apparmor.original_aa[program], program, {})
newprofile = apparmor.aa.serialize_profile(mergeprofiles.user.aa[program], program, '') newprofile = apparmor.aa.serialize_profile(mergeprofiles.user.aa[program], program, {})
aaui.UI_Changes(mergeprofiles.user.filename, newprofile, comments=True) aaui.UI_Changes(mergeprofiles.user.filename, newprofile, comments=True)
elif ans == 'CMD_IGNORE_ENTRY': elif ans == 'CMD_IGNORE_ENTRY':
break break

View file

@ -501,7 +501,7 @@ def get_profile(prof_name):
inactive_profile[prof_name][prof_name].pop('filename') inactive_profile[prof_name][prof_name].pop('filename')
profile_hash[uname]['username'] = uname profile_hash[uname]['username'] = uname
profile_hash[uname]['profile_type'] = 'INACTIVE_LOCAL' profile_hash[uname]['profile_type'] = 'INACTIVE_LOCAL'
profile_hash[uname]['profile'] = serialize_profile(inactive_profile[prof_name], prof_name, None) profile_hash[uname]['profile'] = serialize_profile(inactive_profile[prof_name], prof_name, {})
profile_hash[uname]['profile_data'] = inactive_profile profile_hash[uname]['profile_data'] = inactive_profile
existing_profiles.pop(prof_name) # remove profile filename from list to force storing in /etc/apparmor.d/ instead of extra_profile_dir existing_profiles.pop(prof_name) # remove profile filename from list to force storing in /etc/apparmor.d/ instead of extra_profile_dir
@ -732,7 +732,6 @@ def sync_profile():
repo_profiles = [] repo_profiles = []
changed_profiles = [] changed_profiles = []
new_profiles = [] new_profiles = []
serialize_opts = dict()
status_ok, ret = fetch_profiles_by_user(cfg['repository']['url'], status_ok, ret = fetch_profiles_by_user(cfg['repository']['url'],
cfg['repository']['distro'], user) cfg['repository']['distro'], user)
if not status_ok: if not status_ok:
@ -741,7 +740,7 @@ def sync_profile():
aaui.UI_Important(_('WARNING: Error synchronizing profiles with the repository:\n%s\n') % ret) aaui.UI_Important(_('WARNING: Error synchronizing profiles with the repository:\n%s\n') % ret)
else: else:
users_repo_profiles = ret users_repo_profiles = ret
serialize_opts['NO_FLAGS'] = True serialize_opts = {'FLAGS': False}
for prof in sorted(aa.keys()): for prof in sorted(aa.keys()):
if is_repo_profile([aa[prof][prof]]): if is_repo_profile([aa[prof][prof]]):
repo_profiles.append(prof) repo_profiles.append(prof)
@ -1892,15 +1891,14 @@ def save_profiles():
else: else:
oldprofile = get_profile_filename(which) oldprofile = get_profile_filename(which)
serialize_options = {} serialize_options = {'METADATA': True}
serialize_options['METADATA'] = True
newprofile = serialize_profile(aa[which], which, serialize_options) newprofile = serialize_profile(aa[which], which, serialize_options)
aaui.UI_Changes(oldprofile, newprofile, comments=True) aaui.UI_Changes(oldprofile, newprofile, comments=True)
elif ans == 'CMD_VIEW_CHANGES_CLEAN': elif ans == 'CMD_VIEW_CHANGES_CLEAN':
oldprofile = serialize_profile(original_aa[which], which, '') oldprofile = serialize_profile(original_aa[which], which, {})
newprofile = serialize_profile(aa[which], which, '') newprofile = serialize_profile(aa[which], which, {})
aaui.UI_Changes(oldprofile, newprofile) aaui.UI_Changes(oldprofile, newprofile)
@ -2666,15 +2664,13 @@ def write_piece(profile_data, depth, name, nhat, write_flags):
def serialize_profile(profile_data, name, options): def serialize_profile(profile_data, name, options):
string = '' string = ''
include_metadata = False
include_flags = True
data = [] data = []
if options: # and type(options) == dict: if type(options) is not dict:
if options.get('METADATA', False): raise AppArmorBug('serialize_profile(): options is not a dict: %s' % options)
include_metadata = True
if options.get('NO_FLAGS', False): include_metadata = options.get('METADATA', False)
include_flags = False include_flags = options.get('FLAGS', True)
if include_metadata: if include_metadata:
string = '# Last Modified: %s\n' % time.asctime() string = '# Last Modified: %s\n' % time.asctime()
@ -2737,8 +2733,7 @@ def write_profile(profile):
#os.chmod(newprof.name, permission_600) #os.chmod(newprof.name, permission_600)
pass pass
serialize_options = {} serialize_options = {'METADATA': True}
serialize_options['METADATA'] = True
profile_string = serialize_profile(aa[profile], profile, serialize_options) profile_string = serialize_profile(aa[profile], profile, serialize_options)
newprof.write(profile_string) newprof.write(profile_string)

View file

@ -223,8 +223,8 @@ class aa_tools:
apparmor.write_profile_ui_feedback(program) apparmor.write_profile_ui_feedback(program)
self.reload_profile(filename) self.reload_profile(filename)
elif ans == 'CMD_VIEW_CHANGES': elif ans == 'CMD_VIEW_CHANGES':
#oldprofile = apparmor.serialize_profile(apparmor.original_aa[program], program, '') #oldprofile = apparmor.serialize_profile(apparmor.original_aa[program], program, {})
newprofile = apparmor.serialize_profile(apparmor.aa[program], program, '') newprofile = apparmor.serialize_profile(apparmor.aa[program], program, {})
aaui.UI_Changes(filename, newprofile, comments=True) aaui.UI_Changes(filename, newprofile, comments=True)
else: else:
apparmor.write_profile_ui_feedback(program) apparmor.write_profile_ui_feedback(program)

View file

@ -260,7 +260,7 @@ def logfile_to_profile(logfile):
apparmor.aa.filelist = apparmor.aa.hasher() apparmor.aa.filelist = apparmor.aa.hasher()
apparmor.aa.filelist[profile_dummy_file]['profiles'][profile] = True apparmor.aa.filelist[profile_dummy_file]['profiles'][profile] = True
new_profile = apparmor.aa.serialize_profile(log_dict[aamode][profile], profile, None) new_profile = apparmor.aa.serialize_profile(log_dict[aamode][profile], profile, {})
return profile, new_profile return profile, new_profile