def Handle(self, args, token=None): if not args.hunt_id: raise ValueError("hunt_id can't be None") if not args.client_id: raise ValueError("client_id can't be None") if not args.vfs_path: raise ValueError("vfs_path can't be None") if not args.timestamp: raise ValueError("timestamp can't be None") api_vfs.ValidateVfsPath(args.vfs_path) results = implementation.GRRHunt.ResultCollectionForHID( args.hunt_id.ToURN()) expected_aff4_path = args.client_id.ToClientURN().Add(args.vfs_path) # TODO(user): should after_timestamp be strictly less than the desired # timestamp. timestamp = rdfvalue.RDFDatetime(int(args.timestamp) - 1) # If the entry corresponding to a given path is not found within # MAX_RECORDS_TO_CHECK from a given timestamp, we report a 404. for _, item in results.Scan( after_timestamp=timestamp.AsMicroSecondsFromEpoch(), max_records=self.MAX_RECORDS_TO_CHECK): try: # Do not pass the client id we got from the caller. This will # get filled automatically from the hunt results and we check # later that the aff4_path we get is the same as the one that # was requested. aff4_path = export.CollectionItemToAff4Path(item, client_id=None) except export.ItemNotExportableError: continue if aff4_path != expected_aff4_path: continue try: aff4_stream = aff4.FACTORY.Open( aff4_path, aff4_type=aff4.AFF4Stream, token=token) if not aff4_stream.GetContentAge(): break return api_call_handler_base.ApiBinaryStream( "%s_%s" % (args.client_id, utils.SmartStr(aff4_path.Basename())), content_generator=self._GenerateFile(aff4_stream), content_length=len(aff4_stream)) except aff4.InstantiationError: break raise HuntFileNotFoundError("File %s with timestamp %s and client %s " "wasn't found among the results of hunt %s" % (utils.SmartStr(args.vfs_path), utils.SmartStr(args.timestamp), utils.SmartStr(args.client_id), utils.SmartStr(args.hunt_id)))
def _ItemsToUrns(self, items): """Converts collection items to aff4 urns suitable for downloading.""" for item in items: try: yield flow_export.CollectionItemToAff4Path( item, self.client_id) except flow_export.ItemNotExportableError: pass
def testDisplaysErrorMessageIfSingleHuntFileCanNotBeRead(self): hunt = self._CreateHuntWithDownloadedFile() results = hunt.ResultCollection() aff4_path = flow_export.CollectionItemToAff4Path(results[0]) with aff4.FACTORY.Create(aff4_path, aff4_type=aff4.AFF4Volume, token=self.token) as _: pass self.RequestAndGrantHuntApproval(hunt.urn.Basename()) self.Open("/") self.Click("css=a[grrtarget=hunts]") self.Click("css=td:contains('GenericHunt')") self.Click("css=li[heading=Results]") self.Click( "css=grr-results-collection button:has(span.glyphicon-download)") self.WaitUntil(self.IsTextPresent, "Couldn't download the file.")
def testDownloadsSingleHuntFileIfAuthorizationIsPresent(self): hunt = self._CreateHuntWithDownloadedFile() results = hunt.ResultCollection() fd = aff4.FACTORY.Open(flow_export.CollectionItemToAff4Path( results[0]), token=self.token) self.RequestAndGrantHuntApproval(hunt.urn.Basename()) self.Open("/") self.Click("css=a[grrtarget=hunts]") self.Click("css=td:contains('GenericHunt')") self.Click("css=li[heading=Results]") with mock.patch.object(fd.__class__, "Read") as mock_obj: self.Click( "css=grr-results-collection button:has(span.glyphicon-download)" ) self.WaitUntil(lambda: mock_obj.called)