forked from fido-device-onboard/go-fdo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·494 lines (446 loc) · 17 KB
/
Copy pathutils.sh
File metadata and controls
executable file
·494 lines (446 loc) · 17 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/bash
set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/../ci/utils.sh"
# PLEASE READ:
#
# The FMF tests deploy the FDO servers via RPM packages. These
# packages are either provided by the Packit service or pulled from
# the fedora-iot COPR repository.
#
# All test-related configuration resides under the ${base_dir} working
# directory, which is made available as a testing artifact for
# debugging purposes. The FDO server configuration files also reside
# in ${base_dir} and are copied into the server configuration search
# path at the start of the test.
#
# If a test requires a custom configuration consider the following
# recommendations:
#
# o) Keep all test configuration in ${base_dir} in order to get it
# included in the testing artifacts.
#
# o) To create a custom configuration file copy the necessary
# `generate_${service}_config()` function(s) to your test and modify
# them to produce the desired test configuration. Your generated
# configuration file will automatically be saved in a directory that
# takes precedence over the default in the server's configuration
# search path. A copy of configuration file will be available
# as a testing artifact end of the test.
#
# o) To pass command line arguments to the server create a systemd
# "drop-in" file that overrides the ExecStart= setting in the service
# file. This drop-in file should be placed in the directory defined by
# the variable `systemd_${service}_drop_in_dir`. Remember to run
# `sudo systemctl daemon-reload` after writing the drop-in file.
configs_dir="${base_dir}/configs"
directories+=("${configs_dir}")
rpm_certs_dir="/etc/pki/go-fdo-server" # RPMs generate the default certs/keys
rpm_server_group="go-fdo-server" # server Group ID created by RPM install
rpm_manufacturer_user="go-fdo-server-manufacturer"
rpm_manufacturer_home_dir="/run/go-fdo-server-manufacturer"
rpm_manufacturer_config_dir="${rpm_manufacturer_home_dir}/.config/go-fdo-server"
rpm_manufacturer_config_file="${rpm_manufacturer_config_dir}/manufacturing.yaml"
rpm_manufacturer_db_type="sqlite"
rpm_manufacturer_database_dir="/var/lib/go-fdo-server-manufacturer"
rpm_manufacturer_db_dsn="file:${rpm_manufacturer_database_dir}/db.sqlite"
manufacturer_config_file="${configs_dir}/manufacturing.yaml"
rpm_rendezvous_user="go-fdo-server-rendezvous"
rpm_rendezvous_home_dir="/run/go-fdo-server-rendezvous"
rpm_rendezvous_config_dir="${rpm_rendezvous_home_dir}/.config/go-fdo-server"
rpm_rendezvous_config_file="${rpm_rendezvous_config_dir}/rendezvous.yaml"
rpm_rendezvous_db_type="sqlite"
rpm_rendezvous_database_dir="/var/lib/go-fdo-server-rendezvous"
rpm_rendezvous_db_dsn="file:${rpm_rendezvous_database_dir}/db.sqlite"
rendezvous_config_file="${configs_dir}/rendezvous.yaml"
rpm_owner_user="go-fdo-server-owner"
rpm_owner_home_dir="/run/go-fdo-server-owner"
rpm_owner_config_dir="${rpm_owner_home_dir}/.config/go-fdo-server"
rpm_owner_config_file="${rpm_owner_config_dir}/owner.yaml"
rpm_owner_db_type="sqlite"
rpm_owner_database_dir="/var/lib/go-fdo-server-owner"
rpm_owner_db_dsn="file:${rpm_owner_database_dir}/db.sqlite"
owner_config_file="${configs_dir}/owner.yaml"
owner_reuse_creds="false"
owner_to0_insecure_tls="false"
# systemd drop-in file configuration
#
systemd_drop_in_base_dir="/run/systemd/system"
#shellcheck disable=SC2034
systemd_manufacturer_drop_in_dir="${systemd_drop_in_base_dir}/go-fdo-server-manufacturer.service.d"
#shellcheck disable=SC2034
systemd_rendezvous_drop_in_dir="${systemd_drop_in_base_dir}/go-fdo-server-rendezvous.service.d"
#shellcheck disable=SC2034
systemd_owner_drop_in_dir="${systemd_drop_in_base_dir}/go-fdo-server-owner.service.d"
# Generate a default configuration file for the manufacturing server
# that references resources from the test base working directory
generate_manufacturer_config() {
cat <<EOF
log:
level: "debug"
db:
type: "${rpm_manufacturer_db_type}"
dsn: "${rpm_manufacturer_db_dsn}"
manufacturing:
key: "${rpm_manufacturer_home_dir}/manufacturer.key"
device_ca:
cert: "${rpm_manufacturer_home_dir}/device_ca.crt"
key: "${rpm_manufacturer_home_dir}/device_ca.key"
owner:
cert: "${rpm_manufacturer_home_dir}/owner.crt"
http:
ip: "${manufacturer_dns}"
port: "${manufacturer_port}"
EOF
# Enable HTTP if https protocol is used
if [ "${manufacturer_protocol}" = "https" ]; then
cat <<EOF
cert: "${rpm_manufacturer_home_dir}/manufacturer-http.crt"
key: "${rpm_manufacturer_home_dir}/manufacturer-http.key"
EOF
fi
}
# Setup manufacturer home directory, create the configuration and copy
# all necessary certs/keys
configure_service_manufacturer() {
sudo rm -rf "${rpm_manufacturer_home_dir:?}"
sudo mkdir -p "${rpm_manufacturer_config_dir}" # creates home dir
generate_manufacturer_config >"${manufacturer_config_file}"
sudo cp "${manufacturer_config_file}" "${rpm_manufacturer_config_file}"
sudo cp "${manufacturer_key}" "${rpm_manufacturer_home_dir}"
sudo cp "${owner_crt}" "${rpm_manufacturer_home_dir}"
sudo cp "${device_ca_key}" "${rpm_manufacturer_home_dir}"
sudo cp "${device_ca_crt}" "${rpm_manufacturer_home_dir}"
if [ "${manufacturer_protocol}" = "https" ]; then
sudo cp "${manufacturer_https_key}" "${manufacturer_https_crt}" "${rpm_manufacturer_home_dir}"
fi
sudo chown -R ${rpm_manufacturer_user}:${rpm_server_group} ${rpm_manufacturer_home_dir}
}
generate_rendezvous_config() {
cat <<EOF
log:
level: "debug"
db:
type: "${rpm_rendezvous_db_type}"
dsn: "${rpm_rendezvous_db_dsn}"
http:
ip: "${rendezvous_dns}"
port: "${rendezvous_port}"
EOF
# Enable HTTP if https protocol is used
if [ "${rendezvous_protocol}" = "https" ]; then
cat <<EOF
cert: "${rpm_rendezvous_home_dir}/rendezvous-http.crt"
key: "${rpm_rendezvous_home_dir}/rendezvous-http.key"
EOF
fi
}
# Setup rendezvous home directory, create the configuration and copy
# all necessary certs/keys
configure_service_rendezvous() {
sudo rm -rf "${rpm_rendezvous_home_dir:?}"
sudo mkdir -p "${rpm_rendezvous_config_dir}" # creates home dir
generate_rendezvous_config >"${rendezvous_config_file}"
sudo cp "${rendezvous_config_file}" "${rpm_rendezvous_config_file}"
if [ "${rendezvous_protocol}" = "https" ]; then
sudo cp "${rendezvous_https_key}" "${rendezvous_https_crt}" "${rpm_rendezvous_home_dir}"
fi
sudo chown -R ${rpm_rendezvous_user}:${rpm_server_group} ${rpm_rendezvous_home_dir}
}
generate_owner_config() {
cat <<EOF
log:
level: "debug"
db:
type: "${rpm_owner_db_type}"
dsn: "${rpm_owner_db_dsn}"
device_ca:
cert: "${rpm_owner_home_dir}/device_ca.crt"
owner:
cert: "${rpm_owner_home_dir}/owner.crt"
key: "${rpm_owner_home_dir}/owner.key"
reuse_credentials: "${owner_reuse_creds}"
to0_insecure_tls: "${owner_to0_insecure_tls}"
http:
ip: "${owner_dns}"
port: "${owner_port}"
EOF
# Enable HTTP if https protocol is used
if [ "${owner_protocol}" = "https" ]; then
cat <<EOF
cert: "${rpm_owner_home_dir}/owner-http.crt"
key: "${rpm_owner_home_dir}/owner-http.key"
EOF
fi
}
# Setup owner home directory, create the configuration and copy
# all necessary certs/keys
configure_service_owner() {
sudo rm -rf "${rpm_owner_home_dir:?}"
sudo mkdir -p "${rpm_owner_config_dir}" # creates home dir
generate_owner_config >"${owner_config_file}"
sudo cp "${owner_config_file}" "${rpm_owner_config_file}"
sudo cp "${device_ca_crt}" "${rpm_owner_home_dir}"
sudo cp "${owner_crt}" "${owner_key}" "${rpm_owner_home_dir}"
if [ "${owner_protocol}" = "https" ]; then
sudo cp "${owner_https_key}" "${owner_https_crt}" "${rpm_owner_home_dir}"
fi
sudo chown -R ${rpm_owner_user}:${rpm_server_group} ${rpm_owner_home_dir}
}
go_fdo_server_rpms="go-fdo-server go-fdo-server-manufacturer go-fdo-server-owner go-fdo-server-rendezvous"
go_fdo_client_rpms="go-fdo-client"
install_from_copr() {
rpm -q --whatprovides 'dnf-command(copr)' &>/dev/null || sudo dnf install -y 'dnf-command(copr)'
dnf copr list | grep 'fedora-iot/fedora-iot' || sudo dnf copr enable -y @fedora-iot/fedora-iot
# testing-farm-tag-repository is causing problems with builds see:
# https://docs.testing-farm.io/Testing%20Farm/0.1/test-environment.html#disabling-tag-repository
sudo dnf install --disablerepo=* --enablerepo=copr:copr.fedorainfracloud.org:group_fedora-iot:fedora-iot -y "$@"
sudo dnf copr disable -y @fedora-iot/fedora-iot
}
# Install RPM packages from a compose repository.
#
# The compose base URL is read from COMPOSE_BASE_URL. For Fedora and CentOS a
# reasonable default is computed from /etc/os-release; for RHEL the variable is
# mandatory. COMPOSE_STREAMS optionally overrides the space-separated list of
# stream names (repo sub-directories) to enable.
install_from_compose() {
# shellcheck source=/dev/null
source /etc/os-release
local compose_host compose_id compose_base_url compose_streams
case "${ID}-${VERSION_ID}" in
fedora-rawhide)
compose_host="http://kojipkgs.fedoraproject.org"
compose_id="latest-Fedora-${VERSION_ID^}"
compose_streams="${COMPOSE_STREAMS:-Everything}"
compose_base_url="${COMPOSE_BASE_URL:-${compose_host}/compose/${VERSION_ID}/${compose_id}/compose}"
;;
fedora-*)
compose_host="http://kojipkgs.fedoraproject.org"
compose_streams="${COMPOSE_STREAMS:-Everything}"
compose_base_url="${COMPOSE_BASE_URL:-${compose_host}/compose/updates/f${VERSION_ID}-updates/compose}"
;;
centos-*)
compose_host="https://composes.stream.centos.org"
compose_id="latest-CentOS-Stream"
compose_streams="${COMPOSE_STREAMS:-BaseOS AppStream}"
compose_base_url="${COMPOSE_BASE_URL:-${compose_host}/stream-${VERSION_ID}/production/${compose_id}/compose}"
;;
rhel-*)
compose_base_url="${COMPOSE_BASE_URL:-}"
[ -n "${compose_base_url}" ] || log_error "COMPOSE_BASE_URL must be set for RHEL (e.g. 'http://download.host/.../latest-RHEL-Compose/compose/')"
compose_streams="${COMPOSE_STREAMS:-BaseOS AppStream}"
[ -n "${compose_streams}" ] || log_error "COMPOSE_STREAMS must be set for RHEL (default='BaseOS AppStream')"
;;
*)
log_error "install_from_compose: unsupported OS '${ID}-${VERSION_ID}'"
;;
esac
local arch
arch=$(uname -m)
compose_base_url="${compose_base_url%/}" # strip trailing slash to avoid double slashes in baseurl
for stream in ${compose_streams}; do
local repo_name="compose-${ID}-${VERSION_ID}-${stream}"
sudo tee "/etc/yum.repos.d/${repo_name}.repo" >/dev/null <<EOF
[${repo_name}]
name=${repo_name}
baseurl=${compose_base_url}/${stream}/${arch}/os/
enabled=1
gpgcheck=0
sslverify=0
EOF
done
sudo dnf install --disablerepo=* --enablerepo="compose-*" -y "$@"
sudo rm -f /etc/yum.repos.d/compose-*.repo
}
install_client() {
if [ -v "PACKIT_COPR_RPMS" ]; then
: # pre-installed by CI
elif [ -n "${CLIENT_RPM_URL:-}" ]; then
# Install from a specific brew build base path.
# CLIENT_RPM_URL should point to the version/release directory of the package in brew.
parse_brew_url "${CLIENT_RPM_URL}"
# --nogpgcheck and sslverify=false are intentional: internal brew servers
# use self-signed certificates and builds may not be GPG-signed.
sudo dnf install -y --nogpgcheck --setopt=sslverify=false \
"${_brew_url}/${_brew_arch}/go-fdo-client-${_brew_ver}-${_brew_rel}.${_brew_arch}.rpm"
elif [ -n "${COMPOSE_BASE_URL:-}" ]; then
install_from_compose ${go_fdo_client_rpms}
else
# If running locally install the client from the COPR repo
rpm -q go-fdo-client &>/dev/null || install_from_copr go-fdo-client
fi
log_info "Installed Client RPM:"
echo " ⚙ $(rpm -q go-fdo-client)"
}
uninstall_client() {
# When running a test locally we remove the client package
# after a successful execution.
[ -v "PACKIT_COPR_RPMS" ] || {
sudo dnf remove -y ${go_fdo_client_rpms}
# Only remove the COPR repo when it was used for installation
[ -n "${CLIENT_RPM_URL:-}" ] || [ -n "${COMPOSE_BASE_URL:-}" ] || sudo dnf copr remove -y @fedora-iot/fedora-iot
}
}
install_server() {
if [ -v "PACKIT_COPR_RPMS" ]; then
: # pre-installed by CI
elif [ -n "${SERVER_RPM_URL:-}" ]; then
# Install from a specific brew build base path.
# SERVER_RPM_URL should point to the version/release directory of the package in brew.
parse_brew_url "${SERVER_RPM_URL}"
# --nogpgcheck and sslverify=false are intentional: internal brew servers
# use self-signed certificates and builds may not be GPG-signed.
sudo dnf install -y --nogpgcheck --setopt=sslverify=false \
"${_brew_url}/${_brew_arch}/go-fdo-server-${_brew_ver}-${_brew_rel}.${_brew_arch}.rpm" \
"${_brew_url}/noarch/go-fdo-server-manufacturer-${_brew_ver}-${_brew_rel}.noarch.rpm" \
"${_brew_url}/noarch/go-fdo-server-owner-${_brew_ver}-${_brew_rel}.noarch.rpm" \
"${_brew_url}/noarch/go-fdo-server-rendezvous-${_brew_ver}-${_brew_rel}.noarch.rpm"
elif [ -n "${COMPOSE_BASE_URL:-}" ]; then
install_from_compose ${go_fdo_server_rpms}
else
# Running locally — build and install RPMs from the committed code
commit="$(git rev-parse --short HEAD)"
rpm -q go-fdo-server | grep -q "go-fdo-server.*git${commit}.*" || {
make rpm
sudo dnf install -y rpmbuild/rpms/{noarch,"$(uname -m)"}/*git"${commit}"*.rpm
}
fi
installed_rpms=$(rpm -q --qf "%{nvr}.%{arch} " ${go_fdo_server_rpms})
log_info "Installed Server RPMs:"
for i in ${installed_rpms}; do
echo " ⚙ $i"
done | sort
}
uninstall_server() {
[ -v "PACKIT_COPR_RPMS" ] || sudo dnf remove -y ${go_fdo_server_rpms}
}
start_service_manufacturer() {
sudo systemctl start go-fdo-server-manufacturer
}
start_service_rendezvous() {
sudo systemctl start go-fdo-server-rendezvous
}
start_service_owner() {
sudo systemctl start go-fdo-server-owner
}
# We do not use pid files but functions to stop the services via systemctl
stop_service() {
local service=$1
local stop_service="stop_service_${service}"
! declare -F "${stop_service}" >/dev/null || ${stop_service}
}
stop_service_manufacturer() {
sudo systemctl stop go-fdo-server-manufacturer
}
stop_service_rendezvous() {
sudo systemctl stop go-fdo-server-rendezvous
}
stop_service_owner() {
sudo systemctl stop go-fdo-server-owner
}
get_go_fdo_server_logs() {
local role=$1
journalctl_args=("--no-pager" "--output=cat" "--unit" "go-fdo-server-${role}")
. /etc/os-release
[[ "${ID}" = "centos" && "${VERSION_ID}" = "9" ]] || journalctl_args+=("--invocation=0")
systemctl status "go-fdo-server-${role}.service" || true
journalctl "${journalctl_args[@]}"
}
get_service_logs_manufacturer() {
get_go_fdo_server_logs manufacturer
}
get_service_logs_rendezvous() {
get_go_fdo_server_logs rendezvous
}
get_service_logs_owner() {
get_go_fdo_server_logs owner
}
get_service_logs() {
local service=$1
log "🛑 '${service}' logs:\n"
local get_service_logs_func="get_service_logs_${service}"
! declare -F "${get_service_logs_func}" >/dev/null || ${get_service_logs_func}
}
save_go_fdo_server_logs() {
local role=$1
local log_file=$2
get_go_fdo_server_logs "${role}" >"${log_file}"
}
save_service_logs_manufacturer() {
save_go_fdo_server_logs manufacturer "${manufacturer_log}"
}
save_service_logs_rendezvous() {
save_go_fdo_server_logs rendezvous "${rendezvous_log}"
}
save_service_logs_owner() {
save_go_fdo_server_logs owner "${owner_log}"
}
save_service_logs() {
local service=$1
log "\t⚙ Saving '${service}' logs "
local save_service_logs_func="save_service_logs_${service}"
! declare -F "${save_service_logs_func}" >/dev/null || ${save_service_logs_func}
log_success
}
save_logs() {
log_info "Saving logs"
for service in "${services[@]}"; do
save_service_logs ${service}
done
if [ -v "PACKIT_COPR_RPMS" ]; then
log_info "Submitting files to TMT '${base_dir:?}'"
find "${base_dir:?}" -type f -exec tmt-file-submit -l {} \;
fi
}
cleanup_home_dirs() {
for server in rendezvous manufacturer owner; do
local homedir_var="rpm_${server}_home_dir"
sudo rm -vrf "${!homedir_var:?}"
done
}
cleanup_databases() {
for server in rendezvous manufacturer owner; do
local dbdir_var="rpm_${server}_database_dir"
if [[ -v "${dbdir_var}" ]]; then
sudo rm -vf "${!dbdir_var:?}"/*
fi
done
}
cleanup_drop_ins() {
local reload_systemd=0
for service in rendezvous manufacturer owner; do
local drop_in_dir_var="systemd_${service}_drop_in_dir"
if [[ -d "${!drop_in_dir_var}" ]]; then
reload_systemd=1
sudo rm -vf "${!drop_in_dir_var:?}"/*
fi
done
if [[ ${reload_systemd} -eq 1 ]]; then
sudo systemctl daemon-reload
fi
}
remove_files() {
log_info "Removing files from '${base_dir:?}'"
sudo rm -vrf "${base_dir:?}"/*
log_info "Removing files from '${rpm_certs_dir}'"
sudo rm -vf "${rpm_certs_dir:?}"/*
log_info "Removing systemd drop-in files"
cleanup_drop_ins
log_info "Removing database files"
cleanup_databases
log_info "Removing server home directories"
cleanup_home_dirs
}
on_failure() {
trap - ERR
save_logs
stop_services
test_fail
}
cleanup() {
[ ! -v "PACKIT_COPR_RPMS" ] || save_logs
stop_services
unset_hostnames
uninstall_server
uninstall_client
remove_files
}