forked from checkmarx-ts/CxUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-api-example.ps1
76 lines (59 loc) · 2.31 KB
/
rest-api-example.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
<#
.SYNOPSIS
An example of how to call the CxSAST REST API with OAuth2 Authentication.
.DESCRIPTION
Fetches engine server information using CxSAST REST API w/ OAuth2 AuthN method.
.PARAMETER server
The protocol and hostname of the Cx Manager server that hosts the API. examples: "http://localhost", "https://sast.example.com"
.PARAMETER cxUsername
The name of the user to connect to API.
.PARAMETER cxPassword
The password of the user
.EXAMPLE
rest-api-example.ps1 -server "localhost" -cxUsername "user" -cxPassword "password"
#>
param(
[Parameter(Mandatory = $true)][String]$server,
[Parameter(Mandatory = $true)][String]$cxUsername,
[Parameter(Mandatory = $true)][String]$cxPassword
)
# Login to the REST API via OAuth2 flow and return bearer token
function getOAuth2Token($serverRestEndpoint, $cxUsername, $cxPassword){
$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 "Cannot Get OAuth2 Token"
}
return $response.token_type + " " + $response.access_token
}
# Fetch engine information from REST API using bearer token
function getEngineServers($serverRestEndpoint, $token){
$headers = @{
Authorization = $token
}
try {
$response = Invoke-RestMethod -uri "${serverRestEndpoint}sast/engineServers" -method get -headers $headers
return $response
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
throw "Cannot Get Engine Servers"
}
}
$serverRestEndpoint = $server + "/cxrestapi/"
# Login
$token = getOAuth2Token $serverRestEndpoint $cxUsername $cxPassword
# Make API Call
$engineServers = getEngineServers $serverRestEndpoint $token
# Write data to console
$engineServers | ConvertTo-Json