From 9221cb050da00eae9e53533690d7c427d793171d Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Tue, 24 Mar 2020 20:10:41 +0100 Subject: [PATCH] Adds function to fetch local Icinga 2 Agent host certificate --- .../Get-IcingaAgentHostCertificate.psm1 | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/core/icingaagent/getters/Get-IcingaAgentHostCertificate.psm1 diff --git a/lib/core/icingaagent/getters/Get-IcingaAgentHostCertificate.psm1 b/lib/core/icingaagent/getters/Get-IcingaAgentHostCertificate.psm1 new file mode 100644 index 0000000..75b1aa6 --- /dev/null +++ b/lib/core/icingaagent/getters/Get-IcingaAgentHostCertificate.psm1 @@ -0,0 +1,27 @@ +function Get-IcingaAgentHostCertificate() +{ + # Default for Icinga 2.8.0 and above + [string]$CertDirectory = (Join-Path -Path $Env:ProgramData -ChildPath 'icinga2\var\lib\icinga2\certs\*'); + $FolderContent = Get-ChildItem -Path $CertDirectory -Filter '*.crt' -Exclude 'ca.crt'; + $Hostname = Get-IcingaHostname -LowerCase $TRUE; + $CertPath = $null; + + foreach ($certFile in $FolderContent) { + if ($certFile.Name.Contains($Hostname)) { + $CertPath = $certFile.FullName; + break; + } + } + + if ([string]::IsNullOrEmpty($CertPath)) { + return $null; + } + + $Certificate = New-Object Security.Cryptography.X509Certificates.X509Certificate2 $CertPath; + + return @{ + 'CertFile' = $CertPath; + 'Subject' = $Certificate.SubjectName; + 'Thumbprint' = $Certificate.Thumbprint; + }; +}