Skip to content
This repository has been archived by the owner. It is now read-only.

Commit afc409b

Browse files
committed
Move formatting code to hubble module
1 parent 5b27c2a commit afc409b

18 files changed

+114
-911
lines changed

_modules/hubble.py

+88-23
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,90 @@ def audit(configs=None,
143143
show_profile=show_profile
144144
)
145145

146+
terse_results = {}
147+
verbose_results = {}
148+
149+
# Pull out just the tag and description
150+
terse_results['Failure'] = []
151+
tags_descriptions = set()
152+
153+
for tag_data in ret.get('Failure', []):
154+
tag = tag_data['tag']
155+
description = tag_data.get('description')
156+
if (tag, description) not in tags_descriptions:
157+
terse_results['Failure'].append({tag: description})
158+
tags_descriptions.add((tag, description))
159+
160+
terse_results['Success'] = []
161+
tags_descriptions = set()
162+
163+
for tag_data in ret.get('Success', []):
164+
tag = tag_data['tag']
165+
description = tag_data.get('description')
166+
if (tag, description) not in tags_descriptions:
167+
terse_results['Success'].append({tag: description})
168+
tags_descriptions.add((tag, description))
169+
170+
terse_results['Controlled'] = []
171+
control_reasons = set()
172+
173+
for tag_data in ret.get('Controlled', []):
174+
tag = tag_data['tag']
175+
control_reason = tag_data.get('control', '')
176+
description = tag_data.get('description')
177+
if (tag, description, control_reason) not in control_reasons:
178+
terse_results['Controlled'].append({tag: control_reason})
179+
control_reasons.add((tag, description, control_reason))
180+
181+
# Calculate compliance level
146182
if show_compliance:
147-
compliance = _calculate_compliance(results)
148-
if compliance:
149-
results['Compliance'] = compliance
183+
compliance = _calculate_compliance(terse_results)
184+
else:
185+
compliance = False
150186

151-
if not called_from_top and not results:
152-
results['Messages'] = 'No audits matched this host in the specified profiles.'
187+
if not show_success and 'Success' in terse_results:
188+
terse_results.pop('Success')
153189

154-
if not show_success and 'Success' in results:
155-
results.pop('Success')
190+
if not terse_results['Controlled']:
191+
terse_results.pop('Controlled')
156192

157-
return ret
193+
# Format verbose output as single-key dictionaries with tag as key
194+
if verbose:
195+
verbose_results['Failure'] = []
196+
197+
for tag_data in ret.get('Failure', []):
198+
tag = tag_data['tag']
199+
verbose_results['Failure'].append({tag: tag_data})
200+
201+
verbose_results['Success'] = []
202+
203+
for tag_data in ret.get('Success', []):
204+
tag = tag_data['tag']
205+
verbose_results['Success'].append({tag: tag_data})
158206

207+
if not show_success and 'Success' in verbose_results:
208+
verbose_results.pop('Success')
209+
210+
verbose_results['Controlled'] = []
211+
212+
for tag_data in ret.get('Controlled', []):
213+
tag = tag_data['tag']
214+
verbose_results['Controlled'].append({tag: tag_data})
215+
216+
if not verbose_results['Controlled']:
217+
verbose_results.pop('Controlled')
218+
219+
results = verbose_results
220+
else:
221+
results = terse_results
222+
223+
if compliance:
224+
results['Compliance'] = compliance
225+
226+
if not called_from_top and not results:
227+
results['Messages'] = 'No audits matched this host in the specified profiles.'
228+
229+
return results
159230

160231
def _run_audit(configs, tags, verbose, debug, show_profile):
161232

@@ -246,21 +317,15 @@ def _run_audit(configs, tags, verbose, debug, show_profile):
246317
# Look through the failed results to find audits which match our control config
247318
failures_to_remove = []
248319
for i, failure in enumerate(results.get('Failure', [])):
249-
if isinstance(failure, str):
250-
if failure in processed_controls:
251-
failures_to_remove.append(i)
252-
if 'Controlled' not in results:
253-
results['Controlled'] = []
254-
results['Controlled'].append(
255-
{failure: processed_controls[failure].get('reason')})
256-
else: # dict
257-
for failure_tag in failure:
258-
if failure_tag in processed_controls:
259-
failures_to_remove.append(i)
260-
if 'Controlled' not in results:
261-
results['Controlled'] = []
262-
results['Controlled'].append(
263-
{failure_tag: processed_controls[failure_tag].get('reason')})
320+
failure_tag = failure['tag']
321+
if failure_tag in processed_controls:
322+
failures_to_remove.append(i)
323+
if 'Controlled' not in results:
324+
results['Controlled'] = []
325+
failure.update({
326+
'control': processed_controls[failure_tag].get('reason')
327+
})
328+
results['Controlled'].append(failure)
264329

265330
# Remove controlled failures from results['Failure']
266331
if failures_to_remove:

hubblestack_nova/command.py

-57
Original file line numberDiff line numberDiff line change
@@ -170,63 +170,6 @@ def audit(data_list, tags, verbose=False, show_profile=False, debug=False):
170170
else:
171171
ret['Failure'].append(tag_data)
172172

173-
failure = []
174-
success = []
175-
controlled = []
176-
177-
if not verbose:
178-
# Pull out just the tag and description
179-
tags_descriptions = set()
180-
181-
for tag_data in ret['Failure']:
182-
tag = tag_data['tag']
183-
description = tag_data.get('description')
184-
if (tag, description) not in tags_descriptions:
185-
failure.append({tag: description})
186-
tags_descriptions.add((tag, description))
187-
188-
tags_descriptions = set()
189-
190-
for tag_data in ret['Success']:
191-
tag = tag_data['tag']
192-
description = tag_data.get('description')
193-
if (tag, description) not in tags_descriptions:
194-
success.append({tag: description})
195-
tags_descriptions.add((tag, description))
196-
197-
control_reasons = set()
198-
199-
for tag_data in ret['Controlled']:
200-
tag = tag_data['tag']
201-
control_reason = tag_data.get('control', '')
202-
description = tag_data.get('description')
203-
if (tag, description, control_reason) not in control_reasons:
204-
tag_dict = {'description': description,
205-
'control': control_reason}
206-
controlled.append({tag: tag_dict})
207-
control_reasons.add((tag, description, control_reason))
208-
209-
else:
210-
# Format verbose output as single-key dictionaries with tag as key
211-
for tag_data in ret['Failure']:
212-
tag = tag_data['tag']
213-
failure.append({tag: tag_data})
214-
215-
for tag_data in ret['Success']:
216-
tag = tag_data['tag']
217-
success.append({tag: tag_data})
218-
219-
for tag_data in ret['Controlled']:
220-
tag = tag_data['tag']
221-
controlled.append({tag: tag_data})
222-
223-
ret['Controlled'] = controlled
224-
ret['Success'] = success
225-
ret['Failure'] = failure
226-
227-
if not ret['Controlled']:
228-
ret.pop('Controlled')
229-
230173
return ret
231174

232175

hubblestack_nova/cve_scan_v2.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -383,21 +383,16 @@ def get_report(self, verbose, show_profile, profile):
383383
'''
384384
Return the dictionary of what should be reported in failures, based on verbose.
385385
'''
386-
uid = self.pkg + '-' + self.pkg_version
387-
if verbose:
388-
report = {
389-
'href': self.href,
390-
'affected_version': self.pkg_version,
391-
'reporter': self.reporter,
392-
'score': self.score,
393-
'cve_list': self.cve_list,
394-
'affected_pkg': self.pkg,
395-
'local_version': self.oudated_version,
396-
'description': self.title
397-
}
398-
if show_profile:
399-
report['nova_profile'] = profile
400-
else:
401-
report = self.title
402-
return {uid: report}
386+
return {
387+
'tag': self.pkg + '-' + self.pkg_version,
388+
'href': self.href,
389+
'affected_version': self.pkg_version,
390+
'reporter': self.reporter,
391+
'score': self.score,
392+
'cve_list': self.cve_list,
393+
'affected_pkg': self.pkg,
394+
'local_version': self.oudated_version,
395+
'description': self.title,
396+
'nova_profile': profile
397+
}
403398

hubblestack_nova/firewall.py

-57
Original file line numberDiff line numberDiff line change
@@ -163,63 +163,6 @@ def audit(data_list, tags, verbose=False, show_profile=False, debug=False):
163163
else:
164164
ret['Failure'].append(tag_data)
165165

166-
failure = []
167-
success = []
168-
controlled = []
169-
170-
if not verbose:
171-
# Pull out just the tag and description
172-
tags_descriptions = set()
173-
174-
for tag_data in ret['Failure']:
175-
tag = tag_data['tag']
176-
description = tag_data.get('description')
177-
if (tag, description) not in tags_descriptions:
178-
failure.append({tag: description})
179-
tags_descriptions.add((tag, description))
180-
181-
tags_descriptions = set()
182-
183-
for tag_data in ret['Success']:
184-
tag = tag_data['tag']
185-
description = tag_data.get('description')
186-
if (tag, description) not in tags_descriptions:
187-
success.append({tag: description})
188-
tags_descriptions.add((tag, description))
189-
190-
control_reasons = set()
191-
192-
for tag_data in ret['Controlled']:
193-
tag = tag_data['tag']
194-
control_reason = tag_data.get('control', '')
195-
description = tag_data.get('description')
196-
if (tag, description, control_reason) not in control_reasons:
197-
tag_dict = {'description': description,
198-
'control': control_reason}
199-
controlled.append({tag: tag_dict})
200-
control_reasons.add((tag, description, control_reason))
201-
202-
else:
203-
# Format verbose output as single-key dictionaries with tag as key
204-
for tag_data in ret['Failure']:
205-
tag = tag_data['tag']
206-
failure.append({tag: tag_data})
207-
208-
for tag_data in ret['Success']:
209-
tag = tag_data['tag']
210-
success.append({tag: tag_data})
211-
212-
for tag_data in ret['Controlled']:
213-
tag = tag_data['tag']
214-
controlled.append({tag: tag_data})
215-
216-
ret['Controlled'] = controlled
217-
ret['Success'] = success
218-
ret['Failure'] = failure
219-
220-
if not ret['Controlled']:
221-
ret.pop('Controlled')
222-
223166
return ret
224167

225168

hubblestack_nova/grep.py

-57
Original file line numberDiff line numberDiff line change
@@ -148,63 +148,6 @@ def audit(data_list, tags, verbose=False, show_profile=False, debug=False):
148148
else:
149149
ret['Failure'].append(tag_data)
150150

151-
failure = []
152-
success = []
153-
controlled = []
154-
155-
if not verbose:
156-
# Pull out just the tag and description
157-
tags_descriptions = set()
158-
159-
for tag_data in ret['Failure']:
160-
tag = tag_data['tag']
161-
description = tag_data.get('description')
162-
if (tag, description) not in tags_descriptions:
163-
failure.append({tag: description})
164-
tags_descriptions.add((tag, description))
165-
166-
tags_descriptions = set()
167-
168-
for tag_data in ret['Success']:
169-
tag = tag_data['tag']
170-
description = tag_data.get('description')
171-
if (tag, description) not in tags_descriptions:
172-
success.append({tag: description})
173-
tags_descriptions.add((tag, description))
174-
175-
control_reasons = set()
176-
177-
for tag_data in ret['Controlled']:
178-
tag = tag_data['tag']
179-
control_reason = tag_data.get('control', '')
180-
description = tag_data.get('description')
181-
if (tag, description, control_reason) not in control_reasons:
182-
tag_dict = {'description': description,
183-
'control': control_reason}
184-
controlled.append({tag: tag_dict})
185-
control_reasons.add((tag, description, control_reason))
186-
187-
else:
188-
# Format verbose output as single-key dictionaries with tag as key
189-
for tag_data in ret['Failure']:
190-
tag = tag_data['tag']
191-
failure.append({tag: tag_data})
192-
193-
for tag_data in ret['Success']:
194-
tag = tag_data['tag']
195-
success.append({tag: tag_data})
196-
197-
for tag_data in ret['Controlled']:
198-
tag = tag_data['tag']
199-
controlled.append({tag: tag_data})
200-
201-
ret['Controlled'] = controlled
202-
ret['Success'] = success
203-
ret['Failure'] = failure
204-
205-
if not ret['Controlled']:
206-
ret.pop('Controlled')
207-
208151
return ret
209152

210153

0 commit comments

Comments
 (0)