Skip to content

Commit

Permalink
Add support for not only HSTS, but any other arbitrary headers needed. (
Browse files Browse the repository at this point in the history
#44)

* Add support for not only HSTS, but any other arbitrary headers needed.

Closes #43

* Test headers with molecule

* Find out why test fails
  • Loading branch information
dale-c-anderson authored May 6, 2024
1 parent dbe6345 commit 41855ab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ nginx_listeners:
- port: 443
ssl: true
http2: true
add_headers:
- name: Strict-Transport-Security
value: "max-age=31536000; includeSubDomains"
always: true
server_name: www.example.com
aliases:
- example.com
Expand Down
2 changes: 1 addition & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ssl_protocols: 'TLSv1.2'
default_document: 'index.html index.php'
web_application: 'undefined'

nginx_aliases: []
nginx_aliases: [] # Deprecated and ignored. Use nginx_listeners[] instead.

# Location patterns to help enforce security.
nginx_drupal_uploads_dir_pattern: '/sites/.*/files' # Don't include a trailing slash.
Expand Down
4 changes: 4 additions & 0 deletions molecule/default/group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ nginx_server_name: www.bigcorp.com
nginx_listeners:
- server_name: "{{ nginx_server_name }}"
port: 80
add_headers:
- name: 'x-molecule-foo'
value: 'headers-are-fun'
always: true
php_version: "{{ php_default_version }}"
web_root_dir_name: wwwroot
web_application: php
Expand Down
17 changes: 17 additions & 0 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@
- curl_unauth_result.failed
- '"401 Unauthorized" in curl_unauth_result.stdout or "401 Unauthorized" in curl_unauth_result.stderr'

- name: Output running nginx config
command: /usr/sbin/nginx -T
register: nginx_config

- name: Debug nginx_config
debug:
var: nginx_config

- name: Run a new test to examine headers
command: curl --fail -sSLI http://test:test@{{ nginx_server_name }}/molecule-curl-test.php --resolve {{ nginx_server_name }}:80:127.0.0.1
register: curl_headers_result

- name: Make sure our x-foo header was present in the response.
assert:
that:
- '"x-molecule-foo: headers-are-fun" in curl_headers_result.stdout'


- name: Verify role part 2 - change PHP version on the vhost to make sure things don't blow up.
hosts: all
Expand Down
3 changes: 3 additions & 0 deletions templates/etc/nginx/sites-available/ACCOUNT-PROJECT.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
server {
listen {{ listener.port | default('80') }}
{%- if listener.ssl | default(false) %} ssl {%- if listener.http2 | default(true) %} http2 {%- endif -%}{%- endif -%};
{% for header in listener.add_headers|default([]) %}
add_header {{ header.name }} "{{ header.value }}"{{ header.always | default(false) | ternary(' always', '') }};
{% endfor %}
server_name {{ listener.server_name }} {{ (listener.aliases | default([])) | join(' ') }};
access_log {{ nginx_access_log_conf }};
error_log /var/log/vhosts/{{ linux_owner }}/{{ project }}/nginx-error.log;
Expand Down

0 comments on commit 41855ab

Please sign in to comment.