diff --git a/PowerXaaS.ps1 b/PowerXaaS.ps1 index 879d2cb..6357196 100644 --- a/PowerXaaS.ps1 +++ b/PowerXaaS.ps1 @@ -10,7 +10,7 @@ .NOTES Author: Olivier TABUT - 1.5.0 release (08/04/2018) + 1.6.0 release (08/04/2018) .PARAMETER Version Display this script version and exit @@ -142,7 +142,7 @@ Param( ### GLOBAL SETTINGS ### # This script name, with various levels of details -$ScriptVersion = "1.5.0" +$ScriptVersion = "1.6.0" $argv0 = Get-Item $MyInvocation.MyCommand.Definition $Script = $argv0.basename # Ex: PowerXaaS $ScriptName = $argv0.name # Ex: PowerXaaS.ps1 @@ -205,7 +205,7 @@ if ($Setup) # Install the service { $pss = Get-Service $ServiceName -ErrorAction stop # Will error-out if not installed # Check if this script is newer than the installed copy. - if (((Get-Item $ScriptCopy -ErrorAction SilentlyContinue).LastWriteTime -lt (Get-Item $ScriptFullName -ErrorAction SilentlyContinue).LastWriteTime) -or ((Get-ItemProperty -Path HKLM:\Software\PowerXaaS -Name Bindings).Bindings) -notmatch $ip) + if (((Get-Item $ScriptCopy -ErrorAction SilentlyContinue).LastWriteTime -lt (Get-Item $ScriptFullName -ErrorAction SilentlyContinue).LastWriteTime) -or ((Get-ItemProperty -Path HKLM:\Software\PowerXaaS -Name Bindings -ErrorAction silentlyContinue).Bindings) -notmatch $ip) { Write-Output "Service $ServiceName is already Installed, but requires upgrade or reconfiguration" & $ScriptFullName -Remove @@ -337,6 +337,11 @@ if ($Remove) # Uninstall the service Write-Error "Failed to remove the service ${ServiceName}: $msg" exit 1 } + $processes = @(Get-WmiObject Win32_Process -filter "Name = 'powershell.exe'" | Where-Object { $_.CommandLine -match ".*$ScriptCopyCname.*-Service" }) + foreach ($process in $processes) + { + taskkill /PID $process.ProcessId /F | out-null + } } catch { @@ -345,10 +350,13 @@ if ($Remove) # Uninstall the service # Unconfigure HTTP server Write-Output "Unconfiguring HTTP server" - $Bindings = (Get-ItemProperty -Path HKLM:\Software\PowerXaaS -Name Bindings).Bindings - $IpPort = $Bindings.split('/')[2] - Unregister-URLPrefix -Prefix $Bindings | Out-Null - Unregister-SSLCertificate -IpPort $IpPort | Out-Null + $Bindings = (Get-ItemProperty -Path HKLM:\Software\PowerXaaS -Name Bindings -ErrorAction SilentlyContinue).Bindings + if ($Bindings) + { + $IpPort = $Bindings.split('/')[2] + Unregister-URLPrefix -Prefix $Bindings | Out-Null + Unregister-SSLCertificate -IpPort $IpPort | Out-Null + } # Remove the installed files if (Test-Path $InstallDir) @@ -444,13 +452,16 @@ if ($Status) # Get the current service status if (($pss.Status -eq "Running") -and (!$spid)) # This happened during the debugging phase { Write-Host "The Service Control Manager thinks $ServiceName is started, but $ServiceName.ps1 -Service is not running." -ForegroundColor Red - exit 1 + return "Inconsistent" } if (($pss.Status -eq "Stopped") -and ($spid)) # This happened during the debugging phase { Write-Host "The Service Control Manager thinks $ServiceName is stopped, but $ServiceName.ps1 -Service is running." -ForegroundColor Red - exit 1 - } + $processes = @(Get-WmiObject Win32_Process -filter "Name = 'powershell.exe'" | Where-Object { $_.CommandLine -match ".*$ScriptCopyCname.*-Service" }) + foreach ($process in $processes) + { + taskkill /PID $process.ProcessId /F | out-null + } return "$($pss.Status)$spid" } diff --git a/README.md b/README.md index f646e91..bec1b54 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ PowerXaaS will allow you to : - add, disable (feature-flag) or remove features/endpoints dynamically with no downtime - manage authentication and rights - manage several versions of your API + - validate JSON body based on predefined JSON schema - handle HTTP standard errors - check how your APIs are used - and much more... @@ -24,4 +25,3 @@ PowerXaaS will allow you to : ### [Features and Endpoints](https://github.com/otabut/PowerXaaS/blob/master/docs/features-and-endpoints.md) ### [Custom features scripts](https://github.com/otabut/PowerXaaS/blob/master/docs/custom-features-scripts.md) ### [Authentication with JSON web tokens](https://github.com/otabut/PowerXaaS/blob/master/docs/json-web-tokens.md) - diff --git a/docs/index.md b/docs/index.md index 550b6e2..7db8c4f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -12,6 +12,7 @@ PowerXaaS will allow you to : - add, disable (feature-flag) or remove features/endpoints dynamically with no downtime - manage authentication and rights - manage several versions of your API + - validate JSON body based on predefined JSON schema - handle HTTP standard errors - check how your APIs are used - and much more... diff --git a/functions/JSONvalidator-helper.ps1 b/functions/JSONvalidator-helper.ps1 new file mode 100644 index 0000000..8f6fe18 --- /dev/null +++ b/functions/JSONvalidator-helper.ps1 @@ -0,0 +1,42 @@ + +Function Test-JSON +{ + param( + [Parameter(Mandatory=$true)][String]$Schema, + [Parameter(Mandatory=$true)][String]$JSON + ) + + $ErrorActionPreference = 'stop' + + $NewtonsoftJsonPath = Resolve-Path -Path "C:\Program Files\PowerXaaS\JsonSchema\Newtonsoft.Json.dll" + $NewtonsoftJsonSchemaPath = Resolve-Path -Path "C:\Program Files\PowerXaaS\JsonSchema\Newtonsoft.Json.Schema.dll" + + Add-Type -Path $NewtonsoftJsonPath + Add-Type -Path $NewtonsoftJsonSchemaPath + + $source = @' + public class Validator + { + public static System.Collections.Generic.IList Validate(Newtonsoft.Json.Linq.JToken token, Newtonsoft.Json.Schema.JSchema schema) + { + System.Collections.Generic.IList messages; + Newtonsoft.Json.Schema.SchemaExtensions.IsValid(token, schema, out messages); + return messages; + } + } +'@ + + Add-Type -TypeDefinition $source -ReferencedAssemblies $NewtonsoftJsonPath,$NewtonsoftJsonSchemaPath + + $Token = [Newtonsoft.Json.Linq.JToken]::Parse($JSON) + $Schema = [Newtonsoft.Json.Schema.JSchema]::Parse($Schema) + $ErrorMessages = [Validator]::Validate($Token,$Schema) + + $Result = [PSCustomObject]@{ + IsValid = $ErrorMessages.Count -eq 0 + ErrorCount = $ErrorMessages.Count + ErrorMessages = $ErrorMessages + } + + return $Result +} diff --git a/module/PowerXaaS.psd1 b/module/PowerXaaS.psd1 index e47145c..2e721be 100644 --- a/module/PowerXaaS.psd1 +++ b/module/PowerXaaS.psd1 @@ -12,7 +12,7 @@ RootModule = 'PowerXaaS.psm1' # Version number of this module. -ModuleVersion = '1.5.0' +ModuleVersion = '1.6.0' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/pester/PowerXaaS.tests.ps1 b/pester/PowerXaaS.tests.ps1 index 4440836..a94541b 100644 --- a/pester/PowerXaaS.tests.ps1 +++ b/pester/PowerXaaS.tests.ps1 @@ -33,15 +33,15 @@ Describe "Validate PowerXaaS" { It "Setup" { - & "$PSScriptRoot\..\PowerXaaS.ps1" -Setup -Ip $ip -Port $port | Out-Null + & "$PSScriptRoot\..\PowerXaaS.ps1" -Setup -Ip $ip -Port $port -Protocol $protocol | Out-Null $result = & "$PSScriptRoot\..\PowerXaaS.ps1" -Status - $result | should be "Stopped" + $result | should match "Stopped" } It "Version" { $result = & "$PSScriptRoot\..\PowerXaaS.ps1" -Version - $result | should be "1.5.0" + $result | should be "1.6.0" } It "Start" { @@ -56,7 +56,7 @@ Describe "Validate PowerXaaS" { & "$PSScriptRoot\..\PowerXaaS.ps1" -Stop | Out-Null start-sleep 1 $result = & "$PSScriptRoot\..\PowerXaaS.ps1" -Status - $result | should be "Stopped" + $result | should match "Stopped" } It "Remove" { @@ -64,7 +64,7 @@ Describe "Validate PowerXaaS" { & "$PSScriptRoot\..\PowerXaaS.ps1" -Remove | out-null $result = & "$PSScriptRoot\..\PowerXaaS.ps1" -Status $result | should be "Not installed" - & "$PSScriptRoot\..\PowerXaaS.ps1" -Setup -Ip $ip -Port $port -Start | Out-Null + & "$PSScriptRoot\..\PowerXaaS.ps1" -Setup -Ip $ip -Port $port -Protocol $protocol -Start | Out-Null start-sleep 2 Import-Module PowerXaaS }