def update_queue_information(self, queue_data): if not 'messages' in queue_data: # FIXME: We should do something here, probably delete the queue, # as it's in a weird state. More investigation is required. # See bug 1066338. return None q_size, q_name, q_durable = (queue_data['messages'], queue_data['name'], queue_data['durable']) queue = Queue.query.filter(Queue.name == q_name).first() # If the queue doesn't exist in the db, create it. if queue is None: m = re.match('queue/([^/]+)/', q_name) logging.info("New queue '{0}' encountered. " "Adding to the database.".format(q_name)) if m: owner_name = m.group(1) owner = PulseUser.query.filter( PulseUser.username == owner_name).first() # If the queue was created by a user that isn't in the # pulseguardian database, skip the queue. if owner is None: logging.info( "Queue '{0}' owner, {1}, isn't in the db. Creating " "the user.".format(q_name, owner_name)) owner = PulseUser.new_user(owner_name) # Assign the user to the queue. logging.info("Assigning queue '{0}' to user " "{1}.".format(q_name, owner)) else: logging.warn( "'{0}' is not a standard queue name.".format(q_name)) owner = None queue = Queue(name=q_name, owner=owner) # add the queue bindings to the db. bindings = pulse_management.queue_bindings(config.rabbit_vhost, queue.name) for binding in bindings: db_binding = Binding.query.filter( Binding.exchange == binding["source"], Binding.routing_key == binding["routing_key"], Binding.queue_name == queue.name).first() if not db_binding: # need to create the binding in the DB binding = Binding(exchange=binding["source"], routing_key=binding["routing_key"], queue_name=queue.name) db_session.add(binding) # Update the saved queue size. queue.size = q_size queue.durable = q_durable db_session.add(queue) db_session.commit() return queue
def update_queue_information(self, queue_data, all_bindings): if 'messages' not in queue_data: # FIXME: We should do something here, probably delete the queue, # as it's in a weird state. More investigation is required. # See bug 1066338. return None q_size, q_name, q_durable = (queue_data['messages'], queue_data['name'], queue_data['durable']) queue = Queue.query.filter(Queue.name == q_name).first() # If the queue doesn't exist in the db, create it. if queue is None: log_details = { 'queuename': q_name, 'queuesize': q_size, 'queuedurable': q_durable, } m = re.match('queue/([^/]+)/', q_name) if not m: log_details['valid'] = False owner = None elif (config.reserved_users_regex and re.match(config.reserved_users_regex, m.group(1))): # Ignore this queue entirely as we will see it again on the # next iteration. return None else: log_details['valid'] = True owner_name = m.group(1) owner = RabbitMQAccount.query.filter( RabbitMQAccount.username == owner_name).first() log_details['ownername'] = owner_name log_details['newowner'] = not owner # If the queue belongs to a pulse user that isn't in the # pulseguardian database, add the user to the DB, owned by an # admin. if owner is None: # RabbitMQAccount needs at least one owner as well, but # since we have no way of knowing who really owns it, find # the first admin, and set it to that. user = User.query.filter(User.admin == True).first() owner = RabbitMQAccount.new_user(owner_name, owners=user) mozdef.log( mozdef.NOTICE, mozdef.OTHER, 'New queue.', details=log_details, tags=['queue'], ) queue = Queue(name=q_name, owner=owner) # add the queue bindings to the db. bindings = self.get_queue_bindings(all_bindings, queue.name) for binding in bindings: db_binding = Binding.query.filter( Binding.exchange == binding["source"], Binding.routing_key == binding["routing_key"], Binding.queue_name == queue.name ).first() if not db_binding: # need to create the binding in the DB binding = Binding(exchange=binding["source"], routing_key=binding["routing_key"], queue_name=queue.name) db_session.add(binding) # Update the saved queue size. queue.size = q_size queue.durable = q_durable db_session.add(queue) db_session.commit() return queue
def update_queue_information(self, queue_data, all_bindings): if 'messages' not in queue_data: # FIXME: We should do something here, probably delete the queue, # as it's in a weird state. More investigation is required. # See bug 1066338. return None q_size, q_name, q_durable = (queue_data['messages'], queue_data['name'], queue_data['durable']) queue = Queue.query.filter(Queue.name == q_name).first() # If the queue doesn't exist in the db, create it. if queue is None: log_details = { 'queuename': q_name, 'queuesize': q_size, 'queuedurable': q_durable, } m = re.match('queue/([^/]+)/', q_name) if not m: log_details['valid'] = False owner = None elif (config.reserved_users_regex and re.match(config.reserved_users_regex, m.group(1))): # Ignore this queue entirely as we will see it again on the # next iteration. return None else: log_details['valid'] = True owner_name = m.group(1) owner = RabbitMQAccount.query.filter( RabbitMQAccount.username == owner_name).first() log_details['ownername'] = owner_name log_details['newowner'] = not owner # If the queue belongs to a pulse user that isn't in the # pulseguardian database, add the user to the DB, owned by an # admin. if owner is None: # RabbitMQAccount needs at least one owner as well, but # since we have no way of knowing who really owns it, find # the first admin, and set it to that. user = User.query.filter(User.admin == True).first() owner = RabbitMQAccount.new_user(owner_name, owners=user) mozdef.log( mozdef.NOTICE, mozdef.OTHER, 'New queue.', details=log_details, tags=['queue'], ) queue = Queue(name=q_name, owner=owner) # add the queue bindings to the db. bindings = self.get_queue_bindings(all_bindings, queue.name) for binding in bindings: db_binding = Binding.query.filter( Binding.exchange == binding["source"], Binding.routing_key == binding["routing_key"], Binding.queue_name == queue.name).first() if not db_binding: # need to create the binding in the DB binding = Binding(exchange=binding["source"], routing_key=binding["routing_key"], queue_name=queue.name) db_session.add(binding) # Update the saved queue size. queue.size = q_size queue.durable = q_durable db_session.add(queue) db_session.commit() return queue
def update_queue_information(self, queue_data, all_bindings): if not 'messages' in queue_data: # FIXME: We should do something here, probably delete the queue, # as it's in a weird state. More investigation is required. # See bug 1066338. return None q_size, q_name, q_durable = (queue_data['messages'], queue_data['name'], queue_data['durable']) queue = Queue.query.filter(Queue.name == q_name).first() # If the queue doesn't exist in the db, create it. if queue is None: m = re.match('queue/([^/]+)/', q_name) logging.debug("New queue '{0}' encountered. " "Adding to the database.".format(q_name)) if m: owner_name = m.group(1) owner = PulseUser.query.filter( PulseUser.username == owner_name).first() # If the queue was created by a user that isn't in the # pulseguardian database, skip the queue. if owner is None: logging.info( "Queue '{0}' owner, {1}, isn't in the db. Creating " "the user.".format(q_name, owner_name)) # PulseUser needs at least one owner as well, but since # we have no way of knowing who really owns it, find the # first admin, and set it to that. user = User.query.filter(User.admin == True).first() owner = PulseUser.new_user(owner_name, owners=user) # Assign the user to the queue. logging.debug("Assigning queue '{0}' to user " "{1}.".format(q_name, owner)) else: logging.warn("'{0}' is not a standard queue name.".format( q_name)) owner = None queue = Queue(name=q_name, owner=owner) # add the queue bindings to the db. bindings = self.get_queue_bindings(all_bindings, queue.name) for binding in bindings: db_binding = Binding.query.filter( Binding.exchange == binding["source"], Binding.routing_key == binding["routing_key"], Binding.queue_name == queue.name ).first() if not db_binding: # need to create the binding in the DB binding = Binding(exchange=binding["source"], routing_key=binding["routing_key"], queue_name=queue.name) db_session.add(binding) # Update the saved queue size. queue.size = q_size queue.durable = q_durable db_session.add(queue) db_session.commit() return queue