1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
96
97
99 global truncated
100 if len(string) > max_len:
101 truncated = True
102 return string[:max_len - 3] + "..."
103 return string
104
105
112
113
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
129
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