forked from checkmarx-ts/CxUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckExclusionsFoldersFiles.ps1
330 lines (321 loc) · 12.2 KB
/
CheckExclusionsFoldersFiles.ps1
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
<#
.SYNOPSIS
Powershell script to Find Potential Folder/Files exclusions over CxSRC
.DESCRIPTION
For purposes of Project configuration tuning at large scale for all projects scanned,
I’ve created a Powershell script that goes over the CxSrc folder and find potential
Folders/Files exclusions in order to:
- Reduce LOC
- Reduce Time Scanning
- Reduce FPs rate
.PARAMETER Path
The path to the .
.PARAMETER LiteralPath
Specifies a path to one or more locations. Unlike Path, the value of
LiteralPath is used exactly as it is typed. No characters are interpreted
as wildcards. If the path includes escape characters, enclose it in single
quotation marks. Single quotation marks tell Windows PowerShell not to
interpret any characters as escape sequences.
#>
Param(
# Checkmarx Server URL (i.e. https://checkmarx.company.com)
[Parameter(
Position = 0,
Mandatory = $true,
HelpMessage = "Checkmarx Server URL (i.e. https://checkmarx.company.com)"
)][string] $cxserver = "http://localhost",
# Checkmarx Username (i.e. [email protected])
[Parameter(
Position = 1,
Mandatory = $true,
HelpMessage = "Checkmarx Username (i.e. [email protected])"
)][string] $cxUsername = "admin@cx",
# Checkmarx Password
[Parameter(
Position = 2,
Mandatory = $true,
HelpMessage = "Checkmarx Password"
)][string] $cxPassword,
# Checkmarx Source Folder (i.e. D:\CxSrc)
[Parameter(
Position = 3,
Mandatory = $true,
HelpMessage = "Checkmarx Source Folder (i.e. D:\CxSrc)"
)][string] $cxSrcFolder,
# Exclusions file generated (i.e. D:\exclusions.json)
[Parameter(
Position = 4,
Mandatory = $false,
HelpMessage = "Exclusions file generated (i.e. D:\exclusions.json)"
)][string] $generatedFile = "exclusions.json"
)
$restEndpoint = $cxserver + "/cxrestapi"
$exclusionFolderList = @(
"test", # Tests
"mock", # Tests
"spec", # Tests
"unit", # Tests
"debug", # Tests
"e2e", #Tests
"androidTest", # Tests (Android)
"build", # Build Folders
"dist", # Build Folders
"deploy", # Build Folders
"venv", # Build Folders (Python)
"maven", # Build Folders
"gradle", # Build Folders (Android)
"target", # Build Folders
"example", # Dead Code
"sample", # Dead Code
"bin", # Non-relevant folders
"gen", # Non-relevant folders
"docs", # Non-relevant folders
"proguard", # Non-relevant folders (Android)
"lint", # Non-relevant folders
"images", # Non-relevant folders
"swagger", # Non-relevant folders (Swagger)
"coverage", # Non-relevant folders
"generated", # Non-relevant folders
".vs", # Non-relevant folders (Visual Studio)
".idea", # Non-relevant folders (IntelliJ IDEA)
".temp", # Non-relevant folders (Temporary)
".tmp", # Non-relevant folders (Temporary)
".grunt", # Non-relevant folders (Grunt)
".cache", # Non-relevant folders (Cache)
".dynamodb", # Non-relevant folders (Dinamo DB)
".fusebox", # Non-relevant folders (Fusebox)
".serverless", # Non-relevant folders (Serverless)
".nyc_output", # Non-relevant folders (NYC)
".git", # Non-relevant folders (Git)
".github", # Non-relevant folders (Github)
".dependabot", # Non-relevant folders (Dependabot)
".semaphore", # Non-relevant folders (Semaphore CI)
".circleci", # Non-relevant folders (Circle CI)
".vscode", # Non-relevant folders (VS Code)
".nuget", # Non-relevant folders (CSharp)
".mvn", # Non-relevant folders (Maven)
".m2", # Non-relevant folders (Maven)
".DS_Store", # Non-relevant folders
".sass-cache", # Non-relevant folders
".gradle", # Non-relevant folders (Android)
"__pycache__", # Non-relevant folders (Python)
".pytest_cache", # Non-relevant folders (Python)
".settings", # Non-relevant folders (CSharp)
"imageset", # Non-relevant folders (IOS)
"xcuserdata", # Non-relevant folders (IOS)
"xcshareddata", # Non-relevant folders (IOS)
"xcassets", # Non-relevant folders (IOS)
"appiconset", # Non-relevant folders (IOS)
"xcodeproj", # Non-relevant folders (IOS)
"framework", # Non-relevant folders (IOS)
"lproj", # Non-relevant folders (IOS)
"__MACOSX", # Non-relevant folders (IOS)
"css", # CSS not supported
"react", #3rd Party Libraries (React)
"yui", #3rd Party Libraries
"node_modules", #3rd Party Libraries (Node JS)
"jquery", #3rd Party Libraries (JS)
"angular", #3rd Party Libraries (JS)
"bootstrap", #3rd Party Libraries (JS)
"modernizr", #3rd Party Libraries (JS)
"bower_components", #3rd Party Libraries (Bower)
"jspm_packages", #3rd Party Libraries (JS)
"typings", #3rd Party Libraries (Typescript)
"dojo", #3rd Party Libraries
"package", #3rd Party Libraries (CSharp)
"vendor", #3rd Party Libraries (Golang)
"xjs" #3rd Party Libraries (JS)
)
$exclusionFileList = @(
"min.js", # 3rd Party Libraries (JS)
".spec", # Tests (JS/Typescript/Node JS)
"test", # Tests
"mock" # Tests
)
function getOAuth2Token() {
$body = @{
username = $cxUsername
password = $cxPassword
grant_type = "password"
scope = "sast_rest_api"
client_id = "resource_owner_client"
client_secret = "014DF517-39D1-4453-B7B3-9930C563627C"
}
try {
$response = Invoke-RestMethod -uri "${restEndpoint}/auth/identity/connect/token" -method post -body $body -contenttype 'application/x-www-form-urlencoded'
return $response.token_type + " " + $response.access_token
}
catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "Could not authenticate - Project Name will not be discovered"
return $null
}
}
function getProjectById($id) {
try {
$response = Invoke-RestMethod -uri "${restEndpoint}/projects/${id}" -method get -headers $authHeader
return $response
}
catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "Cannot Get Project ${id}"
return $null
}
}
function getProjectExcludeSettingsById($id) {
try {
$response = Invoke-RestMethod -uri "${restEndpoint}/projects/${id}/sourceCode/excludeSettings" -method get -headers $authHeader
return $response
}
catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "Cannot Get Project ${id} Exclude Settings"
return $null
}
}
$exclusions = @()
$cxSrcFolder = $cxSrcFolder.ToLowerInvariant()
if(Test-Path $cxSrcFolder -PathType Container){
$token = getOAuth2Token
if ($token) {
$authHeader = @{
Authorization = $token
}
}
$children = Get-ChildItem -Path $cxSrcFolder -recurse
$totalCount = $children.Count
Write-Host "`nAnalysing ${totalCount} Files/Folders..."
for ($i = 0; $i -lt $totalCount; $i++) {
$percentage = [math]::Round(($i / $totalCount) * 100, 2)
if([math]::Round($percentage, 0) % 5 -eq 0){
Write-Host "Progress...${i} out of ${totalCount} (${percentage}%)"
Write-Progress -Activity "Analysing ${i} out of ${totalCount} Files/Folders..." -Status "${percentage}% Complete:" -PercentComplete $percentage
}
$child = $children[$i]
$shortName = $child.PSChildName
$fullPath = $child.FullName.ToLowerInvariant()
$projectId = $fullPath.Split("_")[0].Replace("${cxSrcFolder}\", "")
if ($child.PSIsContainer) {
# Is Folder
foreach ($exclusionFolder in $exclusionFolderList) {
if ($shortName -match $exclusionFolder) {
$exclusions += @{
projectId = $projectId
type = "folder"
shortName = $shortName
fullPath = $fullPath
}
break;
}
}
}
else {
# Is File
foreach ($c in $child) {
$shortName = $c.PSChildName
$fullPath = $c.FullName
foreach ($exclusionFile in $exclusionFileList) {
if ($shortName -match $exclusionFile) {
$exclusions += @{
projectId = $projectId
type = "file"
shortName = $shortName
fullPath = $fullPath
}
break;
}
}
}
}
}
### Group Exclusions per Project IDs
$finalExclusions = @()
foreach ($exclusion in $exclusions) {
$projectId = $exclusion.projectId
$shortName = $exclusion.shortName
$fullPath = $exclusion.fullPath
$type = $exclusion.type
$addNew = $true
for ($i = 0; $i -lt $finalExclusions.Count; $i++) {
if ($finalExclusions[$i].projectId -eq $projectId) {
$shortNames = @()
$fullPaths = @()
$proposedExclusions = $finalExclusions[$i].proposedExclusions
if ($type -eq "file") {
$files = $proposedExclusions.files
$shortNames = $files.shortNames
$fullPaths = $files.fullPaths
if ($fullPaths -notcontains $fullPath) {
$finalExclusions[$i].proposedExclusions.files.fullPaths += $fullPath
}
if ($shortNames -notcontains $shortName) {
$finalExclusions[$i].proposedExclusions.files.shortNames += $shortName
}
}
else {
$folders = $proposedExclusions.folders
$shortNames = $folders.shortNames
$fullPaths = $folders.fullPaths
if ($fullPaths -notcontains $fullPath) {
$finalExclusions[$i].proposedExclusions.folders.fullPaths += $fullPath
}
if ($shortNames -notcontains $shortName) {
$finalExclusions[$i].proposedExclusions.folders.shortNames += $shortName
}
}
$addNew = $false
break
}
}
if ($addNew) {
if ($token) {
$project = getProjectById $projectId
if ($project) {
$excludeSettings = getProjectExcludeSettingsById $projectId
}
}
$files = @{
shortNames = @()
fullPaths = @()
}
$folders = @{
shortNames = @()
fullPaths = @()
}
if ($type -eq "file") {
$files = @{
shortNames = @($shortName)
fullPaths = @($fullPath)
}
}
else {
$folders = @{
shortNames = @($shortName)
fullPaths = @($fullPath)
}
}
$finalExclusions += @{
projectId = $projectId
projectName = $project.name
teamId = $project.teamId
currentExclusions = @{
folders = @($excludeSettings.excludeFoldersPattern)
files = @($excludeSettings.excludeFilesPattern)
}
proposedExclusions = @{
files = $files
folders = $folders
}
}
}
}
$finalExclusions = ($finalExclusions | ConvertTo-Json -Depth 99)
$finalExclusions | Out-File $generatedFile
Write-Host "`nPotential exclusions TODO:" $exclusions.Count
Write-Host "File ${generatedFile} was generated !`n"
} else {
Write-Host "Folder ${cxSrcFolder} does not exists"
}