Skip to content

Commit 39815ae

Browse files
committed
configure unix socket on ssl vhost
1 parent 12d6b62 commit 39815ae

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

spec/acceptance/nginx_server_spec.rb

+104
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,108 @@ class { 'nginx': }
264264
end
265265
end
266266
end
267+
268+
context 'should run with unix socket' do
269+
it 'configures a nginx server' do
270+
pp = "
271+
class { 'nginx': }
272+
nginx::resource::server { 'www.puppetlabs.com':
273+
ensure => present,
274+
www_root => '/var/www/www.puppetlabs.com',
275+
listen_unix_socket_enable => true,
276+
listen_unix_socket => '/var/run/nginx.sock'
277+
}
278+
host { 'www.puppetlabs.com': ip => '127.0.0.1', }
279+
file { ['/var/www','/var/www/www.puppetlabs.com']: ensure => directory }
280+
file { '/var/www/www.puppetlabs.com/index.html': ensure => file, content => 'Hello from www\n', }
281+
"
282+
283+
apply_manifest(pp, catch_failures: true)
284+
apply_manifest(pp, catch_changes: true)
285+
end
286+
287+
describe file('/etc/nginx/sites-available/www.puppetlabs.com.conf') do
288+
it { is_expected.to be_file }
289+
it { is_expected.to contain 'www.puppetlabs.com' }
290+
end
291+
292+
describe file('/etc/nginx/sites-enabled/www.puppetlabs.com.conf') do
293+
it { is_expected.to be_linked_to '/etc/nginx/sites-available/www.puppetlabs.com.conf' }
294+
end
295+
296+
describe service('nginx') do
297+
it { is_expected.to be_running }
298+
end
299+
300+
describe port(80) do
301+
it { is_expected.to be_listening }
302+
end
303+
304+
describe file('/var/run/nginx.sock') do
305+
it { is_expected.to be_socket }
306+
end
307+
308+
it 'answers to www.puppetlabs.com and responds with "Hello from www"' do
309+
shell('/usr/bin/curl --unix-socket /var/run/nginx.sock http://www.puppetlabs.com') do |r|
310+
expect(r.stdout).to eq("Hello from www\n")
311+
end
312+
end
313+
314+
it 'answers to www.puppetlabs.com without error' do
315+
shell('/usr/bin/curl --unix-socket /var/run/nginx.sock --fail http://www.puppetlabs.com') do |r|
316+
expect(r.exit_code).to be_zero
317+
end
318+
end
319+
end
320+
321+
context 'should run with unix socket with SSL' do
322+
it 'configures a nginx SSL server' do
323+
pp = "
324+
class { 'nginx': }
325+
nginx::resource::server { 'www.puppetlabs.com':
326+
ensure => present,
327+
ssl => true,
328+
ssl_cert => '/etc/pki/tls/certs/blah.cert',
329+
ssl_key => '/etc/pki/tls/private/blah.key',
330+
www_root => '/var/www/www.puppetlabs.com',
331+
listen_port => 443,
332+
ssl_port => 443,
333+
listen_unix_socket_enable => true,
334+
listen_unix_socket => '/var/run/nginx.sock'
335+
}
336+
host { 'www.puppetlabs.com': ip => '127.0.0.1', }
337+
file { ['/var/www','/var/www/www.puppetlabs.com']: ensure => directory }
338+
file { '/var/www/www.puppetlabs.com/index.html': ensure => file, content => 'Hello from www\n', }
339+
"
340+
341+
apply_manifest(pp, catch_failures: true)
342+
end
343+
344+
describe service('nginx') do
345+
it { is_expected.to be_running }
346+
end
347+
348+
describe port(443) do
349+
it { is_expected.to be_listening }
350+
end
351+
352+
# curl on centos7 does not support curl --unix-socket with https:// addresses :(
353+
describe file('/var/run/nginx.sock') do
354+
it { is_expected.to be_socket }
355+
end
356+
357+
it 'answers to https://www.puppetlabs.com with "Hello from www"' do
358+
# use --insecure because it's a self-signed cert
359+
shell('/usr/bin/curl --insecure https://www.puppetlabs.com:443') do |r|
360+
expect(r.stdout).to eq("Hello from www\n")
361+
end
362+
end
363+
364+
it 'answers to https://www.puppetlabs.com without error' do
365+
# use --insecure because it's a self-signed cert
366+
shell('/usr/bin/curl --fail --insecure https://www.puppetlabs.com:443') do |r|
367+
expect(r.exit_code).to eq(0)
368+
end
369+
end
370+
end
267371
end

templates/server/server_ssl_header.erb

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ server {
99
<%- else -%>
1010
listen <%= @listen_ip %>:<%= @ssl_port %> <% if @ssl_listen_option %>ssl<% end %><% if scope.call_function('versioncmp', [scope['nginx::nginx_version'], '1.25.1']) < 0 && @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_options %> <%= @listen_options %><% end %>;
1111
<%- end -%>
12+
<%- if @listen_unix_socket_enable -%>
13+
<%- if @listen_unix_socket.is_a?(Array) then -%>
14+
<%- @listen_unix_socket.each do |unix_socket| -%>
15+
listen unix:<%= unix_socket %> <% if @ssl_listen_option %>ssl<% end %><% if scope.call_function('versioncmp', [scope['nginx::nginx_version'], '1.25.1']) < 0 && @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_unix_socket_options %> <%= @listen_unix_socket_options %><% end %>;
16+
<%- end -%>
17+
<%- else -%>
18+
listen unix:<%= @listen_unix_socket %> <% if @ssl_listen_option %>ssl<% end %><% if scope.call_function('versioncmp', [scope['nginx::nginx_version'], '1.25.1']) < 0 && @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_unix_socket_options %> <%= @listen_unix_socket_options %><% end %>;
19+
<%- end -%>
20+
<%- end -%>
1221
<%= scope.function_template(["nginx/server/server_ssl_ipv6_listen.erb"]) %>
1322
<%- if @rewrite_www_to_non_www -%>
1423
server_name www.<%= s.gsub(/^www\./, '') %>;
@@ -53,6 +62,15 @@ server {
5362
<%- else -%>
5463
listen <%= @listen_ip %>:<%= @ssl_port %> <% if @ssl_listen_option %>ssl<% end %><% if scope.call_function('versioncmp', [scope['nginx::nginx_version'], '1.25.1']) < 0 && @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_options %> <%= @listen_options %><% end %>;
5564
<%- end -%>
65+
<%- if @listen_unix_socket_enable -%>
66+
<%- if @listen_unix_socket.is_a?(Array) then -%>
67+
<%- @listen_unix_socket.each do |unix_socket| -%>
68+
listen unix:<%= unix_socket %> <% if @ssl_listen_option %>ssl<% end %><% if @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_unix_socket_options %> <%= @listen_unix_socket_options %><% end %>;
69+
<%- end -%>
70+
<%- else -%>
71+
listen unix:<%= @listen_unix_socket %> <% if @ssl_listen_option %>ssl<% end %><% if @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_unix_socket_options %> <%= @listen_unix_socket_options %><% end %>;
72+
<%- end -%>
73+
<%- end -%>
5674
<%= scope.function_template(["nginx/server/server_ssl_ipv6_listen.erb"]) %>
5775
<%- if @rewrite_www_to_non_www -%>
5876
server_name <%= @server_name.join(" ").gsub(/(^| )(www\.)?(?=[a-z0-9])/, '') %>;

0 commit comments

Comments
 (0)