Skip to content

Commit

Permalink
Add support for puppet 8 + code cleaning (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
webalexeu authored Sep 27, 2024
1 parent 97fef0f commit 111c9f7
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 119 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file.

## Release 1.0.0 (2024-09-27)

[Full Changelog](https://github.com/webalexeu/puppet-windows_firewall/compare/v0.1.0...v1.0.0)

**Features**

- Add support for Puppet 8
- Code cleaning

**Bugfixes**

**Known Issues**


## Release 0.1.0 (2024-09-27)

**Features**
Expand Down
66 changes: 66 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Reference

<!-- DO NOT EDIT: This document was generated by Puppet Strings -->

## Table of Contents

### Resource types

* [`winhttp_proxy`](#winhttp_proxy): Manage Windows system proxy (i.e. WinHTTP Proxy) settings.

## Resource types

### <a name="winhttp_proxy"></a>`winhttp_proxy`

Manage Windows system proxy (i.e. WinHTTP Proxy) settings.

#### Properties

The following properties are available in the `winhttp_proxy` type.

##### `bypass_list`

An array of sites that should be visited bypassing the proxy
(use "<local>" to bypass all short name hosts).

Examples:
* ['*.foo.com']
* ['<local>', 'example.org']

##### `ensure`

Valid values: `present`, `absent`

How to ensure proxy settings are defined

Default value: `present`

##### `proxy_server`

%{Proxy server for use for http and/or https protocol.

Examples:
* myproxy
* myproxy:80
* http=proxy.example.com;https=proxy.example.org}

#### Parameters

The following parameters are available in the `winhttp_proxy` type.

* [`name`](#-winhttp_proxy--name)
* [`provider`](#-winhttp_proxy--provider)

##### <a name="-winhttp_proxy--name"></a>`name`

Valid values: `proxy`

namevar

Resource name. Should be "proxy".

##### <a name="-winhttp_proxy--provider"></a>`provider`

The specific backend to use for this `winhttp_proxy` resource. You will seldom need to specify this --- Puppet will
usually discover the appropriate provider for your platform.

65 changes: 30 additions & 35 deletions lib/puppet/provider/winhttp_proxy/netsh.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Puppet::Type.type(:winhttp_proxy).provide(:netsh, :parent => Puppet::Provider) do
confine :operatingsystem => :windows
defaultfor :operatingsystem => :windows
Puppet::Type.type(:winhttp_proxy).provide(:netsh, parent: Puppet::Provider) do
confine operatingsystem: :windows
defaultfor operatingsystem: :windows

desc 'Windows Proxy'

# Actually, Windows as different settings under 32-bit or 64-bit
# How can people pay for this crappy software?!
# FIXME Handle the WOW64 proxy too
def self.netsh_command
if File.exists?("#{ENV['SYSTEMROOT']}\\System32\\netsh.exe")
if File.exist?("#{ENV['SYSTEMROOT']}\\System32\\netsh.exe")
"#{ENV['SYSTEMROOT']}\\System32\\netsh.exe"
else
'netsh.exe'
Expand All @@ -16,43 +18,39 @@ def self.netsh_command
initvars
mk_resource_methods

commands :netsh => netsh_command
commands netsh: netsh_command

def self.instances
proxy = {
'ensure' => :absent
}
cmd = [ 'cmd.exe', '/c', command(:netsh), 'winhttp', 'dump' ]
if Puppet::PUPPETVERSION.to_f < 3.4
raw, status = Puppet::Util::SUIDManager.run_and_capture(cmd)
else
raw = Puppet::Util::Execution.execute(cmd)
status = raw.exitstatus
end
raw = Puppet::Util::Execution.execute(cmd)
_status = raw.exitstatus
instances = []
context = []
raw.each_line() do |line|
next if line =~ /^\s*(#|$)/
if line =~ /^pushd (.*)$/
context << $1
raw.each_line do |line|
next if %r{^\s*(#|$)}.match?(line)
if line =~ %r{^pushd (.*)$}
context << Regexp.last_match(1)
next
end
if line =~ /^popd$/
if %r{^popd$}.match?(line)
context.pop
next
end
if context == [ 'winhttp' ] and line =~ /^reset proxy$/
if (context == [ 'winhttp' ]) && line =~ (%r{^reset proxy$})
next
end
if context == [ 'winhttp' ] and line =~ /^set proxy proxy-server="([^"]+)"( bypass-list="([^"]+)")?$/
if (context == [ 'winhttp' ]) && line =~ (%r{^set proxy proxy-server="([^"]+)"( bypass-list="([^"]+)")?$})
proxy = {
:name => :proxy,
:ensure => :present,
:proxy_server => $1,
:bypass_list => [],
name: :proxy,
ensure: :present,
proxy_server: Regexp.last_match(1),
bypass_list: [],
}
if $3
proxy[:bypass_list] = $3.split(';')
if Regexp.last_match(3)
proxy[:bypass_list] = Regexp.last_match(3).split(';')
end
instances << new(proxy)
next
Expand All @@ -64,7 +62,7 @@ def self.instances

def self.prefetch(resources)
instances.each do |instance|
if proxy = resources[instance.name]
if (proxy = resources[instance.name])
proxy.provider = instance
end
end
Expand All @@ -78,9 +76,9 @@ def exists?
# Keep resource properties, flush will actually apply
def create
@property_hash = {
:ensure => :present,
:proxy_server => @resource[:proxy_server],
:bypass_list => @resource[:bypass_list]
ensure: :present,
proxy_server: @resource[:proxy_server],
bypass_list: @resource[:bypass_list]
}
end

Expand All @@ -91,12 +89,9 @@ def destroy
end

def flush
cmd = [ 'cmd.exe', '/c', command(:netsh), 'winhttp', 'set', 'proxy', 'proxy-server="%s"' % @property_hash[:proxy_server], 'bypass-list="%s"' % (@property_hash[:bypass_list].respond_to?('join') ? @property_hash[:bypass_list].join(";") : @property_hash[:bypass_list]) ]
if Puppet::PUPPETVERSION.to_f < 3.4
raw, status = Puppet::Util::SUIDManager.run_and_capture(cmd)
else
raw = Puppet::Util::Execution.execute(cmd)
status = raw.exitstatus
end
cmd = [ 'cmd.exe', '/c', command(:netsh), 'winhttp', 'set', 'proxy', 'proxy-server="%s"' % @property_hash[:proxy_server],
'bypass-list="%s"' % (@property_hash[:bypass_list].respond_to?('join') ? @property_hash[:bypass_list].join(';') : @property_hash[:bypass_list]) ]
raw = Puppet::Util::Execution.execute(cmd)
_status = raw.exitstatus
end
end
17 changes: 9 additions & 8 deletions lib/puppet/type/winhttp_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
Puppet::Type.newtype(:winhttp_proxy) do
@doc = %q{Manage Windows system proxy (i.e. WinHTTP Proxy) settings.
}
@doc = 'Manage Windows system proxy (i.e. WinHTTP Proxy) settings.
'

ensurable do
desc 'How to ensure proxy settings are defined'
defaultvalues
defaultto :present
end

newparam(:name) do
desc %q{Resource name. Should be "proxy".}
desc 'Resource name. Should be "proxy".'
isnamevar
newvalues(:proxy)
end

newproperty(:proxy_server) do
desc %{Proxy server for use for http and/or https protocol.
desc %(Proxy server for use for http and/or https protocol.
Examples:
* myproxy
* myproxy:80
* http=proxy.example.com;https=proxy.example.org}
* http=proxy.example.com;https=proxy.example.org)
validate do |values|
values.split(';').each do |value|
unless value =~ /^[=a-z._-]+(:\d+)?$/
unless %r{^[=a-z._-]+(:\d+)?$}.match?(value)
raise ArgumentError, "proxy_server item %s is invalid. Examples: 'myproxy', 'myproxy:80', 'http=proxy.example.com'" % value
end
end
end
end

newproperty(:bypass_list, :array_matching => :all) do
newproperty(:bypass_list, array_matching: :all) do
desc %q{An array of sites that should be visited bypassing the proxy
(use "<local>" to bypass all short name hosts).
Expand All @@ -38,7 +39,7 @@
* ['<local>', 'example.org']}
validate do |values|
values.split(';').each do |value|
unless value =~ /^[*a-z0-9._-]+$/ or value == "<local>"
unless value =~ (%r{^[*a-z0-9._-]+$}) || (value == '<local>')
raise ArgumentError, "bypass_list item %s is invalid. Examples: '*.foo.com', 'bar', '<local>'" % value
end
end
Expand Down
4 changes: 2 additions & 2 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webalex-winhttp_proxy",
"version": "0.1.0",
"version": "1.0.0",
"author": "webalex",
"summary": "Manage Windows system proxy (i.e. WinHTTP Proxy) settings",
"license": "Apache-2.0",
Expand All @@ -25,7 +25,7 @@
"requirements": [
{
"name": "puppet",
"version_requirement": ">= 6.0.0 < 7.0.0"
"version_requirement": ">= 6.0.0 < 9.0.0"
}
],
"pdk-version": "3.3.0",
Expand Down
Loading

0 comments on commit 111c9f7

Please sign in to comment.