edit-tang-luks2 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <scorreia@redhat.com>
  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. TEST=$(basename "${0}")
  20. . tests-common-functions
  21. . clevis-luks-common-functions
  22. on_exit() {
  23. local d
  24. for d in "${TMP}" "${TMP2}"; do
  25. [ ! -d "${d}" ] && continue
  26. tang_stop "${d}"
  27. rm -rf "${d}"
  28. done
  29. }
  30. trap 'on_exit' EXIT
  31. trap 'on_exit' ERR
  32. TMP="$(mktemp -d)"
  33. port=$(get_random_port)
  34. tang_run "${TMP}" "${port}" &
  35. tang_wait_until_ready "${port}"
  36. url="http://${TANG_HOST}:${port}"
  37. cfg=$(printf '{"url":"%s"}' "${url}")
  38. # LUKS2.
  39. DEV="${TMP}/luks2-device"
  40. new_device "luks2" "${DEV}"
  41. if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
  42. error "${TEST}: Bind should have succeeded."
  43. fi
  44. # Now let's try to change the config but using the same one we already have.
  45. if clevis luks edit -d "${DEV}" -s 1 -c "${cfg}"; then
  46. error "${TEST}: edit should have failed because the config is the same."
  47. fi
  48. # And now, just a broken config.
  49. new_cfg=$(printf '{"url&:"%s"}' "${url}")
  50. if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
  51. error "${TEST}: edit should have failed because of invalid JSON"
  52. fi
  53. # Now let's have another tang instance running and change the config to use
  54. # the new one.
  55. port2=$(get_random_port)
  56. TMP2="$(mktemp -d)"
  57. tang_run "${TMP2}" "${port2}" &
  58. tang_wait_until_ready "${port2}"
  59. new_url="http://${TANG_HOST}:${port2}"
  60. new_cfg=$(printf '{"url":"%s"}' "${new_url}")
  61. if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
  62. error "${TEST}: edit should have succeeded."
  63. fi
  64. # Now we test an invalid server.
  65. new_cfg='{"url":"localhost:1"}'
  66. if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
  67. error "${TEST}: edit should not have succeeded with a wrong server."
  68. fi
  69. # Make sure we can still unlock the device.
  70. if ! clevis_luks_unlock_device "${DEV}" >/dev/null; then
  71. error "${TEST}: we should have been able to unlock the device"
  72. fi
  73. # And now let's use sss and start with a single tang server, then add a second
  74. # one.
  75. new_device "luks2" "${DEV}"
  76. cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"}]}}' "${url}")
  77. if ! clevis luks bind -y -d "${DEV}" sss "${cfg}" <<< "${DEFAULT_PASS}"; then
  78. error "${TEST}: Bind should have succeeded."
  79. fi
  80. new_cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
  81. "${url}" "${new_url}")
  82. if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
  83. error "${TEST}: edit should have succeeded and added a new tang server"
  84. fi
  85. # Now let's change the threshold to 2.
  86. new_cfg=$(printf '{"t":2,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
  87. "${url}" "${new_url}")
  88. if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
  89. error "${TEST}: edit should have succeeded and added a new tang server"
  90. fi
  91. # And finally, let's try a broken config, with a wrong threshold.
  92. new_cfg=$(printf '{"t":3,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
  93. "${url}" "${new_url}")
  94. if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
  95. error "${TEST}: edit should have failed because threshold > number of servers"
  96. fi