Skip to content
Snippets Groups Projects
Commit 150ddb93 authored by Pavel Kácha's avatar Pavel Kácha
Browse files

Python 2.6 compatibility changes

parent 744ba6c1
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python #!/usr/bin/python26
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (C) 2011-2015 Cesnet z.s.p.o # Copyright (C) 2011-2015 Cesnet z.s.p.o
# Use of this source is governed by a 3-clause BSD-style license, see LICENSE file. # Use of this source is governed by a 3-clause BSD-style license, see LICENSE file.
import os
import sys
sys.path.append('/data/warden/libs')
from warden_client import read_cfg, format_time from warden_client import read_cfg, format_time
from warden_filer import SafeDir from warden_filer import SafeDir
import json import json
import csv import csv
import sys
from time import strptime, mktime from time import strptime, mktime
from uuid import uuid4 from uuid import uuid4
# Conversion/validation routines # Conversion/validation routines
def isotime(t): def isotime(t):
return strptime(t, "%Y-%m-%d %H:%M:%S") if t else None if not t:
return None
return strptime(t, "%Y-%m-%d %H:%M:%S")
def intlist(il): def intlist(il):
return [int(i.strip()) for i in il.split(",")] if il else [] if not il:
return []
return [int(i.strip()) for i in il.split(",")]
def strlist(sl): def strlist(sl):
return [str(s) for s in sl.split(",")] if sl else [] if not sl:
return []
return [str(s) for s in sl.split(",")]
def ip(s): def ip(s):
return s.strip() if s else None if not s:
return None
return s.strip()
def iplist(sl): def iplist(sl):
return [ip(s) for s in sl.split(",")] if sl else [] if not sl:
return []
return [ip(s) for s in sl.split(",")]
ads_fieldnames = { ads_fieldnames = {
...@@ -68,7 +81,9 @@ ads_fieldnames = { ...@@ -68,7 +81,9 @@ ads_fieldnames = {
def xlat_ads_field(key, val): def xlat_ads_field(key, val):
type_ = ads_fieldnames[key]["type"] type_ = ads_fieldnames[key]["type"]
sval = val.strip() if val is not None else "" sval = ""
if val is not None:
sval = val.strip()
return type_(sval) return type_(sval)
...@@ -111,6 +126,8 @@ ads_types = { ...@@ -111,6 +126,8 @@ ads_types = {
def xlat_ads_type(s): def xlat_ads_type(s):
if s not in ads_types.keys():
return []
return ads_types[s] return ads_types[s]
...@@ -152,7 +169,9 @@ def gen_idea_from_ads(ads, anonymised_target): ...@@ -152,7 +169,9 @@ def gen_idea_from_ads(ads, anonymised_target):
source = {} source = {}
if ads["Source"]: if ads["Source"]:
srcip = ads["Source"] srcip = ads["Source"]
key = "IP4" if not ':' in srcip else "IP6" key = "IP6"
if not ':' in srcip:
key = "IP4"
source[key] = [srcip] source[key] = [srcip]
if ads["CapturedSource"]: if ads["CapturedSource"]:
...@@ -166,9 +185,16 @@ def gen_idea_from_ads(ads, anonymised_target): ...@@ -166,9 +185,16 @@ def gen_idea_from_ads(ads, anonymised_target):
if ads["Protocol"]: if ads["Protocol"]:
target["Proto"] = [xlat_ads_proto(p) for p in ads["Protocol"]] target["Proto"] = [xlat_ads_proto(p) for p in ads["Protocol"]]
tgtips = [anonymised_target] if anonymised_target else ads["Targets"] if anonymised_target:
tgtips = [anonymised_target]
else:
tgtips = ads["Targets"]
for tgtip in tgtips: for tgtip in tgtips:
key = "IP4" if not ':' in tgtip else "IP6" if not ':' in tgtip:
key = "IP4"
else:
key = "IP6"
target.setdefault(key, []).append(tgtip) target.setdefault(key, []).append(tgtip)
# Insert subnodes into event # Insert subnodes into event
...@@ -191,9 +217,14 @@ def main(): ...@@ -191,9 +217,14 @@ def main():
row[k] = xlat_ads_field(k, row[k]) row[k] = xlat_ads_field(k, row[k])
event = gen_idea_from_ads(row, anonymised_target) event = gen_idea_from_ads(row, anonymised_target)
nf = sdir.newfile() nf = sdir.newfile()
with nf.f as f: try:
f = nf.f
data = json.dumps(event) data = json.dumps(event)
f.write(data) f.write(data)
except IOError, e:
f.close()
else:
f.close()
nf.moveto(sdir.incoming) nf.moveto(sdir.incoming)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment