def validate(func, self, *args, **kwargs): ''' function that calls authrozing function''' isAuthEnabled = True isPkiEnabled = False authPassed = False try: appGlobal = config['pylons.app_globals'] isAuthEnabled = configutil.getConfigAsBool('basicauth.local') isPkiEnabled = (appGlobal.encryptedtokens and configutil.getConfigAsBool('pkiauth_enabled')) except BaseException as excep: LOG.error('Error loading auth config %s - %s' % (str(excep), traceback.format_exc(2))) if isAuthEnabled: if 'Authorization' not in request.headers and 'authorization' not in request.headers: return invalidAuthHandler('Authorization header missing', {}) message = None result = {} # base authentication if not isPkiEnabled: token = ('%s:%s' % (configutil.getConfig('username.local'), configutil.getConfig('password.local'))) try: isAuthenticated(token) authPassed = True except UnauthorizedException: message = 'Please provide valid username and password' result['scheme'] = 'base' if not authPassed: # pki authentication token = appGlobal.authztoken try: isAuthenticated(token) authPassed = True except UnauthorizedException: if isPkiEnabled: result['scheme'] = 'pki' user = request.headers[ 'AuthorizationUser'] if 'AuthorizationUser' in request.headers else 'agent' pubKey = '%s.cert' % user if pubKey in appGlobal.encryptedtokens: message = appGlobal.encryptedtokens[pubKey] result['key'] = appGlobal.encryptedtokens[pubKey] else: message = 'Unknown AuthroizationUser %s' % user return invalidAuthHandler(message, result) return func(self, *args, **kwargs)
def validate(func, self, *args, **kwargs): ''' function that calls authrozing function''' isAuthEnabled = True isPkiEnabled = False authPassed = False try: appGlobal = config['pylons.app_globals'] isAuthEnabled = configutil.getConfigAsBool('basicauth.local') isPkiEnabled = (appGlobal.encryptedtokens and configutil.getConfigAsBool('pkiauth_enabled')) except BaseException as excep: LOG.error('Error loading auth config %s - %s' % (str(excep), traceback.format_exc(2))) if isAuthEnabled: if 'Authorization' not in request.headers and 'authorization' not in request.headers: return invalidAuthHandler('Authorization header missing', {}) message = None result = {} # base authentication if not isPkiEnabled: token = ('%s:%s' % (configutil.getConfig('username.local'), configutil.getConfig('password.local'))) try: isAuthenticated(token) authPassed = True except UnauthorizedException: message = 'Please provide valid username and password' result['scheme'] = 'base' if not authPassed: # pki authentication token = appGlobal.authztoken try: isAuthenticated(token) authPassed = True except UnauthorizedException: if isPkiEnabled: result['scheme'] = 'pki' user = request.headers['AuthorizationUser'] if 'AuthorizationUser' in request.headers else 'agent' pubKey = '%s.cert' % user if pubKey in appGlobal.encryptedtokens: message = appGlobal.encryptedtokens[pubKey] result['key'] = appGlobal.encryptedtokens[pubKey] else: message = 'Unknown AuthroizationUser %s' % user return invalidAuthHandler(message, result) return func(self, *args, **kwargs)
def post(self, service, manifest): """ Create a new service object """ from agent.lib.agent_thread.manifest_create import ManifestCreate try: LOG.info('Post for service (%s) and manifest (%s) with body: %s', service, manifest, request.body) # check to see if the manifest already exists path = ManifestController.manifestPath(service, manifest) if (os.path.isdir(path)): return doneResult(request, response, httpStatus = 201, controller = self) # parse the body if (request.body == ""): return errorResult(request, response, Errors.MANIFEST_PACKAGE_PARSING_ERROR, 'No body found in post command', controller = self) body = json.loads(request.body) packages = body['package'] forcedPackages = body['forcePackageName'] if 'forcePackageName' in body else None skipProp = asbool(body['skipProp']) if 'skipProp' in body else configutil.getConfigAsBool('download_skip_prop') LOG.debug('pkgs = %s, %s', packages, forcedPackages) # parse the package list for idx, package in enumerate(packages): # to support reuse of an package from an existing manifest (active if possible) # without sending the complete package location in request body if package.startswith('/'): packageRef = package tokens = package.split('/') pkgnamePrefix = tokens[-1].rstrip() fullPkgLoc = manifestutil.getPackageByName(service, manifest = None, pkgnamePrefix = pkgnamePrefix) if fullPkgLoc is None: return errorResult(request, response, Errors.MANIFEST_PACKAGE_DOES_NOT_EXIST, 'manifest (%s/%s) package (%s) does not exist' % (service, manifest, packages), controller = self) else: LOG.info('expanding package reuse ref %s with full package location %s' % (packageRef, fullPkgLoc)) packages[idx] = fullPkgLoc appGlobal = config['pylons.app_globals'] # start a thread to create the package manThread = ManifestCreate(appGlobal.threadMgr, service, manifest, packages, forcePackages = forcedPackages, skipProp = skipProp) self.injectJobCtx(manThread) manThread.start() manThread.threadMgrEvent.wait() return statusResult(request, response, manThread, controller = self) except AgentException as excep: return errorResult(request, response, error = excep.getCode(), errorMsg = excep.getMsg(), controller = self) except Exception as excep: return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)), controller = self)
def startdownload(self): """ Download using http. The protocol is chosen based on package uri. Target folder is packageloc relative to repo_root. """ try: utils.checkDiskFull() reqjson = json.loads(request.body) package = str(reqjson['package']) packageloc = str(reqjson['packageloc']) skipProp = asbool(reqjson['skipProp']) if 'skipProp' in reqjson else configutil.getConfigAsBool('download_skip_prop') LOG.info('Request received for StartDownload %s' % packageloc) appGlobal = config['pylons.app_globals'] downloadThread = None cat = 'DIST_SD' + packageloc if not downloadThread: LOG.info('Starting a new StartDownload Thread %s' % packageloc) downloadThread = DownloadThread(appGlobal.threadMgr, package, packageloc, category = [cat], skipProp = skipProp) downloadThread.setMergeOnFound(True) self.injectJobCtx(downloadThread) downloadThread.start() downloadThread.threadMgrEvent.wait() return statusResult(request, response, downloadThread, controller = self) except AgentException as excep: return errorResult(request, response, error = excep.getCode(), errorMsg = excep.getMsg(), controller = self) except Exception as excp: errorMsg = 'Exception downloading %s - traceback %s' % (str(excp), traceback.format_exc(2)) return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = errorMsg, controller = self)
def deployservice(self, service): """ activate manifest, if already active then skip """ LOG.info('deploy service for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] # parse the body if (request.body == ""): return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller=self) requestjson = json.loads(request.body) manifest = requestjson['manifest'] packages = requestjson['package'] skipProp = asbool( requestjson['skipProp'] ) if 'skipProp' in requestjson else configutil.getConfigAsBool( 'download_skip_prop') skipActivation = asbool( requestjson['skipActivation'] ) if 'skipActivation' in requestjson else False # activate manifest if not already activated if manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) deployServiceThread = DeployService(appGlobal.threadMgr, service, manifest, packages, skipProp=skipProp, skipActivation=skipActivation) self.injectJobCtx(deployServiceThread) deployServiceThread.start() deployServiceThread.threadMgrEvent.wait() return statusResult(request, response, deployServiceThread, controller=self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % ( service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error=Errors.UNKNOWN_ERROR, errorMsg=msg, controller=self)
def __init__(self, threadMgr, packageUri, packageloc, path = None, category = None, parentId = None): AgentThread.__init__(self, threadMgr, cat = category, name = 'download_thread', parentId = parentId) self._mergeOnFound = True self.__path = pylons.config['repo_root'] if path is not None: self.__path = path # check to see if the package path exists if (not os.path.isdir(self.__path)): msg = 'Package path(%s) does not exist' % self.__path LOG.error(msg) raise AgentException(Errors.PACKAGE_PATH_ERROR, msg) self.__uriDict = PackageUtil.parseUri(packageUri, self.__path, packageloc) self.__prop = {} self.__error = None self.__progress = 0.0 self.__timeouts = None self.__skipProp = configutil.getConfigAsBool('download_skip_prop')
def _skipCleanupOnFailure(self): """ skip cleanup on app lifecycle failure """ return configutil.getConfigAsBool('skip_cleanup_on_failure')
def runReport(self, service, monitorname, cmd, metricoptions): ''' 20130326 Change to public Runs reporting to send data. Should not fail or scheduler will fail. metricoptions = {resolution, additionaltags_dict, datatype_dict, metadata_ctx_dict)} ''' monitorKey = (service, monitorname) metricResSec = str(metricoptions[0]) metricTags = metricoptions[1] metricTypes = metricoptions[2] metricCtx = metricoptions[3] try: # if no need to send it out, just skip and return isPublishing = configutil.getConfigAsBool('agent_monitor_publish_enabled') if isPublishing: self._connIfNeeded(metricCtx) #report monitor status for new values tags = [] try: LOG.debug("self.__monitorMessages %s" % self.__monitorMessages) # resolution tags.append(('m:resSecs', metricResSec)) # additional tags for tkey, tvalue in metricTags.iteritems(): tag = 't:%s' % tkey tags.append((tag, tvalue)) monitorMsg = None if monitorKey in self.__monitorMessages and cmd in self.__monitorMessages[monitorKey]: messages = copy.copy(self.__monitorMessages[monitorKey][cmd]) del self.__monitorMessages[monitorKey][cmd][:] if not isPublishing: return for message in messages: if monitorMsg is not None: self._publishMonitorMessage(monitorMsg) monitorMsg = self._createMonitorMessage(metricoptions, message) # pre-processing tags from user script overridingTags = set() for (key, value) in message.items(): if key.startswith('t:'): overridingTags.add(key) for tkey, tvalue in tags: if tkey not in overridingTags: self._addTagToMessage(monitorMsg, (tkey, tvalue)) for (key, msgvalue) in message.items(): # there is possibility that we don't see new/updated data, in that case, # prohibit hb creation so as to not corrupt internal transaction stack value, timestamp, preTimestamp = msgvalue[0], msgvalue[1], msgvalue[2] if timestamp != preTimestamp: msgvalue[2] = timestamp if not key.startswith('t:'): # add metric types, default g: newKey = '%s:%s' % (metricTypes[key] if (key in metricTypes) else 'g', key) self._addValueToMessage(monitorMsg, (newKey, value)) if monitorMsg is not None: self._publishMonitorMessage(monitorMsg) except Exception as excep: LOG.error('error when update monitor for %s status (%s) - %s' % (service, excep, traceback.format_exc(5))) except OSError: pass # ignore this type of error. it's too verbose except Exception as excep: LOG.error('Cannot send monitor heartbeat (%s) - %s' % (excep, traceback.format_exc(5)))
def post(self, service, manifest): """ Create a new service object """ from agent.lib.agent_thread.manifest_create import ManifestCreate try: LOG.info('Post for service (%s) and manifest (%s) with body: %s', service, manifest, request.body) # check to see if the manifest already exists path = ManifestController.manifestPath(service, manifest) if (os.path.isdir(path)): return doneResult(request, response, httpStatus=201, controller=self) # parse the body if (request.body == ""): return errorResult(request, response, Errors.MANIFEST_PACKAGE_PARSING_ERROR, 'No body found in post command', controller=self) body = json.loads(request.body) packages = body['package'] forcedPackages = body[ 'forcePackageName'] if 'forcePackageName' in body else None skipProp = asbool( body['skipProp'] ) if 'skipProp' in body else configutil.getConfigAsBool( 'download_skip_prop') LOG.debug('pkgs = %s, %s', packages, forcedPackages) # parse the package list for idx, package in enumerate(packages): # to support reuse of an package from an existing manifest (active if possible) # without sending the complete package location in request body if package.startswith('/'): packageRef = package tokens = package.split('/') pkgnamePrefix = tokens[-1].rstrip() fullPkgLoc = manifestutil.getPackageByName( service, manifest=None, pkgnamePrefix=pkgnamePrefix) if fullPkgLoc is None: return errorResult( request, response, Errors.MANIFEST_PACKAGE_DOES_NOT_EXIST, 'manifest (%s/%s) package (%s) does not exist' % (service, manifest, packages), controller=self) else: LOG.info( 'expanding package reuse ref %s with full package location %s' % (packageRef, fullPkgLoc)) packages[idx] = fullPkgLoc appGlobal = config['pylons.app_globals'] # start a thread to create the package manThread = ManifestCreate(appGlobal.threadMgr, service, manifest, packages, forcePackages=forcedPackages, skipProp=skipProp) self.injectJobCtx(manThread) manThread.start() manThread.threadMgrEvent.wait() return statusResult(request, response, manThread, controller=self) except AgentException as excep: return errorResult(request, response, error=excep.getCode(), errorMsg=excep.getMsg(), controller=self) except Exception as excep: return errorResult( request, response, error=Errors.UNKNOWN_ERROR, errorMsg='Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)), controller=self)
def deployservice(self, service): """ activate manifest, if already active then skip """ LOG.info('deploy service for service(%s) with body: %s', service, request.body) try: appGlobal = config['pylons.app_globals'] # parse the body if (request.body == ""): return errorResult(request, response, Errors.INVALID_REQUEST, 'No body found in post command', controller = self) requestjson = json.loads(request.body) manifest = requestjson['manifest'] packages = requestjson['package'] skipProp = asbool(requestjson['skipProp']) if 'skipProp' in requestjson else configutil.getConfigAsBool('download_skip_prop') skipActivation = asbool(requestjson['skipActivation']) if 'skipActivation' in requestjson else False # activate manifest if not already activated if manifestutil.getActiveManifest(service) == manifest: return doneResult(request, response, controller=self) deployServiceThread = DeployService(appGlobal.threadMgr, service, manifest, packages, skipProp = skipProp, skipActivation = skipActivation) self.injectJobCtx(deployServiceThread) deployServiceThread.start() deployServiceThread.threadMgrEvent.wait() return statusResult(request, response, deployServiceThread, controller = self) except Exception as excep: msg = 'Unknown error for activateManifest(%s/%s) - %s - %s' % (service, manifest, str(excep), traceback.format_exc(2)) return errorResult(request, response, error = Errors.UNKNOWN_ERROR, errorMsg = msg, controller = self)