Skip to content

Commit

Permalink
added logging to binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jformacek committed Feb 5, 2025
1 parent b165d09 commit 453e01f
Show file tree
Hide file tree
Showing 22 changed files with 279 additions and 69 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ jobs:
- name: Build - SpfAnalyzer
run: dotnet build "$env:GITHUB_WORKSPACE/src/SpfAnalyzer" --no-restore --configuration Release /p:Version=$env:MODULE_VERSION

- name: Build - SpfIpHelper
run: dotnet build "$env:GITHUB_WORKSPACE/src/SpfIpHelper" --no-restore --configuration Release /p:Version=$env:MODULE_VERSION

- name: Build - AutomationHelper
run: dotnet build "$env:GITHUB_WORKSPACE/src/AutomationHelper" --no-restore --configuration Release /p:Version=$env:MODULE_VERSION

- name: Build module
shell: pwsh
run: |
Expand Down
4 changes: 2 additions & 2 deletions Commands/Internal/Expand-SpfMacro.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function Expand-SpfMacro
$senderValid = $senderParts.Count -eq 2
}
if($macro -match '%{i}') {
$dottedIp = [IpHelper.IPAddressExtensions]::ToDotted($IpAddress)
$dottedIp = [SpfIpHelper.IPAddressExtensions]::ToDotted($IpAddress)
$macro = $macro -replace '%{i}', $dottedIp
}
if($macro -match '%{ir}') {
$dottedIp = [IpHelper.IPAddressExtensions]::ToReverseDotted($IpAddress)
$dottedIp = [SpfIpHelper.IPAddressExtensions]::ToReverseDotted($IpAddress)
$macro = $macro -replace '%{ir}', $dottedIp
}
if($macro -match '%{c}') {
Expand Down
17 changes: 13 additions & 4 deletions Commands/Public/Get-DKIMRecord.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Domain,
[Parameter(Mandatory)]
[string]$Record
[string]$Record,
[Parameter()]
[string]$DnsServerIpAddress
)

begin
{
$logger = new-object AutomationHelper.Logger($PSCmdlet)
$parsedRecord = $null
}
process
{
$dnsName = $record + '.' + $domain
$dkimRecords = [SpfAnalyzer.Dns]::GetDkimRecord($dnsName)
$dkimRecords = [SpfAnalyzer.Dns]::GetDkimRecord($dnsName, $DnsServerIpAddress)
foreach($record in $dkimRecords)
{
[SpfAnalyzer.DkimRecord]::Parse($domain, $dnsName, $record)
if([SpfAnalyzer.DkimRecord]::TryParse($domain, $dnsName, $record, $logger, [ref] $parsedRecord))
{
$parsedRecord
}
}
}
}
16 changes: 13 additions & 3 deletions Commands/Public/Get-DmarcRecord.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
param
(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Domain
[string]$Domain,
[Parameter()]
[string]$DnsServerIpAddress
)

begin
{
$logger = new-object AutomationHelper.Logger($PSCmdlet)
$parsedRecord = $null
}
process
{
$dnsName = '_dmarc.{0}' -f $domain
$records = [SpfAnalyzer.Dns]::GetDmarcRecord($dnsName)
$records = [SpfAnalyzer.Dns]::GetDmarcRecord($dnsName, $DnsServerIpAddress)
foreach($record in $records)
{
[SpfAnalyzer.DmarcRecord]::Parse($domain, $dnsName, $record)
if([SpfAnalyzer.DmarcRecord]::TryParse($domain, $dnsName, $record, $logger, [ref] $parsedRecord))
{
$parsedRecord
}
}
}
}
17 changes: 14 additions & 3 deletions Commands/Public/Get-SPFRecord.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,26 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
param
(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Domain
[string]$Domain,
[Parameter()]
[string]$DnsServerIpAddress
)

begin
{
$logger = new-object AutomationHelper.Logger($PSCmdlet)
$parsedRecord = $null
}
process
{
$spfRecords = [SpfAnalyzer.Dns]::GetSpfRecord($domain)
$spfRecords = [SpfAnalyzer.Dns]::GetSpfRecord($domain, $DnsServerIpAddress)
foreach($spfRecord in $spfRecords)
{
[SpfAnalyzer.SpfRecord]::Parse($domain, $domain, $spfRecord, 0)
$success = [SpfAnalyzer.SpfRecord]::TryParse($domain, $domain, $spfRecord, 0, $logger, [ref] $parsedRecord)
if($success)
{
$parsedRecord
}
}
}
}
4 changes: 2 additions & 2 deletions Commands/Public/Get-SpfRecordEntries.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
param
(
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'Record')]
[SpfRecord]$SpfRecord,
[SpfAnalyzer.SpfRecord]$SpfRecord,
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'DomainName')]
[string]$Domain
)
Expand All @@ -41,7 +41,7 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
if ($PSCmdlet.ParameterSetName -eq 'DomainName')
{
Write-Verbose "Processing $Domain"
[SpfRecord[]]$record = Get-SpfRecord -Domain $Domain `
$record = Get-SpfRecord -Domain $Domain
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions Module/SpfAnalyzer/SpfAnalyzer.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PowerShellVersion = '7.2'
# RequiredModules = @('')

# Assemblies that must be loaded prior to importing this module
RequiredAssemblies = @('.\lib\net8.0/IpHelper.dll', '.\lib\net8.0/DnsClient.dll', '.\lib\net8.0/SpfAnalyzer.dll')
RequiredAssemblies = @('.\lib\net8.0\SpfAnalyzer.dll', '.\lib\net8.0\AutomationHelper.dll', '.\lib\net8.0\SpfIpHelper.dll', '.\lib\net8.0\DnsClient.dll')

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
Expand Down Expand Up @@ -88,7 +88,7 @@ AliasesToExport = @()

# List of all files packaged with this module
FileList = @( './SpfAnalyzer.psd1', './SpfAnalyzer.psm1', './SpfAnalyzer.format.ps1xml',
'./lib/net8.0/IpHelper.dll', './lib/net8.0/DnsClient.dll','./lib/net8.0/SpfAnalyzer.dll' )
'./lib/net8.0/SpfIpHelper.dll', './lib/net8.0/DnsClient.dll','./lib/net8.0/SpfAnalyzer.dll', './lib/net8.0/AutomationHelper.dll' )

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
Expand Down
58 changes: 44 additions & 14 deletions Module/SpfAnalyzer/SpfAnalyzer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Domain,
[Parameter(Mandatory)]
[string]$Record
[string]$Record,
[Parameter()]
[string]$DnsServerIpAddress
)

begin
{
$logger = new-object AutomationHelper.Logger($PSCmdlet)
$parsedRecord = $null
}
process
{
$dnsName = $record + '.' + $domain
$dkimRecords = [SpfAnalyzer.Dns]::GetDkimRecord($dnsName)
$dkimRecords = [SpfAnalyzer.Dns]::GetDkimRecord($dnsName, $DnsServerIpAddress)
foreach($record in $dkimRecords)
{
[SpfAnalyzer.DkimRecord]::Parse($domain, $dnsName, $record)
if([SpfAnalyzer.DkimRecord]::TryParse($domain, $dnsName, $record, $logger, [ref] $parsedRecord))
{
$parsedRecord
}
}
}
}
Expand Down Expand Up @@ -64,16 +73,26 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
param
(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Domain
[string]$Domain,
[Parameter()]
[string]$DnsServerIpAddress
)

begin
{
$logger = new-object AutomationHelper.Logger($PSCmdlet)
$parsedRecord = $null
}
process
{
$dnsName = '_dmarc.{0}' -f $domain
$records = [SpfAnalyzer.Dns]::GetDmarcRecord($dnsName)
$records = [SpfAnalyzer.Dns]::GetDmarcRecord($dnsName, $DnsServerIpAddress)
foreach($record in $records)
{
[SpfAnalyzer.DmarcRecord]::Parse($domain, $dnsName, $record)
if([SpfAnalyzer.DmarcRecord]::TryParse($domain, $dnsName, $record, $logger, [ref] $parsedRecord))
{
$parsedRecord
}
}
}
}
Expand Down Expand Up @@ -104,15 +123,26 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
param
(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Domain
[string]$Domain,
[Parameter()]
[string]$DnsServerIpAddress
)

begin
{
$logger = new-object AutomationHelper.Logger($PSCmdlet)
$parsedRecord = $null
}
process
{
$spfRecords = [SpfAnalyzer.Dns]::GetSpfRecord($domain)
$spfRecords = [SpfAnalyzer.Dns]::GetSpfRecord($domain, $DnsServerIpAddress)
foreach($spfRecord in $spfRecords)
{
[SpfAnalyzer.SpfRecord]::Parse($domain, $domain, $spfRecord, 0)
$success = [SpfAnalyzer.SpfRecord]::TryParse($domain, $domain, $spfRecord, 0, $logger, [ref] $parsedRecord)
if($success)
{
$parsedRecord
}
}
}
}
Expand Down Expand Up @@ -149,7 +179,7 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
param
(
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'Record')]
[SpfRecord]$SpfRecord,
[SpfAnalyzer.SpfRecord]$SpfRecord,
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'DomainName')]
[string]$Domain
)
Expand All @@ -159,7 +189,7 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7
if ($PSCmdlet.ParameterSetName -eq 'DomainName')
{
Write-Verbose "Processing $Domain"
[SpfRecord[]]$record = Get-SpfRecord -Domain $Domain `
$record = Get-SpfRecord -Domain $Domain
}
else
{
Expand Down Expand Up @@ -418,11 +448,11 @@ function Expand-SpfMacro
$senderValid = $senderParts.Count -eq 2
}
if($macro -match '%{i}') {
$dottedIp = [IpHelper.IPAddressExtensions]::ToDotted($IpAddress)
$dottedIp = [SpfIpHelper.IPAddressExtensions]::ToDotted($IpAddress)
$macro = $macro -replace '%{i}', $dottedIp
}
if($macro -match '%{ir}') {
$dottedIp = [IpHelper.IPAddressExtensions]::ToReverseDotted($IpAddress)
$dottedIp = [SpfIpHelper.IPAddressExtensions]::ToReverseDotted($IpAddress)
$macro = $macro -replace '%{ir}', $dottedIp
}
if($macro -match '%{c}') {
Expand Down
28 changes: 28 additions & 0 deletions src/AutomationHelper/AutomationHelper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SpfAnalyzer\SpfAnalyzer.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Management.Automation">
<HintPath>..\..\..\..\..\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="CopyFiles" AfterTargets="CopyFilesToOutputDirectory">
<ItemGroup>
<!-- Because this ItemGroup is inside the target, this will enumerate
all files just before calling Copy. If the ItemGroup were outside
the target , it would enumerate the files during evaluation, before
the build starts, which may miss files created during the build. -->
<BuildOutput Include="$(TargetPath)" />
</ItemGroup>
<Copy SourceFiles="@(BuildOutput)" DestinationFolder="$(ProjectDir)/../../Module/SpfAnalyzer/lib/net8.0/" />
</Target>

</Project>
38 changes: 38 additions & 0 deletions src/AutomationHelper/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using SpfAnalyzer;

namespace AutomationHelper
{
public class Logger:ILogger
{
private readonly PSCmdlet _cmdlet;
public Logger(PSCmdlet cmdlet)
{
_cmdlet = cmdlet;
}
public void LogWarning(string message)
{
_cmdlet.WriteWarning(message);
}
public void LogError(string message, Exception? exception, int eventId, Object? targetObject )
{
ErrorCategory category = ErrorCategory.NotSpecified;
if(Enum.IsDefined(typeof(ErrorCategory), eventId))
{
category = (ErrorCategory)eventId;
}
var err = new ErrorRecord(exception ?? new Exception(message), message, category, targetObject);
_cmdlet.WriteError(err);
}
public void LogVerbose(string message)
{
_cmdlet.WriteVerbose(message);
}
}
}
8 changes: 7 additions & 1 deletion src/SpfAnalyzer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpHelper", "IpHelper\IpHelper.csproj", "{40BF999E-93AB-44AB-8231-EBC392B1A4AC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpfIpHelper", "SpfIpHelper\SpfIpHelper.csproj", "{40BF999E-93AB-44AB-8231-EBC392B1A4AC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpfAnalyzer", "SpfAnalyzer\SpfAnalyzer.csproj", "{58FC4A04-4861-40DB-B2C2-907A056DDC87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{E2721B35-FF5A-4AE5-84EC-8713FB812BB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomationHelper", "AutomationHelper\AutomationHelper.csproj", "{B3D259E1-8214-49EC-9AA5-86106E8261ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{E2721B35-FF5A-4AE5-84EC-8713FB812BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2721B35-FF5A-4AE5-84EC-8713FB812BB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2721B35-FF5A-4AE5-84EC-8713FB812BB2}.Release|Any CPU.Build.0 = Release|Any CPU
{B3D259E1-8214-49EC-9AA5-86106E8261ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3D259E1-8214-49EC-9AA5-86106E8261ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3D259E1-8214-49EC-9AA5-86106E8261ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3D259E1-8214-49EC-9AA5-86106E8261ED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 4 additions & 2 deletions src/SpfAnalyzer/DkimRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public DkimRecord(string domain, string source, string rawRecord)
_rawRecord = rawRecord;
}

public static DkimRecord[] Parse(string domain, string source, string rawRecord)
public static bool TryParse(string domain, string source, string rawRecord, ILogger? logger, out DkimRecord[] dkimRecords)
{
logger?.LogVerbose($"Processing record {rawRecord}");
var retVal = new List<DkimRecord>();
var record = new DkimRecord(domain, source, rawRecord);
retVal.Add(record);
Expand Down Expand Up @@ -75,7 +76,8 @@ public static DkimRecord[] Parse(string domain, string source, string rawRecord)
{
record.PublicKey = DkimPublicKey.Parse(algo, key);
}
return retVal.ToArray();
dkimRecords = [.. retVal];
return true;
}

public override string ToString()
Expand Down
Loading

0 comments on commit 453e01f

Please sign in to comment.