Skip to content

Commit

Permalink
Fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchop committed Apr 3, 2024
1 parent 50a3b1a commit 99075d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
19 changes: 12 additions & 7 deletions dftimewolf/lib/collectors/grr_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import datetime
import os
import re
import shutil
import time
import zipfile
from typing import List, Optional, Tuple, Type
Expand Down Expand Up @@ -318,9 +317,9 @@ def _CheckSkippedFlows(self) -> None:
client_id, flow_id, self.reason
))

def _DownloadArchive(self, flow, flow_output_dir):
output_file_path = os.path.join(self.output_path, f"{flow.flow_id}.zip")
file_archive = flow.GetFilesArchive()
def _DownloadArchive(self, grr_flow, flow_output_dir):
output_file_path = os.path.join(self.output_path, f"{grr_flow.flow_id}.zip")
file_archive = grr_flow.GetFilesArchive()
file_archive.WriteToFile(output_file_path)
with zipfile.ZipFile(output_file_path) as archive:
archive.extractall(path=flow_output_dir)
Expand All @@ -342,10 +341,14 @@ def _DownloadBlobs(self, client, pathspecs, flow_output_dir):
with open(os.path.join(base_dir, filename), "wb") as out:
f.GetBlobWithOffset(0).WriteToStream(out)
except grr_errors.ResourceNotFoundError as e:
self.logger.warning(f"Failed to download blob {filename} from {vfspath}: {e}")
self.logger.warning(
f"Failed to download blob {filename} from {vfspath}: {e}"
)

def _DownloadTimeline(self, flow, flow_output_dir):
final_bodyfile_path = os.path.join(flow_output_dir, f"{flow.flow_id}_timeline.body")
final_bodyfile_path = os.path.join(
flow_output_dir, f"{flow.flow_id}_timeline.body"
)
file_archive = flow.GetCollectedTimelineBody()
file_archive.WriteToFile(final_bodyfile_path)

Expand Down Expand Up @@ -379,7 +382,9 @@ def _DownloadFiles(self, client: Client, flow_id: str) -> Optional[str]:
filepath = result.payload.pathspec.path
if result.payload.st_size > self._LARGE_FILE_SIZE_THRESHOLD:
size_gb = result.payload.st_size / 1024 / 1024 / 1024
self.logger.warning(f"Large file detected: {filepath} ({size_gb:.2f} GB)")
self.logger.warning(
f"Large file detected: {filepath} ({size_gb:.2f} GB)"
)
large_files = True
pathspecs.append(result.payload.pathspec)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ build-backend = "poetry.core.masonry.api"

[tool.ruff]
indent-width = 2
line-length = 80
12 changes: 8 additions & 4 deletions tests/lib/collectors/grr_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ def testAwaitFlowGRRError(self, mock_FlowGet):
@mock.patch("grr_api_client.flow.FlowBase.ListResults")
def testDownloadArtifactFilesForFlow(
self,
mock_ListResults,
unused_mock_ListResults,
mock_Get,
mock_GetFilesArchive,
mock_ZipFile,
mock_makedirs,
mock_isdir,
mock_remove,
unused_mock_remove,
):
"""Test if results are downloaded & unzipped in the correct directories."""
# Change output_path to something constant so we can easily assert
Expand All @@ -156,7 +156,9 @@ def testDownloadArtifactFilesForFlow(
mock_GetFilesArchive.assert_called_once()
mock_ZipFile.assert_called_once_with('/tmp/random/F:12345.zip')
mock_isdir.assert_called_once_with('/tmp/random/tomchop/F:12345')
mock_makedirs.assert_called_once_with("/tmp/random/tomchop/F:12345", exist_ok=True)
mock_makedirs.assert_called_once_with(
"/tmp/random/tomchop/F:12345", exist_ok=True
)
mock_remove.assert_called_once_with('/tmp/random/F:12345.zip')

@mock.patch('os.remove')
Expand Down Expand Up @@ -190,7 +192,9 @@ def testDownloadTimelineBodyForFlow(
mock_GetFilesArchive.assert_not_called()
mock_ZipFile.assert_not_called()
mock_isdir.assert_called_once_with('/tmp/random/tomchop/F:12345')
mock_makedirs.assert_called_once_with("/tmp/random/tomchop/F:12345", exist_ok=True)
mock_makedirs.assert_called_once_with(
"/tmp/random/tomchop/F:12345", exist_ok=True
)


class GRRArtifactCollectorTest(unittest.TestCase):
Expand Down

0 comments on commit 99075d4

Please sign in to comment.