Skip to content

Commit 9ae3bfa

Browse files
authored
Merge pull request #109 from Icinga:fix/error_handling_for_checksum_plugin
Fix: Adds exception handling for checksum plugin Adds exception handling in case -Path argument is not set or not directing a file (including invalid path)
2 parents cc7bb48 + ec027fe commit 9ae3bfa

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ If you are going to install this plugin release, please have a look on the [upgr
3232
* [#90](https://github.com/Icinga/icinga-powershell-plugins/issues/90) Adds support to ignore read only/offline disks on `Invoke-IcingaCheckDiskHealth`
3333
* [#101](https://github.com/Icinga/icinga-powershell-plugins/pull/101) Improves `Invoke-IcingaCheckScheduledTask` by changing the `State` argument from `String` to `Array`, allowing the comparison against multiple states. **Important:** Please have a look on the [upgrading docs!](https://icinga.com/docs/windows/latest/plugins/doc/30-Upgrading-Plugins/)
3434
* [#104](https://github.com/Icinga/icinga-powershell-plugins/pull/104) Adds plugin configuration files for Icinga Director and Icinga 2 within the [config directory](https://github.com/Icinga/icinga-powershell-plugins/tree/master/config)
35+
* [#109](https://github.com/Icinga/icinga-powershell-plugins/pull/109) Adds exception handling in case `-Path` argument is not set or not directing a file (including invalid path)
3536

3637
### Bugfixes
3738

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,69 @@
11
<#
22
.SYNOPSIS
3-
Checks hash against filehash of a file
3+
Checks hash against file hash of a file
44
.DESCRIPTION
5-
Invoke-IcingaCheckCheckSum returns either 'OK' or 'CRITICAL', whether the check matches or not.
5+
Invoke-IcingaCheckCheckSum returns either 'OK' or 'CRITICAL', whether the check matches or not.
66
7-
More Information on https://github.com/Icinga/icinga-powershell-plugins
7+
More Information on https://github.com/Icinga/icinga-powershell-plugins
88
.FUNCTIONALITY
9-
This module is intended to be used to check a hash against a filehash of a file, to determine whether changes have occured.
10-
Based on the match result the status will change between 'OK' or 'CRITICAL'. The function will return one of these given codes.
9+
This module is intended to be used to check a hash against a file hash of a file, to determine whether changes have occurred.
10+
Based on the match result the status will change between 'OK' or 'CRITICAL'. The function will return one of these given codes.
1111
.EXAMPLE
12-
PS> Invoke-IcingaCheckCheckSum -Path "C:\Users\Icinga\Downloads\test.txt"
13-
[OK] CheckSum C:\Users\Icinga\Downloads\test.txt is 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B
12+
PS> Invoke-IcingaCheckCheckSum -Path "C:\Users\Icinga\Downloads\test.txt"
13+
[OK] CheckSum C:\Users\Icinga\Downloads\test.txt is 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B
1414
.EXAMPLE
15-
PS> Invoke-IcingaCheckCheckSum -Path "C:\Users\Icinga\Downloads\test.txt" -Hash 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B
16-
[OK] CheckSum C:\Users\Icinga\Downloads\test.txt is 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B|
15+
PS> Invoke-IcingaCheckCheckSum -Path "C:\Users\Icinga\Downloads\test.txt" -Hash 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B
16+
[OK] CheckSum C:\Users\Icinga\Downloads\test.txt is 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B|
1717
.EXAMPLE
18-
PS> Invoke-IcingaCheckCheckSum -Path "C:\Users\Icinga\Downloads\test.txt" -Hash 008FB84A017F5DFDAF038DB2FDD6934E6E5D
19-
[CRITICAL] CheckSum C:\Users\Icinga\Downloads\test.txt 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B is not matching 008FB84A017F5DFDAF038DB2FDD6934E6E5D
18+
PS> Invoke-IcingaCheckCheckSum -Path "C:\Users\Icinga\Downloads\test.txt" -Hash 008FB84A017F5DFDAF038DB2FDD6934E6E5D
19+
[CRITICAL] CheckSum C:\Users\Icinga\Downloads\test.txt 008FB84A017F5DFDAF038DB2FDD6934E6E5D9CD3C7AACE2F2168D7D93AF51E4B is not matching 008FB84A017F5DFDAF038DB2FDD6934E6E5D
2020
.PARAMETER WarningBytes
21-
Used to specify a string to the path of a file, which will be checked.
21+
Used to specify a string to the path of a file, which will be checked.
2222
23-
The string has to be like "C:\Users\Icinga\test.txt"
23+
The string has to be like "C:\Users\Icinga\test.txt"
2424
2525
.PARAMETER Algorithm
26-
Used to specify a string, which contains the algorithm to be used.
26+
Used to specify a string, which contains the algorithm to be used.
2727
28-
Allowed algorithms: 'SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5'
28+
Allowed algorithms: 'SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5'
2929
3030
.INPUTS
31-
System.String
31+
System.String
3232
3333
.OUTPUTS
34-
System.String
34+
System.String
3535
3636
.LINK
37-
https://github.com/Icinga/icinga-powershell-plugins
37+
https://github.com/Icinga/icinga-powershell-plugins
3838
.NOTES
3939
#>
4040

4141
function Invoke-IcingaCheckCheckSum()
4242
{
43-
param(
44-
[string]$Path = $null,
45-
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
46-
[string]$Algorithm = 'SHA256',
47-
[string]$Hash = $null,
48-
[switch]$NoPerfData,
49-
[ValidateSet(0, 1, 2, 3)]
50-
[int]$Verbosity = 0
51-
);
52-
53-
[string]$FileHash = (Get-FileHash $Path -Algorithm $Algorithm).Hash
54-
$CheckSumCheck = New-IcingaCheck -Name "CheckSum $Path" -Value $FileHash;
55-
56-
If(([string]::IsNullOrEmpty($Hash)) -eq $FALSE){
57-
$CheckSumCheck.CritIfNotMatch($Hash) | Out-Null;
58-
}
59-
60-
return (New-IcingaCheckresult -Check $CheckSumCheck -NoPerfData $NoPerfData -Compile);
43+
param (
44+
[string]$Path = $null,
45+
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
46+
[string]$Algorithm = 'SHA256',
47+
[string]$Hash = $null,
48+
[switch]$NoPerfData,
49+
[ValidateSet(0, 1, 2, 3)]
50+
[int]$Verbosity = 0
51+
);
52+
53+
if ([string]::IsNullOrEmpty($Path)) {
54+
Exit-IcingaThrowException -Force -CustomMessage 'Unset argument "-Path"' -ExceptionType 'Configuration' -ExceptionThrown $IcingaExceptions.Configuration.PluginArgumentMissing;
55+
}
56+
57+
if ((Test-Path $Path) -eq $FALSE -Or (Get-Item $Path) -Is [System.IO.DirectoryInfo]) {
58+
Exit-IcingaThrowException -Force -CustomMessage '"-Path" is not directing to a file' -ExceptionType 'Configuration' -ExceptionThrown $IcingaExceptions.Configuration.PluginArgumentConflict;
59+
}
60+
61+
[string]$FileHash = (Get-FileHash $Path -Algorithm $Algorithm).Hash
62+
$CheckSumCheck = New-IcingaCheck -Name "CheckSum $Path" -Value $FileHash;
63+
64+
If (([string]::IsNullOrEmpty($Hash)) -eq $FALSE) {
65+
$CheckSumCheck.CritIfNotMatch($Hash) | Out-Null;
66+
}
67+
68+
return (New-IcingaCheckResult -Check $CheckSumCheck -NoPerfData $NoPerfData -Compile);
6169
}

0 commit comments

Comments
 (0)