]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tilecache/templates/default/nginx_generate_tilecache_qos_map.erb
a8f9378233dee2d2dfacbec53d535390036873f1
[chef.git] / cookbooks / tilecache / templates / default / nginx_generate_tilecache_qos_map.erb
1 #!/bin/bash
2 # DO NOT EDIT - This file is being maintained by Chef
3
4 set -e
5 NUM_TOKENS=4 # current + 4
6 VALID_TOKEN=3600 # in seconds
7 QOS_TOKENS=($(/usr/bin/oathtool --totp --window=${NUM_TOKENS} --time-step-size=${VALID_TOKEN}s -b <%= @totp_key %>))
8
9 # ${qos_tokens[3]/[-1] } = OSM.org exclusive / current
10 # ${qos_tokens[2]/[-2] } = tile.openstreetmap.org default
11 # ${qos_tokens[1]/[-3] } = stale ~ 1 hour
12 # ${qos_tokens[0]} = expired
13
14 # Test if number of tokens returned by oathtool is expected number
15 if [ "${#QOS_TOKENS[@]}" -ne "$((${NUM_TOKENS}+1))" ]; then
16   >&2 echo "ERROR: Unexpected number of tokens"
17   exit 1
18 fi
19
20 QOS_TOKEN_OSM=${QOS_TOKENS[-1]} # Cookie set by openstreetmap.org
21 QOS_TOKEN_OSM_STALE=${QOS_TOKENS[-2]} # Cookie set by openstreetmap.org stale
22 QOS_TOKEN_DEFAULT=${QOS_TOKENS[-3]} # Cookie presented by tile.openstreetmap.org to browsers
23 QOS_TOKEN_STALE=${QOS_TOKENS[-4]} # Cookie which has become stale and will be replaced
24
25 if [ -z "$QOS_TOKEN_OSM" -o -z "$QOS_TOKEN_DEFAULT" -o -z "$QOS_TOKEN_STALE" ]; then
26   >&2 echo "ERROR: Unexpected blank token"
27   exit 2
28 fi
29
30 cat <<EOF >/etc/nginx/conf.d/tile_qos_rates.map
31 default 24576; # Default Rate (No QoS cookie)
32 "${QOS_TOKEN_STALE}" 24576; # Stale
33 "${QOS_TOKEN_DEFAULT}" 24576; # Default
34 "${QOS_TOKEN_OSM_STALE}" 32768; # Exclusive Stale
35 "${QOS_TOKEN_OSM}" 32768; # Exclusive
36 EOF
37
38 cat <<EOF >/etc/nginx/conf.d/tile_qos_cookies.map
39 default 'qos_token=${QOS_TOKEN_DEFAULT}; Max-Age=${VALID_TOKEN}; Domain=openstreetmap.org; Path=/'; # Cookie Domain per RFC6265
40 "${QOS_TOKEN_DEFAULT}" ''; # Do not Set-Cookie. # Default
41 "${QOS_TOKEN_OSM}" ''; # Do not Set-Cookie. # Exclusive
42 EOF
43
44 # Check config, reload config and fail safe
45 /etc/init.d/nginx configtest && /bin/systemctl try-reload-or-restart nginx