add: Added user config to specify if there is a header in the csv to import

pull/190/head
chrisr3d 2018-05-18 11:33:53 +02:00
parent 4ca8e8e0de
commit 1fb72f3c7a
No known key found for this signature in database
GPG Key ID: 6BBED1B63A6D639F
1 changed files with 20 additions and 12 deletions

View File

@ -10,15 +10,21 @@ moduleconfig = []
inputSource = ['file'] inputSource = ['file']
userConfig = {'header': { userConfig = {'header': {
'type': 'String', 'type': 'String',
'message': 'Define the header of the csv file, with types (included in MISP attribute types or attribute fields) separated by commas.\nFor fields that do not match these types, please use space or simply nothing between commas.\nFor instance: ip-src,domain, ,timestamp'}} 'message': 'Define the header of the csv file, with types (included in MISP attribute types or attribute fields) separated by commas.\nFor fields that do not match these types, please use space or simply nothing between commas.\nFor instance: ip-src,domain, ,timestamp'},
'has_header':{
'type': 'Boolean',
'message': 'Tick this box ONLY if there is a header line, NOT COMMENTED, in the file (which will be skipped atm).'
}}
duplicatedFields = {'mispType': {'mispComment': 'comment'}, duplicatedFields = {'mispType': {'mispComment': 'comment'},
'attrField': {'attrComment': 'comment'}} 'attrField': {'attrComment': 'comment'}}
attributesFields = ['type', 'value', 'category', 'to_ids', 'comment', 'distribution'] attributesFields = ['type', 'value', 'category', 'to_ids', 'comment', 'distribution']
class CsvParser(): class CsvParser():
def __init__(self, header): def __init__(self, header, has_header):
self.header = header self.header = header
self.fields_number = len(header)
self.has_header = has_header
self.attributes = [] self.attributes = []
def parse_data(self, data): def parse_data(self, data):
@ -27,12 +33,12 @@ class CsvParser():
l = line.split('#')[0].strip() if '#' in line else line.strip() l = line.split('#')[0].strip() if '#' in line else line.strip()
if l: if l:
return_data.append(l) return_data.append(l)
self.data = return_data self.data = return_data[1:] if self.has_header else return_data
# find which delimiter is used # find which delimiter is used
self.delimiter, self.length = self.findDelimiter() self.delimiter = self.find_delimiter()
def findDelimiter(self): def find_delimiter(self):
n = len(self.header) n = self.fields_number
if n > 1: if n > 1:
tmpData = [] tmpData = []
for da in self.data: for da in self.data:
@ -41,11 +47,11 @@ class CsvParser():
if da.count(d) == (n-1): if da.count(d) == (n-1):
tmp.append(d) tmp.append(d)
if len(tmp) == 1 and tmp == tmpData: if len(tmp) == 1 and tmp == tmpData:
return tmpData[0], n return tmpData[0]
else: else:
tmpData = tmp tmpData = tmp
else: else:
return None, 1 return None
def buildAttributes(self): def buildAttributes(self):
# if there is only 1 field of data # if there is only 1 field of data
@ -63,7 +69,7 @@ class CsvParser():
datamisp = [] datamisp = []
datasplit = data.split(self.delimiter) datasplit = data.split(self.delimiter)
# in case there is an empty line or an error # in case there is an empty line or an error
if len(datasplit) != self.length: if len(datasplit) != self.fields_number:
continue continue
# pop from the line data that matches with a misp type, using the list of indexes # pop from the line data that matches with a misp type, using the list of indexes
for l in list2pop: for l in list2pop:
@ -118,9 +124,11 @@ def handler(q=False):
if not request.get('config') and not request['config'].get('header'): if not request.get('config') and not request['config'].get('header'):
misperrors['error'] = "Configuration error" misperrors['error'] = "Configuration error"
return misperrors return misperrors
config = request['config'].get('header').split(',') header = request['config'].get('header').split(',')
config = [c.strip() for c in config] header = [c.strip() for c in header]
csv_parser = CsvParser(config) has_header = request['config'].get('has_header')
has_header = True if has_header == '1' else False
csv_parser = CsvParser(header, has_header)
csv_parser.parse_data(data.split('\n')) csv_parser.parse_data(data.split('\n'))
# build the attributes # build the attributes
csv_parser.buildAttributes() csv_parser.buildAttributes()