Skip to content

Commit d06a097

Browse files
committed
configure unix socket on ssl vhost
1 parent fa6254d commit d06a097

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

spec/acceptance/nginx_server_spec.rb

+100
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,104 @@ class { 'nginx': }
247247
end
248248
end
249249
end
250+
251+
context 'should run with unix socket' do
252+
it 'configures a nginx server' do
253+
pp = "
254+
class { 'nginx': }
255+
nginx::resource::server { 'www.puppetlabs.com':
256+
ensure => present,
257+
www_root => '/var/www/www.puppetlabs.com',
258+
listen_unix_socket_enable => false,
259+
listen_unix_socket => '/var/run/nginx.sock'
260+
}
261+
host { 'www.puppetlabs.com': ip => '127.0.0.1', }
262+
file { ['/var/www','/var/www/www.puppetlabs.com']: ensure => directory }
263+
file { '/var/www/www.puppetlabs.com/index.html': ensure => file, content => 'Hello from www\n', }
264+
"
265+
266+
apply_manifest(pp, catch_failures: true)
267+
apply_manifest(pp, catch_changes: true)
268+
end
269+
270+
describe file('/etc/nginx/sites-available/www.puppetlabs.com.conf') do
271+
it { is_expected.to be_file }
272+
it { is_expected.to contain 'www.puppetlabs.com' }
273+
end
274+
275+
describe file('/etc/nginx/sites-enabled/www.puppetlabs.com.conf') do
276+
it { is_expected.to be_linked_to '/etc/nginx/sites-available/www.puppetlabs.com.conf' }
277+
end
278+
279+
describe service('nginx') do
280+
it { is_expected.to be_running }
281+
end
282+
283+
describe port(80) do
284+
it { is_expected.to be_listening }
285+
end
286+
287+
describe file('/var/run/nginx.sock') do
288+
it { is_expected.to be_socket }
289+
end
290+
291+
it 'answers to www.puppetlabs.com and responds with "Hello from www"' do
292+
shell('/usr/bin/curl --unix-socket /var/run/nginx.sock http://www.puppetlabs.com') do |r|
293+
expect(r.stdout).to eq("Hello from www\n")
294+
end
295+
end
296+
297+
it 'answers to www.puppetlabs.com without error' do
298+
shell('/usr/bin/curl --unix-socket /var/run/nginx.sock --fail http://www.puppetlabs.com') do |r|
299+
expect(r.exit_code).to be_zero
300+
end
301+
end
302+
end
303+
304+
context 'should run with unix socket with SSL' do
305+
it 'configures a nginx SSL server' do
306+
pp = "
307+
class { 'nginx': }
308+
nginx::resource::server { 'www.puppetlabs.com':
309+
ensure => present,
310+
ssl => true,
311+
ssl_cert => '/etc/pki/tls/certs/blah.cert',
312+
ssl_key => '/etc/pki/tls/private/blah.key',
313+
www_root => '/var/www/www.puppetlabs.com',
314+
listen_port => 443,
315+
ssl_port => 443,
316+
listen_unix_socket_enable => false,
317+
listen_unix_socket => '/var/run/nginx.sock'
318+
319+
}
320+
host { 'www.puppetlabs.com': ip => '127.0.0.1', }
321+
file { ['/var/www','/var/www/www.puppetlabs.com']: ensure => directory }
322+
file { '/var/www/www.puppetlabs.com/index.html': ensure => file, content => 'Hello from www\n', }
323+
"
324+
325+
apply_manifest(pp, catch_failures: true)
326+
end
327+
328+
describe service('nginx') do
329+
it { is_expected.to be_running }
330+
end
331+
332+
describe port(443) do
333+
it { is_expected.to be_listening }
334+
end
335+
336+
it 'answers to https://www.puppetlabs.com with "Hello from www"' do
337+
# use --insecure because it's a self-signed cert
338+
shell('/usr/bin/curl --unix-socket /var/run/nginx.sock --insecure https://www.puppetlabs.com') do |r|
339+
expect(r.stdout).to eq("Hello from www\n")
340+
end
341+
end
342+
343+
it 'answers to https://www.puppetlabs.com without error' do
344+
# use --insecure because it's a self-signed cert
345+
shell('/usr/bin/curl --unix-socket /var/run/nginx.sock --fail --insecure https://www.puppetlabs.com') do |r|
346+
expect(r.exit_code).to eq(0)
347+
end
348+
end
349+
end
250350
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 @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 @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 @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\./, '') %>;
@@ -32,6 +41,15 @@ server {
3241
<%- else -%>
3342
listen <%= @listen_ip %>:<%= @ssl_port %> <% if @ssl_listen_option %>ssl<% end %><% if @http2 == 'on' %> http2<% end %><% if @spdy == 'on' %> spdy<% end %><% if @listen_options %> <%= @listen_options %><% end %>;
3443
<%- end -%>
44+
<%- if @listen_unix_socket_enable -%>
45+
<%- if @listen_unix_socket.is_a?(Array) then -%>
46+
<%- @listen_unix_socket.each do |unix_socket| -%>
47+
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 %>;
48+
<%- end -%>
49+
<%- else -%>
50+
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 %>;
51+
<%- end -%>
52+
<%- end -%>
3553
<%= scope.function_template(["nginx/server/server_ssl_ipv6_listen.erb"]) %>
3654
<%- if @rewrite_www_to_non_www -%>
3755
server_name <%= @server_name.join(" ").gsub(/(^| )(www\.)?(?=[a-z0-9])/, '') %>;

0 commit comments

Comments
 (0)