Repository navigation
Expand file tree
/
Copy pathD3SAuditParser.py
More file actions
204 lines (164 loc) · 6.12 KB
/
Copy pathD3SAuditParser.py
File metadata and controls
204 lines (164 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import sys, traceback, argparse, time
import x509Certificate
import lxml.etree as ET
from iso8601 import iso8601
import csv
from collections import OrderedDict
class BoxUA:
_lockerName = None
_boxName = None
creationDate = None
uaCode = None
aName = None
def __init__(self, _lockerName, _boxName, creationDate, uaCode, uaName):
self._lockerName = _lockerName
self._boxName = _boxName
self.creationDate = creationDate
self.uaCode = uaCode
self.uaName = uaName
def getId(self):
return self._lockerName + "/" + self._boxName
def getUACode(self):
return self.uaCode
class UAReport:
read = 0
write = 0
year = None
month = None
uaCode = None
uaName = None
def __init__(self, boxUA, year, month):
self.uaCode = boxUA.uaCode
self.uaName = boxUA.uaName
self.year = year
self.month = month
def incrementRead(self):
self.read += 1
def incrementWrite(self):
self.write += 1
def updateReport(year, month, ua, uaReports, isRead, byTrimestre):
boxAuditReport = None
key = None
id = None
if byTrimestre:
id = getTrimestre(month)
key = "%d/T%d %s" % (year, id, ua.getUACode())
else:
id = month
key = "%d/%02d %s" % (year, id, ua.getUACode())
if key in uaReports:
boxAuditReport = uaReports.get(key)
else:
boxAuditReport = UAReport(ua, year, id)
UAReport(ua, year, id)
if isRead:
boxAuditReport.incrementRead()
else:
boxAuditReport.incrementWrite()
uaReports.update({key: boxAuditReport})
def getTrimestre(month):
if month == 1 or month == 2 or month == 3:
return 1
if month == 4 or month == 5 or month == 6:
return 2
if month == 7 or month == 8 or month == 9:
return 3
if month == 10 or month == 11 or month == 12:
return 4
else:
return None
def buildCSV(uaReports, isTrimestre):
line = ['Date', 'UA', 'UA Name', 'Lecture', 'Ecriture', 'Nombre de Lecture / Ecriture']
out = ';'.join(line) + '\n'
if isTrimestre:
dtFormat = "T%d %d;"
else:
dtFormat = "%02d/%d;"
for uaReport in uaReports.items():
out += dtFormat % (uaReport[1].month, uaReport[1].year)
out += uaReport[1].uaCode + ";"
out += uaReport[1].uaName + ";"
out += str(uaReport[1].read) + ";"
out += str(uaReport[1].write) + ";"
out += str(uaReport[1].read + uaReport[1].write) + "\n"
return out
try:
parser = argparse.ArgumentParser(description='DxS config parser.')
parser.add_argument('auditFile', metavar='--auditFile', type=str, help='auditFile')
parser.add_argument('uaFile', metavar='--uaFile', type=str, help='uaFile')
parser.add_argument('--boxToExclude', metavar='-bte', type=str, help='boxToExclude')
parser.add_argument('--outFile', metavar='-out', type=str, help='outFile')
args = parser.parse_args()
print "DxS config parser"
print "====================================="
print "auditFile\t:", args.auditFile
print "uaFile\t:", args.uaFile
print "boxToExclude\t:", args.boxToExclude
print "outFile\t\t:", args.outFile
dom = ET.parse(args.auditFile)
root = dom.getroot()
uaConfig = OrderedDict()
uaReports = OrderedDict()
uaReportsDetail = OrderedDict()
with open(args.uaFile, 'rb') as f:
reader = csv.DictReader(f, delimiter=';')
for i, row in enumerate(reader):
boxUA = BoxUA(row['Locker'], row['Safebox'], row['Creation date'],
row['UA code'], row['UA name'])
uaConfig.update({boxUA.getId(): boxUA})
for ops in root.iter('Operations'):
for op in ops.iter('Operation'):
for params in op.iter('Parameters'):
lockerbox = None
safebox = None
if "SECRET_CREATE" == op.attrib['Name']:
isRead = False
elif "UNSEAL_SECRET" == op.attrib['Name'] or "SECRET_READ" == op.attrib[
'Name'] or "SECRET_READ_NEED_APPROBATION" == op.attrib['Name']:
isRead = True
else:
continue
for param in params.iter('Parameter'):
attr = param.attrib['Name']
if attr == "urn:dictao:d3s:reports:lockerbox-name":
lockerbox = param.find('Value').find('StringValue').text
elif attr == "urn:dictao:d3s:reports:safe-box-name":
safebox = param.find('Value').find('StringValue').text
# skip test box
if args.boxToExclude != None:
if args.boxToExclude in safebox: continue
# all other locker had only safebox for the same UA
if ("ARMOIRE_DOCUMENT" != lockerbox):
safebox = "*"
date = iso8601.parse_date(op.find('Date').text)
# date = time.strptime(op.find('Date').text, "%Y-%m-%dT%H:%M:%S.%f%Z")
year = date.year
month = date.month
# print year, month
key = lockerbox + "/" + safebox
if key not in uaConfig:
ua = BoxUA("UNKNOW", lockerbox + "/" + safebox, "UNKNOW", lockerbox + "/" + safebox,
lockerbox + "/" + safebox)
uaConfig.update({key: ua})
else:
ua = uaConfig.get(key)
# by Trimestre
updateReport(year, month, ua, uaReports, isRead, True);
# by month
updateReport(year, month, ua, uaReportsDetail, isRead, False);
out = buildCSV(uaReports, True)
out += "\n\n"
out += buildCSV(uaReportsDetail, False)
if args.outFile is not None:
f = open(args.outFile, "w")
f.write(out)
f.close()
print "\nReport saved \t:", args.outFile
else:
print out
except Exception as e:
print e
traceback.print_exc(file=sys.stdout)
sys.exit(3)