Skip to content

Commit 0a57bce

Browse files
authored
Merge pull request #205 from Icinga:feature/open_config_file_as_readonly
Feature: Ensure config.json is opened as read-only In some cases it can happen that multiple processes access our config file. By default `Get-Content` is not thread save and might result in errors while reading the file content. In general we should use a read-only attempt for the config.json and write it only if required.
2 parents 9a1c187 + 80051d6 commit 0a57bce

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1717
* [#193](https://github.com/Icinga/icinga-powershell-framework/pull/193) Adds optional support for adding milliseconds to `Get-IcingaUnixTime` with the `-Milliseconds` argument for more detailed time comparison
1818
* [#198](https://github.com/Icinga/icinga-powershell-framework/pull/198) Adds support to flush the content of the Icinga Agent API directory with a single Cmdlet `Clear-IcingaAgentApiDirectory`
1919
* [#203](https://github.com/Icinga/icinga-powershell-framework/pull/203) Removes experimental state of the Icinga PowerShell Framework code caching and adds docs on how to use the feature
20+
* [#205](https://github.com/Icinga/icinga-powershell-framework/pull/205) Ensure Icinga for Windows configuration file is opened as read-only for every single task besides actually modifying configuration content
2021

2122
## 1.3.1 (2021-02-04)
2223

icinga-powershell-framework.psd1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
'.\lib\config\Test-IcingaPowerShellConfigItem.psm1',
1515
'.\lib\core\logging\Write-IcingaConsoleOutput.psm1',
1616
'.\lib\core\logging\Write-IcingaConsoleNotice.psm1',
17-
'.\lib\core\logging\Write-IcingaConsoleWarning.psm1'
17+
'.\lib\core\logging\Write-IcingaConsoleWarning.psm1',
18+
'.\lib\core\tools\Read-IcingaFileContent.psm1'
1819
)
1920
FunctionsToExport = @(
2021
'Use-Icinga',
@@ -37,7 +38,8 @@
3738
'Test-IcingaPowerShellConfigItem',
3839
'Write-IcingaConsoleOutput',
3940
'Write-IcingaConsoleNotice',
40-
'Write-IcingaConsoleWarning'
41+
'Write-IcingaConsoleWarning',
42+
'Read-IcingaFileContent'
4143
)
4244
CmdletsToExport = @('*')
4345
VariablesToExport = '*'

lib/config/Read-IcingaPowerShellConfig.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Read-IcingaPowerShellConfig()
2828
return (New-Object -TypeName PSObject);
2929
}
3030

31-
[string]$Content = Get-Content -Path $ConfigFile;
31+
[string]$Content = Read-IcingaFileContent -File $ConfigFile;
3232

3333
if ([string]::IsNullOrEmpty($Content)) {
3434
return (New-Object -TypeName PSObject);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<#
2+
.SYNOPSIS
3+
Reads content of a file in read-only mode, ensuring no data corruption is happening
4+
.DESCRIPTION
5+
Reads content of a file in read-only mode, ensuring no data corruption is happening
6+
.FUNCTIONALITY
7+
Reads content of a file in read-only mode, ensuring no data corruption is happening
8+
.EXAMPLE
9+
PS>Read-IcingaFileContent -File 'config.json';
10+
.OUTPUTS
11+
System.Object
12+
.LINK
13+
https://github.com/Icinga/icinga-powershell-framework
14+
#>
15+
16+
function Read-IcingaFileContent()
17+
{
18+
param (
19+
[string]$File
20+
);
21+
22+
if ((Test-Path $File) -eq $FALSE) {
23+
return $null;
24+
}
25+
26+
[System.IO.FileStream]$FileStream = [System.IO.File]::Open(
27+
$File,
28+
[System.IO.FileMode]::Open,
29+
[System.IO.FileAccess]::Read,
30+
[System.IO.FileShare]::Read
31+
);
32+
33+
$ReadArray = New-Object Byte[] $FileStream.Length;
34+
$UTF8Encoding = New-Object System.Text.UTF8Encoding $TRUE;
35+
$FileContent = '';
36+
37+
while ($FileStream.Read($ReadArray, 0 , $ReadArray.Length)) {
38+
$FileContent = [System.String]::Concat($FileContent, $UTF8Encoding.GetString($ReadArray));
39+
}
40+
41+
$FileStream.Dispose();
42+
43+
return $FileContent;
44+
}

0 commit comments

Comments
 (0)