def Handle(self, args, token=None): end = args.count or sys.maxsize keywords = compatibility.ShlexSplit(args.query) api_clients = [] if data_store.RelationalDBReadEnabled(): index = client_index.ClientIndex() # LookupClients returns a sorted list of client ids. clients = index.LookupClients(keywords)[args.offset:args.offset + end] client_infos = data_store.REL_DB.MultiReadClientFullInfo(clients) for client_info in itervalues(client_infos): api_clients.append(ApiClient().InitFromClientInfo(client_info)) else: index = client_index.CreateClientIndex(token=token) result_urns = sorted( index.LookupClients(keywords))[args.offset:args.offset + end] result_set = aff4.FACTORY.MultiOpen(result_urns, token=token) for child in sorted(result_set): api_clients.append(ApiClient().InitFromAff4Object(child)) UpdateClientsFromFleetspeak(api_clients) return ApiSearchClientsResult(items=api_clients)
def Handle(self, args, context=None): end = args.count or db.MAX_COUNT keywords = compatibility.ShlexSplit(args.query) api_clients = [] index = client_index.ClientIndex() # LookupClients returns a sorted list of client ids. clients = index.LookupClients(keywords)[args.offset:args.offset + end] client_infos = data_store.REL_DB.MultiReadClientFullInfo(clients) for client_info in client_infos.values(): api_clients.append(ApiClient().InitFromClientInfo(client_info)) UpdateClientsFromFleetspeak(api_clients) return ApiSearchClientsResult(items=api_clients)
def Handle(self, args, context=None): if args.count: end = args.offset + args.count # Read <count> clients ahead in case some of them fail to open / verify. batch_size = end + args.count else: end = db.MAX_COUNT batch_size = end keywords = compatibility.ShlexSplit(args.query) api_clients = [] index = client_index.ClientIndex() # TODO(amoser): We could move the label verification into the # database making this method more efficient. Label restrictions # should be on small subsets though so this might not be worth # it. all_client_ids = set() for label in self.allow_labels: label_filter = ["label:" + label] + keywords all_client_ids.update(index.LookupClients(label_filter)) index = 0 for cid_batch in collection.Batch(sorted(all_client_ids), batch_size): client_infos = data_store.REL_DB.MultiReadClientFullInfo(cid_batch) for _, client_info in sorted(client_infos.items()): if not self._VerifyLabels(client_info.labels): continue if index >= args.offset and index < end: api_clients.append( ApiClient().InitFromClientInfo(client_info)) index += 1 if index >= end: UpdateClientsFromFleetspeak(api_clients) return ApiSearchClientsResult(items=api_clients) UpdateClientsFromFleetspeak(api_clients) return ApiSearchClientsResult(items=api_clients)
def testQuoted(self): string = "'Лев Николаевич Толсто́й' 'Сергей Александрович Есенин'" parts = ["Лев Николаевич Толсто́й", "Сергей Александрович Есенин"] self.assertEqual(compatibility.ShlexSplit(string), parts)
def testUnicode(self): self.assertEqual(compatibility.ShlexSplit("żółć jaźń gąskę"), ["żółć", "jaźń", "gąskę"])
def testNormal(self): self.assertEqual(compatibility.ShlexSplit("foo bar baz"), ["foo", "bar", "baz"])
def Handle(self, args, token=None): if args.count: end = args.offset + args.count # Read <count> clients ahead in case some of them fail to open / verify. batch_size = end + args.count else: end = sys.maxsize batch_size = end keywords = compatibility.ShlexSplit(args.query) api_clients = [] if data_store.RelationalDBReadEnabled(): index = client_index.ClientIndex() # TODO(amoser): We could move the label verification into the # database making this method more efficient. Label restrictions # should be on small subsets though so this might not be worth # it. all_client_ids = set() for label in self.labels_whitelist: label_filter = ["label:" + label] + keywords all_client_ids.update(index.LookupClients(label_filter)) index = 0 for cid_batch in collection.Batch(sorted(all_client_ids), batch_size): client_infos = data_store.REL_DB.MultiReadClientFullInfo( cid_batch) for _, client_info in sorted(iteritems(client_infos)): if not self._VerifyLabels(client_info.labels): continue if index >= args.offset and index < end: api_clients.append( ApiClient().InitFromClientInfo(client_info)) index += 1 if index >= end: UpdateClientsFromFleetspeak(api_clients) return ApiSearchClientsResult(items=api_clients) else: index = client_index.CreateClientIndex(token=token) all_urns = set() for label in self.labels_whitelist: label_filter = ["label:" + label] + keywords all_urns.update(index.LookupClients(label_filter)) all_objs = aff4.FACTORY.MultiOpen(all_urns, aff4_type=aff4_grr.VFSGRRClient, token=token) index = 0 for client_obj in sorted(all_objs): if not self._CheckClientLabels(client_obj): continue if index >= args.offset and index < end: api_clients.append( ApiClient().InitFromAff4Object(client_obj)) index += 1 if index >= end: break UpdateClientsFromFleetspeak(api_clients) return ApiSearchClientsResult(items=api_clients)