Skip to content

Commit

Permalink
Pr support credentials manager (#43)
Browse files Browse the repository at this point in the history
* Add gitignore and some code cleaning

add git ignore
code cleaning on SOAP script

* support credentials manager

Support credentials Manager
Add documentation for Task Scheduler automation
  • Loading branch information
miguelfreitas93 authored Oct 20, 2021
1 parent ce6c175 commit e5cfcfd
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 169 deletions.
1 change: 1 addition & 0 deletions data-retention/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
260 changes: 112 additions & 148 deletions data-retention/CxManagement-SOAP.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,91 +81,84 @@ CxManagement.ps1 -StopRetention -serviceUrl "http://domain.mysite.com/" -usernam
- The duration limit is not an immediate limit, scans are deleted in bulks of X (configured in the database with a default value of 3) and the data retention will only stop at the end of a bulk.
#>
[CmdletBinding()]
Param(

[Parameter(Mandatory = $True, ParameterSetName = "stop")]
[switch]
$StopRetention,

[Parameter(Mandatory = $True, ParameterSetName = "DatesRange")]
[Parameter(Mandatory = $True, ParameterSetName = "RollingDate")]
[Parameter(Mandatory = $True, ParameterSetName = "NumOfScans")]
[switch]
$StartRetention,

[Parameter(Mandatory = $True)]
[String]
$serviceUrl,

[Parameter(Mandatory = $True)]
[String]
$username,

[Parameter(Mandatory = $True)]
[String]
$pass,

[Parameter(Mandatory = $True, ParameterSetName = "NumOfScans")]
[switch]
$ByNumOfScans,

[Parameter(Mandatory = $True, ParameterSetName = "NumOfScans")]
[Int]
$numOfScansToKeep,

[Parameter(Mandatory = $True, ParameterSetName = "DatesRange")]
[switch]
$ByDateRange,

[Parameter(Mandatory = $False, ParameterSetName = "DatesRange")]
[Datetime]
$startDate,

[Parameter(Mandatory = $True, ParameterSetName = "DatesRange")]
[Datetime]
$endDate,

[Parameter(Mandatory = $True, ParameterSetName = "RollingDate")]
[switch]
$ByRollingDate,

[Parameter(Mandatory = $True, ParameterSetName = "RollingDate")]
[int]
$rollingDateRange,

[Parameter(Mandatory = $False, ParameterSetName = "DatesRange")]
[Parameter(Mandatory = $False, ParameterSetName = "NumOfScans")]
[int]
$retentionDurationLimit
Param(
[Parameter(Mandatory = $True, ParameterSetName = "stop")]
[switch]
$StopRetention,

[Parameter(Mandatory = $True, ParameterSetName = "DatesRange")]
[Parameter(Mandatory = $True, ParameterSetName = "RollingDate")]
[Parameter(Mandatory = $True, ParameterSetName = "NumOfScans")]
[switch]
$StartRetention,

[Parameter(Mandatory = $True)]
[String]
$serviceUrl,

[Parameter(Mandatory = $True)]
[String]
$username,

[Parameter(Mandatory = $True)]
[String]
$pass,

[Parameter(Mandatory = $True, ParameterSetName = "NumOfScans")]
[switch]
$ByNumOfScans,

[Parameter(Mandatory = $True, ParameterSetName = "NumOfScans")]
[Int]
$numOfScansToKeep,

[Parameter(Mandatory = $True, ParameterSetName = "DatesRange")]
[switch]
$ByDateRange,

[Parameter(Mandatory = $False, ParameterSetName = "DatesRange")]
[Datetime]
$startDate,

[Parameter(Mandatory = $True, ParameterSetName = "DatesRange")]
[Datetime]
$endDate,

[Parameter(Mandatory = $True, ParameterSetName = "RollingDate")]
[switch]
$ByRollingDate,

[Parameter(Mandatory = $True, ParameterSetName = "RollingDate")]
[int]
$rollingDateRange,

[Parameter(Mandatory = $False, ParameterSetName = "DatesRange")]
[Parameter(Mandatory = $False, ParameterSetName = "NumOfScans")]
[int]
$retentionDurationLimit
)

$serviceUrl = $serviceUrl.TrimStart().TrimEnd()
if (-Not $serviceUrl.EndsWith('/'))
{
if (-Not $serviceUrl.EndsWith('/')){
$serviceUrl = $serviceUrl + '/'
}

$resolverUrlExtention = 'Cxwebinterface/CxWSResolver.asmx?wsdl'
$resolverUrl = $serviceUrl + $resolverUrlExtention
$resolver = New-WebServiceProxy -Uri $resolverUrl -UseDefaultCredential

if (!$resolver)
{
"Could not resolve service URL."
"Service might be down or a wrong URL was supplied."
Exit
if (!$resolver){
Write-Host "Could not resolve service URL."
Write-Host "Service might be down or a wrong URL was supplied."
Exit(1)
}


$webServiceAddressObject = $resolver.GetWebServiceUrl('SDK' ,1)



$proxy = New-WebServiceProxy -Uri $webServiceAddressObject.ServiceURL -UseDefaultCredential
if (!$proxy)
{
"Could not find Checkmarx SDK service URL"
Exit
if (!$proxy){
Write-Host "Could not find Checkmarx SDK service URL"
Exit(2)
}

$namespace = $proxy.GetType().Namespace
Expand All @@ -176,84 +169,55 @@ $credentials.User = $username
$credentials.Pass = $pass

$loginResponse = $proxy.Login($credentials, 1033)
If(-Not $loginResponse.IsSuccesfull)
{
"An Error occurred while logging in:"
$loginResponse.ErrorMessage
}
Else
{
$sessionId = $loginResponse.SessionId
$retentionConfigurationType = ($namespace + '.CxDataRetentionConfiguration')
$retentionConfiguration = New-Object ($retentionConfigurationType)

If ($StartRetention.IsPresent)
{

If($ByNumOfScans.IsPresent)
{
$retentionConfiguration.DataRetentionType = "NumOfScansToPreserve"
$retentionConfiguration.NumOfScansToPreserve = $numOfScansToKeep
}
Else
{
If($ByDateRange.IsPresent -OR $ByRollingDate.IsPresent)
{
$retentionConfiguration.DataRetentionType = "DatesRange"

If($rollingDateRange)
{
$startDate = Get-Date "2010-01-02"
$endDate = (Get-Date).AddDays(-$rollingDateRange)

}
Else{

If(!($startDate.IsPresent)){

$startDate = Get-Date "2010-01-01"
}

$startDate = ($startDate).AddDays(1)
$endDate = ($endDate).AddDays(1)
}
$retentionConfiguration.StartDate = $startDate.ticks
$retentionConfiguration.EndDate = $endDate.ticks
}
}
If($retentionDurationLimit)
{
$retentionConfiguration.DurationLimitInHours = $retentionDurationLimit
if(-Not $loginResponse.IsSuccesfull){
Write-Host "An Error occurred while logging in:"
Write-Host $loginResponse.ErrorMessage
} else {
$sessionId = $loginResponse.SessionId
$retentionConfigurationType = ($namespace + '.CxDataRetentionConfiguration')
$retentionConfiguration = New-Object ($retentionConfigurationType)

if($StartRetention.IsPresent){
if($ByNumOfScans.IsPresent){
$retentionConfiguration.DataRetentionType = "NumOfScansToPreserve"
$retentionConfiguration.NumOfScansToPreserve = $numOfScansToKeep
} else {
if($ByDateRange.IsPresent -OR $ByRollingDate.IsPresent) {
$retentionConfiguration.DataRetentionType = "DatesRange"
if($rollingDateRange) {
$startDate = Get-Date "2010-01-02"
$endDate = (Get-Date).AddDays(-$rollingDateRange)
} else {
If(!($startDate.IsPresent)){
$startDate = Get-Date "2010-01-01"
}


$retentionResponse = $proxy.ExecuteDataRetention($sessionId, $retentionConfiguration)
If($retentionResponse.IsSuccesfull)
{
"Data retention ran successfully"
}
else
{
"An error occurred while trying to start data retention:"
$retentionResponse.ErrorMessage
}
}
else
{
$retentionResponse = $proxy.StopDataRetention($sessionId)

If($retentionResponse.IsSuccesfull)
{
"Data retention stopped"
}
else
{
"An error occurred while trying to stop data retention:"
$retentionResponse.ErrorMessage
}
}



$loginResponse = $proxy.Logout($sessionId)
$startDate = ($startDate).AddDays(1)
$endDate = ($endDate).AddDays(1)
}
$retentionConfiguration.StartDate = $startDate.ticks
$retentionConfiguration.EndDate = $endDate.ticks
}
}
if($retentionDurationLimit){
$retentionConfiguration.DurationLimitInHours = $retentionDurationLimit
}

$retentionResponse = $proxy.ExecuteDataRetention($sessionId, $retentionConfiguration)
if($retentionResponse.IsSuccesfull){
Write-Host "Data retention ran successfully"
} else {
Write-Host "An error occurred while trying to start data retention:"
$retentionResponse.ErrorMessage
}
} else {
$retentionResponse = $proxy.StopDataRetention($sessionId)
if($retentionResponse.IsSuccesfull){
Write-Host "Data retention stopped"
} else {
Write-Host "An error occurred while trying to stop data retention:"
Write-Host $retentionResponse.ErrorMessage
}
}
$loginResponse = $proxy.Logout($sessionId)
}
Loading

0 comments on commit e5cfcfd

Please sign in to comment.