| Conditions | 19 |
| Total Lines | 234 |
| Code Lines | 196 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 380 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ssg.build_profile.XCCDFBenchmark.get_profile_stats() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from __future__ import absolute_import |
||
| 94 | def get_profile_stats(self, profile): |
||
| 95 | """Obtain statistics for the profile""" |
||
| 96 | |||
| 97 | # Holds the intermediary statistics for profile |
||
| 98 | profile_stats = { |
||
| 99 | 'profile_id': "", |
||
| 100 | 'ssg_version': 0, |
||
| 101 | 'rules': [], |
||
| 102 | 'rules_count': 0, |
||
| 103 | 'implemented_ovals': [], |
||
| 104 | 'implemented_ovals_pct': 0, |
||
| 105 | 'missing_ovals': [], |
||
| 106 | 'implemented_bash_fixes': [], |
||
| 107 | 'implemented_bash_fixes_pct': 0, |
||
| 108 | 'implemented_ansible_fixes': [], |
||
| 109 | 'implemented_ansible_fixes_pct': 0, |
||
| 110 | 'implemented_ignition_fixes': [], |
||
| 111 | 'implemented_ignition_fixes_pct': 0, |
||
| 112 | 'implemented_kubernetes_fixes': [], |
||
| 113 | 'implemented_kubernetes_fixes_pct': 0, |
||
| 114 | 'implemented_puppet_fixes': [], |
||
| 115 | 'implemented_puppet_fixes_pct': 0, |
||
| 116 | 'implemented_anaconda_fixes': [], |
||
| 117 | 'implemented_anaconda_fixes_pct': 0, |
||
| 118 | 'missing_bash_fixes': [], |
||
| 119 | 'missing_ansible_fixes': [], |
||
| 120 | 'missing_ignition_fixes': [], |
||
| 121 | 'missing_kubernetes_fixes': [], |
||
| 122 | 'missing_puppet_fixes': [], |
||
| 123 | 'missing_anaconda_fixes': [], |
||
| 124 | 'assigned_cces': [], |
||
| 125 | 'assigned_cces_pct': 0, |
||
| 126 | 'missing_cces': [], |
||
| 127 | 'missing_stig_ids': [], |
||
| 128 | 'missing_cis_refs': [], |
||
| 129 | 'missing_hipaa_refs': [], |
||
| 130 | 'missing_anssi_refs': [], |
||
| 131 | 'missing_ospp_refs': [], |
||
| 132 | 'missing_cui_refs': [], |
||
| 133 | 'ansible_parity': [], |
||
| 134 | } |
||
| 135 | |||
| 136 | rule_stats = [] |
||
| 137 | ssg_version_elem = self.tree.find("./{%s}version[@update=\"%s\"]" % |
||
| 138 | (xccdf_ns, ssg_version_uri)) |
||
| 139 | |||
| 140 | rules = [] |
||
| 141 | |||
| 142 | if profile == "all": |
||
| 143 | # "all" is a virtual profile that selects all rules |
||
| 144 | rules = self.indexed_rules.values() |
||
| 145 | else: |
||
| 146 | xccdf_profile = self.tree.find("./{%s}Profile[@id=\"%s\"]" % |
||
| 147 | (xccdf_ns, profile)) |
||
| 148 | if xccdf_profile is None: |
||
| 149 | print("No such profile \"%s\" found in the benchmark!" |
||
| 150 | % profile) |
||
| 151 | print("* Available profiles:") |
||
| 152 | profiles_avail = self.tree.findall("./{%s}Profile" % (xccdf_ns)) |
||
| 153 | for _profile in profiles_avail: |
||
| 154 | print("** %s" % _profile.get('id')) |
||
| 155 | sys.exit(1) |
||
| 156 | |||
| 157 | # This will only work with SSG where the (default) profile has zero |
||
| 158 | # selected rule. If you want to reuse this for custom content, you |
||
| 159 | # need to change this to look into Rule/@selected |
||
| 160 | selects = xccdf_profile.findall("./{%s}select[@selected=\"true\"]" % |
||
| 161 | xccdf_ns) |
||
| 162 | |||
| 163 | for select in selects: |
||
| 164 | rule_id = select.get('idref') |
||
| 165 | xccdf_rule = self.indexed_rules.get(rule_id) |
||
| 166 | if xccdf_rule is not None: |
||
| 167 | # it could also be a Group |
||
| 168 | rules.append(xccdf_rule) |
||
| 169 | |||
| 170 | for rule in rules: |
||
| 171 | if rule is not None: |
||
| 172 | oval = rule.find("./{%s}check[@system=\"%s\"]" % |
||
| 173 | (xccdf_ns, oval_ns)) |
||
| 174 | bash_fix = rule.find("./{%s}fix[@system=\"%s\"]" % |
||
| 175 | (xccdf_ns, bash_rem_system)) |
||
| 176 | ansible_fix = rule.find("./{%s}fix[@system=\"%s\"]" % |
||
| 177 | (xccdf_ns, ansible_rem_system)) |
||
| 178 | ignition_fix = rule.find("./{%s}fix[@system=\"%s\"]" % |
||
| 179 | (xccdf_ns, ignition_rem_system)) |
||
| 180 | kubernetes_fix = rule.find("./{%s}fix[@system=\"%s\"]" % |
||
| 181 | (xccdf_ns, kubernetes_rem_system)) |
||
| 182 | puppet_fix = rule.find("./{%s}fix[@system=\"%s\"]" % |
||
| 183 | (xccdf_ns, puppet_rem_system)) |
||
| 184 | anaconda_fix = rule.find("./{%s}fix[@system=\"%s\"]" % |
||
| 185 | (xccdf_ns, anaconda_rem_system)) |
||
| 186 | cce = rule.find("./{%s}ident[@system=\"%s\"]" % |
||
| 187 | (xccdf_ns, cce_uri)) |
||
| 188 | stig_id = rule.find("./{%s}reference[@href=\"%s\"]" % |
||
| 189 | (xccdf_ns, stig_ns)) |
||
| 190 | cis_ref = rule.find("./{%s}reference[@href=\"%s\"]" % |
||
| 191 | (xccdf_ns, cis_ns)) |
||
| 192 | hipaa_ref = rule.find("./{%s}reference[@href=\"%s\"]" % |
||
| 193 | (xccdf_ns, hipaa_ns)) |
||
| 194 | anssi_ref = rule.find("./{%s}reference[@href=\"%s\"]" % |
||
| 195 | (xccdf_ns, anssi_ns)) |
||
| 196 | ospp_ref = rule.find("./{%s}reference[@href=\"%s\"]" % |
||
| 197 | (xccdf_ns, ospp_ns)) |
||
| 198 | cui_ref = rule.find("./{%s}reference[@href=\"%s\"]" % |
||
| 199 | (xccdf_ns, cui_ns)) |
||
| 200 | |||
| 201 | rule_stats.append( |
||
| 202 | RuleStats(rule.get("id"), oval, |
||
| 203 | bash_fix, ansible_fix, ignition_fix, |
||
| 204 | kubernetes_fix, puppet_fix, anaconda_fix, |
||
| 205 | cce, stig_id, cis_ref, hipaa_ref, anssi_ref, |
||
| 206 | ospp_ref, cui_ref) |
||
| 207 | ) |
||
| 208 | |||
| 209 | if not rule_stats: |
||
| 210 | print('Unable to retrieve statistics for %s profile' % profile) |
||
| 211 | sys.exit(1) |
||
| 212 | |||
| 213 | rule_stats.sort(key=lambda r: r.dict['id']) |
||
| 214 | |||
| 215 | for rule in rule_stats: |
||
| 216 | profile_stats['rules'].append(rule.dict['id']) |
||
| 217 | |||
| 218 | profile_stats['profile_id'] = profile |
||
| 219 | if ssg_version_elem is not None: |
||
| 220 | profile_stats['ssg_version'] = \ |
||
| 221 | 'SCAP Security Guide %s' % ssg_version_elem.text |
||
| 222 | profile_stats['rules_count'] = len(rule_stats) |
||
| 223 | profile_stats['implemented_ovals'] = \ |
||
| 224 | [x.dict['id'] for x in rule_stats if x.dict['oval'] is not None] |
||
| 225 | profile_stats['implemented_ovals_pct'] = \ |
||
| 226 | float(len(profile_stats['implemented_ovals'])) / \ |
||
| 227 | profile_stats['rules_count'] * 100 |
||
| 228 | profile_stats['missing_ovals'] = \ |
||
| 229 | [x.dict['id'] for x in rule_stats if x.dict['oval'] is None] |
||
| 230 | |||
| 231 | profile_stats['implemented_bash_fixes'] = \ |
||
| 232 | [x.dict['id'] for x in rule_stats if x.dict['bash_fix'] is not None] |
||
| 233 | profile_stats['implemented_bash_fixes_pct'] = \ |
||
| 234 | float(len(profile_stats['implemented_bash_fixes'])) / \ |
||
| 235 | profile_stats['rules_count'] * 100 |
||
| 236 | profile_stats['missing_bash_fixes'] = \ |
||
| 237 | [x.dict['id'] for x in rule_stats if x.dict['bash_fix'] is None] |
||
| 238 | |||
| 239 | profile_stats['implemented_ansible_fixes'] = \ |
||
| 240 | [x.dict['id'] for x in rule_stats if x.dict['ansible_fix'] is not None] |
||
| 241 | profile_stats['implemented_ansible_fixes_pct'] = \ |
||
| 242 | float(len(profile_stats['implemented_ansible_fixes'])) / \ |
||
| 243 | profile_stats['rules_count'] * 100 |
||
| 244 | profile_stats['missing_ansible_fixes'] = \ |
||
| 245 | [x.dict['id'] for x in rule_stats if x.dict['ansible_fix'] is None] |
||
| 246 | |||
| 247 | profile_stats['implemented_ignition_fixes'] = \ |
||
| 248 | [x.dict['id'] for x in rule_stats if x.dict['ignition_fix'] is not None] |
||
| 249 | profile_stats['implemented_ignition_fixes_pct'] = \ |
||
| 250 | float(len(profile_stats['implemented_ignition_fixes'])) / \ |
||
| 251 | profile_stats['rules_count'] * 100 |
||
| 252 | profile_stats['missing_ignition_fixes'] = \ |
||
| 253 | [x.dict['id'] for x in rule_stats if x.dict['ignition_fix'] is None] |
||
| 254 | |||
| 255 | profile_stats['implemented_kubernetes_fixes'] = \ |
||
| 256 | [x.dict['id'] for x in rule_stats if x.dict['kubernetes_fix'] is not None] |
||
| 257 | profile_stats['implemented_kubernetes_fixes_pct'] = \ |
||
| 258 | float(len(profile_stats['implemented_kubernetes_fixes'])) / \ |
||
| 259 | profile_stats['rules_count'] * 100 |
||
| 260 | profile_stats['missing_kubernetes_fixes'] = \ |
||
| 261 | [x.dict['id'] for x in rule_stats if x.dict['kubernetes_fix'] is None] |
||
| 262 | |||
| 263 | profile_stats['implemented_puppet_fixes'] = \ |
||
| 264 | [x.dict['id'] for x in rule_stats if x.dict['puppet_fix'] is not None] |
||
| 265 | profile_stats['implemented_puppet_fixes_pct'] = \ |
||
| 266 | float(len(profile_stats['implemented_puppet_fixes'])) / \ |
||
| 267 | profile_stats['rules_count'] * 100 |
||
| 268 | profile_stats['missing_puppet_fixes'] = \ |
||
| 269 | [x.dict['id'] for x in rule_stats if x.dict['puppet_fix'] is None] |
||
| 270 | |||
| 271 | profile_stats['implemented_anaconda_fixes'] = \ |
||
| 272 | [x.dict['id'] for x in rule_stats if x.dict['anaconda_fix'] is not None] |
||
| 273 | |||
| 274 | profile_stats['missing_stig_ids'] = [] |
||
| 275 | if 'stig' in profile_stats['profile_id']: |
||
| 276 | profile_stats['missing_stig_ids'] = \ |
||
| 277 | [x.dict['id'] for x in rule_stats if x.dict['stig_id'] is None] |
||
| 278 | |||
| 279 | profile_stats['missing_cis_refs'] = [] |
||
| 280 | if 'cis' in profile_stats['profile_id']: |
||
| 281 | profile_stats['missing_cis_refs'] = \ |
||
| 282 | [x.dict['id'] for x in rule_stats if x.dict['cis_ref'] is None] |
||
| 283 | |||
| 284 | profile_stats['missing_hipaa_refs'] = [] |
||
| 285 | if 'hipaa' in profile_stats['profile_id']: |
||
| 286 | profile_stats['missing_hipaa_refs'] = \ |
||
| 287 | [x.dict['id'] for x in rule_stats if x.dict['hipaa_ref'] is None] |
||
| 288 | |||
| 289 | profile_stats['missing_anssi_refs'] = [] |
||
| 290 | if 'anssi' in profile_stats['profile_id']: |
||
| 291 | profile_stats['missing_anssi_refs'] = \ |
||
| 292 | [x.dict['id'] for x in rule_stats if x.dict['anssi_ref'] is None] |
||
| 293 | |||
| 294 | profile_stats['missing_ospp_refs'] = [] |
||
| 295 | if 'ospp' in profile_stats['profile_id']: |
||
| 296 | profile_stats['missing_ospp_refs'] = \ |
||
| 297 | [x.dict['id'] for x in rule_stats if x.dict['ospp_ref'] is None] |
||
| 298 | |||
| 299 | profile_stats['missing_cui_refs'] = [] |
||
| 300 | if 'cui' in profile_stats['profile_id']: |
||
| 301 | profile_stats['missing_cui_refs'] = \ |
||
| 302 | [x.dict['id'] for x in rule_stats if x.dict['cui_ref'] is None] |
||
| 303 | |||
| 304 | profile_stats['implemented_anaconda_fixes_pct'] = \ |
||
| 305 | float(len(profile_stats['implemented_anaconda_fixes'])) / \ |
||
| 306 | profile_stats['rules_count'] * 100 |
||
| 307 | profile_stats['missing_anaconda_fixes'] = \ |
||
| 308 | [x.dict['id'] for x in rule_stats if x.dict['anaconda_fix'] is None] |
||
| 309 | |||
| 310 | profile_stats['assigned_cces'] = \ |
||
| 311 | [x.dict['id'] for x in rule_stats if x.dict['cce'] is not None] |
||
| 312 | profile_stats['assigned_cces_pct'] = \ |
||
| 313 | float(len(profile_stats['assigned_cces'])) / \ |
||
| 314 | profile_stats['rules_count'] * 100 |
||
| 315 | profile_stats['missing_cces'] = \ |
||
| 316 | [x.dict['id'] for x in rule_stats if x.dict['cce'] is None] |
||
| 317 | |||
| 318 | profile_stats['ansible_parity'] = \ |
||
| 319 | [rule_id for rule_id in profile_stats["missing_ansible_fixes"] if rule_id not in profile_stats["missing_bash_fixes"]] |
||
| 320 | profile_stats['ansible_parity_pct'] = 0 |
||
| 321 | if len(profile_stats['implemented_bash_fixes']): |
||
| 322 | profile_stats['ansible_parity_pct'] = \ |
||
| 323 | float(len(profile_stats['implemented_bash_fixes']) - |
||
| 324 | len(profile_stats['ansible_parity'])) / \ |
||
| 325 | len(profile_stats['implemented_bash_fixes']) * 100 |
||
| 326 | |||
| 327 | return profile_stats |
||
| 328 | |||
| 672 |