def test_audit_command_in_db(monkeypatch): monkeypatch.setenv('LEAPP_CURRENT_ACTOR', _ACTOR_NAME) monkeypatch.setenv('LEAPP_CURRENT_PHASE', _PHASE_NAME) monkeypatch.setenv('LEAPP_EXECUTION_ID', _CONTEXT_NAME) monkeypatch.setenv('LEAPP_HOSTNAME', _HOSTNAME) _id = str(uuid.uuid4()) cmd = ['whoami'] monkeypatch.setattr(uuid, 'uuid4', lambda: _id) result = run(cmd) assert result['stdout'] in ['travis\n', 'root\n'] event = 'process-start' assert get_audit_entry(event, _CONTEXT_NAME) event = 'process-result' assert get_audit_entry(event, _CONTEXT_NAME)
def _create_reports_from_deprecations(context_id): reports = [] cache = set() for entry in get_audit_entry(event='deprecation', context=context_id): data = json.loads(entry['data']) # Drop duplicates _data_dump = json.dumps(data, sort_keys=True).encode('utf-8') data_hash = hashlib.sha256(_data_dump).hexdigest() if data_hash in cache: continue cache.add(data_hash) # Create the report report = create_report_from_deprecation(data) sha256 = hashlib.sha256() sha256.update(data_hash.encode('utf-8')) sha256.update(entry['context'].encode('utf-8')) envelope = { 'timeStamp': entry['stamp'], 'hostname': os.environ['LEAPP_HOSTNAME'], 'actor': entry['actor'], 'id': sha256.hexdigest() } report.update(envelope) reports.append(report) return reports
def test_create_audit_entry(monkeypatch): monkeypatch.setenv('LEAPP_CURRENT_ACTOR', _ACTOR_NAME) monkeypatch.setenv('LEAPP_CURRENT_PHASE', _PHASE_NAME) monkeypatch.setenv('LEAPP_EXECUTION_ID', _CONTEXT_NAME) monkeypatch.setenv('LEAPP_HOSTNAME', _HOSTNAME) _id = str(uuid.uuid4()) event = 'process-start' create_audit_entry(event, {'id': _id, 'parameters': 'ls'}) assert get_audit_entry(event, _CONTEXT_NAME)
def report_deprecations(context_id, start=None): deprecations = get_audit_entry(event='deprecation', context=context_id) if start: start_stamp = start.isoformat() + 'Z' deprecations = [d for d in deprecations if d['stamp'] > start_stamp] if deprecations: cache = set() with pretty_block("USE OF DEPRECATED ENTITIES", target=sys.stderr, color=Color.red): for deprecation in deprecations: entry_data = json.loads(deprecation['data']) # Deduplicate messages key = hashlib.sha256(json.dumps(entry_data, sort_keys=True)).hexdigest() if key in cache: continue # Add current message to the cache cache.add(key) # Print the message sys.stderr.write( '{message} @ {filename}:{lineno}\nNear: {line}\nReason: {reason}\n{separator}\n'.format( separator='-' * 60, **entry_data) )