-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.py
371 lines (353 loc) · 10.9 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import argparse
import os
from dataclasses import asdict, dataclass
from typing import List, Optional
from socketsecurity import __version__
from socketdev import INTEGRATION_TYPES, IntegrationType
@dataclass
class CliConfig:
api_token: str
repo: Optional[str]
branch: str = ""
committers: Optional[List[str]] = None
pr_number: str = "0"
commit_message: Optional[str] = None
default_branch: bool = False
target_path: str = "./"
scm: str = "api"
sbom_file: Optional[str] = None
commit_sha: str = ""
generate_license: bool = False
enable_debug: bool = False
allow_unverified: bool = False
enable_json: bool = False
enable_sarif: bool = False
disable_overview: bool = False
disable_security_issue: bool = False
files: str = "[]"
ignore_commit_files: bool = False
disable_blocking: bool = False
integration_type: IntegrationType = "api"
integration_org_slug: Optional[str] = None
pending_head: bool = False
timeout: Optional[int] = 1200
exclude_license_details: bool = False
include_module_folders: bool = False
version: str = __version__
@classmethod
def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
parser = create_argument_parser()
args = parser.parse_args(args_list)
# Get API token from env or args
api_token = os.getenv("SOCKET_SECURITY_API_KEY") or args.api_token
# Strip quotes from commit message if present
commit_message = args.commit_message
if commit_message and commit_message.startswith('"') and commit_message.endswith('"'):
commit_message = commit_message[1:-1]
config_args = {
'api_token': api_token,
'repo': args.repo,
'branch': args.branch,
'committers': args.committers,
'pr_number': args.pr_number,
'commit_message': commit_message,
'default_branch': args.default_branch,
'target_path': os.path.expanduser(args.target_path),
'scm': args.scm,
'sbom_file': args.sbom_file,
'commit_sha': args.commit_sha,
'generate_license': args.generate_license,
'enable_debug': args.enable_debug,
'allow_unverified': args.allow_unverified,
'enable_json': args.enable_json,
'enable_sarif': args.enable_sarif,
'disable_overview': args.disable_overview,
'disable_security_issue': args.disable_security_issue,
'files': args.files,
'ignore_commit_files': args.ignore_commit_files,
'disable_blocking': args.disable_blocking,
'integration_type': args.integration,
'pending_head': args.pending_head,
'timeout': args.timeout,
'exclude_license_details': args.exclude_license_details,
'include_module_folders': args.include_module_folders,
'version': __version__
}
if args.owner:
config_args['integration_org_slug'] = args.owner
return cls(**config_args)
def to_dict(self) -> dict:
return asdict(self)
def create_argument_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="socketcli",
description="The Socket Security CLI will get the head scan for the provided repo from Socket, create a new one, and then report any alerts introduced by the changes. Any new alerts will cause the CLI to exit with a non-Zero exit code (1 for error alerts, 5 for warnings)."
)
# Authentication
auth_group = parser.add_argument_group('Authentication')
auth_group.add_argument(
"--api-token",
dest="api_token",
metavar="<token>",
help="Socket Security API token (can also be set via SOCKET_SECURITY_API_KEY env var)",
required=False
)
auth_group.add_argument(
"--api_token",
dest="api_token",
help=argparse.SUPPRESS
)
# Repository info
repo_group = parser.add_argument_group('Repository')
repo_group.add_argument(
"--repo",
metavar="<owner/repo>",
help="Repository name in owner/repo format",
required=False
)
repo_group.add_argument(
"--integration",
choices=INTEGRATION_TYPES,
metavar="<type>",
help="Integration type",
default="api"
)
repo_group.add_argument(
"--owner",
metavar="<name>",
help="Name of the integration owner, defaults to the socket organization slug",
required=False
)
repo_group.add_argument(
"--branch",
metavar="<name>",
help="Branch name",
default=""
)
repo_group.add_argument(
"--committers",
metavar="<name>",
help="Committer(s) to filter by",
nargs="*"
)
# Pull Request and Commit info
pr_group = parser.add_argument_group('Pull Request and Commit')
pr_group.add_argument(
"--pr-number",
dest="pr_number",
metavar="<number>",
help="Pull request number",
default="0"
)
pr_group.add_argument(
"--pr_number",
dest="pr_number",
help=argparse.SUPPRESS
)
pr_group.add_argument(
"--commit-message",
dest="commit_message",
metavar="<message>",
help="Commit message"
)
pr_group.add_argument(
"--commit_message",
dest="commit_message",
help=argparse.SUPPRESS
)
pr_group.add_argument(
"--commit-sha",
dest="commit_sha",
metavar="<sha>",
default="",
help="Commit SHA"
)
pr_group.add_argument(
"--commit_sha",
dest="commit_sha",
help=argparse.SUPPRESS
)
# Path and File options
path_group = parser.add_argument_group('Path and File')
path_group.add_argument(
"--target-path",
dest="target_path",
metavar="<path>",
default="./",
help="Target path for analysis"
)
path_group.add_argument(
"--target_path",
dest="target_path",
help=argparse.SUPPRESS
)
path_group.add_argument(
"--sbom-file",
dest="sbom_file",
metavar="<path>",
help="SBOM file path"
)
path_group.add_argument(
"--sbom_file",
dest="sbom_file",
help=argparse.SUPPRESS
)
path_group.add_argument(
"--files",
metavar="<json>",
default="[]",
help="Files to analyze (JSON array string)"
)
# Branch and Scan Configuration
config_group = parser.add_argument_group('Branch and Scan Configuration')
config_group.add_argument(
"--default-branch",
dest="default_branch",
action="store_true",
help="Make this branch the default branch"
)
config_group.add_argument(
"--default_branch",
dest="default_branch",
action="store_true",
help=argparse.SUPPRESS
)
config_group.add_argument(
"--pending-head",
dest="pending_head",
action="store_true",
help="If true, the new scan will be set as the branch's head scan"
)
config_group.add_argument(
"--pending_head",
dest="pending_head",
action="store_true",
help=argparse.SUPPRESS
)
# Output Configuration
output_group = parser.add_argument_group('Output Configuration')
output_group.add_argument(
"--generate-license",
dest="generate_license",
action="store_true",
help="Generate license information"
)
output_group.add_argument(
"--generate_license",
dest="generate_license",
action="store_true",
help=argparse.SUPPRESS
)
output_group.add_argument(
"--enable-debug",
dest="enable_debug",
action="store_true",
help="Enable debug logging"
)
output_group.add_argument(
"--enable_debug",
dest="enable_debug",
action="store_true",
help=argparse.SUPPRESS
)
output_group.add_argument(
"--enable-json",
dest="enable_json",
action="store_true",
help="Output in JSON format"
)
output_group.add_argument(
"--enable-sarif",
dest="enable_sarif",
action="store_true",
help="Enable SARIF output of results instead of table or JSON format"
)
output_group.add_argument(
"--disable-overview",
dest="disable_overview",
action="store_true",
help="Disable overview output"
)
output_group.add_argument(
"--disable_overview",
dest="disable_overview",
action="store_true",
help=argparse.SUPPRESS
)
output_group.add_argument(
"--exclude-license-details",
dest="exclude_license_details",
action="store_true",
help="Exclude license details from the diff report (boosts performance for large repos)"
)
# Security Configuration
security_group = parser.add_argument_group('Security Configuration')
security_group.add_argument(
"--allow-unverified",
action="store_true",
help="Allow unverified packages"
)
security_group.add_argument(
"--disable-security-issue",
dest="disable_security_issue",
action="store_true",
help="Disable security issue checks"
)
security_group.add_argument(
"--disable_security_issue",
dest="disable_security_issue",
action="store_true",
help=argparse.SUPPRESS
)
# Advanced Configuration
advanced_group = parser.add_argument_group('Advanced Configuration')
advanced_group.add_argument(
"--ignore-commit-files",
dest="ignore_commit_files",
action="store_true",
help="Ignore commit files"
)
advanced_group.add_argument(
"--ignore_commit_files",
dest="ignore_commit_files",
action="store_true",
help=argparse.SUPPRESS
)
advanced_group.add_argument(
"--disable-blocking",
dest="disable_blocking",
action="store_true",
help="Disable blocking mode"
)
advanced_group.add_argument(
"--disable_blocking",
dest="disable_blocking",
action="store_true",
help=argparse.SUPPRESS
)
advanced_group.add_argument(
"--scm",
metavar="<type>",
default="api",
help="Source control management type"
)
advanced_group.add_argument(
"--timeout",
type=int,
metavar="<seconds>",
help="Timeout in seconds for API requests",
required=False
)
config_group.add_argument(
"--include-module-folders",
dest="include_module_folders",
action="store_true",
default=False,
help="Enabling including module folders like node_modules"
)
parser.add_argument(
'--version',
action='version',
version=f'%(prog)s {__version__}'
)
return parser