| 1 |
#!/bin/bash -ex |
| 2 |
# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: |
| 3 |
# |
| 4 |
# Copyright (c) 2020 Red Hat, Inc. |
| 5 |
# Author: Sergio Correia <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
TEST=$(basename "${0}") |
| 21 |
. luks-common-test-functions |
| 22 |
|
| 23 |
. clevis-luks-common-functions |
| 24 |
|
| 25 |
on_exit() { |
| 26 |
local d |
| 27 |
for d in "${TMP}" "${TMP2}"; do |
| 28 |
[ ! -d "${d}" ] && continue |
| 29 |
tang_stop "${d}" |
| 30 |
rm -rf "${d}" |
| 31 |
done |
| 32 |
} |
| 33 |
|
| 34 |
trap 'on_exit' EXIT |
| 35 |
trap 'on_exit' ERR |
| 36 |
|
| 37 |
TMP="$(mktemp -d)" |
| 38 |
|
| 39 |
tang_run "${TMP}" |
| 40 |
port=$(tang_get_port "${TMP}") |
| 41 |
|
| 42 |
url="http://localhost:${port}" |
| 43 |
|
| 44 |
cfg=$(printf '{"url":"%s"}' "${url}") |
| 45 |
|
| 46 |
# LUKS2. |
| 47 |
DEV="${TMP}/luks2-device" |
| 48 |
new_device "luks2" "${DEV}" |
| 49 |
|
| 50 |
if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then |
| 51 |
error "${TEST}: Bind should have succeeded." |
| 52 |
fi |
| 53 |
|
| 54 |
# Now let's try to change the config but using the same one we already have. |
| 55 |
if clevis luks edit -d "${DEV}" -s 1 -c "${cfg}"; then |
| 56 |
error "${TEST}: edit should have failed because the config is the same." |
| 57 |
fi |
| 58 |
|
| 59 |
# And now, just a broken config. |
| 60 |
new_cfg=$(printf '{"url&:"%s"}' "${url}") |
| 61 |
if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then |
| 62 |
error "${TEST}: edit should have failed because of invalid JSON" |
| 63 |
fi |
| 64 |
|
| 65 |
# Now let's have another tang instance running and change the config to use |
| 66 |
# the new one. |
| 67 |
TMP2="$(mktemp -d)" |
| 68 |
tang_run "${TMP2}" |
| 69 |
port2=$(tang_get_port "${TMP2}") |
| 70 |
new_url="http://localhost:${port2}" |
| 71 |
new_cfg=$(printf '{"url":"%s"}' "${new_url}") |
| 72 |
|
| 73 |
if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then |
| 74 |
error "${TEST}: edit should have succeeded." |
| 75 |
fi |
| 76 |
|
| 77 |
# Now we test an invalid server. |
| 78 |
new_cfg='{"url":"localhost:1"}' |
| 79 |
|
| 80 |
if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then |
| 81 |
error "${TEST}: edit should not have succeeded with a wrong server." |
| 82 |
fi |
| 83 |
|
| 84 |
# Make sure we can still unlock the device. |
| 85 |
if ! clevis_luks_unlock_device "${DEV}" >/dev/null; then |
| 86 |
error "${TEST}: we should have been able to unlock the device" |
| 87 |
fi |
| 88 |
|
| 89 |
# And now let's use sss and start with a single tang server, then add a second |
| 90 |
# one. |
| 91 |
new_device "luks2" "${DEV}" |
| 92 |
cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"}]}}' "${url}") |
| 93 |
if ! clevis luks bind -y -d "${DEV}" sss "${cfg}" <<< "${DEFAULT_PASS}"; then |
| 94 |
error "${TEST}: Bind should have succeeded." |
| 95 |
fi |
| 96 |
new_cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \ |
| 97 |
"${url}" "${new_url}") |
| 98 |
|
| 99 |
if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then |
| 100 |
error "${TEST}: edit should have succeeded and added a new tang server" |
| 101 |
fi |
| 102 |
|
| 103 |
# Now let's change the threshold to 2. |
| 104 |
new_cfg=$(printf '{"t":2,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \ |
| 105 |
"${url}" "${new_url}") |
| 106 |
|
| 107 |
if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then |
| 108 |
error "${TEST}: edit should have succeeded and added a new tang server" |
| 109 |
fi |
| 110 |
|
| 111 |
# And finally, let's try a broken config, with a wrong threshold. |
| 112 |
new_cfg=$(printf '{"t":3,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \ |
| 113 |
"${url}" "${new_url}") |
| 114 |
if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then |
| 115 |
error "${TEST}: edit should have failed because threshold > number of servers" |
| 116 |
fi |