clevis-luks-edit 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash -e
  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. #
  20. . clevis-luks-common-functions
  21. SUMMARY="Edit a binding from a clevis-bound slot in a LUKS device"
  22. usage() {
  23. exec >&2
  24. echo "Usage: clevis luks edit [-f] -d DEV -s SLT [-c CONFIG]"
  25. echo
  26. echo "${SUMMARY}"
  27. echo
  28. echo "'clevis luks edit' uses the text editor defined in the EDITOR environment variable."
  29. echo " If EDITOR is not defined, it will attempt to use 'vi' as default editor."
  30. echo
  31. echo " -d DEV The LUKS device to edit clevis-bound pins"
  32. echo
  33. echo " -s SLOT The slot to use when editing the clevis binding"
  34. echo
  35. echo " -f Proceed with the edit operation even if the configuration is unchanged"
  36. echo
  37. echo " -c CONFIG The updated config to use"
  38. echo
  39. exit 1
  40. }
  41. on_exit() {
  42. [ -d "${CLEVIS_EDIT_TMP}" ] && rm -rf "${CLEVIS_EDIT_TMP}"
  43. }
  44. validate_cfg() {
  45. local json="${1}"
  46. [ -z "${json}" ] && return 1
  47. jose fmt --json="${json}" --object 2>/dev/null
  48. }
  49. edit_cfg() {
  50. local cfg_file="${1}"
  51. local editor="${EDITOR:-vi}"
  52. if ! command -v "${editor}" >/dev/null; then
  53. echo "Editor '${editor}' not found. " >&2
  54. echo "Please define a valid text editor with the EDITOR environment variable." >&2
  55. exit 1
  56. fi
  57. "${editor}" "${cfg_file}" || true
  58. if ! validate_cfg "${cfg_file}"; then
  59. local ans=
  60. while true; do
  61. read -r -p \
  62. "Malformed configuration. Would you like to edit again? [ynYN] " \
  63. ans
  64. [ "${ans}" != "y" ] && [ "${ans}" != "Y" ] && return 1
  65. break
  66. done
  67. edit_cfg "${cfg_file}"
  68. fi
  69. return 0
  70. }
  71. if [ "${#}" -eq 1 ] && [ "${1}" = "--summary" ]; then
  72. echo "${SUMMARY}"
  73. exit 0
  74. fi
  75. CFG=
  76. FRC=
  77. while getopts ":fd:s:c:" o; do
  78. case "$o" in
  79. d) DEV=${OPTARG};;
  80. s) SLT=${OPTARG};;
  81. c) CFG=${OPTARG};;
  82. f) FRC=-f;;
  83. *) usage;;
  84. esac
  85. done
  86. if [ -z "${DEV}" ]; then
  87. echo "Did not specify a device!" >&2
  88. usage
  89. fi
  90. if [ -z "${SLT}" ]; then
  91. echo "Did not specify a slot!" >&2
  92. usage
  93. fi
  94. if ! binding="$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null)" \
  95. || [ -z "${binding}" ]; then
  96. echo "Error retrieving current configuration from ${DEV}:${SLT}" >&2
  97. exit 1
  98. fi
  99. pin="$(echo "${binding}" | cut -d' ' -f2)"
  100. cfg="$(echo "${binding}" | cut -d' ' -f3 | sed -e "s/'//g")"
  101. if ! pretty_cfg="$(printf '%s' "${cfg}" | jq --monochrome-output .)" \
  102. || [ -z "${pretty_cfg}" ]; then
  103. echo "Error reading the configuration from ${DEV}:${SLT}" >&2
  104. exit 1
  105. fi
  106. if ! CLEVIS_EDIT_TMP="$(mktemp -d)" || [ -z "${CLEVIS_EDIT_TMP}" ]; then
  107. echo "Creating a temporary dir for editing binding failed" >&2
  108. exit 1
  109. fi
  110. trap 'on_exit' EXIT
  111. if [ -z "${CFG}" ]; then
  112. CFG_FILE="${CLEVIS_EDIT_TMP}/cfg"
  113. echo "${pretty_cfg}" > "${CFG_FILE}"
  114. edit_cfg "${CFG_FILE}" || exit 1
  115. if ! new_cfg="$(jq . -S < "${CFG_FILE}")" || [ -z "${new_cfg}" ]; then
  116. echo "Error reading the updated config for ${DEV}:${SLT}" >&2
  117. exit 1
  118. fi
  119. else
  120. if ! validate_cfg "${CFG}"; then
  121. echo "Invalid configuration given as parameter with -c" >&2
  122. exit 1
  123. fi
  124. new_cfg="$(printf '%s' "${CFG}" | jq --sort-keys --monochrome-output .)"
  125. fi
  126. if [ "${new_cfg}" = "$(printf '%s' "${pretty_cfg}" \
  127. | jq --sort-keys --monochrome-output .)" ] \
  128. && [ -z "${FRC}" ]; then
  129. echo "No changes detected; exiting" >&2
  130. exit 1
  131. fi
  132. if ! jcfg="$(jose fmt --json="${new_cfg}" --object --output=- 2>/dev/null)" \
  133. || [ -z "${jcfg}" ]; then
  134. echo "Error preparing the configuration for the binding update" >&2
  135. exit 1
  136. fi
  137. if [ -z "${CFG}" ]; then
  138. printf "Pin: %s\nNew config:\n%s\n" "${pin}" "${new_cfg}"
  139. while true; do
  140. read -r -p \
  141. "Would you like to proceed with the updated configuration? [ynYN] " \
  142. ans
  143. [ "${ans}" != "y" ] && [ "${ans}" != "Y" ] && exit 0
  144. break
  145. done
  146. fi
  147. # Remove temporary directory.
  148. rm -rf "${CLEVIS_EDIT_TMP}"
  149. echo "Updating binding..."
  150. if ! clevis_luks_do_bind "${DEV}" "${SLT}" "" "${pin}" "${new_cfg}" \
  151. "-y" "overwrite" 2>/dev/null; then
  152. echo "Unable to update binding in ${DEV}:${SLT}. Operation cancelled." >&2
  153. exit 1
  154. fi
  155. echo "Binding edited successfully" >&2