Skip to content

Commit 106766e

Browse files
authored
Since the API Changes to not accept unsupported files changed the tem… (#132)
* Since the API Changes to not accept unsupported files changed the temp scan file to be .socket.facts.json to pass the validator. Also, added the debug flag support to the reachability engine * Fix if there is no supported manifest files
1 parent ec36f08 commit 106766e

File tree

5 files changed

+79
-21
lines changed

5 files changed

+79
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.2.33"
9+
version = "2.2.35"
1010
requires-python = ">= 3.10"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.2.33'
2+
__version__ = '2.2.35'
33
USER_AGENT = f'SocketPythonCLI/{__version__}'

socketsecurity/core/__init__.py

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,13 @@ def empty_head_scan_file() -> List[str]:
442442
Returns:
443443
List containing path to a temporary empty file
444444
"""
445-
# Create a temporary empty file
446-
temp_fd, temp_path = tempfile.mkstemp(suffix='.empty', prefix='socket_baseline_')
445+
# Create a temporary directory and then create our specific filename
446+
temp_dir = tempfile.gettempdir()
447+
temp_path = os.path.join(temp_dir, '.socket.facts.json')
447448

448-
# Close the file descriptor since we just need the path
449-
# The file is already created and empty
450-
os.close(temp_fd)
449+
# Create the empty file
450+
with open(temp_path, 'w') as f:
451+
pass # Creates an empty file
451452

452453
log.debug(f"Created temporary empty file for baseline scan: {temp_path}")
453454
return [temp_path]
@@ -524,18 +525,42 @@ def create_full_scan_with_report_url(
524525
if save_manifest_tar_path and all_files and paths:
525526
self.save_manifest_tar(all_files, save_manifest_tar_path, paths[0])
526527

528+
# If no supported files found, create empty scan
527529
if not all_files:
528-
return diff
529-
530-
try:
531-
# Create new scan
532-
new_scan_start = time.time()
533-
new_full_scan = self.create_full_scan(all_files, params, base_paths=base_paths)
534-
new_scan_end = time.time()
535-
log.info(f"Total time to create new full scan: {new_scan_end - new_scan_start:.2f}")
536-
except APIFailure as e:
537-
log.error(f"Failed to create full scan: {e}")
538-
raise
530+
log.info("No supported manifest files found - creating empty scan")
531+
empty_files = Core.empty_head_scan_file()
532+
try:
533+
# Create new scan
534+
new_scan_start = time.time()
535+
new_full_scan = self.create_full_scan(empty_files, params, base_paths=base_paths)
536+
new_scan_end = time.time()
537+
log.info(f"Total time to create empty full scan: {new_scan_end - new_scan_start:.2f}")
538+
539+
# Clean up the temporary empty file
540+
for temp_file in empty_files:
541+
try:
542+
os.unlink(temp_file)
543+
log.debug(f"Cleaned up temporary file: {temp_file}")
544+
except OSError as e:
545+
log.warning(f"Failed to clean up temporary file {temp_file}: {e}")
546+
except Exception as e:
547+
# Clean up temp files even if scan creation fails
548+
for temp_file in empty_files:
549+
try:
550+
os.unlink(temp_file)
551+
except OSError:
552+
pass
553+
raise e
554+
else:
555+
try:
556+
# Create new scan
557+
new_scan_start = time.time()
558+
new_full_scan = self.create_full_scan(all_files, params, base_paths=base_paths)
559+
new_scan_end = time.time()
560+
log.info(f"Total time to create new full scan: {new_scan_end - new_scan_start:.2f}")
561+
except APIFailure as e:
562+
log.error(f"Failed to create full scan: {e}")
563+
raise
539564

540565
# Construct report URL
541566
base_socket = "https://socket.dev/dashboard/org"
@@ -888,8 +913,11 @@ def create_new_diff(
888913
if save_manifest_tar_path and all_files and paths:
889914
self.save_manifest_tar(all_files, save_manifest_tar_path, paths[0])
890915

916+
# If no supported files found, create empty scan for comparison
917+
scan_files = all_files
891918
if not all_files:
892-
return Diff(id="NO_DIFF_RAN", diff_url="", report_url="")
919+
log.info("No supported manifest files found - creating empty scan for diff comparison")
920+
scan_files = Core.empty_head_scan_file()
893921

894922
try:
895923
# Get head scan ID
@@ -932,19 +960,43 @@ def create_new_diff(
932960
raise e
933961

934962
# Create new scan
963+
temp_files_to_cleanup = []
964+
if not all_files: # We're using empty scan files
965+
temp_files_to_cleanup = scan_files
966+
935967
try:
936968
new_scan_start = time.time()
937-
new_full_scan = self.create_full_scan(all_files, params, base_paths=base_paths)
969+
new_full_scan = self.create_full_scan(scan_files, params, base_paths=base_paths)
938970
new_scan_end = time.time()
939971
log.info(f"Total time to create new full scan: {new_scan_end - new_scan_start:.2f}")
940972
except APIFailure as e:
941973
log.error(f"API Error: {e}")
974+
# Clean up temp files if any
975+
for temp_file in temp_files_to_cleanup:
976+
try:
977+
os.unlink(temp_file)
978+
except OSError:
979+
pass
942980
sys.exit(1)
943981
except Exception as e:
944982
import traceback
945983
log.error(f"Error creating new full scan: {str(e)}")
946984
log.error(f"Stack trace:\n{traceback.format_exc()}")
985+
# Clean up temp files if any
986+
for temp_file in temp_files_to_cleanup:
987+
try:
988+
os.unlink(temp_file)
989+
except OSError:
990+
pass
947991
raise
992+
finally:
993+
# Clean up temporary empty files if they were created
994+
for temp_file in temp_files_to_cleanup:
995+
try:
996+
os.unlink(temp_file)
997+
log.debug(f"Cleaned up temporary file: {temp_file}")
998+
except OSError as e:
999+
log.warning(f"Failed to clean up temporary file {temp_file}: {e}")
9481000

9491001
# Handle diff generation - now we always have both scans
9501002
scans_ready = self.check_full_scans_status(head_full_scan_id, new_full_scan.id)

socketsecurity/core/tools/reachability.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def run_reachability_analysis(
100100
concurrency: Optional[int] = None,
101101
additional_params: Optional[List[str]] = None,
102102
allow_unverified: bool = False,
103+
enable_debug: bool = False,
103104
) -> Dict[str, Any]:
104105
"""
105106
Run reachability analysis.
@@ -123,6 +124,7 @@ def run_reachability_analysis(
123124
concurrency: Concurrency level for analysis (must be >= 1)
124125
additional_params: Additional parameters to pass to coana CLI
125126
allow_unverified: Disable SSL certificate verification (sets NODE_TLS_REJECT_UNAUTHORIZED=0)
127+
enable_debug: Enable debug mode (passes -d flag to coana CLI)
126128
127129
Returns:
128130
Dict containing scan_id and report_path
@@ -173,6 +175,9 @@ def run_reachability_analysis(
173175
if concurrency:
174176
cmd.extend(["--concurrency", str(concurrency)])
175177

178+
if enable_debug:
179+
cmd.append("-d")
180+
176181
# Add any additional parameters provided by the user
177182
if additional_params:
178183
cmd.extend(additional_params)

socketsecurity/socketcli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ def main_code():
288288
version=config.reach_version,
289289
concurrency=config.reach_concurrency,
290290
additional_params=config.reach_additional_params,
291-
allow_unverified=config.allow_unverified
291+
allow_unverified=config.allow_unverified,
292+
enable_debug=config.enable_debug
292293
)
293294

294295
log.info(f"Reachability analysis completed successfully")

0 commit comments

Comments
 (0)