deploy/cloud-init.yaml
316 lines · 10444 bytes
1#cloud-config
2# gitbay VPS bootstrap (Ubuntu 24.04).
3#
4# What this does on first boot:
5# - moves the host's admin sshd to port 2222 (gitbay's embedded SSH
6# listener owns port 22) — CONNECT ON 2222 AFTER FIRST BOOT
7# - creates the unprivileged gitbay user and directory layout
8# - installs /etc/gitbay/config.toml, the systemd unit (with
9# CAP_NET_BIND_SERVICE so ports 22/80/443 work without root), and a
10# nightly backup timer
11# - opens ufw for 22, 80, 443, 2222
12#
13# It does NOT install the gitbayd binary (it is not hosted anywhere yet);
14# scp it to /usr/local/bin/gitbayd afterward and `systemctl start gitbayd`.
15
16package_update: true
17packages:
18 - git
19 - ufw
20 - unattended-upgrades
21 - fail2ban
22 # The CI runner shares this host and the suite drives them; without them
23 # the LFS and signature tests skip themselves and CI goes green having
24 # tested less.
25 - git-lfs
26 - gnupg
27
28write_files:
29 # Admin sshd on 2222. Ubuntu 24.04 socket-activates sshd, so the port
30 # must change in BOTH sshd_config and the socket unit.
31 - path: /etc/ssh/sshd_config.d/60-gitbay-port.conf
32 content: |
33 Port 2222
34 PasswordAuthentication no
35 # Throttle unauthenticated connection floods on the admin sshd
36 # (gitbayd's own port 22 is throttled by limits.ssh_auth_rate).
37 MaxStartups 10:30:60
38 MaxAuthTries 3
39 LoginGraceTime 20
40
41 # OS security patches applied automatically; reboot at 04:30 if needed.
42 - path: /etc/apt/apt.conf.d/51gitbay-unattended
43 content: |
44 Unattended-Upgrade::Allowed-Origins { "${distro_id}:${distro_codename}-security"; };
45 Unattended-Upgrade::Automatic-Reboot "true";
46 Unattended-Upgrade::Automatic-Reboot-Time "04:30";
47 APT::Periodic::Update-Package-Lists "1";
48 APT::Periodic::Unattended-Upgrade "1";
49
50 # fail2ban watches the admin sshd for auth failures.
51 - path: /etc/fail2ban/jail.d/gitbay.conf
52 content: |
53 [sshd]
54 enabled = true
55 port = 2222
56 backend = systemd
57 maxretry = 5
58 bantime = 1h
59
60 # Heartbeat: disk/service/cert status to journald every run, and to a
61 # webhook as well if one is set in /etc/gitbay/monitor.url. Exits non-zero
62 # on an alert so the unit shows up in systemctl --failed.
63 - path: /usr/local/bin/gitbay-monitor.sh
64 permissions: "0755"
65 content: |
66 #!/bin/sh
67 set -eu
68 disk=$(df -P /var/lib/gitbay | awk 'NR==2{print $5}')
69 svc=$(systemctl is-active gitbayd || true)
70 # Soonest ACME cert expiry. Reporting whichever name sorted first said
71 # nothing about the one actually about to lapse, and the cache is under
72 # acme/, so this read autocert/ and reported n/a forever.
73 cert=/var/lib/gitbay/acme
74 exp="n/a"
75 days=""
76 if [ -d "$cert" ]; then
77 soonest=""
78 for f in "$cert"/*; do
79 [ -f "$f" ] || continue
80 case "${f##*/}" in acme_account*) continue ;; esac
81 end=$(openssl x509 -enddate -noout -in "$f" 2>/dev/null | cut -d= -f2 || true)
82 [ -n "$end" ] || continue
83 secs=$(date -u -d "$end" +%s 2>/dev/null || true)
84 [ -n "$secs" ] || continue
85 if [ -z "$soonest" ] || [ "$secs" -lt "$soonest" ]; then
86 soonest="$secs"
87 exp="$end"
88 fi
89 done
90 if [ -n "$soonest" ]; then
91 days=$(( (soonest - $(date -u +%s)) / 86400 ))
92 fi
93 fi
94 alert=""
95 if [ "$svc" != "active" ]; then
96 alert="gitbayd is $svc; "
97 fi
98 pct=$(echo "$disk" | tr -d '%')
99 if [ "$pct" -ge 85 ]; then
100 alert="${alert}disk ${disk}; "
101 fi
102 if [ -n "$days" ] && [ "$days" -lt 21 ]; then
103 alert="${alert}cert expires in ${days}d; "
104 fi
105 # journald always gets the reading, so an unset webhook cannot make a
106 # sick host look like a quiet one.
107 echo "disk=$disk service=$svc cert_expires=$exp${days:+ cert_days=$days}"
108 url_file=/etc/gitbay/monitor.url
109 if [ -f "$url_file" ]; then
110 body=$(printf '{"disk":"%s","service":"%s","cert_expires":"%s","alert":"%s"}' "$disk" "$svc" "$exp" "$alert")
111 if ! curl -fsS -m 10 -H 'Content-Type: application/json' -d "$body" "$(cat "$url_file")" >/dev/null; then
112 echo "monitor webhook post failed" >&2
113 fi
114 fi
115 if [ -n "$alert" ]; then
116 echo "$alert" >&2
117 exit 1
118 fi
119
120 - path: /etc/systemd/system/gitbay-monitor.service
121 content: |
122 [Unit]
123 Description=gitbay host heartbeat
124 [Service]
125 Type=oneshot
126 ExecStart=/usr/local/bin/gitbay-monitor.sh
127
128 - path: /etc/systemd/system/gitbay-monitor.timer
129 content: |
130 [Unit]
131 Description=gitbay host heartbeat
132 [Timer]
133 OnCalendar=*-*-* *:00:00 UTC
134 Persistent=true
135 [Install]
136 WantedBy=timers.target
137 - path: /etc/systemd/system/ssh.socket.d/override.conf
138 content: |
139 [Socket]
140 ListenStream=
141 ListenStream=2222
142
143 - path: /etc/gitbay/config.toml
144 permissions: "0640"
145 content: |
146 [server]
147 root = "/var/lib/gitbay"
148 site_url = "https://gitbay.org"
149
150 [ssh]
151 mode = "embedded"
152 port = 22
153
154 [http]
155 addr = ":443"
156 tls = "acme"
157 acme_email = "hello@gitbay.org"
158 acme_http_addr = ":80"
159
160 [web]
161 mode = "view_only"
162
163 [registration]
164 mode = "closed"
165
166 - path: /etc/systemd/system/gitbayd.service
167 content: |
168 [Unit]
169 Description=gitbay forge daemon
170 After=network-online.target
171 Wants=network-online.target
172
173 [Service]
174 User=gitbay
175 Group=gitbay
176 ExecStart=/usr/local/bin/gitbayd --config /etc/gitbay/config.toml serve
177 Restart=on-failure
178 RestartSec=3
179
180 # Bind 22/80/443 without root; no privilege escalation afterward.
181 AmbientCapabilities=CAP_NET_BIND_SERVICE
182 CapabilityBoundingSet=CAP_NET_BIND_SERVICE
183 NoNewPrivileges=yes
184 ProtectSystem=strict
185 ProtectHome=yes
186 ReadWritePaths=/var/lib/gitbay /var/backups/gitbay
187 PrivateTmp=yes
188 ProtectKernelTunables=yes
189 ProtectKernelModules=yes
190 ProtectControlGroups=yes
191 ProtectHostname=yes
192 ProtectClock=yes
193 ProtectKernelLogs=yes
194 RestrictSUIDSGID=yes
195 RestrictNamespaces=yes
196 RestrictRealtime=yes
197 LockPersonality=yes
198 MemoryDenyWriteExecute=yes
199 PrivateDevices=yes
200 # IPv4/IPv6 for listeners and outbound git/ssh; UNIX for the hook socket.
201 RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
202 # Allow only ordinary service syscalls; the daemon spawns git and ssh,
203 # so keep @process/@exec available (both are within @system-service).
204 SystemCallFilter=@system-service
205 SystemCallErrorNumber=EPERM
206 SystemCallArchitectures=native
207
208 [Install]
209 WantedBy=multi-user.target
210
211 - path: /usr/local/bin/gitbay-backup.sh
212 permissions: "0755"
213 content: |
214 #!/bin/sh
215 # Nightly consistent backup; keeps the last 7 locally.
216 # To ship offsite, add an rclone/s3 upload of $out here.
217 set -eu
218 # The archive carries the database, so it gets the database's mode.
219 umask 027
220 dir=/var/backups/gitbay
221 out="$dir/gitbay-$(date -u +%Y%m%d-%H%M%S).tar.gz"
222 /usr/local/bin/gitbayd --config /etc/gitbay/config.toml admin backup --out "$out"
223 ls -1t "$dir"/gitbay-*.tar.gz | tail -n +8 | xargs -r rm --
224
225 # Hourly database-only snapshot. The nightly full backup below is the one
226 # that can rebuild the host; this one exists because the database holds
227 # issues, merge requests and comments, which unlike the repositories have
228 # no second copy anywhere. 48 of them is two days at a few MB each.
229 - path: /usr/local/bin/gitbay-db-backup.sh
230 permissions: "0755"
231 content: |
232 #!/bin/sh
233 set -eu
234 # The archive is the whole database, so it gets the database's mode.
235 umask 027
236 dir=/var/backups/gitbay/db
237 mkdir -p "$dir"
238 chmod 0750 "$dir"
239 out="$dir/gitbay-db-$(date -u +%Y%m%d-%H%M%S).tar.gz"
240 /usr/local/bin/gitbayd --config /etc/gitbay/config.toml admin backup --db-only --out "$out"
241 ls -1t "$dir"/gitbay-db-*.tar.gz | tail -n +49 | xargs -r rm --
242
243 - path: /etc/systemd/system/gitbay-db-backup.service
244 content: |
245 [Unit]
246 Description=gitbay hourly database backup
247 [Service]
248 Type=oneshot
249 User=gitbay
250 ExecStart=/usr/local/bin/gitbay-db-backup.sh
251
252 - path: /etc/systemd/system/gitbay-db-backup.timer
253 content: |
254 [Unit]
255 Description=gitbay hourly database backup
256 [Timer]
257 OnCalendar=*-*-* *:20:00 UTC
258 RandomizedDelaySec=5m
259 Persistent=true
260 [Install]
261 WantedBy=timers.target
262
263 - path: /etc/systemd/system/gitbay-backup.service
264 content: |
265 [Unit]
266 Description=gitbay nightly backup
267 [Service]
268 Type=oneshot
269 User=gitbay
270 ExecStart=/usr/local/bin/gitbay-backup.sh
271
272 - path: /etc/systemd/system/gitbay-backup.timer
273 content: |
274 [Unit]
275 Description=gitbay nightly backup
276 [Timer]
277 OnCalendar=*-*-* 09:00:00 UTC
278 RandomizedDelaySec=15m
279 Persistent=true
280 [Install]
281 WantedBy=timers.target
282
283 - path: /etc/systemd/system/gitbay-gc.service
284 content: |
285 [Unit]
286 Description=gitbay weekly repository maintenance
287 [Service]
288 Type=oneshot
289 User=gitbay
290 ExecStart=/usr/local/bin/gitbayd --config /etc/gitbay/config.toml admin gc
291
292 - path: /etc/systemd/system/gitbay-gc.timer
293 content: |
294 [Unit]
295 Description=gitbay weekly repository maintenance
296 [Timer]
297 OnCalendar=Sun *-*-* 07:00:00 UTC
298 RandomizedDelaySec=30m
299 Persistent=true
300 [Install]
301 WantedBy=timers.target
302
303runcmd:
304 - adduser --system --group --home /var/lib/gitbay --shell /usr/sbin/nologin gitbay
305 - install -d -o gitbay -g gitbay -m 750 /var/lib/gitbay /var/backups/gitbay
306 - chgrp gitbay /etc/gitbay/config.toml /etc/gitbay
307 - ufw allow 22/tcp
308 - ufw allow 80/tcp
309 - ufw allow 443/tcp
310 - ufw allow 2222/tcp
311 - ufw --force enable
312 - systemctl daemon-reload
313 - systemctl restart ssh.socket || systemctl restart ssh
314 - systemctl enable gitbayd gitbay-backup.timer gitbay-db-backup.timer gitbay-gc.timer gitbay-monitor.timer
315 - systemctl start gitbay-backup.timer gitbay-db-backup.timer gitbay-gc.timer gitbay-monitor.timer
316 - systemctl enable --now unattended-upgrades fail2ban