From 57ad2b5f98071a13b635b4768737adf85fd1d65a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98rnulf=20Nielsen?= <40027793+ornulfn@users.noreply.github.com> Date: Fri, 22 May 2020 20:39:02 +0200 Subject: [PATCH] - Add reports for proxy policy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ørnulf Nielsen <40027793+ornulfn@users.noreply.github.com> --- PSFortigateParser.psd1 | 4 +- PSFortigateParser.psm1 | 204 +++++++++++++++++++++++- src/Classes/PSFortigateConfig.ps1 | 6 +- src/Classes/PSFortigateConfigObject.ps1 | 104 +++++++++++- src/Classes/PSFortigateReport.ps1 | 92 ++++++++++- src/psake-default.ps1 | 2 +- 6 files changed, 396 insertions(+), 16 deletions(-) diff --git a/PSFortigateParser.psd1 b/PSFortigateParser.psd1 index 75f6915..0814892 100644 --- a/PSFortigateParser.psd1 +++ b/PSFortigateParser.psd1 @@ -3,7 +3,7 @@ # # Generated by: Ørnulf Nielsen # -# Generated on: 05.03.2020 +# Generated on: 22.05.2020 # @{ @@ -12,7 +12,7 @@ RootModule = 'PSFortigateParser' # Version number of this module. -ModuleVersion = '1.3.0' +ModuleVersion = '1.4.0' # Supported PSEditions CompatiblePSEditions = 'Core', 'Desktop' diff --git a/PSFortigateParser.psm1 b/PSFortigateParser.psm1 index 454d4d1..116526b 100644 --- a/PSFortigateParser.psm1 +++ b/PSFortigateParser.psm1 @@ -1,4 +1,4 @@ -#Generated at 2020-03-05 10:03 by Ørnulf Nielsen +#Generated at 2020-05-22 20:35 by Ørnulf Nielsen #region Class PSFortigateConfig : System.IDisposable Class PSFortigateConfig : System.IDisposable { #region Properties @@ -121,7 +121,7 @@ Class PSFortigateConfig : System.IDisposable { if ($sLine -match "^\s*(?config|edit)\s+(?
.*)\s*$") { if ($Matches.type -eq 'config') { Write-Debug ('PSFortigateConfig: Found config section {0}' -f $Matches.section) - if ($Matches.section -eq "firewall policy") { + if ($Matches.section -eq "firewall policy" -or $Matches.section -eq "firewall proxy-policy" ) { $this.inPolicySection = $true $this.PolicySequence = 0 } elseif ($Matches.section -eq "firewall service custom") { @@ -155,12 +155,12 @@ Class PSFortigateConfig : System.IDisposable { $PropertyKey = $Matches.Key -replace "`"","" # Inject sequence number for firewall policy - if ($PropertyKey -eq "name" -and $this.inPolicySection) { + if ($PropertyKey -eq "uuid" -and $this.inPolicySection) { $Section['sequence'] = $this.PolicySequence } # Remove double quotes - use array if multi-valued - if ($this.inServiceSection -and $PropertyKey -like "*-portrange") { + if (($this.inServiceSection -and $PropertyKey -like "*-portrange") -or ($this.inPolicySection -and $PropertyKey -like "internet-service-id")) { $PropertyValue = $Matches.Value -split "\s+" if ($PropertyValue.Count -eq 1) { $PropertyValue = $PropertyValue -as [System.String] @@ -188,6 +188,7 @@ Class PSFortigateConfig : System.IDisposable { Class PSFortigateConfigObject : PSFortigateConfig { #region Properties Hidden [PSCustomObject]$PolicyTemplate + Hidden [PSCustomObject]$ProxyPolicyTemplate Hidden [PSCustomObject]$AddressTemplate Hidden [PSCustomObject]$AddressGroupTemplate Hidden [PSCustomObject]$ServiceTemplate @@ -209,6 +210,7 @@ Class PSFortigateConfigObject : PSFortigateConfig { ([PSFortigateConfig]$this).Constructor() # Setup default templates $this.setPolicyTemplate() + $this.setProxyPolicyTemplate() $this.setAddressTemplate() $this.setAddressGroupTemplate() $this.setServiceTemplate() @@ -363,7 +365,6 @@ Class PSFortigateConfigObject : PSFortigateConfig { $oPolicy = $this.PolicyTemplate.PsObject.Copy() $oPolicy.vdom = $vdom.Name $oPolicy.policyid = $Policy.Name -# $oPolicy.sequence = $Policy.Name foreach ($PolicyOption in $Policy.Value.GetEnumerator()) { try { @@ -380,7 +381,26 @@ Class PSFortigateConfigObject : PSFortigateConfig { } return $cPolicies } - Write-Debug ('PSFortigateConfigObject: No vDom found') + elseif ($this.Config['firewall policy'].count -gt 0) { + foreach ($Policy in $this.Config['firewall policy'].GetEnumerator()) { + $oPolicy = $this.PolicyTemplate.PsObject.Copy() + $oPolicy.vdom = "" + $oPolicy.policyid = $Policy.Name + + foreach ($PolicyOption in $Policy.Value.GetEnumerator()) { + try { + Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Policy {1} Option {2}' -f "No vDom", $Policy.Name, $PolicyOption.Name) + $oPolicy.($PolicyOption.Name) = $PolicyOption.Value + } + catch { + Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Address {1} Option {2} - option not found in policy template' -f "No vDom", $Policy.Name, $PolicyOption.Name) + } + } + $cPolicies.Add($oPolicy) + } + return $cPolicies + } + Write-Debug ('PSFortigateConfigObject: No firewall policy found') return $null } @@ -1022,6 +1042,7 @@ Class PSFortigateConfigObject : PSFortigateConfig { set secondary-IP "deleteme" set remote-ip "deleteme" set interface "deleteme" + set explicit-web-proxy "deleteme" next "@.Split([Environment]::NewLine) $this.setInterfaceTemplate($Template) @@ -1410,6 +1431,85 @@ Class PSFortigateConfigObject : PSFortigateConfig { return $null } + #endregion + #region [void]setProxyPolicyTemplate([System.String[]]$Template) + [void]setProxyPolicyTemplate( + [System.String[]]$Template + ) { + # Columns are displayed according to order in template + $Options = [Ordered]@{ vdom = $null; sequence = $null ; policyid = $null } + foreach ($Line in $Template) { + if ($Line -match "^(\s*)set (?