Debian Codegraph @

src/luks/clevis-luks-edit

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