Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/issue 58 #60

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- QualysAsset class: This class defines objects with properties that include all details returned by the QPS API about a host asset, plus optional metadata for function and method use.
- QualysTag class: This class defines objects with properties that include all details returned by the QPS API about a tag, plus optional metadata and parent tag.
- Support using environment variables to automatically override settings.json script-scoped parameters.
- Invoke-QualysTagRestCall: Modify the tagging functions to use this modified version of Invoke-QualysRestCall. The BaseURI for tagging endpoints is different from User Basic auth endpoints and there is nothing to clearly distinguish the two.
- Added the tagging Base URI to the settings.json

### Added

Expand Down
1 change: 0 additions & 1 deletion src/UofIQualys/UofIQualys.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
$Script:Settings = Get-Content -Path "$PSScriptRoot\settings.json" | ConvertFrom-Json

# Check $env:BasicAuthURI and $env:BaseURI and set $script:Settings.BasicAuthURI and $script:Settings.BaseURI from environment variables if present
# This will override the settings.json file if required.
if ($env:QualysSettings) {
$script:Settings= $env:QualysSettings | ConvertFrom-Json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function Add-QualysTagAssignment {
}

try {
Invoke-QualysRestCall @RestSplat
Invoke-QualysTagRestCall @RestSplat
}
catch {
# Dig into the exception to get the Response details.
Expand Down
4 changes: 2 additions & 2 deletions src/UofIQualys/functions/public/Get-QualysAsset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ $BodyAsset = "<ServiceRequest>
$OrigProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'

# Use Invoke-QualysRestCall to make the API request
# Use Invoke-QualysTagRestCall to make the API request
$RestSplat = @{
Method = 'POST'
RelativeURI = 'qps/rest/2.0/search/am/hostasset'
XmlBody = $BodyAsset
Credential = $Credential
}

$ResponseContent = [Xml](Invoke-QualysRestCall @RestSplat)
$ResponseContent = [Xml](Invoke-QualysTagRestCall @RestSplat)

if ($null -eq $ResponseContent.ServiceResponse.data.HostAsset) {
return $null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function Get-QualysAssetInventory {
Credential = $Credential
}

$ResponseHostAdd = [xml](Invoke-QualysRestCall @RestSplat)
$ResponseHostAdd = [xml](Invoke-QualysTagRestCall @RestSplat)

$ProgressPreference = $OrigProgressPreference

Expand Down
4 changes: 2 additions & 2 deletions src/UofIQualys/functions/public/Get-QualysTag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ function Get-QualysTag {
$OrigProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'

# Use Invoke-QualysRestCall to make the API request
# Use Invoke-QualysTagRestCall to make the API request
$RestSplat = @{
Method = 'POST'
RelativeURI = 'qps/rest/2.0/search/am/tag'
Credential = $Credential
XmlBody = $BodyTag
}

$ResponseContent = [xml](Invoke-QualysRestCall @RestSplat)
$ResponseContent = [xml](Invoke-QualysTagRestCall @RestSplat)

if ($null -eq $ResponseContent.ServiceResponse.data.Tag) {
return $null
Expand Down
15 changes: 2 additions & 13 deletions src/UofIQualys/functions/public/Invoke-QualysRestCall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
Method of the REST call Ex: GET
.PARAMETER Body
Body of the REST call as a hashtable
.PARAMETER XmlBody
Body of the REST call as an XML string
.PARAMETER Credential
Optionally used for making REST calls that require Basic Authentication
.EXAMPLE
Expand All @@ -30,8 +28,6 @@ function Invoke-QualysRestCall {
[String]$Method,
[Parameter(Mandatory=$true, ParameterSetName='Body')]
[hashtable]$Body,
[Parameter(Mandatory=$true, ParameterSetName='XmlBody')]
[String]$XmlBody,
[System.Management.Automation.PSCredential]$Credential
)

Expand All @@ -54,15 +50,7 @@ function Invoke-QualysRestCall {
}
Method = $Method
URI = [string]::Empty
}

if($PSCmdlet.ParameterSetName -eq 'XmlBody'){
$IVRSplat['Body'] = $XmlBody
$IVRSplat['Headers'].Add('Content-Type','application/xml')
$IVRSplat['Headers'].Add('Accept','application/xml')
}
else{
$IVRSplat['Body'] = $Body
Body = $Body
}

if($Credential){
Expand All @@ -76,6 +64,7 @@ function Invoke-QualysRestCall {
}
#Retry parameters only available in Powershell 7.1+, so we use a try/catch to retry calls once to compensate for short periods where the Qualys api is unreachable
try{

Invoke-RestMethod @IVRSplat
$Script:APICallCount++
}
Expand Down
86 changes: 86 additions & 0 deletions src/UofIQualys/functions/public/Invoke-QualysTagRestCall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<#
.Synopsis
Makes a REST method call on the given relative URI for Qualys. Utilizes credentials created with New-QualysSession.
.DESCRIPTION
Makes a REST method call on the given relative URI for Qualys. Utilizes credentials created with New-QualysSession.
.PARAMETER RelativeURI
The relativeURI you wish to make a call to. Ex: asset/ip/
.PARAMETER Method
Method of the REST call Ex: GET
.PARAMETER XmlBody
Body of the REST call as an XML string
.PARAMETER Credential
Optionally used for making REST calls that require Basic Authentication
.EXAMPLE
$Body = @{
action = 'list'
echo_request = '1'
}
Invoke-QualysRestCall -RelativeURI asset/ip/ -Method GET -Body $Body
This will return an array of all host assets (IPs) in Qualys
#>
function Invoke-QualysTagRestCall {
[CmdletBinding(DefaultParameterSetName='Body')]
param (
[Parameter(Mandatory=$true)]
[String]$RelativeURI,
[Parameter(Mandatory=$true)]
[String]$Method,
[Parameter(Mandatory=$true, ParameterSetName='XmlBody')]
[String]$XmlBody,
[System.Management.Automation.PSCredential]$Credential
)

begin {
if($null -eq $Script:Session -and $null -eq $Credential){
Write-Verbose -Message 'No Qualys session established. Please provide credentials.'
New-QualysSession
}
}

process {

if ($RelativeURI.StartsWith('/')){
$RelativeURI.Substring(1)
}

$IVRSplat = @{
Headers = @{
'X-Requested-With'='powershell'
}
Method = $Method
URI = [string]::Empty
}

if($PSCmdlet.ParameterSetName -eq 'XmlBody'){
$IVRSplat['Body'] = $XmlBody
$IVRSplat['Headers'].Add('Content-Type','application/xml')
$IVRSplat['Headers'].Add('Accept','application/xml')
}

if($Credential){
$IVRSplat['Uri'] = "$($Script:Settings.TaggingAuthURI)$RelativeURI"
$BasicAuth = ('Basic {0}' -f ([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Credential.UserName,$Credential.GetNetworkCredential().Password)))))
$IVRSplat['Headers'].add('Authorization',$BasicAuth)
}
else{
$IVRSplat['Uri'] = "$($Script:Settings.BaseURI)$RelativeURI"
$IVRSplat.add('WebSession',$Script:Session)
}
#Retry parameters only available in Powershell 7.1+, so we use a try/catch to retry calls once to compensate for short periods where the Qualys api is unreachable
try{

Invoke-RestMethod @IVRSplat
$Script:APICallCount++
}
catch{
Start-Sleep -Seconds 4
Invoke-RestMethod @IVRSplat
$Script:APICallCount++
}
}

end {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function Remove-QualysTagAssignment {
}

try {
Invoke-QualysRestCall @RestSplat
Invoke-QualysTagRestCall @RestSplat
}
catch {
# Dig into the exception to get the Response details.
Expand Down
3 changes: 2 additions & 1 deletion src/UofIQualys/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BaseURI": ["https://qualysguard.qualys.com/api/2.0/fo/"],
"BasicAuthURI": ["https://qualysguard.qualys.com/"]
"BasicAuthURI": ["https://qualysguard.qualys.com/"],
"TaggingAuthURI": ["https://qualysapi.qualys.com/"]
}
Loading