Skip to content

Commit 3595c3f

Browse files
authored
Added upload manifests endpoint for reachability (#54)
* Added upload manifests endpoint for reachability * Fixing the lazy loading logic that was breaking scans
1 parent 4d3d213 commit 3595c3f

File tree

5 files changed

+128
-62
lines changed

5 files changed

+128
-62
lines changed

pyproject.toml

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

55
[project]
66
name = "socketdev"
7-
version = "3.0.14"
7+
version = "3.0.16"
88
requires-python = ">= 3.9"
99
dependencies = [
1010
'requests',

socketdev/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from socketdev.analytics import Analytics
2626
from socketdev.alerttypes import AlertTypes
2727
from socketdev.basics import Basics
28+
from socketdev.uploadmanifests import UploadManifests
2829
from socketdev.log import log
2930

3031
__author__ = "socket.dev"
@@ -74,6 +75,7 @@ def __init__(self, token: str, timeout: int = 1200):
7475
self.analytics = Analytics(self.api)
7576
self.alerttypes = AlertTypes(self.api)
7677
self.basics = Basics(self.api)
78+
self.uploadmanifests = UploadManifests(self.api)
7779

7880
@staticmethod
7981
def set_timeout(timeout: int):
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import logging
3+
from typing import List, Optional, Union
4+
from ..utils import Utils
5+
6+
log = logging.getLogger("socketdev")
7+
8+
9+
class UploadManifests:
10+
def __init__(self, api):
11+
self.api = api
12+
13+
def upload_manifest_files(self, org_slug: str, file_paths: List[str], workspace: Optional[str] = None, base_path: Optional[str] = None, base_paths: Optional[List[str]] = None, use_lazy_loading: bool = True) -> str:
14+
"""
15+
Upload manifest files to Socket API and return tarHash.
16+
17+
Args:
18+
org_slug: Organization slug
19+
file_paths: List of manifest file paths to upload
20+
workspace: Base directory path to make paths relative to
21+
base_path: Optional base path to strip from key names for cleaner file organization
22+
base_paths: Optional list of base paths to strip from key names (takes precedence over base_path)
23+
use_lazy_loading: Whether to use lazy file loading (default: True)
24+
25+
Returns:
26+
str: The tarHash from the upload response
27+
28+
Raises:
29+
Exception: If upload fails
30+
"""
31+
# Filter to only existing files
32+
valid_files = [f for f in file_paths if os.path.exists(f) and os.path.isfile(f)]
33+
34+
if not valid_files:
35+
raise Exception("No valid manifest files found to upload")
36+
37+
# Prepare files for upload using the utility function
38+
if use_lazy_loading:
39+
loaded_files = Utils.load_files_for_sending_lazy(
40+
valid_files,
41+
workspace=workspace,
42+
base_path=base_path,
43+
base_paths=base_paths
44+
)
45+
else:
46+
# Fallback to basic file loading if needed
47+
loaded_files = []
48+
for file_path in valid_files:
49+
key = os.path.basename(file_path)
50+
with open(file_path, 'rb') as f:
51+
loaded_files.append((key, (key, f.read())))
52+
53+
# Make the upload request
54+
path = f"orgs/{org_slug}/upload-manifest-files"
55+
response = self.api.do_request(path=path, files=loaded_files, method="POST")
56+
57+
if response.status_code != 200:
58+
raise Exception(f"Upload failed with status {response.status_code}: {response.text}")
59+
60+
result = response.json()
61+
tar_hash = result.get('tarHash')
62+
63+
if not tar_hash:
64+
raise Exception("Server did not return a tarHash")
65+
66+
log.info(f"Successfully uploaded {len(valid_files)} manifest files, tarHash: {tar_hash}")
67+
return tar_hash

socketdev/utils/__init__.py

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, List, Tuple
1+
from typing import Literal, List, Tuple, Optional
22
import logging
33
import os
44
import weakref
@@ -233,7 +233,7 @@ def validate_integration_type(integration_type: str) -> IntegrationType:
233233
return integration_type # type: ignore
234234

235235
@staticmethod
236-
def load_files_for_sending_lazy(files: List[str], workspace: str = None, max_open_files: int = 100, base_path: str = None, base_paths: List[str] = None) -> List[Tuple[str, Tuple[str, LazyFileLoader]]]:
236+
def load_files_for_sending_lazy(files: List[str], workspace: Optional[str] = None, max_open_files: int = 100, base_path: Optional[str] = None, base_paths: Optional[List[str]] = None) -> List[Tuple[str, Tuple[str, LazyFileLoader]]]:
237237
"""
238238
Prepares files for sending to the Socket API using lazy loading.
239239
@@ -268,78 +268,75 @@ def load_files_for_sending_lazy(files: List[str], workspace: str = None, max_ope
268268
# Normalize file path
269269
if "\\" in file_path:
270270
file_path = file_path.replace("\\", "/")
271-
272-
for file_path in files:
273-
# Normalize file path
274-
if "\\" in file_path:
275-
file_path = file_path.replace("\\", "/")
276271

277-
# Skip directories
278-
if os.path.isdir(file_path):
279-
continue
272+
# Skip directories
273+
if os.path.isdir(file_path):
274+
continue
280275

281-
# Handle file path splitting safely
282-
if "/" in file_path:
283-
_, name = file_path.rsplit("/", 1)
284-
else:
285-
name = file_path
276+
# Handle file path splitting safely
277+
if "/" in file_path:
278+
_, name = file_path.rsplit("/", 1)
279+
else:
280+
name = file_path
286281

287-
# Calculate the key name for the form data
288-
key = file_path
289-
path_stripped = False
282+
# Calculate the key name for the form data
283+
key = file_path
284+
path_stripped = False
290285

291-
# If base_paths is provided, try to strip one of the paths from the file path
292-
if base_paths:
293-
for bp in base_paths:
294-
normalized_base_path = bp.rstrip("/") + "/" if not bp.endswith("/") else bp
295-
if key.startswith(normalized_base_path):
296-
key = key[len(normalized_base_path):]
297-
path_stripped = True
298-
break
299-
elif key.startswith(bp.rstrip("/")):
300-
stripped_base = bp.rstrip("/")
301-
if key.startswith(stripped_base + "/") or key == stripped_base:
302-
key = key[len(stripped_base):]
303-
key = key.lstrip("/")
304-
path_stripped = True
305-
break
306-
elif base_path:
307-
normalized_base_path = base_path.rstrip("/") + "/" if not base_path.endswith("/") else base_path
286+
# If base_paths is provided, try to strip one of the paths from the file path
287+
if base_paths:
288+
for bp in base_paths:
289+
normalized_base_path = bp.rstrip("/") + "/" if not bp.endswith("/") else bp
308290
if key.startswith(normalized_base_path):
309291
key = key[len(normalized_base_path):]
310292
path_stripped = True
311-
elif key.startswith(base_path.rstrip("/")):
312-
stripped_base = base_path.rstrip("/")
293+
break
294+
elif key.startswith(bp.rstrip("/")):
295+
stripped_base = bp.rstrip("/")
313296
if key.startswith(stripped_base + "/") or key == stripped_base:
314297
key = key[len(stripped_base):]
315298
key = key.lstrip("/")
316299
path_stripped = True
317-
318-
# If workspace is provided and no base paths matched, fall back to workspace logic
319-
if not path_stripped and workspace and file_path.startswith(workspace):
320-
key = file_path[len(workspace):]
321-
# Remove all leading slashes (for absolute paths)
322-
while key.startswith("/"):
323-
key = key[1:]
300+
break
301+
elif base_path:
302+
normalized_base_path = base_path.rstrip("/") + "/" if not base_path.endswith("/") else base_path
303+
if key.startswith(normalized_base_path):
304+
key = key[len(normalized_base_path):]
324305
path_stripped = True
306+
elif key.startswith(base_path.rstrip("/")):
307+
stripped_base = base_path.rstrip("/")
308+
if key.startswith(stripped_base + "/") or key == stripped_base:
309+
key = key[len(stripped_base):]
310+
key = key.lstrip("/")
311+
path_stripped = True
312+
313+
# If workspace is provided and no base paths matched, fall back to workspace logic
314+
if not path_stripped and workspace and file_path.startswith(workspace):
315+
key = file_path[len(workspace):]
316+
# Remove all leading slashes (for absolute paths)
317+
while key.startswith("/"):
318+
key = key[1:]
319+
path_stripped = True
320+
321+
# Clean up relative path prefixes, but preserve filename dots
322+
while key.startswith("./"):
323+
key = key[2:]
324+
while key.startswith("../"):
325+
key = key[3:]
326+
# Remove any remaining leading slashes (for absolute paths)
327+
while key.startswith("/"):
328+
key = key[1:]
325329

326-
# Clean up relative path prefixes, but preserve filename dots
327-
while key.startswith("./"):
328-
key = key[2:]
329-
while key.startswith("../"):
330-
key = key[3:]
331-
# Remove any remaining leading slashes (for absolute paths)
330+
# Remove Windows drive letter if present (C:/...)
331+
if len(key) > 2 and key[1] == ':' and (key[2] == '/' or key[2] == '\\'):
332+
key = key[2:]
332333
while key.startswith("/"):
333334
key = key[1:]
334335

335-
# Remove Windows drive letter if present (C:/...)
336-
if len(key) > 2 and key[1] == ':' and (key[2] == '/' or key[2] == '\\'):
337-
key = key[2:]
338-
while key.startswith("/"):
339-
key = key[1:]
336+
# Create lazy file loader instead of opening file immediately
337+
lazy_file = LazyFileLoader(file_path, key)
338+
payload = (key, (key, lazy_file))
339+
send_files.append(payload)
340340

341-
# Create lazy file loader instead of opening file immediately
342-
lazy_file = LazyFileLoader(file_path, key)
343-
payload = (key, (key, lazy_file))
344-
send_files.append(payload)
345-
return send_files
341+
log.debug(f"Prepared {len(send_files)} files for lazy loading")
342+
return send_files

socketdev/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.14"
1+
__version__ = "3.0.16"

0 commit comments

Comments
 (0)