From 7d1e75b5317ca35624e5f539a576f735ab6baa58 Mon Sep 17 00:00:00 2001 From: ITBM <22393016+itbm@users.noreply.github.com> Date: Fri, 28 Sep 2018 08:14:32 +0100 Subject: [PATCH 1/3] Allow setting unquoted on add_headers --- templates/server/locations/headers.erb | 8 ++++++++ templates/server/server_header.erb | 8 ++++++++ templates/server/server_ssl_header.erb | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/templates/server/locations/headers.erb b/templates/server/locations/headers.erb index 97fdbed2b..bad8ffebe 100644 --- a/templates/server/locations/headers.erb +++ b/templates/server/locations/headers.erb @@ -1,3 +1,11 @@ <%- @add_header.keys.sort.each do |key| -%> + <%- if @add_header[key] -%> + <%- if @add_header[key].is_a?(Hash) -%> + <%- @add_header[key].each do |sk, sv| -%> + add_header "<%= key %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; + <%- end -%> + <%- else -%> add_header "<%= key %>" "<%= @add_header[key] %>"; + <%- end -%> + <%- end -%> <%- end -%> diff --git a/templates/server/server_header.erb b/templates/server/server_header.erb index 8aa6d71b8..f7bef0f5a 100644 --- a/templates/server/server_header.erb +++ b/templates/server/server_header.erb @@ -117,7 +117,15 @@ server { <% end -%> <% if @add_header -%> <%- @add_header.keys.sort.each do |key| -%> + <%- if @add_header[key] -%> + <%- if @add_header[key].is_a?(Hash) -%> + <%- @add_header[key].each do |sk, sv| -%> + add_header "<%= key %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; + <%- end -%> + <%- else -%> add_header "<%= key %>" "<%= @add_header[key] %>"; + <%- end -%> + <%- end -%> <%- end -%> <% end -%> <% if @maintenance -%> diff --git a/templates/server/server_ssl_header.erb b/templates/server/server_ssl_header.erb index 27ae5e11f..bd44ad683 100644 --- a/templates/server/server_ssl_header.erb +++ b/templates/server/server_ssl_header.erb @@ -146,6 +146,14 @@ server { <% end -%> <% if @add_header -%> <%- @add_header.keys.sort.each do |key| -%> + <%- if @add_header[key] -%> + <%- if @add_header[key].is_a?(Hash) -%> + <%- @add_header[key].each do |sk, sv| -%> + add_header "<%= key %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; + <%- end -%> + <%- else -%> add_header "<%= key %>" "<%= @add_header[key] %>"; + <%- end -%> + <%- end -%> <%- end -%> <% end -%> From 26153c2871aafd42ade9b0039a2fb8d475b3451d Mon Sep 17 00:00:00 2001 From: ITBM <22393016+itbm@users.noreply.github.com> Date: Mon, 1 Oct 2018 07:55:59 +0100 Subject: [PATCH 2/3] Update tests for add_header --- spec/defines/resource_location_spec.rb | 13 ++++++++----- spec/defines/resource_server_spec.rb | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 536c2a5b2..ff0472ab9 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -404,17 +404,20 @@ let(:params) do default_params.merge( 'add_header' => { - 'X-Frame-Options' => 'SAMEORIGIN', - 'X-Powered-By' => 'Puppet' + 'header 1' => 'test value 1', + 'header 2' => { 'test value 2' => 'tv2' }, + 'header 3' => { '' => '\'test value 3\' tv3' } } ) end - it 'contains both add_header lines' do + it 'contains 3 add_header lines' do is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - with_content(%r{^\s+add_header\s+"X-Frame-Options"\s+"SAMEORIGIN";$}) + with_content(%r{^\s+add_header\s+"header 1"\s+"test value 1";$}) is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - with_content(%r{^\s+add_header\s+"X-Powered-By"\s+"Puppet";$}) + with_content(%r{^\s+add_header\s+"header 2"\s+"test value 2" tv2;$}) + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). + with_content(%r{^\s+add_header\s+"header 3"\s+'test value 3' tv3;$}) end end end diff --git a/spec/defines/resource_server_spec.rb b/spec/defines/resource_server_spec.rb index 1dd28e42c..d4ec18535 100644 --- a/spec/defines/resource_server_spec.rb +++ b/spec/defines/resource_server_spec.rb @@ -1333,24 +1333,24 @@ context 'when add_header is set' do let :params do - default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }) + default_params.merge(add_header: { 'header3' => { '' => '\'test value 3\' tv3' }, 'header2' => { 'test value 2' => 'tv2' }, 'header1' => 'test value 1' }) end it 'has correctly ordered entries in the config' do - is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n}) + is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2" tv2;\n\s+add_header\s+"header3" 'test value 3' tv3;\n}) end end context 'when add_header is set and ssl => true' do let :params do - default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }, + default_params.merge(add_header: { 'header3' => { '' => '\'test value 3\' tv3' }, 'header2' => { 'test value 2' => 'tv2' }, 'header1' => 'test value 1' }, ssl: true, ssl_key: 'dummy.key', ssl_cert: 'dummy.cert') end it 'has correctly ordered entries in the config' do - is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n}) + is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2" tv2;\n\s+add_header\s+"header3" 'test value 3' tv3;\n}) end end end From 3c9b4240a681d746bb4f9833881596a22251319a Mon Sep 17 00:00:00 2001 From: ITBM <22393016+itbm@users.noreply.github.com> Date: Mon, 8 Oct 2018 11:24:11 +0100 Subject: [PATCH 3/3] Change sort.each to header,value and include locations/headers.erb --- templates/server/locations/headers.erb | 12 ++++++------ templates/server/server_header.erb | 14 +------------- templates/server/server_ssl_header.erb | 14 +------------- 3 files changed, 8 insertions(+), 32 deletions(-) diff --git a/templates/server/locations/headers.erb b/templates/server/locations/headers.erb index bad8ffebe..4c94dcb5f 100644 --- a/templates/server/locations/headers.erb +++ b/templates/server/locations/headers.erb @@ -1,11 +1,11 @@ -<%- @add_header.keys.sort.each do |key| -%> - <%- if @add_header[key] -%> - <%- if @add_header[key].is_a?(Hash) -%> - <%- @add_header[key].each do |sk, sv| -%> - add_header "<%= key %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; +<%- @add_header.sort.each do |header, value| -%> + <%- if value -%> + <%- if value.is_a?(Hash) -%> + <%- value.each do |sk, sv| -%> + add_header "<%= header %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; <%- end -%> <%- else -%> - add_header "<%= key %>" "<%= @add_header[key] %>"; + add_header "<%= header %>" "<%= value %>"; <%- end -%> <%- end -%> <%- end -%> diff --git a/templates/server/server_header.erb b/templates/server/server_header.erb index f7bef0f5a..ca58dd6f8 100644 --- a/templates/server/server_header.erb +++ b/templates/server/server_header.erb @@ -115,19 +115,7 @@ server { <% if Array(@resolver).count > 0 -%> resolver <% Array(@resolver).each do |r| %> <%= r %><% end %>; <% end -%> -<% if @add_header -%> - <%- @add_header.keys.sort.each do |key| -%> - <%- if @add_header[key] -%> - <%- if @add_header[key].is_a?(Hash) -%> - <%- @add_header[key].each do |sk, sv| -%> - add_header "<%= key %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; - <%- end -%> - <%- else -%> - add_header "<%= key %>" "<%= @add_header[key] %>"; - <%- end -%> - <%- end -%> - <%- end -%> -<% end -%> +<%= scope.function_template(["nginx/server/locations/headers.erb"]) %> <% if @maintenance -%> <%= @maintenance_value %>; <% end -%> diff --git a/templates/server/server_ssl_header.erb b/templates/server/server_ssl_header.erb index bd44ad683..67560dd91 100644 --- a/templates/server/server_ssl_header.erb +++ b/templates/server/server_ssl_header.erb @@ -144,16 +144,4 @@ server { <% Array(@passenger_env_var).each do |key,value| -%> passenger_env_var <%= key %> <%= value %>; <% end -%> -<% if @add_header -%> - <%- @add_header.keys.sort.each do |key| -%> - <%- if @add_header[key] -%> - <%- if @add_header[key].is_a?(Hash) -%> - <%- @add_header[key].each do |sk, sv| -%> - add_header "<%= key %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>; - <%- end -%> - <%- else -%> - add_header "<%= key %>" "<%= @add_header[key] %>"; - <%- end -%> - <%- end -%> - <%- end -%> -<% end -%> +<%= scope.function_template(["nginx/server/locations/headers.erb"]) %>