diff --git a/api/src/backend/api/v1/views.py b/api/src/backend/api/v1/views.py index 8b0fbe0af0..94f89e531f 100644 --- a/api/src/backend/api/v1/views.py +++ b/api/src/backend/api/v1/views.py @@ -1246,10 +1246,10 @@ class ScanViewSet(BaseRLSViewSet): ) return Response(data=read_serializer.data, status=status.HTTP_200_OK) - def _get_running_task_response(self, scan_instance): + def _get_task_status(self, scan_instance): """ If the scan or its report-generation task is still executing, - return a 202 Response with the task payload and Content-Location. + return an `HTTP 202 Accepted` response with the task payload and Content-Location. """ task = None @@ -1360,7 +1360,7 @@ class ScanViewSet(BaseRLSViewSet): def report(self, request, pk=None): scan = self.get_object() # Check for executing tasks - running_resp = self._get_running_task_response(scan) + running_resp = self._get_task_status(scan) if running_resp: return running_resp @@ -1398,7 +1398,7 @@ class ScanViewSet(BaseRLSViewSet): status=status.HTTP_404_NOT_FOUND, ) - running_resp = self._get_running_task_response(scan) + running_resp = self._get_task_status(scan) if running_resp: return running_resp diff --git a/api/src/backend/tasks/jobs/export.py b/api/src/backend/tasks/jobs/export.py index 0a86df0162..07e43a63ab 100644 --- a/api/src/backend/tasks/jobs/export.py +++ b/api/src/backend/tasks/jobs/export.py @@ -210,7 +210,7 @@ def _upload_to_s3(tenant_id: str, zip_path: str, scan_id: str) -> str: def _generate_output_directory( output_directory, prowler_provider: object, tenant_id: str, scan_id: str -) -> str: +) -> tuple[str, str]: """ Generate a file system path for the output directory of a prowler scan. @@ -235,7 +235,8 @@ def _generate_output_directory( Example: >>> _generate_output_directory("/tmp", "aws", "tenant-1234", "scan-5678") - '/tmp/tenant-1234/aws/scan-5678/prowler-output-2023-02-15T12:34:56' + '/tmp/tenant-1234/aws/scan-5678/prowler-output-2023-02-15T12:34:56', + '/tmp/tenant-1234/aws/scan-5678/compliance/prowler-output-2023-02-15T12:34:56' """ path = ( f"{output_directory}/{tenant_id}/{scan_id}/prowler-output-" @@ -243,42 +244,10 @@ def _generate_output_directory( ) os.makedirs("/".join(path.split("/")[:-1]), exist_ok=True) - return path - - -def _generate_compliance_output_directory( - output_directory, prowler_provider: object, tenant_id: str, scan_id: str -) -> str: - """ - Generate a file system path for the compliance output directory of a prowler scan. - - This function constructs the compliance-specific output directory path by combining - a base temporary output directory, the tenant ID, the scan ID, and details about - the prowler provider along with a timestamp. The resulting path is used to store - compliance-related output files generated during a prowler scan. - - Note: - This function depends on one external variable: - - `output_file_timestamp`: A timestamp (as a string) used to uniquely identify the output. - - Args: - output_directory (str): The base output directory. - prowler_provider (object): An identifier or descriptor for the prowler provider. - Typically, this is a string indicating the provider (e.g., "aws"). - tenant_id (str): The unique identifier for the tenant. - scan_id (str): The unique identifier for the scan. - - Returns: - str: The constructed file system path for the prowler compliance output directory. - - Example: - >>> _generate_compliance_output_directory("/tmp", "aws", "tenant-1234", "scan-5678") - '/tmp/tenant-1234/scan-5678/compliance/prowler-output-aws-2023-02-15T12:34:56' - """ - path = ( + compliance_path = ( f"{output_directory}/{tenant_id}/{scan_id}/compliance/prowler-output-" f"{prowler_provider}-{output_file_timestamp}" ) - os.makedirs("/".join(path.split("/")[:-1]), exist_ok=True) + os.makedirs("/".join(compliance_path.split("/")[:-1]), exist_ok=True) - return path + return path, compliance_path diff --git a/api/src/backend/tasks/tasks.py b/api/src/backend/tasks/tasks.py index 4da28b06e9..8ef5b41af5 100644 --- a/api/src/backend/tasks/tasks.py +++ b/api/src/backend/tasks/tasks.py @@ -13,7 +13,6 @@ from tasks.jobs.export import ( COMPLIANCE_CLASS_MAP, OUTPUT_FORMATS_MAPPING, _compress_output_files, - _generate_compliance_output_directory, _generate_output_directory, _upload_to_s3, ) @@ -263,10 +262,7 @@ def generate_outputs(scan_id: str, provider_id: str, tenant_id: str): frameworks_bulk = Compliance.get_bulk(provider_type) frameworks_avail = get_available_compliance_frameworks(provider_type) - out_dir = _generate_output_directory( - DJANGO_TMP_OUTPUT_DIRECTORY, provider_uid, tenant_id, scan_id - ) - comp_dir = _generate_compliance_output_directory( + out_dir, comp_dir = _generate_output_directory( DJANGO_TMP_OUTPUT_DIRECTORY, provider_uid, tenant_id, scan_id )