Repository navigation
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·163 lines (142 loc) · 3.7 KB
/
Copy pathentrypoint.sh
File metadata and controls
executable file
·163 lines (142 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
CONFIG_DIR=/etc/openvpn/proxy/
port=${PROXY_PORT:-8080}
ip=${PROXY_BIND_IP:-0.0.0.0}
ui_port=${UI_PORT:-3000}
api_port=${API_PORT:-5000}
# Optional environment variables for allowed IPs and public endpoints
# ALLOWED_IPS is a comma-separated list of IP/CDIRs that are allowed to access the proxy. Example: 1.2.3.0/24
# PUBLIC_ENDPOINTS is a comma-separated list of public endpoints that should be accessible. Example: /api/units/ingest
allowed_ips=${ALLOWED_IPS:-""}
public_endpoints=${PUBLIC_ENDPOINTS:-""}
# Router/service/middleware names must be unique across all generated files: they merge into one
# traefik config, and duplicates silently drop (first-wins, alphabetical file order).
output_public_routers () {
if [ -n "$public_endpoints" ]; then
OLD_IFS="$IFS"
IFS=,
set -- $public_endpoints
IFS="$OLD_IFS"
for endpoint; do
name=$(echo "$endpoint" | tr -cd '[:alnum:]')
printf " routerapi%s:\n" "$name"
printf " entryPoints:\n"
printf " - web\n"
# higher than routerapi so public endpoints bypass the IP allow list
printf " priority: 60\n"
printf " middlewares:\n"
printf " - stripprefix-api\n"
printf " service: service-api\n"
printf " rule: PathPrefix(\`%s\`)\n" "$endpoint"
done
fi
}
output_middlewares_list () {
printf " - %s\n" "$1"
if [ -n "$allowed_ips" ]; then
printf " - ipallowlist\n"
fi
}
output_ui_middlewares_list () {
if [ -n "$allowed_ips" ]; then
printf " middlewares:\n"
printf " - ipallowlist\n"
fi
}
output_whitelist_middleware () {
if [ -n "$allowed_ips" ]; then
printf " ipallowlist:\n"
printf " ipAllowList:\n"
printf " ipStrategy:\n"
printf " depth: 1\n"
printf " sourceRange:\n"
OLD_IFS="$IFS"
IFS=,
set -- $allowed_ips
IFS="$OLD_IFS"
for ip; do
printf " - \"%s\"\n" "$ip"
done
fi
}
if [ ! -d "$CONFIG_DIR" ]; then
mkdir -p "$CONFIG_DIR"
fi
cat <<EOF > /config.yaml
global:
checkNewVersion: false
sendAnonymousUsage: false
entryPoints:
web:
address: "$ip:$port"
forwardedHeaders:
trustedIPs:
- "127.0.0.1/32"
accessLog: {}
providers:
file:
directory: $CONFIG_DIR
watch: true
serversTransport:
insecureSkipVerify: true
EOF
cat << EOF > "${CONFIG_DIR}api.yaml"
http:
routers:
$(output_public_routers)
routerapi:
entryPoints:
- web
priority: 50
middlewares:
$(output_middlewares_list stripprefix-api)
service: service-api
rule: PathPrefix(\`/api\`)
services:
service-api:
loadBalancer:
servers:
- url: http://127.0.0.1:${api_port}/
passHostHeader: true
middlewares:
stripprefix-api:
stripPrefix:
prefixes:
- "/api"
$(output_whitelist_middleware)
EOF
# separating single stripprefix into -api and -ui, shared name across files collides on merge
# ipallowlist stays duplicated in both files, gets merged anyway
cat << EOF > "${CONFIG_DIR}ui.yaml"
http:
routers:
routerui:
entryPoints:
- web
priority: 50
middlewares:
$(output_middlewares_list stripprefix-ui)
service: service-ui
rule: PathPrefix(\`/ui\`)
routerui-root:
entryPoints:
- web
# fallback to keep backward compatibility
priority: 1
$(output_ui_middlewares_list)
service: service-ui
rule: PathPrefix(\`/\`)
services:
service-ui:
loadBalancer:
servers:
- url: http://127.0.0.1:${ui_port}/
passHostHeader: true
middlewares:
stripprefix-ui:
stripPrefix:
prefixes:
- "/ui"
$(output_whitelist_middleware)
EOF
exec "$@"