-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestLib.ps1
More file actions
152 lines (127 loc) · 4.86 KB
/
RestLib.ps1
File metadata and controls
152 lines (127 loc) · 4.86 KB
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
$serverRestEndpoint = $server + "/cxrestapi/"
################################################
#debug get Headers
function getHeaders($token){
################################################
return @{
Authorization = $token
"Content-Type" = "application/json;v=1.0"
"Accept" = "application/json"
}
}
################################################
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 "${serverRestEndpoint}auth/identity/connect/token" -method post -body $body -contenttype 'application/x-www-form-urlencoded'
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
throw "Could not authenticate"
}
return $response.token_type + " " + $response.access_token
}
################################################
function getProjects($token){
################################################
$headers = getHeaders $token
try {
Debug "Getting projects"
$response = Invoke-RestMethod -uri "${serverRestEndpoint}projects" -method get -headers $headers
return $response
} catch {
Error "StatusCode: $($_.Exception.Response.StatusCode.value__)"
Log "StatusDescription: $($_.Exception.Response.StatusDescription)"
Log "Message: $($_.ErrorDetails.Message)"
throw "Cannot Get Projects"
}
}
################################################
function getProjectScans($token, $projectId){
################################################
$headers = getHeaders $token
try {
$response = Invoke-RestMethod -uri "${serverRestEndpoint}sast/scans?projectId=${projectId}" -method get -headers $headers
return $response
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
throw "Cannot Get Scans"
}
}
################################################
function getSourcePathByScanId($token, $scanId){
################################################
$headers = getHeaders $token
try {
$scan = Invoke-RestMethod -uri "${serverRestEndpoint}sast/scans/${scanId}" -method get -headers $headers
$cxSourcePath = $cxSourceRoot + "\" + $scan.project.id.ToString() + "_" + $scan.scanState.sourceId
return $cxSourcePath;
} catch {
Error "StatusCode: $($_.Exception.Response.StatusCode.value__)"
Log "StatusDescription: $($_.Exception.Response.StatusDescription)"
Log "Message: $($_.ErrorDetails.Message)"
throw "Cannot Get ScanId"
}
}
################################################
function deleteScan($token, $scanId){
################################################
if ($DryRun) {
Log "`tDryRun Mode:: Deleting Scan with ID $scanId"
return $true
}
#$body = @{
# deleteRunningScans = $true
#}
$body = $body | ConvertTo-Json -Depth 99
#$headers = @{
# Authorization = $token
#}
$headers = getHeaders $token
try {
#$response = Invoke-RestMethod -uri "${serverRestEndpoint}sast/scans/${scanId}" -method Delete -headers $headers -ContentType 'application/json;v=1.0'
Log "${serverRestEndpoint}sast/scans/${scanId}"
$response = Invoke-RestMethod -uri "${serverRestEndpoint}sast/scans/${scanId}" -method Delete -headers $headers
return $true
} catch {
Error "StatusCode: $($_.Exception.Response.StatusCode.value__)"
Log "StatusDescription: $($_.Exception.Response.StatusDescription)"
Log "Message: $($_.ErrorDetails.Message)"
return $false
}
}
################################################
function deleteProject($token, $projectId){
################################################
if ($DryRun) {
Log "`tDryRun Mode:: Deleting Project with ID $projectId"
return $true
}
$body = @{
deleteRunningScans = $true
}
$body = $body | ConvertTo-Json -Depth 99
#$headers = @{
# Authorization = $token
#}
$headers = getHeaders $token
try {
$response = Invoke-RestMethod -uri "${serverRestEndpoint}projects/${projectId}" -method Delete -headers $headers -body $body
#-ContentType 'application/json'
return $true
} catch {
Error "StatusCode: $($_.Exception.Response.StatusCode.value__)"
Log "StatusDescription: $($_.Exception.Response.StatusDescription)"
Log "Message: $($_.ErrorDetails.Message)"
return $false
}
}