Skip to content

Commit e8d011a

Browse files
laflechejonathanJonathan Lafleche
andauthored
support encryption in S3 backup integration test (#12264)
* fix bug for small files in read-ahead cache * misc cleanup * clang-format * revert changes to AsyncFileReadAhead * invoke test with encrypt flag * encrypt at random --------- Co-authored-by: Jonathan Lafleche <[email protected]>
1 parent 3bad966 commit e8d011a

File tree

2 files changed

+103
-23
lines changed

2 files changed

+103
-23
lines changed

fdbbackup/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ endif()
5252
if (NOT WIN32 AND NOT OPEN_FOR_IDE)
5353
enable_testing()
5454
add_test(NAME dir_backup_tests COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/dir_backup_test.sh ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
55-
add_test(NAME s3_backup_tests COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/s3_backup_test.sh ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
55+
add_test(NAME s3_backup_tests COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/s3_backup_test.sh ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} --encrypt-at-random)
5656
endif()

fdbbackup/tests/s3_backup_test.sh

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# so you can manually rerun commands or peruse logs and data
1313
# under SCRATCH_DIR.
1414
#
15+
# Usage:
16+
# s3_backup_unified.sh <source_dir> <build_dir> [scratch_dir] [--encrypt]
17+
#
1518
# See https://apple.github.io/foundationdb/backups.html
1619

1720
# Install signal traps. Depends on globals being set.
@@ -30,6 +33,9 @@ function cleanup {
3033
if type shutdown_aws &> /dev/null; then
3134
shutdown_aws "${TEST_SCRATCH_DIR}"
3235
fi
36+
if [[ -n "${ENCRYPTION_KEY_FILE:-}" ]] && [[ -f "${ENCRYPTION_KEY_FILE}" ]]; then
37+
rm -f "${ENCRYPTION_KEY_FILE}"
38+
fi
3339
}
3440

3541
# Resolve passed in reference to an absolute path.
@@ -45,28 +51,47 @@ function resolve_to_absolute_path {
4551
realpath "${p}"
4652
}
4753

54+
function create_encryption_key_file {
55+
local key_file="${1}"
56+
log "Creating encryption key file at ${key_file}"
57+
dd if=/dev/urandom bs=32 count=1 of="${key_file}" 2>/dev/null
58+
chmod 600 "${key_file}"
59+
}
60+
4861
# Run the fdbbackup command.
4962
# $1 The build directory
5063
# $2 The scratch directory
51-
# $3 The S3 url.
64+
# $3 The S3 url
5265
# $4 credentials file
66+
# $5 encryption key file (optional)
5367
function backup {
5468
local local_build_dir="${1}"
5569
local local_scratch_dir="${2}"
5670
local local_url="${3}"
5771
local local_credentials="${4}"
72+
local local_encryption_key_file="${5:-}"
73+
5874
# Backup to s3. Without the -k argument in the below, the backup gets
5975
# 'No restore target version given, will use maximum restorable version from backup description.'
6076
# TODO: Why is -k needed?
61-
if ! "${local_build_dir}"/bin/fdbbackup start \
62-
-C "${local_scratch_dir}/loopback_cluster/fdb.cluster" \
63-
-t "${TAG}" -w \
64-
-d "${local_url}" \
65-
-k '"" \xff' \
66-
--log --logdir="${local_scratch_dir}" \
67-
--blob-credentials "${local_credentials}" \
68-
"${KNOBS[@]}"
69-
then
77+
local cmd_args=(
78+
"-C" "${local_scratch_dir}/loopback_cluster/fdb.cluster"
79+
"-t" "${TAG}" "-w"
80+
"-d" "${local_url}"
81+
"-k" '"" \xff'
82+
"--log" "--logdir=${local_scratch_dir}"
83+
"--blob-credentials" "${local_credentials}"
84+
)
85+
86+
if [[ -n "${local_encryption_key_file}" ]]; then
87+
cmd_args+=("--encryption-key-file" "${local_encryption_key_file}")
88+
fi
89+
90+
for knob in "${KNOBS[@]}"; do
91+
cmd_args+=("${knob}")
92+
done
93+
94+
if ! "${local_build_dir}"/bin/fdbbackup start "${cmd_args[@]}"; then
7095
err "Start fdbbackup failed"
7196
return 1
7297
fi
@@ -77,19 +102,31 @@ function backup {
77102
# $2 The scratch directory
78103
# $3 The S3 url
79104
# $4 credentials file
105+
# $5 encryption key file (optional)
80106
function restore {
81107
local local_build_dir="${1}"
82108
local local_scratch_dir="${2}"
83109
local local_url="${3}"
84110
local local_credentials="${4}"
85-
if ! "${local_build_dir}"/bin/fdbrestore start \
86-
--dest-cluster-file "${local_scratch_dir}/loopback_cluster/fdb.cluster" \
87-
-t "${TAG}" -w \
88-
-r "${url}" \
89-
--log --logdir="${local_scratch_dir}" \
90-
--blob-credentials "${local_credentials}" \
91-
"${KNOBS[@]}"
92-
then
111+
local local_encryption_key_file="${5:-}"
112+
113+
local cmd_args=(
114+
"--dest-cluster-file" "${local_scratch_dir}/loopback_cluster/fdb.cluster"
115+
"-t" "${TAG}" "-w"
116+
"-r" "${url}"
117+
"--log" "--logdir=${local_scratch_dir}"
118+
"--blob-credentials" "${local_credentials}"
119+
)
120+
121+
if [[ -n "${local_encryption_key_file}" ]]; then
122+
cmd_args+=("--encryption-key-file" "${local_encryption_key_file}")
123+
fi
124+
125+
for knob in "${KNOBS[@]}"; do
126+
cmd_args+=("${knob}")
127+
done
128+
129+
if ! "${local_build_dir}"/bin/fdbrestore start "${cmd_args[@]}"; then
93130
err "Start fdbrestore failed"
94131
return 1
95132
fi
@@ -100,11 +137,14 @@ function restore {
100137
# $2 the scratch directory
101138
# $3 The credentials file.
102139
# $4 build directory
140+
# $5 encryption key file (optional)
103141
function test_s3_backup_and_restore {
104142
local local_url="${1}"
105143
local local_scratch_dir="${2}"
106144
local credentials="${3}"
107145
local local_build_dir="${4}"
146+
local local_encryption_key_file="${5:-}"
147+
108148
log "Load data"
109149
if ! load_data "${local_build_dir}" "${local_scratch_dir}"; then
110150
err "Failed loading data into fdb"
@@ -130,7 +170,7 @@ function test_s3_backup_and_restore {
130170
fi
131171
fi
132172
log "Run s3 backup"
133-
if ! backup "${local_build_dir}" "${local_scratch_dir}" "${local_url}" "${credentials}"; then
173+
if ! backup "${local_build_dir}" "${local_scratch_dir}" "${local_url}" "${credentials}" "${local_encryption_key_file}"; then
134174
err "Failed backup"
135175
return 1
136176
fi
@@ -140,7 +180,7 @@ function test_s3_backup_and_restore {
140180
return 1
141181
fi
142182
log "Restore from s3"
143-
if ! restore "${local_build_dir}" "${local_scratch_dir}" "${local_url}" "${credentials}"; then
183+
if ! restore "${local_build_dir}" "${local_scratch_dir}" "${local_url}" "${credentials}" "${local_encryption_key_file}"; then
144184
err "Failed restore"
145185
return 1
146186
fi
@@ -179,6 +219,34 @@ set -o nounset # a.k.a. set -u
179219
set -o pipefail
180220
set -o noclobber
181221

222+
# Parse command line arguments
223+
USE_ENCRYPTION=false
224+
PARAMS=()
225+
226+
while (( "$#" )); do
227+
case "$1" in
228+
--encrypt)
229+
USE_ENCRYPTION=true
230+
shift
231+
;;
232+
--encrypt-at-random)
233+
USE_ENCRYPTION=$(((RANDOM % 2)) && echo true || echo false )
234+
shift
235+
;;
236+
-*|--*=) # unsupported flags
237+
err "Error: Unsupported flag $1" >&2
238+
exit 1
239+
;;
240+
*) # preserve positional arguments
241+
PARAMS+=("$1")
242+
shift
243+
;;
244+
esac
245+
done
246+
247+
# Set positional arguments in their proper place
248+
set -- "${PARAMS[@]}"
249+
182250
# Globals
183251
# TEST_SCRATCH_DIR gets set below. Tests should be their data in here.
184252
# It gets cleaned up on the way out of the test.
@@ -248,7 +316,7 @@ if (( $# < 2 )) || (( $# > 3 )); then
248316
echo "leave the download of seaweed this directory for other"
249317
echo "tests to find if they need it. Otherwise, we clean everything"
250318
echo "else up on our way out."
251-
echo "Example: ${0} ./foundationdb ./build_output ./scratch_dir"
319+
echo "Example: ${0} ./foundationdb ./build_output ./scratch_dir [--encrypt]"
252320
exit 1
253321
fi
254322
if ! source_dir=$(is_fdb_source_dir "${1}"); then
@@ -267,6 +335,18 @@ if (( $# == 3 )); then
267335
fi
268336
readonly scratch_dir
269337

338+
# Create encryption key file if needed
339+
ENCRYPTION_KEY_FILE=""
340+
if [[ "${USE_ENCRYPTION}" == "true" ]]; then
341+
log "Enabling encryption for backups"
342+
ENCRYPTION_KEY_FILE="${scratch_dir}/test_encryption_key_file"
343+
create_encryption_key_file "${ENCRYPTION_KEY_FILE}"
344+
log "Created encryption key file at ${ENCRYPTION_KEY_FILE}"
345+
else
346+
log "Using plaintext for backups"
347+
fi
348+
readonly ENCRYPTION_KEY_FILE
349+
270350
# Set host, bucket, and blob_credentials_file whether seaweed or s3.
271351
readonly path_prefix="ctests"
272352
host=
@@ -346,5 +426,5 @@ log "Backup_agent is up"
346426
# Run tests.
347427
test="test_s3_backup_and_restore"
348428
url="blobstore://${host}/${path_prefix}/${test}?${query_str}"
349-
test_s3_backup_and_restore "${url}" "${TEST_SCRATCH_DIR}" "${blob_credentials_file}" "${build_dir}"
429+
test_s3_backup_and_restore "${url}" "${TEST_SCRATCH_DIR}" "${blob_credentials_file}" "${build_dir}" "${ENCRYPTION_KEY_FILE}"
350430
log_test_result $? "test_s3_backup_and_restore"

0 commit comments

Comments
 (0)