Skip to content

Commit 5e3c219

Browse files
committed
just waiting on diff endpoint fix
1 parent 17c7be1 commit 5e3c219

File tree

5 files changed

+138
-46
lines changed

5 files changed

+138
-46
lines changed

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ dependencies = [
1212
'prettytable',
1313
'argparse',
1414
'GitPython',
15-
'packaging'
15+
'packaging',
1616
'python-dotenv', # Add this
17-
'socket-sdk-python @ file:///${PROJECT_ROOT}/../socket-sdk-python'
17+
'socket-sdk-python'
1818
]
1919
readme = "README.md"
2020
description = "Socket Security CLI for CI/CD"
@@ -62,7 +62,6 @@ include = [
6262
version = {attr = "socketsecurity.__version__"}
6363

6464

65-
pythonpath = "."
6665

6766
[tool.coverage.run]
6867
source = ["socketsecurity"]

socketsecurity/config.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from dataclasses import dataclass
44
from typing import List, Optional
55

6+
from socketdev import INTEGRATION_TYPES, IntegrationType
7+
68

79
@dataclass
810
class CliConfig:
911
api_token: str
1012
repo: Optional[str]
1113
branch: str = ""
12-
committer: Optional[List[str]] = None
14+
committers: Optional[List[str]] = None
1315
pr_number: str = "0"
1416
commit_message: Optional[str] = None
1517
default_branch: bool = False
@@ -26,6 +28,8 @@ class CliConfig:
2628
files: str = "[]"
2729
ignore_commit_files: bool = False
2830
disable_blocking: bool = False
31+
integration_type: IntegrationType = "api"
32+
integration_org_slug: Optional[str] = None
2933

3034
@classmethod
3135
def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
@@ -35,28 +39,34 @@ def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
3539
# Get API token from env or args
3640
api_token = os.getenv("SOCKET_SECURITY_API_KEY") or args.api_token
3741

38-
return cls(
39-
api_token=api_token,
40-
repo=args.repo,
41-
branch=args.branch,
42-
committer=args.committer,
43-
pr_number=args.pr_number,
44-
commit_message=args.commit_message,
45-
default_branch=args.default_branch,
46-
target_path=args.target_path,
47-
scm=args.scm,
48-
sbom_file=args.sbom_file,
49-
commit_sha=args.commit_sha,
50-
generate_license=args.generate_license,
51-
enable_debug=args.enable_debug,
52-
allow_unverified=args.allow_unverified,
53-
enable_json=args.enable_json,
54-
disable_overview=args.disable_overview,
55-
disable_security_issue=args.disable_security_issue,
56-
files=args.files,
57-
ignore_commit_files=args.ignore_commit_files,
58-
disable_blocking=args.disable_blocking
59-
)
42+
config_args = {
43+
'api_token': api_token,
44+
'repo': args.repo,
45+
'branch': args.branch,
46+
'committers': args.committers,
47+
'pr_number': args.pr_number,
48+
'commit_message': args.commit_message,
49+
'default_branch': args.default_branch,
50+
'target_path': args.target_path,
51+
'scm': args.scm,
52+
'sbom_file': args.sbom_file,
53+
'commit_sha': args.commit_sha,
54+
'generate_license': args.generate_license,
55+
'enable_debug': args.enable_debug,
56+
'allow_unverified': args.allow_unverified,
57+
'enable_json': args.enable_json,
58+
'disable_overview': args.disable_overview,
59+
'disable_security_issue': args.disable_security_issue,
60+
'files': args.files,
61+
'ignore_commit_files': args.ignore_commit_files,
62+
'disable_blocking': args.disable_blocking,
63+
'integration_type': args.integration,
64+
}
65+
66+
if args.owner:
67+
config_args['integration_org_slug'] = args.owner
68+
69+
return cls(**config_args)
6070

6171
def create_argument_parser() -> argparse.ArgumentParser:
6272
parser = argparse.ArgumentParser(
@@ -76,14 +86,27 @@ def create_argument_parser() -> argparse.ArgumentParser:
7686
required=False
7787
)
7888

89+
parser.add_argument(
90+
"--integration",
91+
choices=INTEGRATION_TYPES,
92+
help="Integration type",
93+
default="api"
94+
)
95+
96+
parser.add_argument(
97+
"--owner",
98+
help="Name of the integration owner, defaults to the socket organization slug",
99+
required=False
100+
)
101+
79102
parser.add_argument(
80103
"--branch",
81104
help="Branch name",
82105
default=""
83106
)
84107

85108
parser.add_argument(
86-
"--committer",
109+
"--committers",
87110
help="Committer(s) to filter by",
88111
nargs="*"
89112
)
@@ -103,7 +126,7 @@ def create_argument_parser() -> argparse.ArgumentParser:
103126
parser.add_argument(
104127
"--default-branch",
105128
action="store_true",
106-
help="Use default branch"
129+
help="Make this branch the default branch"
107130
)
108131

109132
parser.add_argument(

socketsecurity/core/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22
import time
33
from glob import glob
44
from pathlib import PurePath
5-
from typing import Dict, List, Tuple, Optional
5+
from typing import Dict, List, Tuple
66

77
from socketdev import socketdev
88
from socketdev.fullscans import (
9-
DiffArtifacts,
109
DiffArtifact,
11-
SecurityCapabilities,
12-
Alert as SDKAlert, # To distinguish from our Alert class
13-
FullScanDiffReport
10+
DiffArtifacts,
1411
)
15-
1612
from socketdev.org import Organization
1713

1814
from socketsecurity import __version__
15+
from socketsecurity.config import CliConfig
1916
from socketsecurity.core.classes import Diff, FullScan, FullScanParams, Issue, Package, Purl, Report, Repository
2017
from socketsecurity.core.exceptions import (
2118
APIResourceNotFound,
2219
)
2320

24-
25-
2621
from .socket_config import SocketConfig
2722
from .utils import socket_globs
2823

@@ -40,6 +35,7 @@ class Core:
4035

4136
config: SocketConfig
4237
sdk: socketdev
38+
4339
def __init__(self, config: SocketConfig, sdk: socketdev) -> None:
4440
self.config = config
4541
self.sdk = sdk
@@ -79,7 +75,7 @@ def get_sbom_data(self, full_scan_id: str) -> list:
7975
"""
8076

8177
response = self.sdk.fullscans.stream(self.config.org_slug, full_scan_id)
82-
if(response.get("success", False) == False):
78+
if(response.get("success", False) is False):
8379
log.debug(f"Failed to get SBOM data for full-scan {full_scan_id}")
8480
log.debug(response.get("message", "No message"))
8581
return []
@@ -143,7 +139,7 @@ def get_manifest_files(artifact: DiffArtifact, is_head: bool = True) -> str:
143139
def create_sbom_output(self, diff: Diff) -> dict:
144140
try:
145141
result = self.sdk.export.cdx_bom(self.config.org_slug, diff.id)
146-
if(result.get("success", False) == False):
142+
if(result.get("success", False) is False):
147143
log.error(f"Failed to get CycloneDX Output for full-scan {diff.id}")
148144
log.error(result.get("message", "No message"))
149145
return {}
@@ -200,7 +196,7 @@ def create_full_scan(self, files: List[str], params: FullScanParams, workspace:
200196

201197
create_full_start = time.time()
202198
log.debug("Creating new full scan")
203-
params.org_slug = self.config.org_slug
199+
204200
res = self.sdk.fullscans.post(files, params)
205201

206202
# If the response is a string, it's an error message
@@ -334,7 +330,7 @@ def create_new_diff(
334330
except APIResourceNotFound:
335331
head_full_scan_id = None
336332

337-
# Create new scan and get diff report
333+
# Create new scan
338334
new_scan_start = time.time()
339335
new_full_scan = self.create_full_scan(files, params, workspace)
340336

socketsecurity/core/classes.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def __init__(self, **kwargs):
6565
def __str__(self):
6666
return json.dumps(self.__dict__)
6767

68+
def to_dict(self) -> dict:
69+
return {
70+
"supplyChain": self.supplyChain if hasattr(self, "supplyChain") else 0,
71+
"quality": self.quality if hasattr(self, "quality") else 0,
72+
"maintenance": self.maintenance if hasattr(self, "maintenance") else 0,
73+
"license": self.license if hasattr(self, "license") else 0,
74+
"overall": self.overall if hasattr(self, "overall") else 0,
75+
"vulnerability": self.vulnerability if hasattr(self, "vulnerability") else 0
76+
}
77+
6878

6979
class Package:
7080
type: str
@@ -130,6 +140,31 @@ def __init__(self, **kwargs):
130140
def __str__(self):
131141
return json.dumps(self.__dict__)
132142

143+
def to_dict(self) -> dict:
144+
return {
145+
"type": self.type,
146+
"name": self.name,
147+
"version": self.version,
148+
"release": self.release if hasattr(self, "release") else None,
149+
"id": self.id,
150+
"direct": self.direct,
151+
"manifestFiles": self.manifestFiles,
152+
"author": self.author,
153+
"size": self.size,
154+
"score": self.score if hasattr(self, "score") else {},
155+
"scores": self.scores.to_dict() if hasattr(self, "scores") else {},
156+
"alerts": self.alerts,
157+
"error_alerts": self.error_alerts,
158+
"alert_counts": self.alert_counts,
159+
"topLevelAncestors": self.topLevelAncestors,
160+
"url": self.url,
161+
"transitives": self.transitives,
162+
"license": self.license,
163+
"license_text": self.license_text,
164+
"purl": self.purl
165+
}
166+
167+
133168

134169
class Issue:
135170
pkg_type: str
@@ -300,7 +335,7 @@ class FullScanParams:
300335
commit_message: str
301336
commit_hash: str
302337
pull_request: int
303-
committer: str
338+
committers: str
304339
make_default_branch: bool
305340
set_as_pending_head: bool
306341

@@ -340,6 +375,19 @@ def __init__(self, **kwargs):
340375
def __str__(self):
341376
return json.dumps(self.__dict__)
342377

378+
def to_dict(self) -> dict:
379+
return {
380+
"new_packages": [p.to_dict() for p in self.new_packages],
381+
"new_capabilities": self.new_capabilities,
382+
"removed_packages": [p.to_dict() for p in self.removed_packages],
383+
"new_alerts": [alert.__dict__ for alert in self.new_alerts],
384+
"id": self.id,
385+
"sbom": self.sbom if hasattr(self, "sbom") else [],
386+
"packages": {k: v.to_dict() for k, v in self.packages.items()} if hasattr(self, "packages") else {},
387+
"report_url": self.report_url if hasattr(self, "report_url") else None,
388+
"diff_url": self.diff_url if hasattr(self, "diff_url") else None
389+
}
390+
343391

344392
class Purl:
345393
id: str
@@ -387,6 +435,24 @@ def generate_author_data(authors: list, ecosystem: str) -> str:
387435
def __str__(self):
388436
return json.dumps(self.__dict__)
389437

438+
def to_dict(self) -> dict:
439+
return {
440+
"id": self.id,
441+
"name": self.name,
442+
"version": self.version,
443+
"ecosystem": self.ecosystem,
444+
"direct": self.direct,
445+
"author": self.author,
446+
"size": self.size,
447+
"transitives": self.transitives,
448+
"introduced_by": self.introduced_by,
449+
"capabilities": self.capabilities,
450+
"is_new": self.is_new,
451+
"author_url": self.author_url,
452+
"url": self.url,
453+
"purl": self.purl
454+
}
455+
390456

391457
class GithubComment:
392458
url: str

socketsecurity/socketcli.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
from dotenv import load_dotenv
55
from git import InvalidGitRepositoryError, NoSuchPathError
66
from socketdev import socketdev
7+
from socketdev.fullscans import FullScanParams
78

89
from socketsecurity.config import CliConfig
910
from socketsecurity.core import Core
10-
from socketsecurity.core.classes import Diff, FullScanParams
11+
from socketsecurity.core.classes import Diff
1112
from socketsecurity.core.cli_client import CliClient
12-
from socketsecurity.core.socket_config import SocketConfig
1313
from socketsecurity.core.git_interface import Git
1414
from socketsecurity.core.logging import initialize_logging, set_debug_mode
1515
from socketsecurity.core.messages import Messages
1616
from socketsecurity.core.scm_comments import Comments
17+
from socketsecurity.core.socket_config import SocketConfig
1718
from socketsecurity.output import OutputHandler
1819

1920
socket_logger, log = initialize_logging()
@@ -131,15 +132,22 @@ def main_code():
131132
else:
132133
log.debug("Found manifest files or forced scan, proceeding")
133134

135+
org_slug = core.config.org_slug
136+
integration_type = config.integration_type
137+
integration_org_slug = config.integration_org_slug or org_slug
138+
134139
params = FullScanParams(
140+
org_slug=org_slug,
141+
integration_type=integration_type,
142+
integration_org_slug=integration_org_slug,
135143
repo=config.repo,
136144
branch=config.branch,
137145
commit_message=config.commit_message,
138146
commit_hash=config.commit_sha,
139147
pull_request=config.pr_number,
140-
committers=config.committer,
141-
make_default_branch=config.default_branch, # This and
142-
set_as_pending_head=config.default_branch # This are the same, do we need both?
148+
committers=config.committers,
149+
make_default_branch=config.default_branch,
150+
set_as_pending_head=True
143151
)
144152

145153
# Initialize diff

0 commit comments

Comments
 (0)