Repository navigation
Expand file tree
/
Copy pathinstall-platform.sh
More file actions
269 lines (219 loc) · 7.53 KB
/
Copy pathinstall-platform.sh
File metadata and controls
269 lines (219 loc) · 7.53 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/sh
set -e
get_key() {
head -c 32 /dev/urandom | base64 -w 0
}
setup_keychain() {
echo "Creating keys."
read -p "What hostname will challenges be hosted on (e.g., challs.example.com)? " -r challs_hostname </dev/tty
challs_ip=""
if [ -n "$challs_hostname" ]; then
resolved_ip=$(getent hosts "$challs_hostname" | awk '{print $1}' | head -n 1)
if [ -n "$resolved_ip" ]; then
echo "Hostname '$challs_hostname' resolved to IP: $resolved_ip"
challs_ip="$resolved_ip"
else
echo "WARN: Could not resolve IP for hostname '$challs_hostname'."
fi
fi
if [ -z "$challs_ip" ]; then
read -p "Please enter the challenges IP address directly: " -r direct_ip </dev/tty
challs_ip="$direct_ip"
fi
challs_ip="${challs_ip:-'<insert-chall-ip>'}"
mkdir keys
cd keys
mkdir docker
cd docker
# Create CA
openssl genrsa -out ca-key.pem 4096
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem <<EOF 2> /dev/null
.
.
.
.
.
$1
.
EOF
# Generate server key & cert signing request
openssl genrsa -out server-key.pem 4096
openssl req -subj "/CN=docker" -sha256 -new -key server-key.pem -out server.csr
cat >extfile.cnf <<EOF
subjectAltName = DNS:$challs_hostname,IP:$challs_ip,IP:127.0.0.1
extendedKeyUsage = serverAuth
EOF
# Sign server cert
openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
# Create client key & cert signing request
openssl genrsa -out key.pem 4096
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
cat >extfile-client.cnf <<EOF
extendedKeyUsage = clientAuth
EOF
# Sign client cert
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile-client.cnf
########################
### Repeat for caddy ###
########################
cd ..
mkdir caddy
cd caddy
# Create CA
openssl genrsa -out ca-key.pem 4096
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem <<EOF 2> /dev/null
.
.
.
.
.
$1
.
EOF
# Generate server key & cert signing request
openssl genrsa -out server-key.pem 4096
openssl req -subj "/CN=caddy" -sha256 -new -key server-key.pem -out server.csr
cat >extfile.cnf <<EOF
subjectAltName = DNS:$challs_hostname,IP:$challs_ip,IP:127.0.0.1
extendedKeyUsage = serverAuth
EOF
# Sign server cert
openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
# Create client key & cert signing request
openssl genrsa -out key.pem 4096
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
cat >extfile-client.cnf <<EOF
extendedKeyUsage = clientAuth
EOF
# Sign client cert
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile-client.cnf
echo "Created keys and certs"
cd ../..
tar -czf keys.tar.gz keys
read_pem_json() {
<"$1" sed 's/\\/\\\\/g; s/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g'
}
cd keys
cat <<EOF > ../keychain.json
[{
"id": "default",
"caddy": {
"endpoint": "https://$challs_ip:995",
"base": "$challs_hostname",
"cacert": "$(read_pem_json caddy/ca.pem)",
"cert": "$(read_pem_json caddy/cert.pem)",
"key": "$(read_pem_json caddy/key.pem)"
},
"docker": {
"docker": {
"type": "ssl",
"address": "$challs_ip:996",
"ca": "$(read_pem_json docker/ca.pem)",
"cert": "$(read_pem_json docker/cert.pem)",
"key": "$(read_pem_json docker/key.pem)"
},
"docker_credentials": {
"username": "<docker-registry-username>",
"password": "<docker-registry-password>",
"serveraddress": "<docker-registry-address>"
},
"image_prefix": "",
"repo": "<docker-registry-repo>"
}
}]
EOF
cd ..
rm -r keys
echo "Keychain created. Next steps:"
echo "... Copy keys.tar.gz (in install dir) to your challenges VM and run install-challenges.sh"
echo "... Edit keychain.json (in install dir) to add docker registry credentials."
}
do_install() {
echo "Running nerine install script."
if [ ! "$(id -u)" = 0 ]; then
echo "ERROR: You must run this script as root."
exit 1
fi
if [ ! -x "$(command -v curl)" ]; then
echo "ERROR: curl is not available. You must have curl to install nerine."
exit 1
fi
NERINE_INSTALL_PATH="${NERINE_INSTALL_PATH:-/srv/nerine}"
if [ -d "$NERINE_INSTALL_PATH" ]; then
echo "nerine appears to already have been installed in ${NERINE_INSTALL_PATH}"
echo "Would you like to setup deployer keychain (y/N)? "
read -r result </dev/tty
if [ "$result" = "n" ] || [ "$result" = "N" ]; then
exit 1
fi
# This can be read from the installation but lazy
read -p "What host will nerine be hosted at (used for CA CN)? " -r nerine_url </dev/tty
nerine_url="${nerine_url##*://}"
cd "$NERINE_INSTALL_PATH"
setup_keychain "$nerine_url"
return
fi
mkdir "$NERINE_INSTALL_PATH"
cd "$NERINE_INSTALL_PATH"
echo "Installing dependencies."
if [ ! -x "$(command -v docker)" ]; then
curl -fsS https://get.docker.com | sh
fi
DOCKER_CONFIG=${DOCKER_CONFIG:-/usr/local/lib/docker}
if [ ! -f $DOCKER_CONFIG/cli-plugins/docker-compose ]; then
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL "https://github.com/docker/compose/releases/download/v2.40.1/docker-compose-$(uname -s)-$(uname -m)" -o $DOCKER_CONFIG/cli-plugins/docker-compose
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
fi
mkdir -p site-assets
NERINE_POSTGRES_PASSWORD=$(get_key)
read -p "What host will nerine be hosted at? " -r nerine_url </dev/tty
nerine_url="${nerine_url##*://}"
echo "Generating configuration."
NERINE_ADMIN_TOKEN="${NERINE_ADMIN_TOKEN:-$(get_key)}"
printf "%s\n" \
"RUST_LOG=debug" \
"CORS_ORIGIN=https://${nerine_url}" \
"NERINE_POSTGRES_PASSWORD=${NERINE_POSTGRES_PASSWORD}" \
"DATABASE_URL=postgres://nerine:${NERINE_POSTGRES_PASSWORD}@db/nerine" \
"ADMIN_TOKEN=${NERINE_ADMIN_TOKEN}" \
"JWT_SECRET=$(get_key)" \
> .env
printf "%s\n" \
"name = \"nerineCTF\"" \
"description = \"\"\"
Write markdown here for the front page!
You can put things like sponsor logos in \`site-assets\`, then access them at \`https://${nerine_url}/assets/\`
\"\"\"" \
"start_time = \"$(date +%FT%T.000)\"" \
"end_time = \"$(date -d +1week +%FT%T.000)\"" \
> event.toml
mkdir caddy
cat <<EOF > caddy/Caddyfile
https://${nerine_url:-"<insert-platform-url>"} {
reverse_proxy /api/* localhost:3333
reverse_proxy /* localhost:3334
log {
output file /var/log/caddy/access.log {
roll_size 1gb
roll_keep 20
roll_keep_for 720h
}
}
}
EOF
NERINE_GIT_REF="${NERINE_GIT_REF:-main}"
echo "Pulling Images."
curl -fsSo docker-compose.yml "https://raw.githubusercontent.com/ctf-gg/nerine/$NERINE_GIT_REF/docker-compose.prod.yml"
docker compose pull
read -p "Would you like to setup deployer keychain (Y/n)? " -r result </dev/tty
if [ "$result" = "n" ] || [ "$result" = "N" ]; then
return
fi
setup_keychain "$nerine_url"
}
do_install
echo "Finished installation to $NERINE_INSTALL_PATH."
echo "... Your admin token is: $NERINE_ADMIN_TOKEN. It can also be found in $NERINE_INSTALL_PATH/.env"
echo "... Configuration files can be found in $NERINE_INSTALL_PATH."
echo "... If you would like to start nerine, run \`docker compose up -d\` in $NERINE_INSTALL_PATH."