def growL1Cache(self, sp, cacheL1, cacheL2, assigned): ''' This method tries to enlarge L1 cache. If for some reason the number of deployed services (Counting all, ACTIVE and PREPARING, assigned, L1 and L2) is over max allowed service deployments, this method will not grow the L1 cache ''' logger.debug("Growing L1 cache creating a new service for {0}".format(sp)) # First, we try to assign from L2 cache if cacheL2 > 0: valid = None with transaction.atomic(): for n in sp.cachedUserServices().select_for_update().filter(UserServiceManager.getCacheStateFilter(services.UserDeployment.L2_CACHE)).order_by('creation_date'): if n.needsOsManager(): if State.isUsable(n.state) is False or State.isUsable(n.os_state): valid = n break else: valid = n break if valid is not None: valid.moveToLevel(services.UserDeployment.L1_CACHE) return try: UserServiceManager.manager().createCacheFor(sp.activePublication(), services.UserDeployment.L1_CACHE) except MaxServicesReachedException as e: log.doLog(sp, log.ERROR, 'Max number of services reached for this service', log.INTERNAL) logger.error(str(e)) except: logger.exception('Exception')
def cancel(self): """ Asks the UserServiceManager to cancel the current operation of this user deployed service. """ from uds.core.managers.UserServiceManager import UserServiceManager UserServiceManager.manager().cancel(self)
def process(self, userService, msg, data, options): ''' We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class), old method * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class), new method * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used ''' logger.info( "Invoked LinuxOsManager for {0} with params: {1},{2}".format( userService, msg, data)) # We get from storage the name for this userService. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = userService.os_state # Old "info" state, will be removed in a near future if msg == "info": ret = self.infoVal(userService) state = State.PREPARING elif msg == "information": ret = self.infoValue(userService) state = State.PREPARING elif msg == "log": self.doLog(userService, data, log.ACTOR) elif msg == "login": self.loggedIn(userService, data, False) ip, hostname = userService.getConnectionSource() deadLine = userService.deployed_service.getDeadline() ret = "{0}\t{1}\t{2}".format(ip, hostname, 0 if deadLine is None else deadLine) elif msg == "logout": self.loggedOut(userService, data, False) if userService.in_use == False and self._onLogout == 'remove': doRemove = True elif msg == "ip": # This ocurss on main loop inside machine, so userService is usable state = State.USABLE self.notifyIp(userService.unique_id, userService, data) elif msg == "ready": self.toReady(userService) state = State.USABLE notifyReady = True self.notifyIp(userService.unique_id, userService, data) userService.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: userService.release() else: if notifyReady is False: userService.save() else: UserServiceManager.manager().notifyReadyFromOsManager( userService, '') logger.debug('Returning {0}'.format(ret)) return ret
def process(self, userService, msg, data, options=None): """ We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class), old method * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class), new method * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used """ logger.info("Invoked LinuxOsManager for {0} with params: {1},{2}".format(userService, msg, data)) # We get from storage the name for this userService. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = userService.os_state if msg in ('ready', 'ip'): if not isinstance(data, dict): # Old actors, previous to 2.5, convert it information.. data = { 'ips': [v.split('=') for v in data.split(',')], 'hostname': userService.friendly_name } # Old "info" state, will be removed in a near future if msg == "info": ret = self.infoVal(userService) state = State.PREPARING elif msg == "information": ret = self.infoValue(userService) state = State.PREPARING elif msg == "log": self.doLog(userService, data, log.ACTOR) elif msg == "login": self.loggedIn(userService, data) ip, hostname = userService.getConnectionSource() deadLine = userService.deployed_service.getDeadline() ret = "{0}\t{1}\t{2}".format(ip, hostname, 0 if deadLine is None else deadLine) elif msg == "logout": self.loggedOut(userService, data) doRemove = self.isRemovableOnLogout(userService) elif msg == "ip": # This ocurss on main loop inside machine, so userService is usable state = State.USABLE self.notifyIp(userService.unique_id, userService, data) elif msg == "ready": self.toReady(userService) state = State.USABLE notifyReady = True self.notifyIp(userService.unique_id, userService, data) userService.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: userService.release() else: if notifyReady: UserServiceManager.manager().notifyReadyFromOsManager(userService, '') logger.debug('Returning {0}'.format(ret)) return ret
def process(self, service, msg, data, options): ''' We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (old method) * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (new method) * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used ''' logger.info("Invoked WindowsOsManager for {0} with params: {1},{2}".format(service, msg, data)) # We get from storage the name for this service. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = service.os_state if msg == "info": ret = self.infoVal(service) state = State.PREPARING elif msg == "information": ret = self.infoValue(service) state = State.PREPARING elif msg == "log": self.doLog(service, data, log.ACTOR) elif msg == "logon" or msg == 'login': if '\\' not in data: self.loggedIn(service, data, False) service.setInUse(True) # We get the service logged hostname & ip and returns this ip, hostname = service.getConnectionSource() ret = "{0}\t{1}".format(ip, hostname) elif msg == "logoff" or msg == 'logout': self.loggedOut(service, data, False) if self._onLogout == 'remove': doRemove = True elif msg == "ip": # This ocurss on main loop inside machine, so service is usable state = State.USABLE self.notifyIp(service.unique_id, service, data) elif msg == "ready": state = State.USABLE notifyReady = True self.notifyIp(service.unique_id, service, data) service.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: service.remove() else: if notifyReady is False: service.save() else: logger.debug('Notifying ready') UserServiceManager.manager().notifyReadyFromOsManager(service, '') logger.debug('Returning {} to {} message'.format(ret, msg)) if options is not None and options.get('scramble', True) is False: return ret return scrambleMsg(ret)
def moveToLevel(self, cacheLevel): """ Moves cache items betwen levels, managed directly Args: cacheLevel: New cache level to put object in """ from uds.core.managers.UserServiceManager import UserServiceManager UserServiceManager.manager().moveToLevel(self, cacheLevel)
def moveToLevel(self, cacheLevel): ''' Moves cache items betwen levels, managed directly Args: cacheLevel: New cache level to put object in ''' from uds.core.managers.UserServiceManager import UserServiceManager UserServiceManager.manager().moveToLevel(self, cacheLevel)
def run(self): removeFrom = getSqlDatetime() - timedelta(seconds=10) # We keep at least 10 seconds the machine before removing it, so we avoid connections errors removables = UserService.objects.filter(state=State.REMOVABLE, state_date__lt=removeFrom, deployed_service__service__provider__maintenance_mode=False)[0:UserServiceRemover.removeAtOnce] for us in removables: try: UserServiceManager.manager().remove(us) except Exception: logger.exception('Exception invoking remove user service {}'.format(us))
def getService(request, idService, idTransport, doTest=True): kind, idService = idService[0], idService[1:] logger.debug('Kind of service: {0}, idService: {1}'.format(kind, idService)) if kind == 'A': # This is an assigned service logger.debug('Getting A service {}'.format(idService)) ads = UserService.objects.get(uuid=idService) ads.deployed_service.validateUser(request.user) else: ds = DeployedService.objects.get(uuid=idService) # We first do a sanity check for this, if the user has access to this service # If it fails, will raise an exception ds.validateUser(request.user) # Now we have to locate an instance of the service, so we can assign it to user. ads = UserServiceManager.manager().getAssignationForUser(ds, request.user) if ads.isInMaintenance() is True: raise ServiceInMaintenanceMode() logger.debug('Found service: {0}'.format(ads)) trans = Transport.objects.get(uuid=idTransport) # Ensures that the transport is allowed for this service if trans not in ads.deployed_service.transports.all(): raise InvalidServiceException() # If transport is not available for the request IP... if trans.validForIp(request.ip) is False: raise InvalidServiceException() if doTest is False: return (None, ads, None, trans, None) # Test if the service is ready if ads.isReady(): log.doLog(ads, log.INFO, "User {0} from {1} has initiated access".format(request.user.name, request.ip), log.WEB) # If ready, show transport for this service, if also ready ofc iads = ads.getInstance() ip = iads.getIp() events.addEvent(ads.deployed_service, events.ET_ACCESS, username=request.user.name, srcip=request.ip, dstip=ip, uniqueid=ads.unique_id) if ip is not None: itrans = trans.getInstance() if itrans.isAvailableFor(ip): ads.setConnectionSource(request.ip, 'unknown') log.doLog(ads, log.INFO, "User service ready", log.WEB) UserServiceManager.manager().notifyPreconnect(ads, itrans.processedUser(ads, request.user), itrans.protocol) return (ip, ads, iads, trans, itrans) else: log.doLog(ads, log.WARN, "User service is not accessible (ip {0})".format(ip), log.TRANSPORT) logger.debug('Transport is not ready for user service {0}'.format(ads)) else: logger.debug('Ip not available from user service {0}'.format(ads)) else: log.doLog(ads, log.WARN, "User {0} from {1} tried to access, but machine was not ready".format(request.user.name, request.ip), log.WEB) return None
def run(self): removeFrom = getSqlDatetime() - timedelta( seconds=10 ) # We keep at least 30 seconds the machine before removing it, so we avoid connections errors removables = UserService.objects.filter( state=State.REMOVABLE, state_date__lt=removeFrom, deployed_service__service__provider__maintenance_mode=False )[0:UserServiceRemover.removeAtOnce] for us in removables: UserServiceManager.manager().remove(us)
def process(self, service, msg, data, options): ''' We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class), old method * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class), new method * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used ''' logger.info("Invoked LinuxOsManager for {0} with params: {1},{2}".format(service, msg, data)) # We get from storage the name for this service. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = service.os_state # Old "info" state, will be removed in a near future if msg == "info": ret = self.infoVal(service) state = State.PREPARING elif msg == "information": ret = self.infoValue(service) state = State.PREPARING elif msg == "log": self.doLog(service, data, log.ACTOR) elif msg == "login": self.loggedIn(service, data, False) elif msg == "logout": self.loggedOut(service, data, False) if self._onLogout == 'remove': doRemove = True elif msg == "ip": # This ocurss on main loop inside machine, so service is usable state = State.USABLE self.notifyIp(service.unique_id, service, data) elif msg == "ready": state = State.USABLE notifyReady = True self.notifyIp(service.unique_id, service, data) service.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: service.remove() else: if notifyReady is False: service.save() else: UserServiceManager.manager().notifyReadyFromOsManager(service, '') logger.debug('Returning {0}'.format(ret)) return ret
def growL2Cache(self, sp, cacheL1, cacheL2, assigned): ''' Tries to grow L2 cache of service. If for some reason the number of deployed services (Counting all, ACTIVE and PREPARING, assigned, L1 and L2) is over max allowed service deployments, this method will not grow the L1 cache ''' logger.debug("Growing L2 cache creating a new service for {0}".format(sp)) try: UserServiceManager.manager().createCacheFor(sp.activePublication(), services.UserDeployment.L2_CACHE) except MaxServicesReachedException as e: logger.error(str(e))
def setInUse(self, state): ''' Set the "in_use" flag for this user deployed service Args: state: State to set to the "in_use" flag of this record :note: If the state is Fase (set to not in use), a check for removal of this deployed service is launched. ''' from uds.core.managers.UserServiceManager import UserServiceManager self.in_use = state self.in_use_date = getSqlDatetime() if state is False: # Service released, check y we should mark it for removal # If our publication is not current, mark this for removal UserServiceManager.manager().checkForRemoval(self)
def growL2Cache(self, sp, cacheL1, cacheL2, assigned): """ Tries to grow L2 cache of service. If for some reason the number of deployed services (Counting all, ACTIVE and PREPARING, assigned, L1 and L2) is over max allowed service deployments, this method will not grow the L1 cache """ logger.debug( "Growing L2 cache creating a new service for {0}".format(sp)) try: UserServiceManager.manager().createCacheFor( sp.activePublication(), services.UserDeployment.L2_CACHE) except MaxServicesReachedError as e: logger.error(str(e))
def isReady(self): """ Returns if this service is ready (not preparing or marked for removal) """ # Call to isReady of the instance from uds.core.managers.UserServiceManager import UserServiceManager return UserServiceManager.manager().isReady(self)
def reduceL1Cache(self, sp, cacheL1, cacheL2, assigned): logger.debug("Reducing L1 cache erasing a service in cache for {0}".format(sp)) # We will try to destroy the newest cacheL1 element that is USABLE if the deployer can't cancel a new service creation cacheItems = sp.cachedUserServices().filter(UserServiceManager.getCacheStateFilter(services.UserDeployment.L1_CACHE)).order_by('-creation_date') if len(cacheItems) == 0: logger.debug('There is more services than configured, but could not reduce cache cause its already empty') return if cacheL2 < sp.cache_l2_srvs: valid = None for n in cacheItems: if n.needsOsManager(): if State.isUsable(n.state) is False or State.isUsable(n.os_state): valid = n break else: valid = n break if valid is not None: valid.moveToLevel(services.UserDeployment.L2_CACHE) return cache = cacheItems[0] cache.removeOrCancel()
def isReady(self): ''' Returns if this service is ready (not preparing or marked for removal) ''' # Call to isReady of the instance from uds.core.managers.UserServiceManager import UserServiceManager return UserServiceManager.manager().isReady(self)
def reduceL2Cache(self, sp, cacheL1, cacheL2, assigned): logger.debug("Reducing L2 cache erasing a service in cache for {0}".format(sp)) if cacheL2 > 0: cacheItems = sp.cachedUserServices().filter(UserServiceManager.getCacheStateFilter(services.UserDeployment.L2_CACHE)).order_by('creation_date') # TODO: Look first for non finished cache items and cancel them cache = cacheItems[0] cache.removeOrCancel()
def reduceL1Cache(self, sp, cacheL1, cacheL2, assigned): logger.debug( "Reducing L1 cache erasing a service in cache for {0}".format(sp)) # We will try to destroy the newest cacheL1 element that is USABLE if the deployer can't cancel a new service creation cacheItems = sp.cachedUserServices().filter( UserServiceManager.getCacheStateFilter( services.UserDeployment.L1_CACHE)).order_by('-creation_date') if len(cacheItems) == 0: logger.debug( 'There is more services than configured, but could not reduce cache cause its already empty' ) return if cacheL2 < sp.cache_l2_srvs: valid = None for n in cacheItems: if n.needsOsManager(): if State.isUsable(n.state) is False or State.isUsable( n.os_state): valid = n break else: valid = n break if valid is not None: valid.moveToLevel(services.UserDeployment.L2_CACHE) return cache = cacheItems[0] cache.removeOrCancel()
def serviceList(self): # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(self._user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availUserServices = UserService.getUserAssignedServices(self._user) # Extract required data to show to user services = [] # Select assigned user services for svr in availUserServices: # Skip maintenance services... trans = [] for t in svr.transports.all().order_by('priority'): if t.validForIp(self._request.ip) and t.getType( ).providesConnetionInfo(): trans.append({'id': t.uuid, 'name': t.name}) services.append({ 'id': 'A' + svr.uuid, 'name': svr['name'], 'transports': trans, 'maintenance': svr.isInMaintenance(), 'in_use': svr.in_use }) logger.debug(services) # Now generic user service for svr in availServices: trans = [] for t in svr.transports.all().order_by('priority'): if t.validForIp(self._request.ip) and t.getType( ).providesConnetionInfo(): trans.append({'id': t.uuid, 'name': t.name}) # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser( svr, self._user) if ads is None: in_use = False else: in_use = ads.in_use services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'transports': trans, 'maintenance': svr.isInMaintenance(), 'in_use': in_use }) logger.debug('Services: {0}'.format(services)) services = sorted(services, key=lambda s: s['name'].upper()) return Connection.result(result=services)
def reduceL2Cache(self, sp, cacheL1, cacheL2, assigned): logger.debug( "Reducing L2 cache erasing a service in cache for {0}".format(sp)) if cacheL2 > 0: cacheItems = sp.cachedUserServices().filter( UserServiceManager.getCacheStateFilter( services.UserDeployment.L2_CACHE)).order_by( 'creation_date') # TODO: Look first for non finished cache items and cancel them cache = cacheItems[0] cache.removeOrCancel()
def growL1Cache(self, sp, cacheL1, cacheL2, assigned): """ This method tries to enlarge L1 cache. If for some reason the number of deployed services (Counting all, ACTIVE and PREPARING, assigned, L1 and L2) is over max allowed service deployments, this method will not grow the L1 cache """ logger.debug( "Growing L1 cache creating a new service for {0}".format(sp)) # First, we try to assign from L2 cache if cacheL2 > 0: valid = None with transaction.atomic(): for n in sp.cachedUserServices().select_for_update().filter( UserServiceManager.getCacheStateFilter( services.UserDeployment.L2_CACHE)).order_by( 'creation_date'): if n.needsOsManager(): if State.isUsable(n.state) is False or State.isUsable( n.os_state): valid = n break else: valid = n break if valid is not None: valid.moveToLevel(services.UserDeployment.L1_CACHE) return try: UserServiceManager.manager().createCacheFor( sp.activePublication(), services.UserDeployment.L1_CACHE) except MaxServicesReachedError as e: log.doLog(sp, log.ERROR, 'Max number of services reached for this service', log.INTERNAL) logger.error(str(e)) except: logger.exception('Exception')
def serviceList(self): # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(self._user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availUserServices = UserService.getUserAssignedServices(self._user) # Extract required data to show to user services = [] # Select assigned user services for svr in availUserServices: # Skip maintenance services... trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(self._request.ip) and t.getType().providesConnetionInfo(): trans.append({'id': t.uuid, 'name': t.name, 'needsJava': t.getType().needsJava}) services.append({'id': 'A' + svr.uuid, 'name': svr['name'], 'transports': trans, 'maintenance': svr.deployed_service.service.provider.maintenance_mode, 'in_use': svr.in_use}) logger.debug(services) # Now generic user service for svr in availServices: trans = [] for t in svr.transports.all().order_by('priority'): if t.validForIp(self._request.ip) and t.getType().providesConnetionInfo(): typeTrans = t.getType() trans.append({'id': t.uuid, 'name': t.name, 'needsJava': typeTrans.needsJava}) # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser(svr, self._user) if ads is None: in_use = False else: in_use = ads.in_use services.append({'id': 'F' + svr.uuid, 'name': svr.name, 'transports': trans, 'maintenance': svr.service.provider.maintenance_mode, 'in_use': in_use}) logger.debug('Services: {0}'.format(services)) services = sorted(services, key=lambda s: s['name'].upper()) return Connection.result(result=services)
def setInUse(self, state): """ Set the "in_use" flag for this user deployed service Args: state: State to set to the "in_use" flag of this record :note: If the state is Fase (set to not in use), a check for removal of this deployed service is launched. """ from uds.core.managers.UserServiceManager import UserServiceManager self.in_use = state self.in_use_date = getSqlDatetime() self.save(update_fields=['in_use', 'in_use_date']) # Start/stop accounting if state is True: self.startUsageAccounting() else: self.stopUsageAccounting() if state is False: # Service released, check y we should mark it for removal # If our publication is not current, mark this for removal UserServiceManager.manager().checkForRemoval(self)
def index(request): """ Renders the main page. :param request: http request """ if request.session.get('ticket') == '1': return webLogout(request) # Session data os = request.os # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(request.user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availUserServices = UserService.getUserAssignedServices(request.user) # Information for administrators nets = '' validTrans = '' logger.debug('OS: {0}'.format(os['OS'])) if request.user.isStaff(): nets = ','.join([n.name for n in Network.networksFor(request.ip)]) tt = [] for t in Transport.objects.all(): if t.validForIp(request.ip): tt.append(t.name) validTrans = ','.join(tt) # Extract required data to show to user services = [] # Select assigned user services (manually assigned) for svr in availUserServices: trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']) and t.validForOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('A' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'A' + svr.uuid, t.uuid) trans.append( { 'id': t.uuid, 'name': t.name, 'link': link } ) servicePool = svr.deployed_service if servicePool.image is not None: imageId = servicePool.image.uuid else: imageId = 'x' # Invalid # Extract app group group = servicePool.servicesPoolGroup if servicePool.servicesPoolGroup is not None else ServicesPoolGroup.default().as_dict services.append({ 'id': 'A' + svr.uuid, 'name': servicePool.name, 'visual_name': servicePool.visual_name, 'description': servicePool.comments, 'group': group, 'transports': trans, 'imageId': imageId, 'show_transports': servicePool.show_transports, 'allow_users_remove': servicePool.allow_users_remove, 'maintenance': servicePool.isInMaintenance(), 'not_accesible': not servicePool.isAccessAllowed(), 'in_use': svr.in_use, 'to_be_replaced': False, # Manually assigned will not be autoremoved never 'comments': servicePool.comments, }) logger.debug(services) # Now generic user service for svr in availServices: trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if typeTrans is None: # This may happen if we "remove" a transport type but we have a transport of that kind on DB continue if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']) and t.validForOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'F' + svr.uuid, t.uuid) trans.append( { 'id': t.uuid, 'name': t.name, 'link': link } ) if svr.image is not None: imageId = svr.image.uuid else: imageId = 'x' # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser(svr, request.user) if ads is None: in_use = False else: in_use = ads.in_use group = svr.servicesPoolGroup.as_dict if svr.servicesPoolGroup is not None else ServicesPoolGroup.default().as_dict tbr = svr.toBeReplaced() if tbr is not None: tbr = formats.date_format(tbr, "SHORT_DATETIME_FORMAT") tbrt = ugettext('This service is about to be replaced by a new version. Please, close the session before {} and save all your work to avoid loosing it.').format(tbr) else: tbrt = '' services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'visual_name': svr.visual_name, 'description': svr.comments, 'group': group, 'transports': trans, 'imageId': imageId, 'show_transports': svr.show_transports, 'allow_users_remove': svr.allow_users_remove, 'maintenance': svr.isInMaintenance(), 'not_accesible': not svr.isAccessAllowed(), 'in_use': in_use, 'to_be_replaced': tbr, 'to_be_replaced_text': tbrt, 'comments': svr.comments, }) logger.debug('Services: {0}'.format(services)) services = sorted(services, key=lambda s: s['name'].upper()) autorun = False if len(services) == 1 and GlobalConfig.AUTORUN_SERVICE.getBool(True) and len(services[0]['transports']) > 0: if request.session.get('autorunDone', '0') == '0': request.session['autorunDone'] = '1' autorun = True # return redirect('uds.web.views.service', idService=services[0]['id'], idTransport=services[0]['transports'][0]['id']) # List of services groups allGroups = [v for v in sorted([ser['group'] for ser in services], key=lambda s: s['priority'])] # Now remove duplicates groups = [] already = [] for g in allGroups: if g['name'] not in already: already.append(g['name']) groups.append(g) logger.debug('Groups: {}'.format(groups)) response = render( request, theme.template('index.html'), { 'groups': groups, 'services': services, 'ip': request.ip, 'nets': nets, 'transports': validTrans, 'autorun': autorun } ) return response
def process(self, userService, msg, data, options=None): """ We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (old method) * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (new method) * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used """ logger.info("Invoked WindowsOsManager for {0} with params: {1},{2}".format(userService, msg, data)) if msg in ('ready', 'ip'): if not isinstance(data, dict): # Old actors, previous to 2.5, convert it information.. data = { 'ips': [v.split('=') for v in data.split(',')], 'hostname': userService.friendly_name } # We get from storage the name for this userService. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = userService.os_state if msg == "info": ret = self.infoVal(userService) state = State.PREPARING elif msg == "information": ret = self.infoValue(userService) state = State.PREPARING elif msg == "log": self.doLog(userService, data, log.ACTOR) elif msg == "logon" or msg == 'login': if '\\' not in data: self.loggedIn(userService, data, False) userService.setInUse(True) # We get the userService logged hostname & ip and returns this ip, hostname = userService.getConnectionSource() deadLine = userService.deployed_service.getDeadline() if userService.getProperty('actor_version', '0.0.0') >= '2.0.0': ret = "{0}\t{1}\t{2}".format(ip, hostname, 0 if deadLine is None else deadLine) else: ret = "{0}\t{1}".format(ip, hostname) elif msg == "logoff" or msg == 'logout': self.loggedOut(userService, data, False) if userService.in_use == False and self._onLogout == 'remove': doRemove = True elif msg == "ip": # This ocurss on main loop inside machine, so userService is usable state = State.USABLE self.notifyIp(userService.unique_id, userService, data) elif msg == "ready": self.toReady(userService) state = State.USABLE notifyReady = True self.notifyIp(userService.unique_id, userService, data) self.readyReceived(userService, data) userService.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: userService.release() else: if notifyReady is False: userService.save() else: logger.debug('Notifying ready') UserServiceManager.manager().notifyReadyFromOsManager(userService, '') logger.debug('Returning {} to {} message'.format(ret, msg)) if options is not None and options.get('scramble', True) is False: return ret return scrambleMsg(ret)
def getServicesData(request): # Session data os = request.os # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(request.user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availMetas = MetaPool.getForGroups(groups) # Information for administrators nets = '' validTrans = '' logger.debug('OS: {0}'.format(os['OS'])) if request.user.isStaff(): nets = ','.join([n.name for n in Network.networksFor(request.ip)]) tt = [] for t in Transport.objects.all(): if t.validForIp(request.ip): tt.append(t.name) validTrans = ','.join(tt) logger.debug('Checking meta pools: %s', availMetas) services = [] # Add meta pools data first for meta in availMetas: # Check that we have access to at least one transport on some of its children hasUsablePools = False in_use = False for pool in meta.pools.all(): # if pool.isInMaintenance(): # continue for t in pool.transports.all(): typeTrans = t.getType() if t.getType() and t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']) and t.validForOs(os['OS']): hasUsablePools = True break if not in_use: assignedUserService = UserServiceManager.manager().getExistingAssignationForUser(pool, request.user) if assignedUserService: in_use = assignedUserService.in_use # Stop when 1 usable pool is found if hasUsablePools: break # If no usable pools, this is not visible if hasUsablePools: group = meta.servicesPoolGroup.as_dict if meta.servicesPoolGroup else ServicesPoolGroup.default().as_dict services.append({ 'id': 'M' + meta.uuid, 'name': meta.name, 'visual_name': meta.visual_name, 'description': meta.comments, 'group': group, 'transports': [{ 'id': 'meta', 'name': 'meta', 'link': html.udsMetaLink(request, 'M' + meta.uuid), 'priority': 0 }], 'imageId': meta.image and meta.image.uuid or 'x', 'show_transports': False, 'allow_users_remove': False, 'allow_users_reset': False, 'maintenance': meta.isInMaintenance(), 'not_accesible': not meta.isAccessAllowed(), 'in_use': in_use, 'to_be_replaced': None, 'to_be_replaced_text': '', }) # Now generic user service for svr in availServices: # Skip pools that are part of meta pools if svr.is_meta: continue trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if typeTrans is None: # This may happen if we "remove" a transport type but we have a transport of that kind on DB continue if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']) and t.validForOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'F' + svr.uuid, t.uuid) trans.append( { 'id': t.uuid, 'name': t.name, 'link': link, 'priority': t.priority } ) # If empty transports, do not include it on list if not trans: continue if svr.image is not None: imageId = svr.image.uuid else: imageId = 'x' # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser(svr, request.user) if ads is None: in_use = False else: in_use = ads.in_use group = svr.servicesPoolGroup.as_dict if svr.servicesPoolGroup else ServicesPoolGroup.default().as_dict tbr = svr.toBeReplaced(request.user) if tbr: tbr = formats.date_format(tbr, "SHORT_DATETIME_FORMAT") tbrt = ugettext('This service is about to be replaced by a new version. Please, close the session before {} and save all your work to avoid loosing it.').format(tbr) else: tbrt = '' services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'visual_name': svr.visual_name, 'description': svr.comments, 'group': group, 'transports': trans, 'imageId': imageId, 'show_transports': svr.show_transports, 'allow_users_remove': svr.allow_users_remove, 'allow_users_reset': svr.allow_users_reset, 'maintenance': svr.isInMaintenance(), 'not_accesible': not svr.isAccessAllowed(), 'in_use': in_use, 'to_be_replaced': tbr, 'to_be_replaced_text': tbrt, }) logger.debug('Services: {0}'.format(services)) # Sort services and remove services with no transports... services = [s for s in sorted(services, key=lambda s: s['name'].upper()) if len(s['transports']) > 0] autorun = False if len(services) == 1 and GlobalConfig.AUTORUN_SERVICE.getBool(True) and len(services[0]['transports']) > 0: if request.session.get('autorunDone', '0') == '0': request.session['autorunDone'] = '1' autorun = True # return redirect('uds.web.views.service', idService=services[0]['id'], idTransport=services[0]['transports'][0]['id']) return { 'services': services, 'ip': request.ip, 'nets': nets, 'transports': validTrans, 'autorun': autorun }
def index(request): ''' Renders the main page. :param request: http request ''' # Session data os = request.os # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(request.user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availUserServices = UserService.getUserAssignedServices(request.user) # Information for administrators nets = '' validTrans = '' logger.debug('OS: {0}'.format(os['OS'])) if request.user.isStaff(): nets = ','.join([n.name for n in Network.networksFor(request.ip)]) tt = [] for t in Transport.objects.all(): if t.validForIp(request.ip): tt.append(t.name) validTrans = ','.join(tt) # Extract required data to show to user services = [] # Select assigned user services for svr in availUserServices: trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('A' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'A' + svr.uuid, t.uuid) trans.append({'id': t.uuid, 'name': t.name, 'link': link}) if svr.deployed_service.image is not None: imageId = svr.deployed_service.image.uuid else: imageId = 'x' # Invalid services.append({ 'id': 'A' + svr.uuid, 'name': svr['name'], 'transports': trans, 'imageId': imageId, 'show_transports': svr.deployed_service.show_transports, 'maintenance': svr.deployed_service.service.provider.maintenance_mode, 'in_use': svr.in_use, }) logger.debug(services) # Now generic user service for svr in availServices: # Generate ticket trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'F' + svr.uuid, t.uuid) trans.append({'id': t.uuid, 'name': t.name, 'link': link}) if svr.image is not None: imageId = svr.image.uuid else: imageId = 'x' # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser( svr, request.user) if ads is None: in_use = False else: in_use = ads.in_use services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'transports': trans, 'imageId': imageId, 'show_transports': svr.show_transports, 'maintenance': svr.service.provider.maintenance_mode, 'in_use': in_use, }) logger.debug('Services: {0}'.format(services)) services = sorted(services, key=lambda s: s['name'].upper()) if len(services) == 1 and GlobalConfig.AUTORUN_SERVICE.get( True) == '1' and len(services[0]['transports']) > 0: if request.session.get('autorunDone', '0') == '0': request.session['autorunDone'] = '1' # TODO: Make this to redirect to uds link directly return redirect('uds.web.views.service', idService=services[0]['id'], idTransport=services[0]['transports'][0]['id']) response = render_to_response(theme.template('index.html'), { 'services': services, 'ip': request.ip, 'nets': nets, 'transports': validTrans, }, context_instance=RequestContext(request)) return response
def index(request): ''' Renders the main page. :param request: http request ''' # Session data os = request.os # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(request.user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availUserServices = UserService.getUserAssignedServices(request.user) # Information for administrators nets = '' validTrans = '' logger.debug('OS: {0}'.format(os['OS'])) if request.user.isStaff(): nets = ','.join([n.name for n in Network.networksFor(request.ip)]) tt = [] for t in Transport.objects.all(): if t.validForIp(request.ip): tt.append(t.name) validTrans = ','.join(tt) # Extract required data to show to user services = [] # Select assigned user services for svr in availUserServices: trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('A' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'A' + svr.uuid, t.uuid) trans.append( { 'id': t.uuid, 'name': t.name, 'link': link } ) if svr.deployed_service.image is not None: imageId = svr.deployed_service.image.uuid else: imageId = 'x' # Invalid services.append({ 'id': 'A' + svr.uuid, 'name': svr['name'], 'transports': trans, 'imageId': imageId, 'show_transports': svr.deployed_service.show_transports, 'maintenance': svr.deployed_service.service.provider.maintenance_mode, 'in_use': svr.in_use, }) logger.debug(services) # Now generic user service for svr in availServices: # Generate ticket trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'F' + svr.uuid, t.uuid) trans.append( { 'id': t.uuid, 'name': t.name, 'link': link } ) if svr.image is not None: imageId = svr.image.uuid else: imageId = 'x' # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser(svr, request.user) if ads is None: in_use = False else: in_use = ads.in_use services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'transports': trans, 'imageId': imageId, 'show_transports': svr.show_transports, 'maintenance': svr.service.provider.maintenance_mode, 'in_use': in_use, }) logger.debug('Services: {0}'.format(services)) services = sorted(services, key=lambda s: s['name'].upper()) if len(services) == 1 and GlobalConfig.AUTORUN_SERVICE.get(True) == '1' and len(services[0]['transports']) > 0: if request.session.get('autorunDone', '0') == '0': request.session['autorunDone'] = '1' # TODO: Make this to redirect to uds link directly return redirect('uds.web.views.service', idService=services[0]['id'], idTransport=services[0]['transports'][0]['id']) response = render_to_response( theme.template('index.html'), { 'services': services, 'ip': request.ip, 'nets': nets, 'transports': validTrans, }, context_instance=RequestContext(request) ) return response
def servicesPoolsNeedingCacheUpdate(self): # State filter for cached and inAssigned objects # First we get all deployed services that could need cache generation DeployedService.objects.update() # We start filtering out the deployed services that do not need caching at all. whichNeedsCaching = DeployedService.objects.filter( Q(initial_srvs__gte=0) | Q(cache_l1_srvs__gte=0)).filter( max_srvs__gt=0, state=State.ACTIVE, service__provider__maintenance_mode=False)[:] # We will get the one that proportionally needs more cache servicesPools = [] for sp in whichNeedsCaching: sp.userServices.update() # Cleans cached queries # If this deployedService don't have a publication active and needs it, ignore it spServiceInstance = sp.service.getInstance() if sp.activePublication( ) is None and spServiceInstance.publicationType is not None: logger.debug( '{} Needs publication but do not have one, cache test ignored' .format(sp)) continue # If it has any running publication, do not generate cache anymore if sp.publications.filter(state=State.PREPARING).count() > 0: logger.debug( 'Stopped cache generation for deployed service with publication running: {0}' .format(sp)) continue if sp.isRestrained(): ServiceCacheUpdater.__notifyRestrain(sp) continue # Get data related to actual state of cache inCacheL1 = sp.cachedUserServices().filter( UserServiceManager.getCacheStateFilter( services.UserDeployment.L1_CACHE)).count() inCacheL2 = sp.cachedUserServices().filter( UserServiceManager.getCacheStateFilter( services.UserDeployment.L2_CACHE)).count() inAssigned = sp.assignedUserServices().filter( UserServiceManager.getStateFilter()).count() # if we bypasses max cache, we will reduce it in first place. This is so because this will free resources on service provider logger.debug( "Examining {0} with {1} in cache L1 and {2} in cache L2, {3} inAssigned" .format(sp, inCacheL1, inCacheL2, inAssigned)) totalL1Assigned = inCacheL1 + inAssigned # We have more than we want if totalL1Assigned > sp.max_srvs: logger.debug('We have more services than max configured') servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # We have more in L1 cache than needed if totalL1Assigned > sp.initial_srvs and inCacheL1 > sp.cache_l1_srvs: logger.debug( 'We have more services in cache L1 than configured') servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # If we have more in L2 cache than needed, decrease L2 cache, but int this case, we continue checking cause L2 cache removal # has less priority than l1 creations or removals, but higher. In this case, we will simply take last l2 oversized found and reduce it if inCacheL2 > sp.cache_l2_srvs: logger.debug( 'We have more services in L2 cache than configured, decreasing it' ) servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # If this service don't allows more starting user services, continue if UserServiceManager.manager( ).canInitiateServiceFromDeployedService(sp) is False: logger.debug( 'This provider has the max allowed starting services running: {0}' .format(sp)) continue # If wee need to grow l2 cache, annotate it # Whe check this before checking the total, because the l2 cache is independent of max services or l1 cache. # It reflects a value that must be keeped in cache for futre fast use. if inCacheL2 < sp.cache_l2_srvs: logger.debug('Needs to grow L2 cache for {}'.format(sp)) servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # We skip it if already at max if totalL1Assigned == sp.max_srvs: continue if totalL1Assigned < sp.initial_srvs or inCacheL1 < sp.cache_l1_srvs: logger.debug('Needs to grow L1 cache for {}'.format(sp)) servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) # We also return calculated values so we can reuse then return servicesPools
def connection(self, doNotCheck=False): kind, idService = self._args[0][0], self._args[0][1:] idTransport = self._args[1] logger.debug('Type: {}, Service: {}, Transport: {}'.format( kind, idService, idTransport)) try: logger.debug('Kind of service: {0}, idService: {1}'.format( kind, idService)) if kind == 'A': # This is an assigned service ads = UserService.objects.get(uuid=processUuid(idService)) else: ds = DeployedService.objects.get(uuid=processUuid(idService)) # We first do a sanity check for this, if the user has access to this service # If it fails, will raise an exception ds.validateUser(self._user) # Now we have to locate an instance of the service, so we can assign it to user. ads = UserServiceManager.manager().getAssignationForUser( ds, self._user) if ads.isInMaintenance() is True: return Connection.result(error='Service in maintenance') logger.debug('Found service: {0}'.format(ads)) trans = Transport.objects.get(uuid=processUuid(idTransport)) if trans.validForIp(self._request.ip) is False: return Connection.result(error='Access denied') # Test if the service is ready if doNotCheck or ads.isReady(): log.doLog( ads, log.INFO, "User {0} from {1} has initiated access".format( self._user.name, self._request.ip), log.WEB) # If ready, show transport for this service, if also ready ofc iads = ads.getInstance() ip = iads.getIp() logger.debug('IP: {}'.format(ip)) events.addEvent(ads.deployed_service, events.ET_ACCESS, username=self._user.name, srcip=self._request.ip, dstip=ip, uniqueid=ads.unique_id) if ip is not None: itrans = trans.getInstance() if itrans.providesConnetionInfo() and ( doNotCheck or itrans.isAvailableFor(ads, ip)): ads.setConnectionSource(self._request.ip, 'unknown') log.doLog(ads, log.INFO, "User service ready, rendering transport", log.WEB) ci = { 'username': '', 'password': '', 'domain': '', 'protocol': 'unknown', 'ip': ip } ci.update( itrans.getConnectionInfo(ads, self._user, 'UNKNOWN')) UserServiceManager.manager().notifyPreconnect( ads, itrans.processedUser(ads, self._user), itrans.protocol) return Connection.result(result=ci) else: log.doLog( ads, log.WARN, "User service is not accessible by REST (ip {0})". format(ip), log.TRANSPORT) logger.debug( 'Transport {} is not accesible for user service {} from {}' .format(trans, ads, self._request.ip)) logger.debug("{}, {}".format( itrans.providesConnetionInfo(), itrans.isAvailableFor(ads, ip))) else: logger.debug( 'Ip not available from user service {0}'.format(ads)) else: log.doLog( ads, log.WARN, "User {0} from {1} tried to access, but service was not ready" .format(self._user.name, self._request.ip), log.WEB) # Not ready, show message and return to this page in a while return Connection.result(error='Service not ready') except Exception as e: return Connection.result(error=six.text_type(e))
def servicesPoolsNeedingCacheUpdate(self): # State filter for cached and inAssigned objects # First we get all deployed services that could need cache generation DeployedService.objects.update() # We start filtering out the deployed services that do not need caching at all. whichNeedsCaching = DeployedService.objects.filter(Q(initial_srvs__gte=0) | Q(cache_l1_srvs__gte=0)).filter(max_srvs__gt=0, state=State.ACTIVE, service__provider__maintenance_mode=False)[:] # We will get the one that proportionally needs more cache servicesPools = [] for sp in whichNeedsCaching: sp.userServices.update() # Cleans cached queries # If this deployedService don't have a publication active and needs it, ignore it if sp.activePublication() is None and sp.service.getInstance().publicationType is not None: logger.debug('Needs publication but do not have one, cache test ignored') continue # If it has any running publication, do not generate cache anymore if sp.publications.filter(state=State.PREPARING).count() > 0: logger.debug('Stopped cache generation for deployed service with publication running: {0}'.format(sp)) continue if sp.isRestrained(): ServiceCacheUpdater.__notifyRestrain(sp) continue # Get data related to actual state of cache inCacheL1 = sp.cachedUserServices().filter(UserServiceManager.getCacheStateFilter(services.UserDeployment.L1_CACHE)).count() inCacheL2 = sp.cachedUserServices().filter(UserServiceManager.getCacheStateFilter(services.UserDeployment.L2_CACHE)).count() inAssigned = sp.assignedUserServices().filter(UserServiceManager.getStateFilter()).count() # if we bypasses max cache, we will reduce it in first place. This is so because this will free resources on service provider logger.debug("Examining {0} with {1} in cache L1 and {2} in cache L2, {3} inAssigned".format( sp, inCacheL1, inCacheL2, inAssigned)) totalL1Assigned = inCacheL1 + inAssigned # We have more than we want if totalL1Assigned > sp.max_srvs: logger.debug('We have more services than max configured') servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # We have more in L1 cache than needed if totalL1Assigned > sp.initial_srvs and inCacheL1 > sp.cache_l1_srvs: logger.debug('We have more services in cache L1 than configured') servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # If we have more in L2 cache than needed, decrease L2 cache, but int this case, we continue checking cause L2 cache removal # has less priority than l1 creations or removals, but higher. In this case, we will simply take last l2 oversized found and reduce it if inCacheL2 > sp.cache_l2_srvs: logger.debug('We have more services in L2 cache than configured, decreasing it') servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # If this service don't allows more starting user services, continue if UserServiceManager.manager().canInitiateServiceFromDeployedService(sp) is False: logger.debug('This provider has the max allowed starting services running: {0}'.format(sp)) continue # If wee need to grow l2 cache, annotate it # Whe check this before checking the total, because the l2 cache is independent of max services or l1 cache. # It reflects a value that must be keeped in cache for futre fast use. if inCacheL2 < sp.cache_l2_srvs: logger.debug('Needs to grow L2 cache for {}'.format(sp)) servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) continue # We skip it if already at max if totalL1Assigned == sp.max_srvs: continue if totalL1Assigned < sp.initial_srvs or inCacheL1 < sp.cache_l1_srvs: logger.debug('Needs to grow L1 cache for {}'.format(sp)) servicesPools.append((sp, inCacheL1, inCacheL2, inAssigned)) # We also return calculated values so we can reuse then return servicesPools
def getServicesData(request): # Session data os = request.os # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(request.user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availMetas = MetaPool.getForGroups(groups) # Information for administrators nets = '' validTrans = '' logger.debug('OS: {0}'.format(os['OS'])) if request.user.isStaff(): nets = ','.join([n.name for n in Network.networksFor(request.ip)]) tt = [] for t in Transport.objects.all(): if t.validForIp(request.ip): tt.append(t.name) validTrans = ','.join(tt) logger.debug('Checking meta pools: %s', availMetas) services = [] # Add meta pools data first for meta in availMetas: # Check that we have access to at least one transport on some of its children hasUsablePools = False in_use = False for pool in meta.pools.all(): # if pool.isInMaintenance(): # continue for t in pool.transports.all(): typeTrans = t.getType() if t.getType() and t.validForIp( request.ip) and typeTrans.supportsOs( os['OS']) and t.validForOs(os['OS']): hasUsablePools = True break if not in_use: assignedUserService = UserServiceManager.manager( ).getExistingAssignationForUser(pool, request.user) if assignedUserService: in_use = assignedUserService.in_use # Stop when 1 usable pool is found if hasUsablePools: break # If no usable pools, this is not visible if hasUsablePools: group = meta.servicesPoolGroup.as_dict if meta.servicesPoolGroup else ServicesPoolGroup.default( ).as_dict services.append({ 'id': 'M' + meta.uuid, 'name': meta.name, 'visual_name': meta.visual_name, 'description': meta.comments, 'group': group, 'transports': [{ 'id': 'meta', 'name': 'meta', 'link': html.udsMetaLink(request, 'M' + meta.uuid), 'priority': 0 }], 'imageId': meta.image and meta.image.uuid or 'x', 'show_transports': False, 'allow_users_remove': False, 'allow_users_reset': False, 'maintenance': meta.isInMaintenance(), 'not_accesible': not meta.isAccessAllowed(), 'in_use': in_use, 'to_be_replaced': None, 'to_be_replaced_text': '', }) # Now generic user service for svr in availServices: # Skip pools that are part of meta pools if svr.is_meta: continue trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if typeTrans is None: # This may happen if we "remove" a transport type but we have a transport of that kind on DB continue if t.validForIp(request.ip) and typeTrans.supportsOs( os['OS']) and t.validForOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'F' + svr.uuid, t.uuid) trans.append({ 'id': t.uuid, 'name': t.name, 'link': link, 'priority': t.priority }) # If empty transports, do not include it on list if not trans: continue if svr.image is not None: imageId = svr.image.uuid else: imageId = 'x' # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser( svr, request.user) if ads is None: in_use = False else: in_use = ads.in_use group = svr.servicesPoolGroup.as_dict if svr.servicesPoolGroup else ServicesPoolGroup.default( ).as_dict tbr = svr.toBeReplaced(request.user) if tbr: tbr = formats.date_format(tbr, "SHORT_DATETIME_FORMAT") tbrt = ugettext( 'This service is about to be replaced by a new version. Please, close the session before {} and save all your work to avoid loosing it.' ).format(tbr) else: tbrt = '' services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'visual_name': svr.visual_name, 'description': svr.comments, 'group': group, 'transports': trans, 'imageId': imageId, 'show_transports': svr.show_transports, 'allow_users_remove': svr.allow_users_remove, 'allow_users_reset': svr.allow_users_reset, 'maintenance': svr.isInMaintenance(), 'not_accesible': not svr.isAccessAllowed(), 'in_use': in_use, 'to_be_replaced': tbr, 'to_be_replaced_text': tbrt, }) logger.debug('Services: {0}'.format(services)) # Sort services and remove services with no transports... services = [ s for s in sorted(services, key=lambda s: s['name'].upper()) if len(s['transports']) > 0 ] autorun = False if len(services) == 1 and GlobalConfig.AUTORUN_SERVICE.getBool( True) and len(services[0]['transports']) > 0: if request.session.get('autorunDone', '0') == '0': request.session['autorunDone'] = '1' autorun = True # return redirect('uds.web.views.service', idService=services[0]['id'], idTransport=services[0]['transports'][0]['id']) return { 'services': services, 'ip': request.ip, 'nets': nets, 'transports': validTrans, 'autorun': autorun }
def process(self, userService, msg, data, options=None): """ We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (old method) * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (new method) * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used """ logger.info( "Invoked WindowsOsManager for {0} with params: {1},{2}".format( userService, msg, data)) if msg in ('ready', 'ip'): if not isinstance( data, dict ): # Old actors, previous to 2.5, convert it information.. data = { 'ips': [v.split('=') for v in data.split(',')], 'hostname': userService.friendly_name } # We get from storage the name for this userService. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = userService.os_state if msg == "info": ret = self.infoVal(userService) state = State.PREPARING elif msg == "information": ret = self.infoValue(userService) state = State.PREPARING elif msg == "log": self.doLog(userService, data, log.ACTOR) elif msg == "logon" or msg == 'login': if '\\' not in data: self.loggedIn(userService, data) userService.setInUse(True) # We get the userService logged hostname & ip and returns this ip, hostname = userService.getConnectionSource() deadLine = userService.deployed_service.getDeadline() if userService.getProperty('actor_version', '0.0.0') >= '2.0.0': ret = "{0}\t{1}\t{2}".format( ip, hostname, 0 if deadLine is None else deadLine) else: ret = "{0}\t{1}".format(ip, hostname) elif msg == "logoff" or msg == 'logout': self.loggedOut(userService, data) doRemove = self.isRemovableOnLogout(userService) elif msg == "ip": # This ocurss on main loop inside machine, so userService is usable state = State.USABLE self.notifyIp(userService.unique_id, userService, data) elif msg == "ready": self.toReady(userService) state = State.USABLE notifyReady = True self.notifyIp(userService.unique_id, userService, data) self.readyReceived(userService, data) userService.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: userService.release() else: if notifyReady is False: userService.save(update_fields=[ 'in_use', 'in_use_date', 'os_state', 'state', 'data' ]) else: logger.debug('Notifying ready') UserServiceManager.manager().notifyReadyFromOsManager( userService, '') logger.debug('Returning {} to {} message'.format(ret, msg)) if options is not None and options.get('scramble', True) is False: return ret return scrambleMsg(ret)
def connection(self, doNotCheck=False): kind, idService = self._args[0][0], self._args[0][1:] idTransport = self._args[1] logger.debug('Type: {}, Service: {}, Transport: {}'.format(kind, idService, idTransport)) try: logger.debug('Kind of service: {0}, idService: {1}'.format(kind, idService)) if kind == 'A': # This is an assigned service ads = UserService.objects.get(uuid=processUuid(idService)) else: ds = DeployedService.objects.get(uuid=processUuid(idService)) # We first do a sanity check for this, if the user has access to this service # If it fails, will raise an exception ds.validateUser(self._user) # Now we have to locate an instance of the service, so we can assign it to user. ads = UserServiceManager.manager().getAssignationForUser(ds, self._user) if ads.isInMaintenance() is True: return Connection.result(error='Service in maintenance') logger.debug('Found service: {0}'.format(ads)) trans = Transport.objects.get(uuid=processUuid(idTransport)) if trans.validForIp(self._request.ip) is False: return Connection.result(error='Access denied') # Test if the service is ready if doNotCheck or ads.isReady(): log.doLog(ads, log.INFO, "User {0} from {1} has initiated access".format(self._user.name, self._request.ip), log.WEB) # If ready, show transport for this service, if also ready ofc iads = ads.getInstance() ip = iads.getIp() logger.debug('IP: {}'.format(ip)) events.addEvent(ads.deployed_service, events.ET_ACCESS, username=self._user.name, srcip=self._request.ip, dstip=ip, uniqueid=ads.unique_id) if ip is not None: itrans = trans.getInstance() if itrans.providesConnetionInfo() and (doNotCheck or itrans.isAvailableFor(ads, ip)): ads.setConnectionSource(self._request.ip, 'unknown') log.doLog(ads, log.INFO, "User service ready, rendering transport", log.WEB) ci = { 'username': '', 'password': '', 'domain': '', 'protocol': 'unknown', 'ip': ip } ci.update(itrans.getConnectionInfo(ads, self._user, 'UNKNOWN')) UserServiceManager.manager().notifyPreconnect(ads, itrans.processedUser(ads, self._user), itrans.protocol) return Connection.result(result=ci) else: log.doLog(ads, log.WARN, "User service is not accessible by REST (ip {0})".format(ip), log.TRANSPORT) logger.debug('Transport {} is not accesible for user service {} from {}'.format(trans, ads, self._request.ip)) logger.debug("{}, {}".format(itrans.providesConnetionInfo(), itrans.isAvailableFor(ads, ip))) else: logger.debug('Ip not available from user service {0}'.format(ads)) else: log.doLog(ads, log.WARN, "User {0} from {1} tried to access, but service was not ready".format(self._user.name, self._request.ip), log.WEB) # Not ready, show message and return to this page in a while return Connection.result(error='Service not ready') except Exception as e: return Connection.result(error=six.text_type(e))
def process(self, service, msg, data, options): ''' We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (old method) * msg = information, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (new method) * msg = logon, data = Username, Informs that the username has logged in inside the machine * msg = logoff, data = Username, Informs that the username has logged out of the machine * msg = ready, data = None, Informs machine ready to be used ''' logger.info( "Invoked WindowsOsManager for {0} with params: {1},{2}".format( service, msg, data)) # We get from storage the name for this service. If no name, we try to assign a new one ret = "ok" notifyReady = False doRemove = False state = service.os_state if msg == "info": ret = self.infoVal(service) state = State.PREPARING elif msg == "information": ret = self.infoValue(service) state = State.PREPARING elif msg == "log": self.doLog(service, data, log.ACTOR) elif msg == "logon" or msg == 'login': if '\\' not in data: self.loggedIn(service, data, False) service.setInUse(True) # We get the service logged hostname & ip and returns this ip, hostname = service.getConnectionSource() ret = "{0}\t{1}".format(ip, hostname) elif msg == "logoff" or msg == 'logout': self.loggedOut(service, data, False) if self._onLogout == 'remove': doRemove = True elif msg == "ip": # This ocurss on main loop inside machine, so service is usable state = State.USABLE self.notifyIp(service.unique_id, service, data) elif msg == "ready": state = State.USABLE notifyReady = True self.notifyIp(service.unique_id, service, data) service.setOsState(state) # If notifyReady is not true, save state, let UserServiceManager do it for us else if doRemove is True: service.remove() else: if notifyReady is False: service.save() else: logger.debug('Notifying ready') UserServiceManager.manager().notifyReadyFromOsManager( service, '') logger.debug('Returning {} to {} message'.format(ret, msg)) if options is not None and options.get('scramble', True) is False: return ret return scrambleMsg(ret)
def index(request): """ Renders the main page. :param request: http request """ if request.session.get('ticket') == '1': return webLogout(request) # Session data os = request.os # We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds groups = list(request.user.getGroups()) availServices = DeployedService.getDeployedServicesForGroups(groups) availUserServices = UserService.getUserAssignedServices(request.user) # Information for administrators nets = '' validTrans = '' logger.debug('OS: {0}'.format(os['OS'])) if request.user.isStaff(): nets = ','.join([n.name for n in Network.networksFor(request.ip)]) tt = [] for t in Transport.objects.all(): if t.validForIp(request.ip): tt.append(t.name) validTrans = ','.join(tt) # Extract required data to show to user services = [] # Select assigned user services (manually assigned) for svr in availUserServices: trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if t.validForIp(request.ip) and typeTrans.supportsOs( os['OS']) and t.validForOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('A' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'A' + svr.uuid, t.uuid) trans.append({'id': t.uuid, 'name': t.name, 'link': link}) servicePool = svr.deployed_service if servicePool.image is not None: imageId = servicePool.image.uuid else: imageId = 'x' # Invalid # Extract app group group = servicePool.servicesPoolGroup if servicePool.servicesPoolGroup is not None else ServicesPoolGroup.default( ).as_dict services.append({ 'id': 'A' + svr.uuid, 'name': servicePool.name, 'visual_name': servicePool.visual_name, 'description': servicePool.comments, 'group': group, 'transports': trans, 'imageId': imageId, 'show_transports': servicePool.show_transports, 'allow_users_remove': servicePool.allow_users_remove, 'allow_users_reset': servicePool.allow_users_reset, 'maintenance': servicePool.isInMaintenance(), 'not_accesible': not servicePool.isAccessAllowed(), 'in_use': svr.in_use, 'to_be_replaced': False, # Manually assigned will not be autoremoved never 'comments': servicePool.comments, }) logger.debug(services) # Now generic user service for svr in availServices: trans = [] for t in svr.transports.all().order_by('priority'): typeTrans = t.getType() if typeTrans is None: # This may happen if we "remove" a transport type but we have a transport of that kind on DB continue if t.validForIp(request.ip) and typeTrans.supportsOs( os['OS']) and t.validForOs(os['OS']): if typeTrans.ownLink is True: link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) else: link = html.udsAccessLink(request, 'F' + svr.uuid, t.uuid) trans.append({'id': t.uuid, 'name': t.name, 'link': link}) if svr.image is not None: imageId = svr.image.uuid else: imageId = 'x' # Locate if user service has any already assigned user service for this ads = UserServiceManager.manager().getExistingAssignationForUser( svr, request.user) if ads is None: in_use = False else: in_use = ads.in_use group = svr.servicesPoolGroup.as_dict if svr.servicesPoolGroup is not None else ServicesPoolGroup.default( ).as_dict tbr = svr.toBeReplaced() if tbr is not None: tbr = formats.date_format(tbr, "SHORT_DATETIME_FORMAT") tbrt = ugettext( 'This service is about to be replaced by a new version. Please, close the session before {} and save all your work to avoid loosing it.' ).format(tbr) else: tbrt = '' services.append({ 'id': 'F' + svr.uuid, 'name': svr.name, 'visual_name': svr.visual_name, 'description': svr.comments, 'group': group, 'transports': trans, 'imageId': imageId, 'show_transports': svr.show_transports, 'allow_users_remove': svr.allow_users_remove, 'allow_users_reset': svr.allow_users_reset, 'maintenance': svr.isInMaintenance(), 'not_accesible': not svr.isAccessAllowed(), 'in_use': in_use, 'to_be_replaced': tbr, 'to_be_replaced_text': tbrt, 'comments': svr.comments, }) logger.debug('Services: {0}'.format(services)) services = sorted(services, key=lambda s: s['name'].upper()) autorun = False if len(services) == 1 and GlobalConfig.AUTORUN_SERVICE.getBool( True) and len(services[0]['transports']) > 0: if request.session.get('autorunDone', '0') == '0': request.session['autorunDone'] = '1' autorun = True # return redirect('uds.web.views.service', idService=services[0]['id'], idTransport=services[0]['transports'][0]['id']) # List of services groups allGroups = [ v for v in sorted([ser['group'] for ser in services], key=lambda s: s['priority']) ] # Now remove duplicates groups = [] already = [] for g in allGroups: if g['name'] not in already: already.append(g['name']) groups.append(g) logger.debug('Groups: {}'.format(groups)) response = render( request, theme.template('index.html'), { 'groups': groups, 'services': services, 'ip': request.ip, 'nets': nets, 'transports': validTrans, 'autorun': autorun }) return response
def getService(request, idService, idTransport, doTest=True): kind, idService = idService[0], idService[1:] logger.debug('Kind of service: {0}, idService: {1}'.format( kind, idService)) if kind == 'A': # This is an assigned service logger.debug('Getting A service {}'.format(idService)) ads = UserService.objects.get(uuid=idService) ads.deployed_service.validateUser(request.user) else: ds = DeployedService.objects.get(uuid=idService) # We first do a sanity check for this, if the user has access to this service # If it fails, will raise an exception ds.validateUser(request.user) # Now we have to locate an instance of the service, so we can assign it to user. ads = UserServiceManager.manager().getAssignationForUser( ds, request.user) if ads.isInMaintenance() is True: raise ServiceInMaintenanceMode() logger.debug('Found service: {0}'.format(ads)) trans = Transport.objects.get(uuid=idTransport) # Ensures that the transport is allowed for this service if trans not in ads.deployed_service.transports.all(): raise InvalidServiceException() # If transport is not available for the request IP... if trans.validForIp(request.ip) is False: raise InvalidServiceException() if doTest is False: return (None, ads, None, trans, None) # Test if the service is ready if ads.isReady(): log.doLog( ads, log.INFO, "User {0} from {1} has initiated access".format( request.user.name, request.ip), log.WEB) # If ready, show transport for this service, if also ready ofc iads = ads.getInstance() ip = iads.getIp() events.addEvent(ads.deployed_service, events.ET_ACCESS, username=request.user.name, srcip=request.ip, dstip=ip, uniqueid=ads.unique_id) if ip is not None: itrans = trans.getInstance() if itrans.isAvailableFor(ip): ads.setConnectionSource(request.ip, 'unknown') log.doLog(ads, log.INFO, "User service ready", log.WEB) UserServiceManager.manager().notifyPreconnect( ads, itrans.processedUser(ads, request.user), itrans.protocol) return (ip, ads, iads, trans, itrans) else: log.doLog(ads, log.WARN, "User service is not accessible (ip {0})".format(ip), log.TRANSPORT) logger.debug( 'Transport is not ready for user service {0}'.format(ads)) else: logger.debug('Ip not available from user service {0}'.format(ads)) else: log.doLog( ads, log.WARN, "User {0} from {1} tried to access, but machine was not ready". format(request.user.name, request.ip), log.WEB) return None