Mercurial > farol > farolluz
annotate farolluz/producttree.py @ 46:1b7f3f4f1238
Add the possibility to batch-import trees
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Tue, 30 Dec 2014 12:28:06 +0100 |
parents | b87f2a6e613a |
children | 652f59fbea3a |
rev | line source |
---|---|
0 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Authors: | |
4 # BenoƮt Allard <benoit.allard@greenbone.net> | |
5 # | |
6 # Copyright: | |
7 # Copyright (C) 2014 Greenbone Networks GmbH | |
8 # | |
9 # This program is free software; you can redistribute it and/or | |
10 # modify it under the terms of the GNU General Public License | |
11 # as published by the Free Software Foundation; either version 2 | |
12 # of the License, or (at your option) any later version. | |
13 # | |
14 # This program is distributed in the hope that it will be useful, | |
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 # GNU General Public License for more details. | |
18 # | |
19 # You should have received a copy of the GNU General Public License | |
20 # along with this program; if not, write to the Free Software | |
21 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 | |
23 """\ | |
26
809db989cac5
Reorganize the code in smaller mpodules
Benoît Allard <benoit.allard@greenbone.net>
parents:
22
diff
changeset
|
24 Product Tree Objects related to CVRF Documents |
0 | 25 """ |
26 | |
26
809db989cac5
Reorganize the code in smaller mpodules
Benoît Allard <benoit.allard@greenbone.net>
parents:
22
diff
changeset
|
27 from .common import ValidationError |
0 | 28 |
29 class CVRFProductTree(object): | |
30 def __init__(self): | |
31 # All the branches, they can be order with their `parent` attribute | |
32 self._branches = [] | |
33 self._groups = [] | |
34 self._relationships = [] | |
35 self._products = [] | |
36 self._groups = [] | |
37 | |
38 def addProduct(self, product): | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
39 """ Add to the product list """ |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
40 self._products.append(product) |
0 | 41 |
42 def addRelationship(self, rel): | |
43 self._relationships.append(rel) | |
44 | |
45 def addGroup(self, group): | |
46 self._groups.append(group) | |
47 | |
48 def getProductForID(self, productid): | |
49 for product in self._products: | |
50 if product._productid == productid: | |
51 return product | |
52 raise KeyError(productid) | |
53 | |
43
b87f2a6e613a
Add CVE parsing (from OpenVAS GSA)
Benoît Allard <benoit.allard@greenbone.net>
parents:
26
diff
changeset
|
54 def getProductForCPE(self, cpe): |
b87f2a6e613a
Add CVE parsing (from OpenVAS GSA)
Benoît Allard <benoit.allard@greenbone.net>
parents:
26
diff
changeset
|
55 for product in self._products: |
b87f2a6e613a
Add CVE parsing (from OpenVAS GSA)
Benoît Allard <benoit.allard@greenbone.net>
parents:
26
diff
changeset
|
56 if product._cpe == cpe: |
b87f2a6e613a
Add CVE parsing (from OpenVAS GSA)
Benoît Allard <benoit.allard@greenbone.net>
parents:
26
diff
changeset
|
57 return product |
b87f2a6e613a
Add CVE parsing (from OpenVAS GSA)
Benoît Allard <benoit.allard@greenbone.net>
parents:
26
diff
changeset
|
58 raise KeyError(cpe) |
b87f2a6e613a
Add CVE parsing (from OpenVAS GSA)
Benoît Allard <benoit.allard@greenbone.net>
parents:
26
diff
changeset
|
59 |
0 | 60 def getGroupForID(self, groupid): |
61 for group in self._groups: | |
62 if group._groupid == groupid: | |
63 return group | |
64 raise KeyError(groupid) | |
65 | |
66 def decomposeProduct(self, productid): | |
67 """ In case of product defined as a relationship (product X installed | |
68 on OS Y), this gives us the following tuple: (OS, product). """ | |
69 product = self.getProductForID(productid) | |
70 parent = product._parent | |
71 if parent is None: | |
72 return (None, None) | |
73 if not isinstance(parent, CVRFRelationship): | |
74 return (None, None) | |
75 relationtype = parent._relationtype.replace(' ', '').lower() | |
76 if relationtype not in ('defaultcomponentof', 'installedon'): | |
77 return (None, None) | |
78 return ( | |
79 self.getProductForID(parent._relatestoproductreference), | |
80 self.getProductForID(parent._productreference) | |
81 ) | |
82 | |
83 def getBranch(self, path): | |
84 if len(path) == 0: | |
85 return self | |
86 branches = self._branches | |
87 node = None | |
88 for idx in path: | |
89 node = branches[idx] | |
90 branches = node._childs | |
91 return node | |
92 | |
93 def getBranches(self): | |
94 for branch in self._branches: | |
95 yield branch | |
96 for sub_branch in branch.getBranches(): | |
97 yield sub_branch | |
98 | |
99 def getPath(self): | |
100 return () | |
101 | |
102 def getNameOfRelationship(self, relationship): | |
103 if relationship is None: | |
104 return '' | |
105 return ' '.join((self.getProductForID(relationship._productreference)._name, 'as', | |
106 relationship._relationtype.lower(), | |
107 self.getProductForID(relationship._relatestoproductreference)._name)) | |
108 | |
109 def getOrphanedBranches(self, product=None): | |
110 """ The branches that could accept `product` as Product Definition """ | |
111 white_list = [] | |
112 if product is not None: | |
113 white_list = [product._parent] | |
114 for branch in self.getBranches(): | |
115 if (branch in white_list) or branch.isOrphaned(): | |
116 yield branch | |
117 | |
118 def getNotTerminalBranches(self, b2=None): | |
119 """\ | |
120 The branches that could accept `b2` as new sub-branches | |
121 Note that b2 and all its sub-branches cannot be listed | |
122 """ | |
123 black_list = [] | |
124 if b2 is not None: | |
125 black_list = [b2] + list(b2.getBranches()) | |
126 for branch in self.getBranches(): | |
127 if branch in black_list: | |
128 continue | |
129 if branch._product is None: | |
130 yield branch | |
131 | |
132 def getOrphanedRelationships(self, product=None): | |
133 """ The relationships that need a product defninition """ | |
134 white_list = [] | |
135 if product is not None: | |
136 white_list = [product.getCurrentRelationship()] | |
137 for i, relationship in enumerate(self._relationships): | |
138 if (relationship in white_list) or relationship.isOrphaned(): | |
139 yield (i, relationship) | |
140 | |
141 def nbProducts(self): | |
142 """ Amount of 'raw' Products """ | |
143 return len([p for p in self._products if p._parent is self]) | |
144 | |
46
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
145 def importTree(self, tree): |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
146 """ tree is a list of tuple (type, value) like the one generated by |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
147 getTree() """ |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
148 if len(tree) == 0: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
149 return self |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
150 found = None |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
151 for branch in self._branches: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
152 if branch.getTree() == tree[:1]: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
153 found = branch |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
154 if found is None: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
155 found = CVRFProductBranch(tree[0][0], tree[0][1], self) |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
156 return found.importTree(tree, 1) |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
157 |
0 | 158 def validate(self): |
159 for branch in self._branches: | |
160 branch.validate() | |
161 productids = set() | |
162 for product in self._products: | |
163 product.validate() | |
164 if product._productid in productids: | |
165 raise ValidationError('Each ProductID must be unique (%s)' % product._productid) | |
166 productids.add(product._productid) | |
167 for relationship in self._relationships: | |
168 relationship.validate() | |
169 for productid in (relationship._productreference, | |
170 relationship._relatestoproductreference): | |
171 if productid not in productids: | |
172 raise ValidationError('ProductID %s is unknown' % productid) | |
173 groupids = set() | |
174 for group in self._groups: | |
175 group.validate() | |
176 if group._groupid in groupids: | |
177 raise ValidationError('Duplicated GroupID: %s' % group._groupid) | |
178 groupids.add(group._groupid) | |
179 for productid in group._productids: | |
180 if productid not in productids: | |
181 raise ValidationError('ProductID %s is unknown' % productid) | |
182 return productids, groupids | |
183 | |
184 def __str__(self): | |
185 return 'Products: %s' % '\n'.join(str(p) for p in self._products) | |
186 | |
187 | |
188 class CVRFProductBranch(object): | |
189 TYPES = ('Vendor', 'Product Family', 'Product Name', 'Product Version', | |
190 'Patch Level', 'Service Pack', 'Architecture', 'Language', | |
191 'Legacy', 'Specification') | |
192 def __init__(self, _type, name, parentbranch): | |
193 self._type = _type | |
194 self._name = name | |
195 self._childs = [] | |
196 self._product = None | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
197 self.link(parentbranch) |
0 | 198 |
199 def getParent(self): | |
200 return self._parentbranch | |
201 | |
202 def getPath(self, string=False): | |
203 """ return the path to that branch element as a tuple """ | |
204 if self.isRoot(): | |
205 for i, b in enumerate(self._parentbranch._branches): | |
206 if b is self: | |
207 if string: | |
208 return '%d' % i | |
209 return (i, ) | |
210 else: | |
211 for i, b in enumerate(self._parentbranch._childs): | |
212 if b is self: | |
213 if string: | |
214 return '/'.join([self._parentbranch.getPath(string), '%d' % i]) | |
215 return self._parentbranch.getPath(string) + (i,) | |
216 if string: | |
217 return '' | |
218 return () | |
219 | |
220 def getTree(self): | |
221 """ this returns a list of tuples (type, name) leading to here""" | |
222 if self.isRoot(): | |
223 return [(self._type, self._name)] | |
224 return self._parentbranch.getTree() + [(self._type, self._name)] | |
225 | |
226 def getName(self): | |
227 return ' / '.join("%s: %s" % (type_, name) for type_, name in self.getTree()) | |
228 | |
229 def getParentPath(self): | |
230 """ return as string the path to the parent """ | |
231 return '/'.join('%s' % p for p in self.getPath()[:-1]) | |
232 | |
233 def isRoot(self): | |
234 return isinstance(self._parentbranch, CVRFProductTree) | |
235 | |
236 def isOrphaned(self): | |
237 """ Has no childs and no product """ | |
238 return len(self._childs) == 0 and (self._product is None) | |
239 | |
240 def getBranches(self): | |
241 for branch in self._childs: | |
242 yield branch | |
243 for sub_branch in branch.getBranches(): | |
244 yield sub_branch | |
245 | |
246 def unlink(self): | |
247 """ Unset our _parent, and remove us from the _parent._childs """ | |
248 if self.isRoot(): | |
249 self.getParent()._branches.remove(self) | |
250 else: | |
251 self.getParent()._childs.remove(self) | |
252 self._parentbranch = None | |
253 | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
254 def link(self, parent): |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
255 """ Actually, only set the parent """ |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
256 self._parentbranch = parent |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
257 if self.isRoot(): |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
258 parent._branches.append(self) |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
259 else: |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
260 parent._childs.append(self) |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
261 |
46
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
262 def importTree(self, tree, index): |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
263 """ tree is a list of tuple (type, value) like the one generated by |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
264 getTree() """ |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
265 if len(tree) == index: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
266 return self |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
267 found = None |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
268 for branch in self._childs: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
269 if branch.getTree() == tree[:index + 1]: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
270 found = branch |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
271 if found is None: |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
272 found = CVRFProductBranch(tree[index][0], tree[index][1], self) |
1b7f3f4f1238
Add the possibility to batch-import trees
Benoît Allard <benoit.allard@greenbone.net>
parents:
43
diff
changeset
|
273 return found.importTree(tree, index + 1) |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
274 |
0 | 275 def validate(self): |
276 if not self._type: | |
277 raise ValidationError('A Branch must have a Type') | |
278 if self._type not in self.TYPES: | |
279 raise ValidationError('A Branch Type must be one of %s' % ', '.join(self.TYPES)) | |
280 if not self._name: | |
281 raise ValidationError('A Branch must have a Name') | |
282 for branch in self._childs: | |
283 branch.validate() | |
284 if self.isOrphaned(): | |
285 raise ValidationError('A Branch must have at least a sub-product or sub-branches') | |
286 | |
287 def __str__(self): | |
288 return "%s: %s" % (self._type, self._name) | |
289 | |
290 | |
291 class CVRFFullProductName(object): | |
292 def __init__(self, productid, name, parent, cpe=None): | |
293 self._productid = productid | |
294 self._name = name | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
295 self._cpe = cpe |
0 | 296 # Can be None (directly under the tree), a ProductBranch, or a |
297 # Relationship | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
298 self.link(parent) |
0 | 299 |
300 def isRoot(self): | |
301 return isinstance(self._parent, CVRFProductTree) | |
302 | |
303 def isRelationship(self): | |
304 return isinstance(self._parent, CVRFRelationship) | |
305 | |
306 def getTree(self): | |
307 if not isinstance(self._parent, CVRFProductBranch): | |
308 return [] | |
309 return self._parent.getTree() | |
310 | |
311 def getParentPath(self): | |
312 if self.isRoot() or self.isRelationship(): | |
313 return '' | |
314 return self._parent.getPath(True) | |
315 | |
316 def getCurrentRelationship(self): | |
317 if self.isRelationship(): | |
318 return self._parent | |
319 return None | |
320 | |
321 def unlink(self): | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
322 """ Unset our _parent, and remove us from the _parent._childs |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
323 We are still in the product list. |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
324 """ |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
325 if not self.isRoot(): |
0 | 326 self._parent._product = None |
327 self._parent = None | |
328 | |
15
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
329 def link(self, parent): |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
330 self._parent = parent |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
331 if not self.isRoot(): |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
332 parent._product = self |
dcc946b30343
Consolidate productTree edition
Benoît Allard <benoit.allard@greenbone.net>
parents:
14
diff
changeset
|
333 |
0 | 334 def validate(self): |
335 if not self._productid: | |
336 raise ValidationError('A Product must have a ProductID') | |
337 if not self._name: | |
338 raise ValidationError('A Product must have a Name') | |
339 | |
340 def __str__(self): | |
341 return "%s (%s)" % (self._productid, self._name) | |
342 | |
343 | |
344 class CVRFRelationship(object): | |
345 TYPES = ('Default Component Of', 'Optional Component Of', | |
346 'External Component Of', 'Installed On', 'Installed With') | |
347 def __init__(self, productref, reltype, relatestoproductref): | |
348 self._productreference = productref | |
349 self._relationtype = reltype | |
350 self._relatestoproductreference = relatestoproductref | |
351 self._product = None | |
352 | |
353 def getParent(self): | |
354 """ All parent element of a FullProductName should implement that | |
355 method """ | |
356 return None | |
357 | |
358 def isOrphaned(self): | |
359 return self._product is None | |
360 | |
361 def validate(self): | |
362 if not self._productreference: | |
363 raise ValidationError('A Relationship must have a Product Reference') | |
364 if not self._relationtype: | |
365 raise ValidationError('A Relationship must have a Relation Type') | |
366 if self._relationtype not in self.TYPES: | |
367 raise ValidationError('Relation Type must be one of %s' % ', '.join(self.TYPES)) | |
368 if not self._relatestoproductreference: | |
369 raise ValidationError('A Relationship must have a "Relates To product Reference"') | |
370 if self._productreference == self._relatestoproductreference: | |
371 raise ValidationError('A Relationship cannot reference twice the same Product') | |
372 | |
373 | |
374 class CVRFGroup(object): | |
375 def __init__(self, groupid): | |
376 self._groupid = groupid | |
377 self._description = None | |
378 self._productids = [] | |
379 | |
380 def setDescription(self, description): | |
381 self._description = description | |
382 | |
383 def addProductID(self, productid): | |
384 self._productids.append(productid) | |
385 | |
386 def getTitle(self): | |
387 if self._description: | |
388 return "%s (%d products)" % (self._description, len(self._productids)) | |
389 return "#%s (%d products)" % (self._groupid, len(self._productids)) | |
390 | |
391 def validate(self): | |
392 if not self._groupid: | |
393 raise ValidationError('A Group must have a GroupID') | |
394 if not self._productids or len(self._productids) < 2: | |
395 raise ValidationError('A Group must contain at least two products') | |
396 |