misp-taxonomies/tools/gen_manifest.py

53 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-11-05 14:47:54 +01:00
# -*- coding: utf-8 -*-
import json
from pathlib import Path
from datetime import datetime
TAXONOMY_ROOT_PATH = Path(__file__).resolve().parent.parent
def fetchTaxonomies():
taxonomiesFolder = TAXONOMY_ROOT_PATH
taxonomies = []
2019-11-05 15:03:02 +01:00
allTaxonomies = list(taxonomiesFolder.glob('./*/machinetag.json'))
allTaxonomies.sort()
for taxonomyFile in allTaxonomies:
2019-11-05 15:13:41 +01:00
with open(taxonomyFile, 'rb') as f:
taxonomy = json.load(f)
taxonomies.append(taxonomy)
return taxonomies
def generateManifest(taxonomies):
manifest = {}
manifest['taxonomies'] = []
manifest['path'] = 'machinetag.json'
2020-09-08 11:42:38 +02:00
manifest['url'] = 'https://raw.githubusercontent.com/MISP/misp-taxonomies/main/'
manifest['description'] = 'Manifest file of MISP taxonomies available.'
manifest['license'] = 'CC-0'
now = datetime.now()
manifest['version'] = '{}{:02}{:02}'.format(now.year, now.month, now.day)
for taxonomy in taxonomies:
taxObj = {
'name': taxonomy['namespace'],
'description': taxonomy['description'],
'version': taxonomy['version']
}
manifest['taxonomies'].append(taxObj)
return manifest
def saveManifest(manifest):
with open(TAXONOMY_ROOT_PATH / 'MANIFEST.json', 'w') as f:
2019-11-05 15:13:41 +01:00
json.dump(manifest, f, indent=2, sort_keys=True, ensure_ascii=False)
2019-11-05 15:20:30 +01:00
f.write('\n')
def awesomePrint(text):
print('\033[1;32m{}\033[0;39m'.format(text))
if __name__ == "__main__":
taxonomies = fetchTaxonomies()
manifest = generateManifest(taxonomies)
saveManifest(manifest)
2019-11-05 14:47:54 +01:00
awesomePrint('> Manifest saved!')