]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/recipes/frontend.rb
web: Use Fastly client IP, remove Cloudflare
[chef.git] / cookbooks / web / recipes / frontend.rb
1 #
2 # Cookbook:: web
3 # Recipe:: frontend
4 #
5 # Copyright:: 2011, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 node.default[:memcached][:ip_address] = node.internal_ipaddress || "127.0.0.1"
21
22 include_recipe "memcached"
23 include_recipe "apache"
24 include_recipe "fail2ban"
25 include_recipe "web::rails"
26 include_recipe "web::cgimap"
27
28 web_passwords = data_bag_item("web", "passwords")
29
30 apache_module "alias"
31 apache_module "expires"
32 apache_module "headers"
33 apache_module "proxy"
34 apache_module "proxy_fcgi"
35 apache_module "lbmethod_byrequests"
36 apache_module "lbmethod_bybusyness"
37 apache_module "remoteip"
38 apache_module "reqtimeout"
39 apache_module "rewrite"
40 apache_module "unique_id"
41
42 apache_site "default" do
43   action [:disable]
44 end
45
46 remote_directory "#{node[:web][:base_directory]}/static" do
47   source "static"
48   owner "root"
49   group "root"
50   mode "755"
51   files_owner "root"
52   files_group "root"
53   files_mode "644"
54 end
55
56 template "#{node[:web][:base_directory]}/static/.well-known/security.txt" do
57   source "security.txt.erb"
58   owner "root"
59   group "root"
60   mode "644"
61 end
62
63 remote_file "#{Chef::Config[:file_cache_path]}/fastly-ip-list.json" do
64   source "https://api.fastly.com/public-ip-list"
65   compile_time true
66   ignore_failure true
67 end
68
69 fastlyips = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/fastly-ip-list.json"))
70
71 apache_site "www.openstreetmap.org" do
72   template "apache.frontend.erb"
73   variables :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
74             :status => node[:web][:status],
75             :secret_key_base => web_passwords["secret_key_base"]
76 end
77
78 template "/etc/logrotate.d/apache2" do
79   source "logrotate.apache.erb"
80   owner "root"
81   group "root"
82   mode "644"
83 end
84
85 fail2ban_filter "apache-request-timeout" do
86   failregex '^<ADDR> .* "-" 408 .*$'
87 end
88
89 fail2ban_jail "apache-request-timeout" do
90   filter "apache-request-timeout"
91   logpath "/var/log/apache2/access.log"
92   ports [80, 443]
93 end
94
95 fail2ban_filter "apache-trackpoints-timeout" do
96   failregex '^<ADDR> .* "GET /api/0\.6/trackpoints\?.*" 408 .*$'
97 end
98
99 fail2ban_jail "apache-trackpoints-timeout" do
100   filter "apache-trackpoints-timeout"
101   logpath "/var/log/apache2/access.log"
102   ports [80, 443]
103   bantime "12h"
104   findtime "30m"
105 end
106
107 fail2ban_filter "apache-notes-search" do
108   failregex '^<ADDR> .* "GET /api/0\.6/notes/search\?q=abcde&.*$'
109 end
110
111 fail2ban_jail "apache-notes-search" do
112   filter "apache-notes-search"
113   logpath "/var/log/apache2/access.log"
114   ports [80, 443]
115 end
116
117 if %w[database_offline database_readonly].include?(node[:web][:status])
118   service "rails-jobs@mailers" do
119     action :stop
120   end
121
122   service "rails-jobs@storage" do
123     action :stop
124   end
125
126   service "rails-jobs@traces" do
127     action :stop
128   end
129 else
130   service "rails-jobs@mailers" do
131     action [:enable, :start]
132     supports :restart => true
133     subscribes :restart, "rails_port[www.openstreetmap.org]"
134     subscribes :restart, "systemd_service[rails-jobs@]"
135   end
136
137   service "rails-jobs@storage" do
138     action [:enable, :start]
139     supports :restart => true
140     subscribes :restart, "rails_port[www.openstreetmap.org]"
141     subscribes :restart, "systemd_service[rails-jobs@]"
142   end
143
144   service "rails-jobs@traces" do
145     action [:enable, :start]
146     supports :restart => true
147     subscribes :restart, "rails_port[www.openstreetmap.org]"
148     subscribes :restart, "systemd_service[rails-jobs@]"
149   end
150 end
151
152 template "/usr/local/bin/deliver-message" do
153   source "deliver-message.erb"
154   owner "rails"
155   group "rails"
156   mode "0700"
157   variables :secret_key_base => web_passwords["secret_key_base"]
158 end