Skip to content

Now using supported files SDK endpoint for file globs #60

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

Merged
merged 2 commits into from
Mar 7, 2025
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
'GitPython',
'packaging',
'python-dotenv',
'socket-sdk-python>=2.0.8'
'socket-sdk-python>=2.0.9'
]
readme = "README.md"
description = "Socket Security CLI for CI/CD"
Expand Down
2 changes: 1 addition & 1 deletion socketsecurity/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__author__ = 'socket.dev'
__version__ = '2.0.10'
__version__ = '2.0.11'
93 changes: 65 additions & 28 deletions socketsecurity/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def create_sbom_output(self, diff: Diff) -> dict:
log.error(result.get("message", "No error message provided"))
return {}

@staticmethod
def find_files(path: str) -> List[str]:
def find_files(self, path: str) -> List[str]:
"""
Finds supported manifest files in the given path.

Expand All @@ -138,10 +137,19 @@ def find_files(path: str) -> List[str]:
start_time = time.time()
files = set()

for ecosystem in socket_globs:
patterns = socket_globs[ecosystem]
for file_name in patterns:
pattern = Core.to_case_insensitive_regex(patterns[file_name]["pattern"])
# Get supported patterns from the API
try:
patterns = self.get_supported_patterns()
except Exception as e:
log.error(f"Error getting supported patterns from API: {e}")
log.warning("Falling back to local patterns")
from .utils import socket_globs as fallback_patterns
patterns = fallback_patterns

for ecosystem in patterns:
ecosystem_patterns = patterns[ecosystem]
for file_name in ecosystem_patterns:
pattern = Core.to_case_insensitive_regex(ecosystem_patterns[file_name]["pattern"])
file_path = f"{path}/**/{pattern}"
#log.debug(f"Globbing {file_path}")
glob_start = time.time()
Expand All @@ -164,6 +172,57 @@ def find_files(path: str) -> List[str]:
log.debug(f"{len(files_list)} Files found ({total_time:.2f}s): {', '.join(files_list)}")
return list(files)

def get_supported_patterns(self) -> Dict:
"""
Gets supported file patterns from the Socket API.

Returns:
Dictionary of supported file patterns with 'general' key removed
"""
response = self.sdk.report.supported()
if not response:
log.error("Failed to get supported patterns from API")
# Import the old patterns as fallback
from .utils import socket_globs
return socket_globs

# Remove the 'general' key if it exists
if 'general' in response:
response.pop('general')

# The response is already in the format we need
return response

def has_manifest_files(self, files: list) -> bool:
"""
Checks if any files in the list are supported manifest files.

Args:
files: List of file paths to check

Returns:
True if any files match manifest patterns, False otherwise
"""
# Get supported patterns
try:
patterns = self.get_supported_patterns()
except Exception as e:
log.error(f"Error getting supported patterns from API: {e}")
log.warning("Falling back to local patterns")
from .utils import socket_globs as fallback_patterns
patterns = fallback_patterns

for ecosystem in patterns:
ecosystem_patterns = patterns[ecosystem]
for file_name in ecosystem_patterns:
pattern_str = ecosystem_patterns[file_name]["pattern"]
for file in files:
if "\\" in file:
file = file.replace("\\", "/")
if PurePath(file).match(pattern_str):
return True
return False

@staticmethod
def to_case_insensitive_regex(input_string: str) -> str:
"""
Expand Down Expand Up @@ -740,28 +799,6 @@ def save_file(file_name: str, content: str) -> None:
log.error(f"Failed to save file {file_name}: {e}")
raise

@staticmethod
def has_manifest_files(files: list) -> bool:
"""
Checks if any files in the list are supported manifest files.

Args:
files: List of file paths to check

Returns:
True if any files match manifest patterns, False otherwise
"""
for ecosystem in socket_globs:
patterns = socket_globs[ecosystem]
for file_name in patterns:
pattern = patterns[file_name]["pattern"]
for file in files:
if "\\" in file:
file = file.replace("\\", "/")
if PurePath(file).match(pattern):
return True
return False

@staticmethod
def get_capabilities_for_added_packages(added_packages: Dict[str, Package]) -> Dict[str, List[str]]:
"""
Expand Down
Loading