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

Source Code for Module backend.server.handlers.xmlrpc.scap

  1  # 
  2  # Copyright (c) 2013--2016 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  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
30 - def __init__(self):
31 rhnHandler.__init__(self) 32 self.functions.append('upload_result')
33
34 - def upload_result(self, system_id, action_id, scap_file):
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
46 - def _store_file(self, action_id, scap_file):
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 # Create the file on filer 61 filecontent = decodestring(scap_file['filecontent']) 62 # TODO assert for the size of the file 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
79 - def _authorize_request(self, action_id):
80 # Make sure that database contains records in 81 # rhnServerAction and rhnActionScap 82 # and not contains records in 83 # rhnXccdfTestResult 84 # for given (system_id, action_id) pair 85 h = rhnSQL.prepare(_query_authorize_request) 86 h.execute(server_id=self.server_id, action_id=action_id) 87 exists = h.fetchone() 88 if len(exists) != 1: 89 raise rhnFault(50, _("Invalid system_id/action_id pair."))
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