Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added f-strings to collector modules #669

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dftimewolf/lib/collectors/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self,
def Process(self) -> None:
"""Copies a volume and attaches it to the analysis VM."""
for volume in self._FindVolumesToCopy():
print('Volume copy of {0:s} started...'.format(volume.volume_id))
print(f'Volume copy of {volume.volume_id:s} started...')
new_volume = aws_forensics.CreateVolumeCopy(
self.remote_zone,
dst_zone=self.analysis_zone,
Expand Down Expand Up @@ -168,8 +168,8 @@ def SetUp(self,
self.analysis_zone = analysis_zone or remote_zone
self.analysis_profile_name = analysis_profile_name or remote_profile_name

analysis_vm_name = 'aws-forensics-vm-{0:s}'.format(self.incident_id)
print('Your analysis VM will be: {0:s}'.format(analysis_vm_name))
analysis_vm_name = f'aws-forensics-vm-{self.incident_id:s}'
print(f'Your analysis VM will be: {analysis_vm_name:s}')
self.state.StoreContainer(
containers.TicketAttribute(
name=self._ANALYSIS_VM_CONTAINER_ATTRIBUTE_NAME,
Expand Down
2 changes: 1 addition & 1 deletion dftimewolf/lib/collectors/aws_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def Process(self) -> None:
output_file = tempfile.NamedTemporaryFile(
mode='w', delete=False, encoding='utf-8', suffix='.jsonl')
output_path = output_file.name
self.logger.info('Downloading logs to {0:s}'.format(output_path))
self.logger.info(f'Downloading logs to {output_path:s}')

if self._profile_name:
try:
Expand Down
6 changes: 3 additions & 3 deletions dftimewolf/lib/collectors/aws_snapshot_s3_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def SetUp(self,
def PreProcess(self) -> None:
"""Set up for the snapshot copy operation."""
if not self.bucket_exists:
self.logger.info('Creating AWS bucket {0:s}'.format(self.bucket))
self.logger.info(f'Creating AWS bucket {self.bucket:s}')

create_bucket_args = {'Bucket': self.bucket}
# us-east-1 is the default, but throws an error if actually specified.
Expand Down Expand Up @@ -132,8 +132,8 @@ def Process(self, container: containers.AWSSnapshot) -> None:
for h in result['hashes']:
self.state.StoreContainer(containers.AWSS3Object(h))
except ResourceCreationError as exception:
self.ModuleError('Exception during copy operation: {0!s}'.
format(exception), critical=True)
self.ModuleError(
f'Exception during copy operation: {exception!s}', critical=True)

def PostProcess(self) -> None:
"""Clean up afterwards."""
Expand Down
6 changes: 2 additions & 4 deletions dftimewolf/lib/collectors/aws_volume_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@ def Process(self) -> None:
snapshot_ids = []
try:
# Snapshot taking is an asynchronous call, no need for threading
self.logger.info('Taking snapshots of volumes {0:s}'.
format(','.join(volumes)))
self.logger.info(f'Taking snapshots of volumes {",".join(volumes):s}')
for volume in volumes:
response = ec2.create_snapshot(VolumeId=volume)
snapshot_ids.append(response['SnapshotId'])

self.logger.info('Waiting for snapshot completion')
ec2.get_waiter('snapshot_completed').wait(SnapshotIds=snapshot_ids)
self.logger.info('Snapshots complete: {0:s}'.
format(','.join(snapshot_ids)))
self.logger.info(f'Snapshots complete: {",".join(snapshot_ids):s}')
except ec2.exceptions.ClientError as exception:
self.ModuleError('Error encountered snapshotting volumes: {0!s}'.
format(exception), critical=True)
Expand Down
6 changes: 3 additions & 3 deletions dftimewolf/lib/collectors/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self,
def Process(self) -> None:
"""Copies a disk to the analysis account."""
for disk in self._FindDisksToCopy():
self.logger.info('Disk copy of {0:s} started...'.format(disk.name))
self.logger.info(f'Disk copy of {disk.name:s} started...')
new_disk = az_forensics.CreateDiskCopy(
self.analysis_resource_group_name,
disk_name=disk.name,
Expand Down Expand Up @@ -168,8 +168,8 @@ def SetUp(self,
self.analysis_region = analysis_region
self.analysis_profile_name = analysis_profile_name or remote_profile_name

analysis_vm_name = 'azure-forensics-vm-{0:s}'.format(self.incident_id)
print('Your analysis VM will be: {0:s}'.format(analysis_vm_name))
analysis_vm_name = f'azure-forensics-vm-{self.incident_id:s}'
print(f'Your analysis VM will be: {analysis_vm_name:s}')
self.state.StoreContainer(
containers.TicketAttribute(
name=self._ANALYSIS_VM_CONTAINER_ATTRIBUTE_NAME,
Expand Down
2 changes: 1 addition & 1 deletion dftimewolf/lib/collectors/azure_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def Process(self) -> None:
output_file = tempfile.NamedTemporaryFile(
mode='w', delete=False, encoding='utf-8', suffix='.jsonl')
output_path = output_file.name
self.logger.info('Downloading logs to {0:s}'.format(output_path))
self.logger.info(f'Downloading logs to {output_path:s}')

try:
_, credentials = lcf_common.GetCredentials(
Expand Down
14 changes: 7 additions & 7 deletions dftimewolf/lib/collectors/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def SetUp(self, project_name: str, query: str, description: str) -> None:
def Process(self) -> None:
"""Collects data from BigQuery."""
output_file = tempfile.NamedTemporaryFile(
mode="w", delete=False, encoding="utf-8", suffix=".jsonl")
mode='w', delete=False, encoding='utf-8', suffix='.jsonl')
output_path = output_file.name
self.logger.info("Downloading results to {0:s}".format(output_path))
self.logger.info(f'Downloading results to {output_path:s}')

try:
if self._project_name:
Expand All @@ -53,20 +53,20 @@ def Process(self) -> None:
bq_client = bigquery.Client()

records = bq_client.query(self._query).to_dataframe().to_json(
orient="records", lines=True, date_format="iso")
orient='records', lines=True, date_format='iso')
output_file.write(records)

# pytype: disable=module-attr
except google.cloud.exceptions.NotFound as exception:
self.ModuleError(f"Error accessing project: {exception!s}",
self.ModuleError(f'Error accessing project: {exception!s}',
critical=True)
# pytype: enable=module-attr

except (google_auth_exceptions.DefaultCredentialsError) as exception:
self.ModuleError(
"Something is wrong with your gcloud access token or "
"Application Default Credentials. Try running:\n "
"$ gcloud auth application-default login"
'Something is wrong with your gcloud access token or '
'Application Default Credentials. Try running:\n '
'$ gcloud auth application-default login'
)
self.ModuleError(exception, critical=True)

Expand Down
2 changes: 1 addition & 1 deletion dftimewolf/lib/collectors/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def Process(self) -> None:
container = containers.File(os.path.basename(path), path)
file_containers.append(container)
else:
self.logger.warning('Path {0:s} does not exist'.format(path))
self.logger.warning(f'Path {path:s} does not exist')
if not file_containers:
self.ModuleError(
message='No valid paths collected, bailing',
Expand Down
8 changes: 4 additions & 4 deletions dftimewolf/lib/collectors/gcp_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def Process(self) -> None:
output_file = tempfile.NamedTemporaryFile(
mode='w', delete=False, encoding='utf-8', suffix='.jsonl')
output_path = output_file.name
self.logger.info('Downloading logs to {0:s}'.format(output_path))
self.logger.info(f'Downloading logs to {output_path:s}')

try:
if self._project_name:
Expand Down Expand Up @@ -99,8 +99,8 @@ def Process(self) -> None:

if not have_page and retries >= MAX_RETRIES:
self.ModuleError(
'Hit max retries ({0:d}) requesting GCP logs'.format(
MAX_RETRIES) , critical=True)
f'Hit max retries ({MAX_RETRIES:d}) requesting GCP logs',
critical=True)

for entry in page:

Expand All @@ -110,7 +110,7 @@ def Process(self) -> None:

except google_api_exceptions.NotFound as exception:
self.ModuleError(
'Error accessing project: {0!s}'.format(exception), critical=True)
f'Error accessing project: {exception!s}', critical=True)

except google_api_exceptions.InvalidArgument as exception:
self.ModuleError(
Expand Down
2 changes: 1 addition & 1 deletion dftimewolf/lib/collectors/grr_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _WrapGRRRequestWithApproval(
return grr_function(*args, **kwargs)

except grr_errors.AccessForbiddenError as exception:
logger.info('No valid approval found: {0!s}'.format(exception))
logger.info(f'No valid approval found: {exception!s}')
# If approval was already sent, just wait a bit more.
if approval_sent:
logger.info('Approval not yet granted, waiting {0:d}s'.format(
Expand Down
40 changes: 19 additions & 21 deletions dftimewolf/lib/collectors/grr_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _GetClientBySelector(self, selector: str) -> Client:
DFTimewolfError: if no client ID found for selector.
"""
# Search for the selector in GRR
self.logger.info('Searching for client: {0:s}'.format(selector))
self.logger.info(f'Searching for client: {selector:s}')
try:
search_result = self.grr_api.SearchClients(selector)
except grr_errors.UnknownError as exception:
Expand All @@ -160,8 +160,7 @@ def _GetClientBySelector(self, selector: str) -> Client:
result = self._FilterSelectionCriteria(selector, search_result)

if not result:
self.ModuleError('Could not get client for {0:s}'.format(
selector), critical=True)
self.ModuleError(f'Could not get client for {selector:s}', critical=True)

active_clients = self._FilterActiveClients(result)
if len(active_clients) >1:
Expand All @@ -187,7 +186,7 @@ def _GetClientBySelector(self, selector: str) -> Client:
datetime.datetime.utcnow() - last_seen_datetime).total_seconds()
last_seen_minutes = int(round(last_seen_seconds / 60))

self.logger.info('Found active client: {0:s}'.format(client.client_id))
self.logger.info(f'Found active client: {client.client_id:s}')
self.logger.info('Client last seen: {0:s} ({1:d} minutes ago)'.format(
last_seen_datetime.strftime('%Y-%m-%dT%H:%M:%S+0000'),
last_seen_minutes))
Expand Down Expand Up @@ -242,7 +241,7 @@ def _LaunchFlow(self, client: Client, name: str, args: str) -> str:
keepalive_flow = client.CreateFlow(
name='KeepAlive', args=flows_pb2.KeepAliveArgs())
self.logger.info(
'KeepAlive Flow:{0:s} scheduled'.format(keepalive_flow.flow_id))
f'KeepAlive Flow:{keepalive_flow.flow_id:s} scheduled')

return flow_id

Expand All @@ -257,7 +256,7 @@ def _AwaitFlow(self, client: Client, flow_id: str) -> None:
Raises:
DFTimewolfError: If a Flow error was encountered.
"""
self.logger.info('{0:s}: Waiting to finish'.format(flow_id))
self.logger.info(f'{flow_id:s}: Waiting to finish')
if self.skip_offline_clients:
self.logger.info('Client will be skipped if offline.')

Expand All @@ -275,11 +274,10 @@ def _AwaitFlow(self, client: Client, flow_id: str) -> None:
if 'ArtifactNotRegisteredError' in status.context.backtrace:
message = status.context.backtrace.split('\n')[-2]
raise DFTimewolfError(
'{0:s}: FAILED! Message from GRR:\n{1:s}'.format(
flow_id, message))
f'{flow_id:s}: FAILED! Message from GRR:\n{message:s}')

if status.state == flows_pb2.FlowContext.TERMINATED:
self.logger.info('{0:s}: Complete'.format(flow_id))
self.logger.info(f'{flow_id:s}: Complete')
break

time.sleep(self._CHECK_FLOW_INTERVAL_SEC)
Expand Down Expand Up @@ -337,7 +335,7 @@ def _DownloadFiles(self, client: Client, flow_id: str) -> Optional[str]:

if os.path.exists(output_file_path):
self.logger.info(
'{0:s} already exists: Skipping'.format(output_file_path))
f'{output_file_path:s} already exists: Skipping')
return None

if is_timeline_flow:
Expand All @@ -357,7 +355,7 @@ def _DownloadFiles(self, client: Client, flow_id: str) -> Optional[str]:
shutil.copy2(
output_file_path,
os.path.join(client_output_folder,
'{}_timeline.body'.format(flow_id)))
f'{flow_id}_timeline.body'))
else:
with zipfile.ZipFile(output_file_path) as archive:
archive.extractall(path=client_output_folder)
Expand Down Expand Up @@ -483,13 +481,13 @@ def Process(self, container: containers.Host) -> None:
"""
for client in self._FindClients([container.hostname]):
system_type = client.data.os_info.system
self.logger.info('System type: {0:s}'.format(system_type))
self.logger.info(f'System type: {system_type:s}')

# If the list is supplied by the user via a flag, honor that.
artifact_list = []
if self.artifacts:
self.logger.info(
'Artifacts to be collected: {0!s}'.format(self.artifacts))
f'Artifacts to be collected: {self.artifacts!s}')
artifact_list = self.artifacts
else:
default_artifacts = self.artifact_registry.get(system_type, None)
Expand All @@ -501,7 +499,7 @@ def Process(self, container: containers.Host) -> None:

if self.extra_artifacts:
self.logger.info(
'Throwing in an extra {0!s}'.format(self.extra_artifacts))
f'Throwing in an extra {self.extra_artifacts!s}')
artifact_list.extend(self.extra_artifacts)
artifact_list = list(set(artifact_list))

Expand All @@ -520,8 +518,8 @@ def Process(self, container: containers.Host) -> None:
max_file_size=self.max_file_size)
flow_id = self._LaunchFlow(client, 'ArtifactCollectorFlow', flow_args)
if not flow_id:
msg = 'Flow could not be launched on {0:s}.'.format(client.client_id)
msg += '\nArtifactCollectorFlow args: {0!s}'.format(flow_args)
msg = f'Flow could not be launched on {client.client_id:s}.'
msg += f'\nArtifactCollectorFlow args: {flow_args!s}'
self.ModuleError(msg, critical=True)
self._AwaitFlow(client, flow_id)
collected_flow_data = self._DownloadFiles(client, flow_id)
Expand Down Expand Up @@ -624,7 +622,7 @@ def SetUp(self,
if action.lower() in self._ACTIONS:
self.action = self._ACTIONS[action.lower()]
if self.action is None:
self.ModuleError("Invalid action {0!s}".format(action),
self.ModuleError(f"Invalid action {action!s}",
critical=True)
if max_file_size:
self.max_file_size = int(max_file_size)
Expand Down Expand Up @@ -672,8 +670,8 @@ def PreProcess(self) -> None:
message = 'Would fetch 0 files - bailing out instead.'
self.logger.critical(message)
raise DFTimewolfError(message, critical=False)
self.logger.info('Filefinder to collect {0:d} items on each host'.format(
len(self.files)))
self.logger.info(
f'Filefinder to collect {len(self.files):d} items on each host')

def PostProcess(self) -> None:
"""Check if we're skipping any offline clients."""
Expand Down Expand Up @@ -1084,7 +1082,7 @@ def Process(self, container: containers.Host) -> None:
if not root_path:
return
self.logger.info(
'Timeline to start from "{0:s}" items'.format(root_path.decode()))
f'Timeline to start from "{root_path.decode():s}" items')

timeline_args = timeline_pb2.TimelineArgs(root=root_path,)
flow_id = self._LaunchFlow(client, 'TimelineFlow', timeline_args)
Expand Down Expand Up @@ -1112,7 +1110,7 @@ def _DownloadTimeline(self, client: Client, flow_id: str) -> Optional[str]:

if os.path.exists(output_file_path):
self.logger.info(
'{0:s} already exists: Skipping'.format(output_file_path))
f'{output_file_path:s} already exists: Skipping')
return None

flow = client.Flow(flow_id)
Expand Down
20 changes: 9 additions & 11 deletions dftimewolf/lib/collectors/grr_hunt.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def Process(self) -> None:
Raises:
RuntimeError: if no items specified for collection.
"""
self.logger.info('Artifacts to be collected: {0!s}'.format(self.artifacts))
self.logger.info(f'Artifacts to be collected: {self.artifacts!s}')
hunt_args = grr_flows.ArtifactCollectorFlowArgs(
artifact_list=self.artifacts,
use_raw_filesystem_access=self.use_raw_filesystem_access,
Expand Down Expand Up @@ -316,9 +316,9 @@ def Process(self) -> None:
RuntimeError: if no items specified for collection.
"""
self.logger.info(
'Hunt to collect {0:d} items'.format(len(self.file_path_list)))
f'Hunt to collect {len(self.file_path_list):d} items')
self.logger.info(
'Files to be collected: {0!s}'.format(self.file_path_list))
f'Files to be collected: {self.file_path_list!s}')
hunt_action = grr_flows.FileFinderAction(
action_type=grr_flows.FileFinderAction.DOWNLOAD,
download=grr_flows.FileFinderDownloadActionOptions(
Expand Down Expand Up @@ -544,7 +544,7 @@ def _CollectHuntResults(self, hunt: Hunt) -> List[Tuple[str, str]]:

if os.path.exists(output_file_path):
self.logger.info(
'{0:s} already exists: Skipping'.format(output_file_path))
f'{output_file_path:s} already exists: Skipping')
return []

self._WrapGRRRequestWithApproval(
Expand Down Expand Up @@ -624,24 +624,22 @@ def _ExtractHuntResults(self, output_file_path: str) -> List[Tuple[str, str]]:
try:
archive.extract(f, self.output_path)
except KeyError as exception:
self.logger.warning('Extraction error: {0:s}'.format(exception))
self.logger.warning(f'Extraction error: {exception:s}')
return []

except OSError as exception:
msg = 'Error manipulating file {0:s}: {1!s}'.format(
output_file_path, exception)
msg = f'Error manipulating file {output_file_path:s}: {exception!s}'
self.ModuleError(msg, critical=True)
except zipfile.BadZipfile as exception:
msg = 'Bad zipfile {0:s}: {1!s}'.format(
output_file_path, exception)
msg = f'Bad zipfile {output_file_path:s}: {exception!s}'
self.ModuleError(msg, critical=True)

try:
os.remove(output_file_path)
except OSError as exception:
self.logger.warning(
'Output path {0:s} could not be removed: {1:s}'.format(
output_file_path, exception))
f'Output path {output_file_path:s} '
f'could not be removed: {exception:s}')

# Translate GRR client IDs to FQDNs with the information retrieved
# earlier
Expand Down
Loading