class Chef::Resource::WindowsFirewallProfile

Public Instance Methods

convert_to_powershell(obj) click to toggle source
# File lib/chef/resource/windows_firewall_profile.rb, line 112
def convert_to_powershell(obj)
  if obj.to_s.downcase == "true"
    "True"
  elsif obj.to_s.downcase == "false"
    "False"
  elsif obj.to_s.downcase == "notconfigured"
    "NotConfigured"
  end
end
convert_to_ruby(obj) click to toggle source
# File lib/chef/resource/windows_firewall_profile.rb, line 102
def convert_to_ruby(obj)
  if obj.to_s.downcase == "true"
    true
  elsif obj.to_s.downcase == "false"
    false
  elsif obj.to_s.downcase == "notconfigured"
    "NotConfigured"
  end
end
firewall_command(fw_profile) click to toggle source
# File lib/chef/resource/windows_firewall_profile.rb, line 146
def firewall_command(fw_profile)
  cmd = "Set-NetFirewallProfile -Profile \"#{fw_profile}\""
  cmd << " -DefaultInboundAction \"#{new_resource.default_inbound_action}\"" unless new_resource.default_inbound_action.nil?
  cmd << " -DefaultOutboundAction \"#{new_resource.default_outbound_action}\"" unless new_resource.default_outbound_action.nil?
  cmd << " -AllowInboundRules \"#{convert_to_powershell(new_resource.allow_inbound_rules)}\"" unless new_resource.allow_inbound_rules.nil?
  cmd << " -AllowLocalFirewallRules \"#{convert_to_powershell(new_resource.allow_local_firewall_rules)}\"" unless new_resource.allow_local_firewall_rules.nil?
  cmd << " -AllowLocalIPsecRules \"#{convert_to_powershell(new_resource.allow_local_ipsec_rules)}\"" unless new_resource.allow_local_ipsec_rules.nil?
  cmd << " -AllowUserApps \"#{convert_to_powershell(new_resource.allow_user_apps)}\"" unless new_resource.allow_user_apps.nil?
  cmd << " -AllowUserPorts \"#{convert_to_powershell(new_resource.allow_user_ports)}\"" unless new_resource.allow_user_ports.nil?
  cmd << " -AllowUnicastResponseToMulticast \"#{convert_to_powershell(new_resource.allow_unicast_response)}\"" unless new_resource.allow_unicast_response.nil?
  cmd << " -NotifyOnListen \"#{convert_to_powershell(new_resource.display_notification)}\"" unless new_resource.display_notification.nil?
  cmd
end
firewall_enabled?(profile_name) click to toggle source
# File lib/chef/resource/windows_firewall_profile.rb, line 160
        def firewall_enabled?(profile_name)
          cmd = <<~CODE
            $#{profile_name} = Get-NetFirewallProfile -Profile #{profile_name}
            if ($#{profile_name}.Enabled) {
                return $true
            } else {return $false}
          CODE
          powershell_exec!(cmd).result
        end

Private Instance Methods

load_firewall_state(profile_name) click to toggle source

build the command to load the current resource @return [String] current firewall state

# File lib/chef/resource/windows_firewall_profile.rb, line 175
      def load_firewall_state(profile_name)
        <<-EOH
          Remove-TypeData System.Array # workaround for PS bug here: https://bit.ly/2SRMQ8M
          $#{profile_name} = Get-NetFirewallProfile -Profile #{profile_name}
          ([PSCustomObject]@{
            default_inbound_action = $#{profile_name}.DefaultInboundAction.ToString()
            default_outbound_action = $#{profile_name}.DefaultOutboundAction.ToString()
            allow_inbound_rules = $#{profile_name}.AllowInboundRules.ToString()
            allow_local_firewall_rules = $#{profile_name}.AllowLocalFirewallRules.ToString()
            allow_local_ipsec_rules = $#{profile_name}.AllowLocalIPsecRules.ToString()
            allow_user_apps = $#{profile_name}.AllowUserApps.ToString()
            allow_user_ports = $#{profile_name}.AllowUserPorts.ToString()
            allow_unicast_response = $#{profile_name}.AllowUnicastResponseToMulticast.ToString()
            display_notification = $#{profile_name}.NotifyOnListen.ToString()
          })
        EOH
      end