Repository navigation
Expand file tree
/
Copy pathmigrate-seaweedfs-kek.sh
More file actions
120 lines (104 loc) · 4.39 KB
/
Copy pathmigrate-seaweedfs-kek.sh
File metadata and controls
120 lines (104 loc) · 4.39 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
echo "${_group}Migrating the SeaweedFS encryption key ..."
# weed mini generates /data/.mini_sse_kek before it reads the KEK left by
# weed server in the filer. Recover that filer key first so mini does not start
# with two different keys. The temporary server keeps S3 disabled, so reading
# the filer cannot trigger another KEK migration.
migration_marker="/data/.sentry-seaweedfs-kek-migrated"
migration_state=$(
$dcr --no-deps -T --entrypoint sh seaweedfs -c \
"if test -d /data/filerldb2 && test ! -e $migration_marker; then printf needed; else printf skipped; fi"
)
if [[ "$migration_state" == "skipped" ]]; then
echo "No legacy SeaweedFS encryption key migration is needed."
elif [[ "$migration_state" == "needed" ]]; then
(
migration_container="sentry-seaweedfs-kek-migration-$$"
trap '$CONTAINER_ENGINE rm -f "$migration_container" >/dev/null 2>&1 || true' EXIT
$dc run -d --no-deps --name "$migration_container" --entrypoint weed seaweedfs \
server \
-dir=/data \
-master=true \
-master.port=9333 \
-master.telemetry=false \
-volume=false \
-filer=true \
-filer.port=8888 \
-s3=false \
-webdav=false \
-ip.bind=0.0.0.0 >/dev/null
filer_ready=0
for _ in $(seq 1 60); do
if $CONTAINER_ENGINE exec "$migration_container" sh -c \
"HTTP_PROXY='' HTTPS_PROXY='' http_proxy='' https_proxy='' wget -q -O /dev/null http://127.0.0.1:8888/"; then
filer_ready=1
break
fi
if [[ "$($CONTAINER_ENGINE inspect --format '{{.State.Running}}' "$migration_container")" != "true" ]]; then
break
fi
sleep 2
done
if [[ "$filer_ready" != "1" ]]; then
echo "The temporary SeaweedFS filer did not become ready." >&2
$CONTAINER_ENGINE logs "$migration_container" >&2
exit 1
fi
$CONTAINER_ENGINE exec "$migration_container" sh -c \
"HTTP_PROXY='' HTTPS_PROXY='' http_proxy='' https_proxy='' wget -S -O /tmp/sentry-existing-kek http://127.0.0.1:8888/etc/s3/sse_kek 2>/tmp/sentry-kek-response" || true
status_code=$($CONTAINER_ENGINE exec "$migration_container" awk \
'$1 ~ /^HTTP\// { code=$2 } END { print code }' /tmp/sentry-kek-response)
if [[ "$status_code" == "404" ]]; then
echo "No existing filer encryption key was found."
$CONTAINER_ENGINE exec "$migration_container" touch "$migration_marker"
exit 0 # Exit only the migration subshell; install.sh continues.
fi
if [[ "$status_code" != "200" ]]; then
echo "Failed to read the existing SeaweedFS encryption key (HTTP ${status_code:-unknown})." >&2
exit 1
fi
restore_xtrace=0
if [[ "$-" == *x* ]]; then
restore_xtrace=1
set +x
fi
stored_kek=$($CONTAINER_ENGINE exec "$migration_container" cat /tmp/sentry-existing-kek)
if [[ "$stored_kek" =~ ^[[:xdigit:]]{64}$ ]]; then
recovered_kek=${stored_kek,,}
else
recovered_kek=$(
printf '%s' "$stored_kek" | $CONTAINER_ENGINE run --rm -i \
--user 0 \
--entrypoint python3 \
-v "$(pwd)/scripts/recover_seaweedfs_kek.py:/recover_seaweedfs_kek.py:ro" \
--volumes-from "${migration_container}:ro" \
sentry-self-hosted-local \
/recover_seaweedfs_kek.py /data/.mini_kek_passphrase
)
fi
if [[ ! "$recovered_kek" =~ ^[[:xdigit:]]{64}$ ]]; then
echo "The recovered SeaweedFS encryption key is invalid." >&2
exit 1
fi
if current_kek=$($CONTAINER_ENGINE exec "$migration_container" sh -c 'test -f /data/.mini_sse_kek && cat /data/.mini_sse_kek'); then
:
else
current_kek=""
fi
if [[ "$current_kek" != "$recovered_kek" ]]; then
$CONTAINER_ENGINE exec "$migration_container" sh -c \
'if test -f /data/.mini_sse_kek && ! test -e /data/.mini_sse_kek.pre-migration; then cp -p /data/.mini_sse_kek /data/.mini_sse_kek.pre-migration; fi'
printf '%s' "$recovered_kek" | $CONTAINER_ENGINE exec -i "$migration_container" sh -c \
'umask 077; cat > /data/.mini_sse_kek'
fi
$CONTAINER_ENGINE exec "$migration_container" touch "$migration_marker"
unset stored_kek recovered_kek current_kek
if [[ "$restore_xtrace" == "1" ]]; then
set -x
fi
echo "The existing SeaweedFS encryption key is ready for weed mini."
)
else
echo "Could not determine whether the SeaweedFS encryption key migration is needed." >&2
exit 1
fi
echo "${_endgroup}"