61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a private, temporary Holmes configuration for the local Bash agent."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def compact_evidence(rows):
|
|
result = []
|
|
for line in rows.splitlines():
|
|
fields = line.split('\t')
|
|
if len(fields) < 4:
|
|
continue
|
|
row = dict(zip(('status', 'area', 'check', 'summary'), fields[:4]))
|
|
facts = []
|
|
for part in (fields[4] if len(fields) > 4 else '').split(' | '):
|
|
if part.startswith(('$ ', 'python3 ')) or not part.strip():
|
|
continue
|
|
prefix = 'Conditional recovery candidate (not executed): '
|
|
if part.startswith(prefix):
|
|
try:
|
|
row['conditional_recovery_candidate'] = json.loads(part[len(prefix):])
|
|
continue
|
|
except ValueError:
|
|
pass
|
|
if part not in facts:
|
|
facts.append(part)
|
|
row['observed_evidence'] = facts
|
|
result.append(row)
|
|
return result
|
|
|
|
|
|
def local_config(config, names):
|
|
if 'bash' not in names:
|
|
raise ValueError('Installed Holmes has no Bash toolset')
|
|
result = dict(config)
|
|
for key in ('custom_toolsets', 'custom_toolsets_from_cli', 'additional_toolsets'):
|
|
result.pop(key, None)
|
|
result['toolsets'] = {name: {'enabled': name == 'bash'} for name in names}
|
|
return result
|
|
|
|
|
|
def main():
|
|
if sys.argv[1] == 'evidence':
|
|
print(json.dumps(compact_evidence(Path(sys.argv[2]).read_text()), indent=2))
|
|
return
|
|
import yaml
|
|
from holmes.plugins.toolsets import load_builtin_toolsets
|
|
|
|
source, target = map(Path, sys.argv[1:])
|
|
config = yaml.safe_load(source.read_text()) or {}
|
|
result = local_config(config, [toolset.name for toolset in load_builtin_toolsets(None)])
|
|
fd = os.open(target, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(fd, 'w') as stream:
|
|
json.dump(result, stream)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|