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