This repository was archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnapshot.py
More file actions
232 lines (191 loc) · 7.84 KB
/
snapshot.py
File metadata and controls
232 lines (191 loc) · 7.84 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#
# PacketSled - 2015
# Aaron Eppert (aaron.eppert@packetsled.com)
#
# BroControl Configuration Snapshot Interface
#
# Broctl.cfg options:
# snapshot.option = <string> <string> ... -> Paths that should be included in the snapshot - separated by a space
#
# Example:
# snapshot.option = /usr/local/bro/lib/bro/plugins /usr/local/bro/etc /usr/local/bro/share/bro
#
# snapshot.dest = "<string>" -> OPTIONAL Path to write the resulting tarball to (${BroBase}/snapshot is the default)
#
# snapshot.exclude = <string> <string> ... -> Entries that should be excluded from inclusion in the snapshot
#
import BroControl.plugin
import os
import sys
import errno
import json
import tarfile
import datetime
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
class SnapshotBro(BroControl.plugin.Plugin):
snapshot_ext = 'tar.bz2'
def __init__(self):
super(SnapshotBro, self).__init__(apiversion=1)
def name(self):
return "snapshot"
def pluginVersion(self):
return 1
def init(self):
self.snapshot_option = self.getOption("option")
self.snapshot_dest = self.getOption("dest")
self.snapshot_exclude = self.getOption("exclude")
self.brobase = self.getGlobalOption("brobase")
self.snapshotstate = self.getState("snapshotstate")
mkdir_p(self.snapshot_dest)
return True
def commands(self):
ret = [("list", "[-v]", "List Available Snapshots"),
("take", "[<identifier>]", "Take a Snapshot of the Current Bro Configuration"),
("revert", "<identifier>", "Revert the Bro Configuration to the Identifier Specified"),
("revertfile", "<path>", "Revert a Snapshot-formatted File"),
("remove", "<identifier>", "Remove the Snapshot of the Bro Configuration Specified by Identifier")]
return ret
def options(self):
return [("option", "string", "", "Directories or Files to Snapshot"),
("dest", "string", "${BroBase}/snapshot", "Directory to Write Snapshots"),
("exclude", "string", "", "Entries to exclude from Snapshot"),
("ext", "string", "tar.bz", "Extension for the Snapshot")]
def __gen_tarfile_name(self, args):
ret = args.replace(':', '-')
ret = ret.replace(' ', '_')
ret = ret.replace('\?', '_')
ret = ret.replace('\!', '_')
ret = '.'.join([ret, SnapshotBro.snapshot_ext])
return ret
def __create_tarfile(self, destdir, name, args):
tar = tarfile.open(os.path.sep.join([destdir, name]), "w:bz2")
for name in args:
tar.add(name, exclude=lambda x: any(ex in x for ex in self.__string_to_list(self.snapshot_exclude)))
tar.close()
def __extract_tarfile(self, name, basedir='/'):
tar = tarfile.open(name)
tar.extractall(path=basedir)
tar.close()
def __string_to_list(self, sp_sep_list):
ret = []
if len(sp_sep_list) > 0:
ret = [str(i) for i in sp_sep_list.split()]
return ret
def __gen_snapshot_entry(self, state_id, value, ts):
return {'id': state_id, 'file': value, 'ts': str(ts)}
def __snapshotstate_get(self):
ret = {}
if len(self.snapshotstate) > 0:
ret = json.loads(self.snapshotstate)
return ret
def __snapshotstate_set(self, state):
self.snapshotstate = json.dumps(state)
self.setState('snapshotstate', self.snapshotstate)
def __snapshotstate_entry_id_exist(self, state, identifier):
ret = False
if state:
ret = len([True for x in state if x['id'] == identifier]) > 0
return ret
def __snapshotstate_find(self, state_id):
ret = [x for x in self.__snapshotstate_get() if x['id'] == state_id]
return ret
def __snapshotstate_insert(self, state_id, value, ts):
t_state = self.__snapshotstate_get()
if t_state:
if self.__snapshotstate_entry_id_exist(t_state, state_id) == False:
t_state.append(self.__gen_snapshot_entry(state_id, value, ts))
else:
print 'Duplicate identifier "{0}" found - Aborting.'.format(str(state_id))
else:
if len(state_id) > 0 and len(value) > 0:
t_state = []
t_state.append(self.__gen_snapshot_entry(state_id, value, ts))
else:
assert(len(state_id) > 0 and len(value) > 0)
self.__snapshotstate_set(t_state)
def __snapshotstate_remove(self, state_id, unlink_file=True):
t_state = self.__snapshotstate_get()
if t_state:
ts = self.__snapshotstate_find(state_id)
if len(ts) > 0:
t_file = ts[0]['file']
try:
os.unlink(os.path.sep.join([self.snapshot_dest, t_file]))
except:
pass
t_state = [x for x in t_state if x['id'] != state_id]
self.__snapshotstate_set(t_state)
def _handle_list(self, args):
ret = []
t_state = self.__snapshotstate_get()
if t_state:
t_state_sorted = sorted(t_state, key=lambda k: k['ts'])
if '-v' in args:
print '{0:30s} {1:30s} {2:32s}'.format('IDENTIFIER', 'TIMESTAMP', 'FILE')
for t in t_state_sorted:
print u'{0:30s} {1:30s} {2:32s}'.format(t['id'], t['ts'], t['file'])
else:
print '{0:32s} {1:30s}'.format('IDENTIFIER', 'TIMESTAMP')
for t in t_state_sorted:
print '{0:32s} {1:30s}'.format(t['id'], t['ts'])
ret = t_state_sorted
return ret
def _handle_take(self, args):
ret = True
t_list = self.__string_to_list(self.snapshot_option)
if t_list:
ts = str(datetime.datetime.utcnow())
t_id = ts
if len(args) > 0:
t_id = str(args)
t_file = self.__gen_tarfile_name(t_id)
self.__create_tarfile(self.snapshot_dest,
t_file,
t_list)
self.__snapshotstate_insert(t_id, t_file, ts)
else:
print 'snapshot.option is not configured in broctl.cfg'
ret = False
return ret
def _handle_revert(self, args):
ret = False
if len(args) > 0:
t_s = self.__snapshotstate_find(args)
if len(t_s) > 0:
self.__extract_tarfile(os.path.sep.join([self.snapshot_dest, t_s[0]['file']]))
ret = True
else:
print 'Cannot find snapshot for identifier "{0}"'.format(args)
ret = False
return ret
def _handle_revertfile(self, args):
ret = False
if len(args) > 0:
if os.path.exists(args):
self.__extract_tarfile(self, name, basedir='/')
ret = True
return ret
def _handle_remove(self, args):
ret = False
t_state = self.__snapshotstate_get()
if t_state and len(args) > 0:
self.__snapshotstate_remove(args)
ret = True
return ret
def cmd_custom(self, cmd, args, cmdout):
valid_cmds = {'list': self._handle_list,
'take': self._handle_take,
'revert': self._handle_revert,
'revertfile': self._handle_revertfile,
'remove': self._handle_remove }
vc = valid_cmds.get(cmd, None)
assert(vc != None) # Can't be anything else.
# Run the handler here
return vc(args)