<commit_before>$Path = 'C:\base_windows_rules.json'
$RuleHash = @{}
foreach($Rule in ($Firewall.Rules | where {$_.Direction -eq 1 -and $_.enabled -eq "True"}))
{
    $Properties = $Rule | Select-Object `
        @{Name='description';E={$_.Description}}, `
        @{Name='application_name';E={$_.ApplicationName}}, `
        @{Name='service_name';E={$_.serviceName}}, `
        @{Name='protocol';E={$_.Protocol}}, `
        @{Name='local_ports';E={$_.LocalPorts}}, `
        @{Name='remote_ports';E={$_.RemotePorts}}, `
        @{Name='local_addresses';E={$_.LocalAddresses}}, `
        @{Name='remote_addresses';E={$_.RemoteAddresses}}, `
        @{Name='icmp_types_and_codes';E={$_.IcmpTypesAndCodes}}, `
        @{Name='direction';E={$_.Direction}}, `
        @{Name='interfaces';E={$_.Interfaces}}, `
        @{Name='interface_types';E={$_.InterfaceTypes}}, `
        @{Name='enabled';E={$_.Enabled}}, `
        @{Name='grouping';E={$_.Grouping}}, `
        @{Name='profiles';E={$_.Profiles}}, `
        @{Name='edge_traversal';E={$_.EdgeTraversal}}, `
        @{Name='action';E={$_.Action}}, `
        @{Name='edge_traversal_options';E={$_.EdgeTraversalOptions}}
	
	$RuleHash.Add($Rule.Name, $Properties)
}
$Networks = @{"networks" = $RuleHash} | ConvertTo-Json | Out-File -FilePath $Path<commit_msg>Update generate_rule_definitions.ps1<commit_after>#This script can help you get started with a baseline json hiera data source.
#Just run it on a Windows system with preferred firewall rules already created and enabled through advfirewall.
$Path = 'C:\firewall_rules.json'
$SystemRules = (New-Object -ComObject HNetCfg.FwPolicy2).Rules

#Would use get-member but it does not preserve original object attribute order.
#You can define attributes in your json/yaml hiera data source out of order if you wish.
#'firewall_rule' custom type will ensure proper order before building rule objects with 'rule' provider.
$AttrNames = ((New-Object -ComObject HNetCfg.FwRule |  ConvertTo-Csv)[1].split(',') -replace '"', '')

#Make sure system rules that you are generating this json for all have UNIQUE names.
#This is a must as duplicate keys are not allowed and windows_firewall module prunes duplicate names caught on advfirewall.
#This script will append any subsequent duplicate names with an integer value in order found.
$RuleHash = New-Object System.Collections.Specialized.OrderedDictionary

foreach($SystemRule in ($SystemRules | where {$_.enabled -eq $true} | Sort-Object -Property Name))
{
  $AttrHash = New-Object System.Collections.Specialized.OrderedDictionary
  foreach($AttrName in $AttrNames[1..$AttrNames.Count])
  {
    $AttrValue = $SystemRule.$AttrName
    if ($AttrValue)
	{
      while($AttrName -cmatch "^.*[a-z](?=[A-Z])")
      {
        $AttrName = $AttrName.Insert($Matches[0].Length, '_')
      }

      $AttrHash.add($AttrName.toLower(), $AttrValue)
	}
  }

  if($RuleHash[$SystemRule.Name] -ne $Null)
  {
    $Count = ($RuleHash.Keys -cmatch "^$([regex]::Escape($SystemRule.Name))\s\d.*$").Count + 1
    $RuleHash.add("$($SystemRule.Name) $Count" , $AttrHash)
  }
  else
  {
    $RuleHash.add($SystemRule.Name, $AttrHash)
  }
}

#ConvertTo-Json has terrible white space issues unless you compress output which then makes it unreadable.
$JSON = @{'windows_networks' = $RuleHash} | ConvertTo-Json
$JSON = $JSON -split "\n"
$JSON = $JSON | foreach{$_ -replace "^\s+", ''}
$JSON = $JSON | foreach{$_ -replace ":\s+(?=({|`"|\d))", ': '}
for($x=0; $x -lt 2; $x++){$i=0; $JSON = $JSON | foreach{if($i -gt $x -and $i -lt $JSON.Count-1-$x){$_.Insert(0, '  '); $i++}else{$_; $i++}}}
$JSON = $JSON | foreach{if($_ -match "^\s+`".*`":\s(?!{)"){$_.Insert(0, '  ')}else{$_}}
$JSON -join "" | Out-File $Path -Encoding ascii

{'fraction_non_alphanumeric': 0.19009370816599733, 'fraction_numerical': 0.0034805890227576973, 'mean_word_length': 4.23249299719888, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1768617', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1215, 'n_words': 283}