1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import stat
18 from base64 import decodestring
19
20 from spacewalk.common.rhnException import rhnFault
21 from spacewalk.common.rhnConfig import CFG
22 from spacewalk.common.rhnLog import log_debug
23 from spacewalk.server import rhnSQL
24 from spacewalk.server.rhnHandler import rhnHandler
25 from spacewalk.server.rhnLib import get_action_path, get_actionfile_path
26
27
28 -class Scap(rhnHandler):
29
33
35 self.auth_system(system_id)
36 self._authorize_request(action_id)
37
38 required_keys = ['filename', 'filecontent', 'content-encoding']
39 for k in required_keys:
40 if k not in scap_file:
41 log_debug(1, self.server_id, "The scap file data is invalid or incomplete: %s" % scap_file)
42 raise rhnFault(5101, "Missing or invalid key: %s" % k)
43
44 return self._store_file(action_id, scap_file)
45
47 r_dir = get_action_path(self.server.server['org_id'], self.server_id, action_id)
48 if not r_dir:
49 log_debug(1, self.server_id, "Error composing SCAP action directory path")
50 raise rhnFault(5102)
51 r_file = get_actionfile_path(self.server.server['org_id'], self.server_id, action_id, scap_file['filename'])
52 if not r_file:
53 log_debug(1, self.server_id, "Error composing SCAP action file path")
54 raise rhnFault(5103)
55
56 if not scap_file['content-encoding'] == 'base64':
57 log_debug(1, self.server_id, "Invalid content encoding: %s" % scap_file['content-encoding'])
58 raise rhnFault(5104)
59
60
61 filecontent = decodestring(scap_file['filecontent'])
62
63
64 absolute_dir = os.path.join(CFG.MOUNT_POINT, r_dir)
65 absolute_file = os.path.join(absolute_dir, scap_file['filename'])
66
67 if not os.path.exists(absolute_dir):
68 log_debug(1, self.server_id, "Creating action directory: %s" % absolute_dir)
69 os.makedirs(absolute_dir)
70 mode = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
71 os.chmod(absolute_dir, mode)
72 os.chmod(os.path.dirname(os.path.normpath(absolute_dir)), mode)
73 log_debug(1, self.server_id, "Creating file: %s" % absolute_file)
74 f = open(absolute_file, 'w+')
75 f.write(filecontent)
76 return {'result': True,
77 }
78
90
91 _query_authorize_request = rhnSQL.Statement("""
92 select 1
93 from rhnServerAction rsa,
94 rhnActionScap ras
95 where rsa.server_id = :server_id
96 and rsa.action_id = :action_id
97 and rsa.action_id = ras.action_id
98 and rsa.status in (0, 1)
99 and not exists
100 (
101 select rxt.id
102 from rhnXccdfTestresult rxt
103 where rxt.server_id = rsa.server_id
104 and rxt.action_scap_id = ras.id
105 )
106 """)
107