Package backend :: Package server :: Package action_extra_data :: Module scap
[hide private]
[frames] | no frames]

Source Code for Module backend.server.action_extra_data.scap

  1  # 
  2  # Copyright (c) 2012--2020 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16   
 17  import xml.dom.minidom 
 18  from base64 import decodestring 
 19  from spacewalk.common.rhnLog import log_debug, log_error 
 20  from spacewalk.server import rhnSQL 
 21   
 22  __rhnexport__ = ['xccdf_eval'] 
 23   
 24   
25 -def xccdf_eval(server_id, action_id, data={}):
26 log_debug(3) 27 h = rhnSQL.prepare(_query_clear_tresult) 28 h.execute(server_id=server_id, action_id=action_id) 29 if not data: 30 log_debug(4, "No data sent by client") 31 return 32 33 for item in ('resume', 'errors'): 34 data[item] = decodestring(data[item]) 35 36 resume = xml.dom.minidom.parseString(data['resume']) 37 benchmark = resume.getElementsByTagName('benchmark-resume')[0] 38 profiles = benchmark.getElementsByTagName('profile') or [_dummyDefaultProfile()] 39 testresults = benchmark.getElementsByTagName('TestResult') 40 if len(profiles) < 1 or len(testresults) < 1: 41 log_error('Scap report misses profile or testresult element') 42 return 43 if len(profiles) != 1 or len(testresults) != 1: 44 log_error('Scap report containst multiple results', 45 len(profiles), len(testresults)) 46 _process_testresult(testresults[0], server_id, action_id, benchmark, 47 profiles[0], data['errors'])
48 49
50 -def _process_testresult(tr, server_id, action_id, benchmark, profile, errors):
51 start_time = None 52 if tr.hasAttribute('start-time'): 53 start_time = tr.getAttribute('start-time') 54 55 h = rhnSQL.prepare(_query_insert_tresult, blob_map={'errors': 'errors'}) 56 h.execute(server_id=server_id, 57 action_id=action_id, 58 bench_id=_truncate(benchmark.getAttribute('id'), 120), 59 bench_version=_truncate(benchmark.getAttribute('version'), 80), 60 profile_id=profile.getAttribute('id'), 61 profile_title=_truncate(profile.getAttribute('title'), 120), 62 identifier=_truncate(tr.getAttribute('id'), 120), 63 start_time=start_time.replace('T', ' '), 64 end_time=tr.getAttribute('end-time').replace('T', ' '), 65 errors=errors 66 ) 67 h = rhnSQL.prepare(_query_get_tresult) 68 h.execute(server_id=server_id, action_id=action_id) 69 testresult_id = h.fetchone()[0] 70 if not _process_ruleresults(testresult_id, tr): 71 h = rhnSQL.prepare(_query_update_errors, blob_map={'errors': 'errors'}) 72 h.execute(testresult_id=testresult_id, 73 errors=errors + 74 '\nSome text strings were truncated when saving to the database.')
75 76 truncated = False 77 78
79 -def _process_ruleresults(testresult_id, tr):
80 global truncated 81 truncated = False 82 inserts = {'rr_id': [], 'system': [], 'ident': []} 83 for result in tr.childNodes: 84 for rr in result.childNodes: 85 rr_id = _create_rresult(testresult_id, result.nodeName) 86 87 inserts['rr_id'].append(rr_id) 88 inserts['system'].append('#IDREF#') 89 inserts['ident'].append(_truncate(rr.getAttribute('id'), 255)) 90 for ident in rr.childNodes: 91 inserts['rr_id'].append(rr_id) 92 inserts['system'].append(_truncate(ident.getAttribute('system'), 80)) 93 inserts['ident'].append(_truncate(_get_text(ident), 255)) 94 _store_idents(inserts) 95 return not truncated
96 97
98 -def _truncate(string, max_len):
99 global truncated 100 if len(string) > max_len: 101 truncated = True 102 return string[:max_len - 3] + "..." 103 return string
104 105
106 -def _create_rresult(testresult_id, result_label):
107 rr_id = rhnSQL.Sequence("rhn_xccdf_rresult_id_seq")() 108 h = rhnSQL.prepare(_query_insert_rresult) 109 h.execute(rr_id=rr_id, testresult_id=testresult_id, 110 result_label=result_label) 111 return rr_id
112 113
114 -def _store_idents(data):
115 h = rhnSQL.prepare(_query_insert_identmap) 116 rowcount = h.execute_bulk(data) 117 log_debug(5, "Inserted xccdf_ruleresults rows:", rowcount)
118 119
120 -def _get_text(node):
121 rc = [] 122 for node in node.childNodes: 123 if node.nodeType == node.TEXT_NODE: 124 rc.append(node.data) 125 return ''.join(rc)
126 127
128 -class _dummyDefaultProfile:
129
130 - def getAttribute(self, name):
131 if name == 'id': 132 return 'None' 133 elif name == 'title': 134 return 'No profile selected. Using defaults.' 135 return ''
136 137 _query_clear_tresult = rhnSQL.Statement(""" 138 delete from rhnXccdfTestresult 139 where server_id = :server_id 140 and action_scap_id = ( 141 select id from rhnActionScap 142 where action_id = :action_id) 143 """) 144 145 _query_insert_tresult = rhnSQL.Statement(""" 146 insert into rhnXccdfTestresult( 147 id, 148 server_id, 149 action_scap_id, 150 benchmark_id, 151 profile_id, 152 identifier, 153 start_time, 154 end_time, 155 errors) 156 values ( 157 sequence_nextval('rhn_xccdf_tresult_id_seq'), 158 :server_id, 159 (select ras.id 160 from rhnActionScap ras 161 where ras.action_id = :action_id), 162 lookup_xccdf_benchmark(:bench_id, :bench_version), 163 lookup_xccdf_profile(:profile_id, :profile_title), 164 :identifier, 165 TO_TIMESTAMP(:start_time, 'YYYY-MM-DD HH24:MI:SS'), 166 TO_TIMESTAMP(:end_time, 'YYYY-MM-DD HH24:MI:SS'), 167 :errors 168 ) 169 """) 170 171 _query_get_tresult = rhnSQL.Statement(""" 172 select id from rhnXccdfTestresult 173 where server_id = :server_id 174 and action_scap_id = ( 175 select ras.id 176 from rhnActionScap ras 177 where ras.action_id = :action_id 178 ) 179 """) 180 181 _query_insert_rresult = """ 182 insert into rhnXccdfRuleresult (id, testresult_id, result_id) 183 values ( 184 :rr_id, 185 :testresult_id, 186 (select rt.id 187 from rhnXccdfRuleresultType rt 188 where rt.label = :result_label) 189 ) 190 """ 191 192 _query_insert_identmap = rhnSQL.Statement(""" 193 insert into rhnXccdfRuleIdentMap (rresult_id, ident_id) 194 values ( 195 :rr_id, 196 lookup_xccdf_ident(:system, :ident) 197 ) 198 """) 199 200 _query_update_errors = rhnSQL.Statement(""" 201 update rhnXccdfTestresult 202 set errors = :errors 203 where id = :testresult_id 204 """) 205