-
-
Notifications
You must be signed in to change notification settings - Fork 881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for new feature: port ranges #1325
base: master
Are you sure you want to change the base?
Changes from 24 commits
b40a4a1
c00d5e2
efc321e
d61fb86
b727650
b416b8b
9dbf0a6
be02ef8
dd1afc4
513d75d
d0df89c
fe09594
ec6a201
e09f5b4
366bc45
dc8b14a
6a02424
addd3c6
d4021eb
bcad22d
ffaa006
d9292c6
f36f469
01692cf
558b32b
8907ee4
bf23d6b
4045958
f96006a
0c7e28a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
# Parameters: | ||
# [*ensure*] - Enables or disables the specified server (present|absent) | ||
# [*listen_ip*] - Default IP Address for NGINX to listen with this server on. Defaults to all interfaces (*) | ||
# [*listen_port*] - Default IP Port for NGINX to listen with this server on. Defaults to TCP 80 | ||
# [*listen_port*] - Default IP Port for NGINX to listen with this server on. Defaults to TCP 80. It can be a port or a port range (eg. '8081-8085'). | ||
# [*listen_port_range*] - From Nginx 1.15.10, support for port ranges was added (eg. '8081-8085'). | ||
# [*listen_options*] - Extra options for listen directive like 'default_server' to catchall. Undef by default. | ||
# [*listen_unix_socket_enable*] - BOOL value to enable/disable UNIX socket listening support (false|true). | ||
# [*listen_unix_socket*] - Default unix socket for NGINX to listen with this server on. Defaults to UNIX /var/run/nginx.sock | ||
|
@@ -17,6 +18,7 @@ | |
# exists on your system before enabling. | ||
# [*ipv6_listen_ip*] - Default IPv6 Address for NGINX to listen with this server on. Defaults to all interfaces (::) | ||
# [*ipv6_listen_port*] - Default IPv6 Port for NGINX to listen with this server on. Defaults to TCP 80 | ||
# [*ipv6_listen_port_range*] - From Nginx 1.15.10, support for port ranges was added (eg. '8081-8085'). | ||
# [*ipv6_listen_options*] - Extra options for listen directive like 'default' to catchall. Template will allways add ipv6only=on. | ||
# While issue jfryman/puppet-nginx#30 is discussed, default value is 'default'. | ||
# [*add_header*] - Hash: Adds headers to the HTTP response when response code is equal to 200, 204, 301, 302 or 304. | ||
|
@@ -147,6 +149,7 @@ | |
Enum['absent', 'present'] $ensure = 'present', | ||
Variant[Array, String] $listen_ip = '*', | ||
Integer $listen_port = 80, | ||
Optional[Nginx::PortRange] $listen_port_range = undef, | ||
Optional[String] $listen_options = undef, | ||
Boolean $listen_unix_socket_enable = false, | ||
Variant[Array[Stdlib::Absolutepath], Stdlib::Absolutepath] $listen_unix_socket = '/var/run/nginx.sock', | ||
|
@@ -157,6 +160,7 @@ | |
Boolean $ipv6_enable = false, | ||
Variant[Array, String] $ipv6_listen_ip = '::', | ||
Integer $ipv6_listen_port = 80, | ||
Optional[Nginx::PortRange] $ipv6_listen_port_range = undef, | ||
String $ipv6_listen_options = 'default ipv6only=on', | ||
Hash $add_header = {}, | ||
Boolean $ssl = false, | ||
|
@@ -316,9 +320,39 @@ | |
} | ||
} | ||
|
||
# If port range is defined, ignore any other $listen_port defined | ||
if versioncmp(fact('nginx_version'), '1.15.10') < 0 { | ||
$port_range_support = false | ||
} | ||
else{ | ||
$port_range_support = true | ||
} | ||
|
||
if ($listen_port_range != undef) and ($port_range_support == true) { | ||
$port = $listen_port_range | ||
} | ||
elsif ($listen_port_range != undef) and ($port_range_support == false) { | ||
fail('nginx: this version of nginx does not support port ranges (must be >= 1.15.10)') | ||
} | ||
else{ | ||
$port = $listen_port | ||
} | ||
|
||
if $ipv6_enable == true{ | ||
if ($ipv6_listen_port_range != undef) and ($port_range_support == true) { | ||
$ipv6_port = $ipv6_listen_port_range | ||
} | ||
elsif ($ipv6_listen_port_range != undef) and ($port_range_support == false) { | ||
fail('nginx: this version of nginx does not support port ranges (must be >= 1.15.10)') | ||
} | ||
else { | ||
$ipv6_port = $ipv6_listen_port | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @00ssh I think your code is to complicated and does not need to throw an error message. Maybe something like this could work:
https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/REFERENCE.md#pick_default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dhoppe Hi, Thanks for your suggestions. I've just tested with pick_default and it works just fine. However, I still have to figure out why the travis pipeline is failing (I don't have an environment connected to the internet right now). It looks like "fact('nginx_version')" doesn't return anything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @00ssh Maybe, for that reason I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dhoppe Thanks. I've updated accordingly. Waiting for tests to pass this time! |
||
# Try to error in the case where the user sets ssl_port == listen_port but | ||
# doesn't set ssl = true | ||
if !$ssl and $ssl_port == $listen_port { | ||
if !$ssl and $ssl_port == $port { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be an issue when port is an array. This repeats below. |
||
warning('nginx: ssl must be true if listen_port is the same as ssl_port') | ||
} | ||
|
||
|
@@ -341,7 +375,7 @@ | |
|
||
# Suppress unneeded stuff in non-SSL location block when certain conditions are | ||
# met. | ||
$ssl_only = ($ssl and $ssl_port == $listen_port) or $ssl_redirect | ||
$ssl_only = ($ssl and $ssl_port == $port) or $ssl_redirect | ||
|
||
# If we're redirecting to SSL, the default location block is useless, *unless* | ||
# SSL is enabled for this server | ||
|
@@ -424,7 +458,7 @@ | |
} | ||
} | ||
|
||
if $listen_port != $ssl_port { | ||
if $port != $ssl_port { | ||
concat::fragment { "${name_sanitized}-header": | ||
target => $config_file, | ||
content => template('nginx/server/server_header.erb'), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,18 @@ | |
value: 45, | ||
match: %r{\s+listen\s+\[::\]:45 default ipv6only=on;} | ||
}, | ||
{ | ||
title: 'should set the IPv6 listen port', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to change the title to 'should set the IPv6 listen port range'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. Thanks for pointing it out! |
||
attr: 'ipv6_listen_port_range', | ||
value: '45-50', | ||
match: %r{\s+listen\s+\[::\]:45-50 default ipv6only=on;} | ||
}, | ||
{ | ||
title: 'should set the IPv4 listen port range', | ||
attr: 'listen_port_range', | ||
value: '45-50', | ||
match: %r{\s+listen\s+\*:45-50;} | ||
}, | ||
{ | ||
title: 'should set the IPv6 listen options', | ||
attr: 'ipv6_listen_options', | ||
|
@@ -353,6 +365,7 @@ | |
].each do |param| | ||
context "when #{param[:attr]} is #{param[:value]}" do | ||
let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } | ||
let(:facts) { facts.merge(nginx_version: '1.15.0') } | ||
|
||
it { is_expected.to contain_concat__fragment("#{title}-header") } | ||
it param[:title] do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Nginx::PortRange = Pattern[/^\d+-\d+?$/] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the question mark at the end? Could you add some tests for the type alias? See https://rspec-puppet.com/documentation/type_aliases/ as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment here is incorrect. It's forced to be an integer so it can't be a range. Can't we change
listen_port
to a typeVariant[Stdlib::Port, Nginx::PortRange]
and handle that in the code? I dislike having two parameters for the same thing.