mirror of
https://gitlab.com/apparmor/apparmor.git
synced 2025-03-04 00:14:44 +01:00
Add option to log aa-logprof json input and output
Add a json_log option (default: disabled) to logprof.conf that enables logging of all aa-logprof and aa-genprof input and output to a /tmp/aa-jsonlog-* file. This can be useful for debugging, and maybe also to create tests that do a full aa-logprof run. This patch introduces a minor behaviour change if aa-logprof errors out on startup (for example if the config file is broken or the parser can't be found): Before: ``` $ aa-logprof --json {"dialog": "apparmor-json-version","data": "2.12"} ERROR: Can't find apparmor_parser at /sbin/apparmor_parser ``` After: ``` $ aa-logprof --json ERROR: Can't find apparmor_parser at /sbin/apparmor_parser ``` Note that the json version line will not be printed if aa-logprof or aa-genprof error out that early. If there are no startup errors, the behaviour will not change.
This commit is contained in:
parent
17a521ff50
commit
7dc0254b90
5 changed files with 31 additions and 10 deletions
|
@ -72,12 +72,13 @@ parser.add_argument('-j', '--json', action="store_true", help=_('Input and Outpu
|
|||
parser.add_argument('--configdir', type=str, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.json:
|
||||
aaui.set_json_mode()
|
||||
|
||||
profiling = args.program
|
||||
|
||||
apparmor.init_aa(confdir=args.configdir, profiledir=args.dir)
|
||||
|
||||
if args.json:
|
||||
aaui.set_json_mode(apparmor.cfg)
|
||||
|
||||
apparmor.set_logfile(args.file)
|
||||
|
||||
aa_mountpoint = apparmor.check_for_apparmor()
|
||||
|
|
|
@ -31,13 +31,13 @@ parser.add_argument('-j', '--json', action='store_true', help=_('Input and Outpu
|
|||
parser.add_argument('--configdir', type=str, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.json:
|
||||
aaui.set_json_mode()
|
||||
|
||||
logmark = args.mark or ''
|
||||
|
||||
apparmor.init_aa(confdir=args.configdir, profiledir=args.dir)
|
||||
|
||||
if args.json:
|
||||
aaui.set_json_mode(apparmor.cfg)
|
||||
|
||||
apparmor.set_logfile(args.file)
|
||||
|
||||
aa_mountpoint = apparmor.check_for_apparmor()
|
||||
|
|
|
@ -33,14 +33,20 @@ debug_logger = DebugLogger('UI')
|
|||
ARROWS = {'A': 'UP', 'B': 'DOWN', 'C': 'RIGHT', 'D': 'LEFT'}
|
||||
|
||||
UI_mode = 'text'
|
||||
jsonlog = None
|
||||
|
||||
|
||||
def write_json(jsonout):
|
||||
print(json.dumps(jsonout, sort_keys=False, separators=(',', ': ')))
|
||||
jtxt = json.dumps(jsonout, sort_keys=False, separators=(',', ': '))
|
||||
|
||||
if jsonlog:
|
||||
jsonlog.write('o ' + jtxt + '\n')
|
||||
|
||||
print(jtxt)
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def set_json_mode():
|
||||
def set_json_mode(cfg):
|
||||
"""
|
||||
Currently this is only used by aa-genprof and aa-logprof, while e.g.
|
||||
aa-status generates its own JSON output.
|
||||
|
@ -50,8 +56,14 @@ def set_json_mode():
|
|||
|
||||
Current known consumers of the JSON output:
|
||||
- YaST
|
||||
|
||||
The cfg parameter expects the parsed logprof.conf aka apparmor.aa.cfg.
|
||||
"""
|
||||
global UI_mode
|
||||
global UI_mode, jsonlog
|
||||
|
||||
if int(cfg['settings'].get('json_log', False)):
|
||||
jsonlog = NamedTemporaryFile('w', prefix='aa-jsonlog-', delete=False, encoding='utf-8')
|
||||
|
||||
UI_mode = 'json'
|
||||
jsonout = {'dialog': 'apparmor-json-version', 'data': '2.12'}
|
||||
write_json(jsonout)
|
||||
|
@ -67,6 +79,10 @@ def set_text_mode():
|
|||
# for the dialog type
|
||||
def json_response(dialog_type):
|
||||
string = input('\n')
|
||||
|
||||
if jsonlog:
|
||||
jsonlog.write('i ' + string + '\n')
|
||||
|
||||
rh = json.loads(string.strip())
|
||||
if rh["dialog"] != dialog_type:
|
||||
raise AppArmorException('Expected response %s got %s.' % (dialog_type, string))
|
||||
|
|
|
@ -35,6 +35,10 @@
|
|||
# files.
|
||||
custom_includes =
|
||||
|
||||
# When called with --json, log all input and output to a tempfile (/tmp/aa-jsonlog-*)
|
||||
# Only enable for debugging.
|
||||
# Note that aa-logprof will not display any hint that aa-jsonlog-* gets written.
|
||||
json_log = 0
|
||||
|
||||
[qualifiers]
|
||||
# things will be painfully broken if bash has a profile
|
||||
|
|
|
@ -61,7 +61,7 @@ class AACliBootstrapTest(AATest):
|
|||
self.assertEqual(sys.stdout.getvalue(), 'Test string\n')
|
||||
|
||||
def test_aa_ui_info_json(self):
|
||||
aaui.set_json_mode()
|
||||
aaui.set_json_mode({'settings': {}})
|
||||
sys.stdout.getvalue()
|
||||
aaui.UI_Info('Test string')
|
||||
self.assertEqual(
|
||||
|
|
Loading…
Add table
Reference in a new issue