Skip to content

Commit e674926

Browse files
committed
Enhance project creation and Docker integration features
- Updated ProjectViewSet to handle Docker Compose project names and container targets, improving scan configuration generation. - Refactored target details handling to accommodate new container data structure and added warnings for malformed input. - Enhanced frontend ProjectList component with a new UI for selecting host paths for multiple containers, including a button for automatic path discovery. - Improved state management for selected container paths and loading indicators during path fetching operations.
1 parent 9b8683b commit e674926

2 files changed

Lines changed: 213 additions & 109 deletions

File tree

backend/apps/core/views.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,55 @@ def perform_create(self, serializer):
6363
project = serializer.save(owner=self.request.user)
6464
ProjectMembership.objects.create(user=self.request.user, project=project, role=ProjectMembership.Role.MANAGER)
6565

66-
# Prepare target_details for the default ScanConfiguration
67-
target_details = {}
68-
if project.codebase_path_or_url:
69-
value = project.codebase_path_or_url
70-
if value.startswith(('http://', 'https://', 'git@', 'ssh://')):
71-
target_details['codebase_git'] = value
72-
elif value.startswith('/'): # Basic check for an absolute local path
73-
target_details['codebase_local_path'] = value
74-
else: # Fallback or could be a relative path if you support those
75-
# For now, let's assume it's a local path if not clearly a URL
76-
target_details['codebase_local_path'] = value
77-
print(f"Warning: codebase_path_or_url '{value}' for project {project.id} is not a clear URL or absolute path, treating as local path.")
66+
target_details_for_scan_config = {}
67+
68+
docker_compose_project_name = self.request.data.get('selected_compose_project_name')
69+
container_targets_data = self.request.data.get('container_targets')
70+
71+
if docker_compose_project_name and isinstance(container_targets_data, list):
72+
target_details_for_scan_config['compose_project_name'] = docker_compose_project_name
73+
processed_containers = []
74+
for container_data in container_targets_data:
75+
if isinstance(container_data, dict) and \
76+
all(key in container_data for key in ['id', 'name', 'image', 'host_code_path']):
77+
processed_containers.append({
78+
"id": container_data.get('id'),
79+
"name": container_data.get('name'),
80+
"image": container_data.get('image'),
81+
"host_code_path": container_data.get('host_code_path')
82+
})
83+
else:
84+
print(f"Warning: Malformed container data received for project {project.id}: {container_data}")
85+
target_details_for_scan_config['containers'] = processed_containers
86+
else:
87+
if project.codebase_path_or_url:
88+
value = project.codebase_path_or_url
89+
if value.startswith(('http://', 'https://', 'git@', 'ssh://')):
90+
target_details_for_scan_config['codebase_git'] = value
91+
elif value.startswith('/'):
92+
target_details_for_scan_config['codebase_local_path'] = value
93+
else:
94+
target_details_for_scan_config['codebase_local_path'] = value
95+
print(f"Warning: codebase_path_or_url '{value}' for project {project.id} is not a clear URL or absolute path, treating as local path.")
7896

7997
if project.web_app_url:
80-
target_details['web_url'] = project.web_app_url
98+
target_details_for_scan_config['primary_web_app_url'] = project.web_app_url
99+
100+
has_predefined_targets = bool(
101+
target_details_for_scan_config.get('containers') or \
102+
target_details_for_scan_config.get('codebase_git') or \
103+
target_details_for_scan_config.get('codebase_local_path') or \
104+
target_details_for_scan_config.get('primary_web_app_url')
105+
)
81106

82-
# Automatically create a default scan configuration for the new project.
83-
# This default configuration can then be customized by the user.
84107
ScanConfiguration.objects.create(
85108
project=project,
86109
name=f"Default Configuration for {project.name}",
87-
description="Automatically created default scan configuration. Please edit to define targets and tools.",
88-
# If target_details is empty, it will be stored as {} (empty JSON object)
89-
target_details_json=target_details if target_details else None,
90-
tool_configurations_json=None, # Starts with no specific tool configs
110+
description="Automatically created default scan configuration. Please review and customize targets and tools.",
111+
target_details_json=target_details_for_scan_config if target_details_for_scan_config else None,
112+
tool_configurations_json=None,
91113
created_by=self.request.user,
92-
has_predefined_targets=bool(target_details) # True if any target was set
114+
has_predefined_targets=has_predefined_targets
93115
)
94116

95117
def get_queryset(self):

0 commit comments

Comments
 (0)