Blame SOURCES/0010-Add-clevis-luks-edit-command.patch

64015d
From 46dc5904a9a6b0f1dcdb2511cc3f09671ef54b2a Mon Sep 17 00:00:00 2001
64015d
From: Sergio Correia <scorreia@redhat.com>
64015d
Date: Wed, 20 May 2020 21:09:15 -0300
64015d
Subject: [PATCH] Add clevis luks edit command
64015d
64015d
---
64015d
 src/luks/clevis-luks-common-functions    |  65 ++++--
64015d
 src/luks/clevis-luks-edit                | 242 +++++++++++++++++++++++
64015d
 src/luks/clevis-luks-edit.1.adoc         |  69 +++++++
64015d
 src/luks/clevis-luks-regen               | 125 ++----------
64015d
 src/luks/meson.build                     |   3 +
64015d
 src/luks/tests/edit-tang-luks1           | 106 ++++++++++
64015d
 src/luks/tests/edit-tang-luks2           | 106 ++++++++++
64015d
 src/luks/tests/meson.build               |  28 ++-
64015d
 src/luks/tests/regen-inplace-luks1       |   2 +-
64015d
 src/luks/tests/regen-inplace-luks2       |   2 +-
64015d
 src/luks/tests/regen-not-inplace-luks1   |   2 +-
64015d
 src/luks/tests/regen-not-inplace-luks2   |   2 +-
64015d
 src/luks/tests/tests-common-functions.in |  26 ---
64015d
 src/luks/tests/unlock-tang-luks1         |   7 +-
64015d
 src/luks/tests/unlock-tang-luks2         |   8 +-
64015d
 15 files changed, 606 insertions(+), 187 deletions(-)
64015d
 create mode 100755 src/luks/clevis-luks-edit
64015d
 create mode 100644 src/luks/clevis-luks-edit.1.adoc
64015d
 create mode 100755 src/luks/tests/edit-tang-luks1
64015d
 create mode 100755 src/luks/tests/edit-tang-luks2
64015d
64015d
diff --git a/src/luks/clevis-luks-common-functions b/src/luks/clevis-luks-common-functions
64015d
index c9d712a..f3a875c 100644
64015d
--- a/src/luks/clevis-luks-common-functions
64015d
+++ b/src/luks/clevis-luks-common-functions
64015d
@@ -141,11 +141,7 @@ clevis_luks_decode_jwe() {
64015d
     local jwe="${1}"
64015d
 
64015d
     local coded
64015d
-    if ! coded=$(jose jwe fmt -i- <<< "${jwe}"); then
64015d
-        return 1
64015d
-    fi
64015d
-
64015d
-    coded=$(jose fmt -j- -g protected -u- <<< "${coded}" | tr -d '"')
64015d
+    read -r -d . coded <<< "${jwe}"
64015d
     jose b64 dec -i- <<< "${coded}"
64015d
 }
64015d
 
64015d
@@ -291,6 +287,46 @@ clevis_luks_read_pins_from_slot() {
64015d
     printf "%s: %s\n" "${SLOT}" "${cfg}"
64015d
 }
64015d
 
64015d
+# clevis_luks_is_key_valid() checks whether the given key is valid to unlock
64015d
+# the given device.
64015d
+clevis_luks_is_key_valid() {
64015d
+    local DEV="${1}"
64015d
+    local KEY="${2}"
64015d
+
64015d
+    if ! cryptsetup open --test-passphrase "${DEV}" \
64015d
+                         --key-file <(echo -n "${KEY}") 2>/dev/null; then
64015d
+        return 1
64015d
+    fi
64015d
+    return 0
64015d
+}
64015d
+
64015d
+# clevis_luks_unlock_device_by_slot() does the unlock of the device and slot
64015d
+# passed as parameters and returns the decoded passphrase.
64015d
+clevis_luks_unlock_device_by_slot() {
64015d
+    local DEV="${1}"
64015d
+    local SLT="${2}"
64015d
+
64015d
+    [ -z "${DEV}" ] && return 1
64015d
+    [ -z "${SLT}" ] && return 1
64015d
+
64015d
+    local jwe passphrase
64015d
+    if ! jwe="$(clevis_luks_read_slot "${DEV}" "${SLT}" 2>/dev/null)" \
64015d
+                || [ -z "${jwe}" ]; then
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    if ! passphrase="$(clevis decrypt < <(echo -n "${jwe}") 2>/dev/null)" \
64015d
+                       || [ -z "${passphrase}" ]; then
64015d
+        return 1
64015d
+    fi
64015d
+
64015d
+    if ! clevis_luks_is_key_valid "${DEV}" "${passphrase}"; then
64015d
+        return 1
64015d
+    fi
64015d
+    echo -n "${passphrase}"
64015d
+    return 0
64015d
+}
64015d
+
64015d
 # clevis_luks_unlock_device() does the unlock of the device passed as
64015d
 # parameter and returns the decoded passphrase.
64015d
 clevis_luks_unlock_device() {
64015d
@@ -303,26 +339,15 @@ clevis_luks_unlock_device() {
64015d
         return 1
64015d
     fi
64015d
 
64015d
-    local slt jwe passphrase
64015d
+    local slt pt
64015d
     for slt in ${used_slots}; do
64015d
-        if ! jwe="$(clevis_luks_read_slot "${DEV}" "${slt}" 2>/dev/null)" \
64015d
-                   || [ -z "${jwe}" ]; then
64015d
-            continue
64015d
-        fi
64015d
-
64015d
-        if ! passphrase="$(clevis decrypt < <(echo -n "${jwe}"))" \
64015d
-                           || [ -z "${passphrase}" ]; then
64015d
+        if ! pt=$(clevis_luks_unlock_device_by_slot "${DEV}" "${slt}") \
64015d
+                  || [ -z "${pt}" ]; then
64015d
             continue
64015d
         fi
64015d
-
64015d
-        if ! cryptsetup luksOpen --test-passphrase "${DEV}" \
64015d
-             --key-file <(echo -n "${passphrase}"); then
64015d
-            continue
64015d
-        fi
64015d
-        echo -n "${passphrase}"
64015d
+        echo -n "${pt}"
64015d
         return 0
64015d
     done
64015d
-
64015d
     return 1
64015d
 }
64015d
 
64015d
diff --git a/src/luks/clevis-luks-edit b/src/luks/clevis-luks-edit
64015d
new file mode 100755
64015d
index 0000000..fc95f75
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-edit
64015d
@@ -0,0 +1,242 @@
64015d
+#!/bin/bash -e
64015d
+# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2020 Red Hat, Inc.
64015d
+# Author: Sergio Correia <scorreia@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+#
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+SUMMARY="Edit a binding from a clevis-bound slot in a LUKS device"
64015d
+
64015d
+usage() {
64015d
+    echo >&2
64015d
+    echo "Usage: clevis luks edit [-f] -d DEV -s SLT [-c CONFIG]" >&2
64015d
+    echo >&2
64015d
+    echo "$SUMMARY": >&2
64015d
+    echo >&2
64015d
+    echo "  -d DEV     The LUKS device to edit clevis-bound pins" >&2
64015d
+    echo >&2
64015d
+    echo "  -s SLOT    The slot to use when editing the clevis binding" >&2
64015d
+    echo >&2
64015d
+    echo "  -f         Proceed with the edit operation even if the configuration is the same" >&2
64015d
+    echo >&2
64015d
+    echo "  -c CONFIG  The updated config to use" >&2
64015d
+    echo >&2
64015d
+    exit 1
64015d
+}
64015d
+
64015d
+on_exit() {
64015d
+    [ -d "$TMP" ] && rm -rf "${TMP}"
64015d
+}
64015d
+
64015d
+validate_cfg() {
64015d
+    local json="${1}"
64015d
+    jose fmt -j- -O <<< "${json}" 2>/dev/null
64015d
+}
64015d
+
64015d
+edit_cfg() {
64015d
+    local cfg_file="${1}"
64015d
+    local editor="${EDITOR:-vi}"
64015d
+
64015d
+    "${editor}" "${cfg_file}" || true
64015d
+    if ! validate_cfg "$(<"${cfg_file}")"; then
64015d
+        local ans=
64015d
+        while true; do
64015d
+            read -r -p \
64015d
+              "Malformed configuration. Would you like to edit again? [ynYN] " \
64015d
+             ans < /dev/tty
64015d
+
64015d
+            [[ "${ans}" =~ ^[nN]$ ]] && return 1
64015d
+            [[ "${ans}" =~ ^[yY]$ ]] && break
64015d
+        done
64015d
+        edit_cfg "${cfg_file}"
64015d
+    fi
64015d
+    return 0
64015d
+}
64015d
+
64015d
+if [ "${#}" -eq 1 ] && [ "${1}" = "--summary" ]; then
64015d
+    echo "${SUMMARY}"
64015d
+    exit 0
64015d
+fi
64015d
+
64015d
+CFG=
64015d
+FRC=
64015d
+while getopts ":fd:s:c:" o; do
64015d
+    case "$o" in
64015d
+    d) DEV=${OPTARG};;
64015d
+    s) SLT=${OPTARG};;
64015d
+    c) CFG=${OPTARG};;
64015d
+    f) FRC=-f;;
64015d
+    *) usage;;
64015d
+    esac
64015d
+done
64015d
+
64015d
+if [ -z "${DEV}" ]; then
64015d
+    echo "Did not specify a device!" >&2
64015d
+    usage
64015d
+fi
64015d
+
64015d
+if [ -z "${SLT}" ]; then
64015d
+    echo "Did not specify a slot!" >&2
64015d
+    usage
64015d
+fi
64015d
+
64015d
+if ! binding="$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null)" \
64015d
+                || [ -z "${binding}" ]; then
64015d
+    echo "Error retrieving current configuration from ${DEV}:${SLT}" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+read -r _ pin cfg <<< "${binding}"
64015d
+# Remove single quotes.
64015d
+cfg=${cfg//\'}
64015d
+if ! pretty_cfg="$(jq . <<< "${cfg}")" || [ -z "${pretty_cfg}" ]; then
64015d
+    echo "Error reading the configuration from ${DEV}:${SLT}" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! TMP="$(mktemp -d)" || [ -z "${TMP}" ]; then
64015d
+    echo "Creating a temporary dir for editing binding failed" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+trap 'on_exit' EXIT
64015d
+trap 'on_exit' ERR
64015d
+
64015d
+if [ -z "${CFG}" ]; then
64015d
+   CFG_FILE="${TMP}/cfg"
64015d
+    echo "${pretty_cfg}" > "${CFG_FILE}"
64015d
+    if ! edit_cfg "${CFG_FILE}"; then
64015d
+        exit 1
64015d
+    fi
64015d
+
64015d
+    if ! new_cfg="$(jq . -S < "${CFG_FILE}")" || [ -z "${new_cfg}" ]; then
64015d
+        echo "Error reading the updated config for ${DEV}:${SLT}" >&2
64015d
+        exit 1
64015d
+    fi
64015d
+else
64015d
+    if ! validate_cfg "${CFG}"; then
64015d
+        echo "Invalid configuration given as parameter with -c" >&2
64015d
+        exit 1
64015d
+    fi
64015d
+    new_cfg="$(jq . -S <<< "${CFG}")"
64015d
+fi
64015d
+
64015d
+if [ "${new_cfg}" = "$(jq -S . <<< "${pretty_cfg}")" ] && [ -z "${FRC}" ]; then
64015d
+    echo "No changes detected; exiting" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if ! jcfg="$(jose fmt -j- -Oo- <<< "${new_cfg}" 2>/dev/null)" \
64015d
+             || [ -z "${jcfg}" ]; then
64015d
+    echo "Error preparing the configuration for the binding update" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+if [ -z "${CFG}" ]; then
64015d
+    printf "Pin: %s\nNew config:\n%s\n" "${pin}" "${new_cfg}"
64015d
+    while true; do
64015d
+        read -r -p \
64015d
+          "Would you like to proceed with the updated configuration? [ynYN] " \
64015d
+         ans < /dev/tty
64015d
+        [[ "${ans}" =~ ^[nN]$ ]] && exit 0
64015d
+        [[ "${ans}" =~ ^[yY]$ ]] && break
64015d
+    done
64015d
+fi
64015d
+
64015d
+echo "Updating binding..."
64015d
+
64015d
+# Create new key.
64015d
+if ! new_pass=$(generate_key "${DEV}"); then
64015d
+    echo "Error generating new key for device ${DEV}" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+# Reencrypt the new password.
64015d
+if ! jwe="$(clevis encrypt "${pin}" "${jcfg}" -y <<< "${new_pass}")"; then
64015d
+    echo "Error using pin '${pin}' with config '${jcfg}'" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+# Backup LUKS header.
64015d
+if ! clevis_luks_backup_dev "${DEV}" "${TMP}"; then
64015d
+    echo "Error while trying to back up LUKS header from ${DEV}" >&2
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+# Get passphrase.
64015d
+if ! pt="$(clevis_luks_unlock_device "${DEV}")" \
64015d
+           || [ -z "${pt}" ]; then
64015d
+    # Unable to retrieve a passphrase from the bindings, so let's query
64015d
+    # the user.
64015d
+    read -r -s -p "Enter existing LUKS password: " pt; echo
64015d
+    # Check if the key is valid.
64015d
+    if ! clevis_luks_is_key_valid "${DEV}" "${pt}"; then
64015d
+        echo "The key provided is not valid for ${DEV}" >&2
64015d
+        exit 1
64015d
+    fi
64015d
+fi
64015d
+
64015d
+# Check if we can do the update in-place, i.e., if the key we got is the one
64015d
+# for the slot we are interested in.
64015d
+in_place=
64015d
+if cryptsetup open --test-passphrase --key-slot "${SLT}" "${DEV}" \
64015d
+        <<< "${pt}"; then
64015d
+    in_place=true
64015d
+fi
64015d
+
64015d
+# Update the key slot with the new key. If we have the key for this slot,
64015d
+# the change happens in-place. Otherwise, we kill the slot and re-add it.
64015d
+if [ -n "${in_place}" ]; then
64015d
+    if ! cryptsetup luksChangeKey "${DEV}" --key-slot "${SLT}" \
64015d
+            <(echo -n "${new_pass}") <<< "${pt}"; then
64015d
+        echo "Error updating LUKS passphrase in ${DEV}:${SLT}" >&2
64015d
+        clevis_luks_restore_dev "${DEV}" "${TMP}"
64015d
+        exit 1
64015d
+    fi
64015d
+else
64015d
+    if ! cryptsetup luksKillSlot --batch-mode "${DEV}" "${SLT}"; then
64015d
+        echo "Error wiping slot ${SLT} from ${DEV}" >&2
64015d
+        clevis_luks_restore_dev "${DEV}" "${TMP}"
64015d
+        exit 1
64015d
+    fi
64015d
+
64015d
+    if ! echo -n "${new_pass}" \
64015d
+            | cryptsetup luksAddKey --key-slot "${SLT}" \
64015d
+                         --key-file <(echo -n "${pt}") "${DEV}"; then
64015d
+        echo "Error updating LUKS passphrase in ${DEV}:${SLT}." >&2
64015d
+        clevis_luks_restore_dev "${DEV}" "${TMP}"
64015d
+        exit 1
64015d
+    fi
64015d
+fi
64015d
+
64015d
+# Update the metadata.
64015d
+if ! clevis_luks_save_slot "${DEV}" "${SLT}" "${jwe}" "overwrite"; then
64015d
+    echo "Error updating metadata in ${DEV}:${SLT}" >&2
64015d
+    clevis_luks_restore_dev "${DEV}" "${TMP}"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+# Make sure we can unlock the device with this keyslot.
64015d
+if ! clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}" >/dev/null \
64015d
+                                       2>/dev/null; then
64015d
+    echo "Invalid configuration detected. Reverting changes." >&2
64015d
+    clevis_luks_restore_dev "${DEV}" "${TMP}"
64015d
+    exit 1
64015d
+fi
64015d
+
64015d
+echo "Binding edited successfully."
64015d
diff --git a/src/luks/clevis-luks-edit.1.adoc b/src/luks/clevis-luks-edit.1.adoc
64015d
new file mode 100644
64015d
index 0000000..454de89
64015d
--- /dev/null
64015d
+++ b/src/luks/clevis-luks-edit.1.adoc
64015d
@@ -0,0 +1,69 @@
64015d
+CLEVIS-LUKS-EDIT(1)
64015d
+===================
64015d
+:doctype: manpage
64015d
+
64015d
+
64015d
+== NAME
64015d
+
64015d
+clevis-luks-edit - Edit a binding from a clevis-bound slot in a LUKS device
64015d
+
64015d
+== SYNOPSIS
64015d
+
64015d
+*clevis luks edit* -d DEV -s SLT [-c CONFIG]
64015d
+
64015d
+== OVERVIEW
64015d
+
64015d
+The *clevis luks edit* command edits clevis bindings from a LUKS device.
64015d
+For example:
64015d
+
64015d
+    clevis luks edit -d /dev/sda1 -s 1
64015d
+
64015d
+== OPTIONS
64015d
+
64015d
+* *-d* _DEV_ :
64015d
+  The LUKS device to edit clevis-bound pins
64015d
+
64015d
+* *-s* _SLT_ :
64015d
+  The slot to use when editing the clevis binding
64015d
+
64015d
+* *-f* :
64015d
+  Proceed with the edit operation even if the config is the same as the current one
64015d
+
64015d
+* *-c* _CONFIG_ :
64015d
+  The updated config to use
64015d
+
64015d
+
64015d
+== EXAMPLES
64015d
+
64015d
+    clevis luks list -d /dev/sda1
64015d
+    1: tang '{"url":"addr"}'
64015d
+
64015d
+As we can see in the example above, */dev/sda1* has one slots bound, in this case, to a _tang_ pin.
64015d
+
64015d
+We can edit this binding by issuing the following command:
64015d
+
64015d
+    clevis luks edit -d /dev/sda1 -s 1
64015d
+
64015d
+This will open a text editor -- the one set in the $EDITOR environment variable, or _vi_, as a fallback -- with the current
64015d
+configuration of this binding to be edited. In this case, we should have the following:
64015d
+
64015d
+    {
64015d
+        "url": "addr"
64015d
+    }
64015d
+
64015d
+Once at the editor, we can edit the pin configuration. For _tang_, we could edit the _url_, for instance. After completing the change,
64015d
+save the file and exit. The updated configuration will be validated for JSON, and if there are no errors, you will be shown the
64015d
+updated configuration and prompted whether to proceed.
64015d
+
64015d
+By proceeding, the binding will be updated. There may be required to provide a valid LUKS passphrase for the device.
64015d
+
64015d
+In the second example, we will update the same device and slot, but we will be providing the updated configuration as well:
64015d
+
64015d
+    clevis luks edit -d /dev/sda1 -s 1 -c '{"url":"new-addr-here"}'
64015d
+
64015d
+In this case, the binding update will be done in non-interactive mode, however it also may be required to provide a valud LUKS
64015d
+passphrase for the device.
64015d
+
64015d
+== SEE ALSO
64015d
+
64015d
+link:clevis-luks-list.1.adoc[*clevis-luks-list*(1)],
64015d
diff --git a/src/luks/clevis-luks-regen b/src/luks/clevis-luks-regen
64015d
index 6071d85..8f32e08 100755
64015d
--- a/src/luks/clevis-luks-regen
64015d
+++ b/src/luks/clevis-luks-regen
64015d
@@ -41,15 +41,7 @@ function usage_and_exit () {
64015d
     exit "${1}"
64015d
 }
64015d
 
64015d
-on_exit() {
64015d
-    if [ ! -d "${TMP}" ] || ! rm -rf "${TMP}"; then
64015d
-        echo "Delete temporary files failed!" >&2
64015d
-        echo "You need to clean up: ${TMP}" >&2
64015d
-        exit 1
64015d
-    fi
64015d
-}
64015d
-
64015d
-while getopts ":hfd:s:" o; do
64015d
+while getopts ":hd:s:" o; do
64015d
     case "$o" in
64015d
     d) DEV="$OPTARG";;
64015d
     h) usage_and_exit 0;;
64015d
@@ -68,117 +60,22 @@ if [ -z "$SLT" ]; then
64015d
     exit 1
64015d
 fi
64015d
 
64015d
-### ----------------------------------------------------------------------
64015d
-if ! pin_cfg=$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null); then
64015d
-    echo "Error obtaining current configuration of device ${DEV}:${SLT}" >&2
64015d
+if ! binding="$(clevis luks list -d "${DEV}" -s "${SLT}" 2>/dev/null)" \
64015d
+                || [ -z "${binding}" ]; then
64015d
+    echo "Error retrieving current configuration from ${DEV}:${SLT}" >&2
64015d
     exit 1
64015d
 fi
64015d
 
64015d
-PIN=$(echo "${pin_cfg}" | awk '{ print $2 }')
64015d
-CFG=$(echo "${pin_cfg}" | awk '{ print $3 }' | tr -d "'")
64015d
+read -r _ pin cfg <<< "${binding}"
64015d
 
64015d
 echo "Regenerating with:"
64015d
-echo "PIN: $PIN"
64015d
-echo "CONFIG: $CFG"
64015d
-
64015d
-trap 'echo "Ignoring CONTROL-C!"' INT TERM
64015d
-
64015d
-# Get the existing key.
64015d
-if ! existing_key=$(clevis luks pass -d "${DEV}" -s "${SLT}" 2>/dev/null); then
64015d
-    # We failed to obtain the passphrase for the slot -- perhaps
64015d
-    # it was rotated? --, so let's request user input.
64015d
-    read -r -s -p "Enter existing LUKS password: " existing_key; echo
64015d
-fi
64015d
-
64015d
-# Check if the key is valid.
64015d
-if ! cryptsetup open --test-passphrase "${DEV}" <<< "${existing_key}"; then
64015d
-    exit 1
64015d
-fi
64015d
-
64015d
-# Check if we can do the update in-place, i.e., if the key we got is the one
64015d
-# for the slot we are interested in.
64015d
-in_place=
64015d
-if cryptsetup open --test-passphrase --key-slot "${SLT}" "${DEV}" \
64015d
-        <<< "${existing_key}"; then
64015d
-    in_place=true
64015d
-fi
64015d
-
64015d
-# Create new key.
64015d
-if ! new_passphrase=$(generate_key "${DEV}"); then
64015d
-    echo "Error generating new key for device ${DEV}" >&2
64015d
-    exit 1
64015d
-fi
64015d
-
64015d
-# Reencrypt the new password.
64015d
-if ! jwe="$(clevis encrypt "${PIN}" "${CFG}" <<< "${new_passphrase}")"; then
64015d
-    echo "Error using pin '${PIN}' with config '${CFG}'" >&2
64015d
-    exit 1
64015d
-fi
64015d
-
64015d
-# Updating the metadata and the actual passphrase are destructive operations,
64015d
-# hence we will do a backup of the LUKS header and restore it later in case
64015d
-# we have issues performing these operations.
64015d
-if ! TMP="$(mktemp -d)"; then
64015d
-    echo "Creating a temporary dir for device backup/restore failed!" >&2
64015d
-    exit 1
64015d
-fi
64015d
-trap 'on_exit' EXIT
64015d
-
64015d
-# Backup LUKS header.
64015d
-if ! clevis_luks_backup_dev "${DEV}" "${TMP}"; then
64015d
-    echo "Error while trying to back up LUKS header from ${DEV}" >&2
64015d
-    exit 1
64015d
-fi
64015d
-
64015d
-restore_device() {
64015d
-    local DEV="${1}"
64015d
-    local TMP="${2}"
64015d
-
64015d
-    if ! clevis_luks_restore_dev "${DEV}" "${TMP}"; then
64015d
-        echo "Error while trying to restore LUKS header from ${DEV}." >&2
64015d
-    else
64015d
-        echo "LUKS header restored successfully." >&2
64015d
-    fi
64015d
-}
64015d
-
64015d
-# Update the key slot with the new key. If we have the key for this slot,
64015d
-# the change happens in-place. Otherwise, we kill the slot and re-add it.
64015d
-if [ -n "${in_place}" ]; then
64015d
-    if ! cryptsetup luksChangeKey "${DEV}" --key-slot "${SLT}" \
64015d
-            <(echo -n "${new_passphrase}") <<< "${existing_key}"; then
64015d
-        echo "Error updating LUKS passphrase in ${DEV}:${SLT}" >&2
64015d
-        restore_device "${DEV}" "${TMP}"
64015d
-        exit 1
64015d
-    fi
64015d
-else
64015d
-    if ! cryptsetup luksKillSlot --batch-mode "${DEV}" "${SLT}"; then
64015d
-        echo "Error wiping slot ${SLT} from ${DEV}" >&2
64015d
-        restore_device "${DEV}" "${TMP}"
64015d
-        exit 1
64015d
-    fi
64015d
-
64015d
-    if ! echo -n "${new_passphrase}" \
64015d
-            | cryptsetup luksAddKey --key-slot "${SLT}" \
64015d
-                         --key-file <(echo -n "${existing_key}") "${DEV}"; then
64015d
-        echo "Error updating LUKS passphrase in ${DEV}:${SLT}." >&2
64015d
-        restore_device "${DEV}" "${TMP}"
64015d
-        exit 1
64015d
-    fi
64015d
-fi
64015d
-
64015d
-# Update the metadata.
64015d
-if ! clevis_luks_save_slot "${DEV}" "${SLT}" "${jwe}" "overwrite"; then
64015d
-    echo "Error updating metadata in ${DEV}:${SLT}" >&2
64015d
-    restore_device "${DEV}" "${TMP}"
64015d
-    exit 1
64015d
-fi
64015d
+echo "PIN: ${pin}"
64015d
+echo "CONFIG: ${cfg}"
64015d
 
64015d
-# Now make sure that we can unlock this device after the change.
64015d
-# If we can't, undo the changes.
64015d
-if ! cryptsetup open --test-passphrase --key-slot "${SLT}" "${DEV}" 2>/dev/null \
64015d
-        <<< "$(clevis luks pass -d "${DEV}" -s "${SLT}" 2>/dev/null)"; then
64015d
-    echo "Invalid configuration detected after rebinding. Reverting changes."
64015d
-    restore_device "${DEV}" "${TMP}"
64015d
+# Remove single quotes.
64015d
+cfg=${cfg//\'}
64015d
+if ! clevis luks edit -f -d "${DEV}" -s "${SLT}" -c "${cfg}" >/dev/null; then
64015d
+    echo "Error rotating keys in ${DEV}:${SLT}" >&2
64015d
     exit 1
64015d
 fi
64015d
 
64015d
diff --git a/src/luks/meson.build b/src/luks/meson.build
64015d
index ee588c3..ba02bd9 100644
64015d
--- a/src/luks/meson.build
64015d
+++ b/src/luks/meson.build
64015d
@@ -54,6 +54,9 @@ if libcryptsetup.found() and luksmeta.found() and pwmake.found()
64015d
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-sss')
64015d
   bins += join_paths(meson.current_source_dir(), 'clevis-luks-report-tang')
64015d
   mans += join_paths(meson.current_source_dir(), 'clevis-luks-report.1')
64015d
+
64015d
+  bins += join_paths(meson.current_source_dir(), 'clevis-luks-edit')
64015d
+  mans += join_paths(meson.current_source_dir(), 'clevis-luks-edit.1')
64015d
 else
64015d
   warning('Will not install LUKS support due to missing dependencies!')
64015d
 endif
64015d
diff --git a/src/luks/tests/edit-tang-luks1 b/src/luks/tests/edit-tang-luks1
64015d
new file mode 100755
64015d
index 0000000..3d42d68
64015d
--- /dev/null
64015d
+++ b/src/luks/tests/edit-tang-luks1
64015d
@@ -0,0 +1,106 @@
64015d
+#!/bin/bash -ex
64015d
+# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2020 Red Hat, Inc.
64015d
+# Author: Sergio Correia <scorreia@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+
64015d
+TEST=$(basename "${0}")
64015d
+. tests-common-functions
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+on_exit() {
64015d
+    local d
64015d
+    for d in "${TMP}" "${TMP2}"; do
64015d
+        [ ! -d "${d}" ] && continue
64015d
+        tang_stop "${d}"
64015d
+        rm -rf "${d}"
64015d
+    done
64015d
+}
64015d
+
64015d
+trap 'on_exit' EXIT
64015d
+trap 'on_exit' ERR
64015d
+
64015d
+TMP="$(mktemp -d)"
64015d
+
64015d
+port=$(get_random_port)
64015d
+tang_run "${TMP}" "${port}" &
64015d
+tang_wait_until_ready "${port}"
64015d
+
64015d
+url="http://${TANG_HOST}:${port}"
64015d
+
64015d
+cfg=$(printf '{"url":"%s"}' "${url}")
64015d
+
64015d
+# LUKS1.
64015d
+DEV="${TMP}/luks1-device"
64015d
+new_device "luks1" "${DEV}"
64015d
+
64015d
+if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+
64015d
+# Now let's try to change the config but using the same one we already have.
64015d
+if clevis luks edit -d "${DEV}" -s 1 -c "${cfg}"; then
64015d
+    error "${TEST}: edit should not have succeeded because the config is the same."
64015d
+fi
64015d
+
64015d
+# And now, just a broken config.
64015d
+new_cfg=$(printf '{"url&:"%s"}' "${url}")
64015d
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have failed because of invalid JSON"
64015d
+fi
64015d
+
64015d
+# Now let's have another tang instance running and change the config to use
64015d
+# the new one.
64015d
+port2=$(get_random_port)
64015d
+TMP2="$(mktemp -d)"
64015d
+tang_run "${TMP2}" "${port2}" &
64015d
+tang_wait_until_ready "${port2}"
64015d
+new_url="http://${TANG_HOST}:${port2}"
64015d
+new_cfg=$(printf '{"url":"%s"}' "${new_url}")
64015d
+
64015d
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have succeeded."
64015d
+fi
64015d
+
64015d
+# And now let's use sss and start with a single tang server, then add a second
64015d
+# one.
64015d
+new_device "luks1" "${DEV}"
64015d
+cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"}]}}' "${url}")
64015d
+if ! clevis luks bind -y -d "${DEV}" sss "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+new_cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
64015d
+          "${url}" "${new_url}")
64015d
+
64015d
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have succeeded and added a new tang server"
64015d
+fi
64015d
+
64015d
+# Now let's change the threshold to 2.
64015d
+new_cfg=$(printf '{"t":2,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
64015d
+          "${url}" "${new_url}")
64015d
+
64015d
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have succeeded and added a new tang server"
64015d
+fi
64015d
+
64015d
+# And finally, let's try a broken config, with a wrong threshold.
64015d
+new_cfg=$(printf '{"t":3,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
64015d
+          "${url}" "${new_url}")
64015d
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have failed because threshold > number of servers"
64015d
+fi
64015d
diff --git a/src/luks/tests/edit-tang-luks2 b/src/luks/tests/edit-tang-luks2
64015d
new file mode 100755
64015d
index 0000000..9000053
64015d
--- /dev/null
64015d
+++ b/src/luks/tests/edit-tang-luks2
64015d
@@ -0,0 +1,106 @@
64015d
+#!/bin/bash -ex
64015d
+# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
64015d
+#
64015d
+# Copyright (c) 2020 Red Hat, Inc.
64015d
+# Author: Sergio Correia <scorreia@redhat.com>
64015d
+#
64015d
+# This program is free software: you can redistribute it and/or modify
64015d
+# it under the terms of the GNU General Public License as published by
64015d
+# the Free Software Foundation, either version 3 of the License, or
64015d
+# (at your option) any later version.
64015d
+#
64015d
+# This program is distributed in the hope that it will be useful,
64015d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
64015d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64015d
+# GNU General Public License for more details.
64015d
+#
64015d
+# You should have received a copy of the GNU General Public License
64015d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
64015d
+
64015d
+TEST=$(basename "${0}")
64015d
+. tests-common-functions
64015d
+
64015d
+. clevis-luks-common-functions
64015d
+
64015d
+on_exit() {
64015d
+    local d
64015d
+    for d in "${TMP}" "${TMP2}"; do
64015d
+        [ ! -d "${d}" ] && continue
64015d
+        tang_stop "${d}"
64015d
+        rm -rf "${d}"
64015d
+    done
64015d
+}
64015d
+
64015d
+trap 'on_exit' EXIT
64015d
+trap 'on_exit' ERR
64015d
+
64015d
+TMP="$(mktemp -d)"
64015d
+
64015d
+port=$(get_random_port)
64015d
+tang_run "${TMP}" "${port}" &
64015d
+tang_wait_until_ready "${port}"
64015d
+
64015d
+url="http://${TANG_HOST}:${port}"
64015d
+
64015d
+cfg=$(printf '{"url":"%s"}' "${url}")
64015d
+
64015d
+# LUKS2.
64015d
+DEV="${TMP}/luks2-device"
64015d
+new_device "luks2" "${DEV}"
64015d
+
64015d
+if ! clevis luks bind -y -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+
64015d
+# Now let's try to change the config but using the same one we already have.
64015d
+if clevis luks edit -d "${DEV}" -s 1 -c "${cfg}"; then
64015d
+    error "${TEST}: edit should not have succeeded because the config is the same."
64015d
+fi
64015d
+
64015d
+# And now, just a broken config.
64015d
+new_cfg=$(printf '{"url&:"%s"}' "${url}")
64015d
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have failed because of invalid JSON"
64015d
+fi
64015d
+
64015d
+# Now let's have another tang instance running and change the config to use
64015d
+# the new one.
64015d
+port2=$(get_random_port)
64015d
+TMP2="$(mktemp -d)"
64015d
+tang_run "${TMP2}" "${port2}" &
64015d
+tang_wait_until_ready "${port2}"
64015d
+new_url="http://${TANG_HOST}:${port2}"
64015d
+new_cfg=$(printf '{"url":"%s"}' "${new_url}")
64015d
+
64015d
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have succeeded."
64015d
+fi
64015d
+
64015d
+# And now let's use sss and start with a single tang server, then add a second
64015d
+# one.
64015d
+new_device "luks2" "${DEV}"
64015d
+cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"}]}}' "${url}")
64015d
+if ! clevis luks bind -y -d "${DEV}" sss "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
+    error "${TEST}: Bind should have succeeded."
64015d
+fi
64015d
+new_cfg=$(printf '{"t":1,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
64015d
+          "${url}" "${new_url}")
64015d
+
64015d
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have succeeded and added a new tang server"
64015d
+fi
64015d
+
64015d
+# Now let's change the threshold to 2.
64015d
+new_cfg=$(printf '{"t":2,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
64015d
+          "${url}" "${new_url}")
64015d
+
64015d
+if ! clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have succeeded and added a new tang server"
64015d
+fi
64015d
+
64015d
+# And finally, let's try a broken config, with a wrong threshold.
64015d
+new_cfg=$(printf '{"t":3,"pins":{"tang":[{"url":"%s"},{"url":"%s"}]}}' \
64015d
+          "${url}" "${new_url}")
64015d
+if clevis luks edit -d "${DEV}" -s 1 -c "${new_cfg}"; then
64015d
+    error "${TEST}: edit should have failed because threshold > number of servers"
64015d
+fi
64015d
diff --git a/src/luks/tests/meson.build b/src/luks/tests/meson.build
64015d
index 4795488..67fa597 100644
64015d
--- a/src/luks/tests/meson.build
64015d
+++ b/src/luks/tests/meson.build
64015d
@@ -4,8 +4,9 @@ actv = find_program(
64015d
   required: false
64015d
 )
64015d
 
64015d
-# We use jq for comparing the pin config in the clevis luks list tests.
64015d
-jq = find_program('jq', required: false)
64015d
+# We use jq for comparing the pin config in the clevis luks list tests
64015d
+# and also in clevis luks edit.
64015d
+jq = find_program('jq', required: true)
64015d
 
64015d
 # we use systemd-socket-activate for running test tang servers.
64015d
 actv = find_program(
64015d
@@ -74,19 +75,16 @@ test('bind-pass-with-newline', find_program('bind-pass-with-newline-luks1'), env
64015d
 test('bind-pass-with-newline-keyfile', find_program('bind-pass-with-newline-keyfile-luks1'), env: env)
64015d
 # Bug #70.
64015d
 test('bind-already-used-luksmeta-slot', find_program('bind-already-used-luksmeta-slot'), env: env, timeout: 60)
64015d
-
64015d
-if jq.found()
64015d
-  test('list-recursive-luks1', find_program('list-recursive-luks1'), env: env)
64015d
-  test('list-tang-luks1', find_program('list-tang-luks1'), env: env)
64015d
-  test('list-sss-tang-luks1', find_program('list-sss-tang-luks1'), env: env)
64015d
-else
64015d
-  warning('Will not run "clevis luks list" tests due to missing jq dependency')
64015d
-endif
64015d
+test('list-recursive-luks1', find_program('list-recursive-luks1'), env: env)
64015d
+test('list-tang-luks1', find_program('list-tang-luks1'), env: env)
64015d
+test('list-sss-tang-luks1', find_program('list-sss-tang-luks1'), env: env)
64015d
 
64015d
 if has_tang
64015d
   test('unlock-tang-luks1', find_program('unlock-tang-luks1'), env: env, timeout: 90)
64015d
   test('assume-yes-luks1', find_program('assume-yes-luks1'), env: env)
64015d
+  test('edit-tang-luks1', find_program('edit-tang-luks1'), env: env, timeout: 90)
64015d
 endif
64015d
+
64015d
 test('pass-tang-luks1', find_program('pass-tang-luks1'), env: env)
64015d
 test('backup-restore-luks1', find_program('backup-restore-luks1'), env: env)
64015d
 test('regen-inplace-luks1', find_program('regen-inplace-luks1'), env: env, timeout: 90)
64015d
@@ -100,16 +98,14 @@ if luksmeta_data.get('OLD_CRYPTSETUP') == '0'
64015d
   test('bind-luks2', find_program('bind-luks2'), env: env, timeout: 60)
64015d
   test('unbind-unbound-slot-luks2', find_program('unbind-unbound-slot-luks2'), env: env)
64015d
   test('unbind-luks2', find_program('unbind-luks2'), env: env, timeout: 60)
64015d
-
64015d
-  if jq.found()
64015d
-    test('list-recursive-luks2', find_program('list-recursive-luks2'), env: env, timeout: 60)
64015d
-    test('list-tang-luks2', find_program('list-tang-luks2'), env: env, timeout: 60)
64015d
-    test('list-sss-tang-luks2', find_program('list-sss-tang-luks2'), env: env, timeout: 60)
64015d
-  endif
64015d
+  test('list-recursive-luks2', find_program('list-recursive-luks2'), env: env, timeout: 60)
64015d
+  test('list-tang-luks2', find_program('list-tang-luks2'), env: env, timeout: 60)
64015d
+  test('list-sss-tang-luks2', find_program('list-sss-tang-luks2'), env: env, timeout: 60)
64015d
 
64015d
   if has_tang
64015d
     test('unlock-tang-luks2', find_program('unlock-tang-luks2'), env: env, timeout: 120)
64015d
     test('assume-yes-luks2', find_program('assume-yes-luks2'), env: env, timeout: 60)
64015d
+    test('edit-tang-luks2', find_program('edit-tang-luks2'), env: env, timeout: 120)
64015d
   endif
64015d
   test('pass-tang-luks2', find_program('pass-tang-luks2'), env: env, timeout: 60)
64015d
   test('backup-restore-luks2', find_program('backup-restore-luks2'), env:env, timeout: 90)
64015d
diff --git a/src/luks/tests/regen-inplace-luks1 b/src/luks/tests/regen-inplace-luks1
64015d
index 3a42ced..32072c4 100755
64015d
--- a/src/luks/tests/regen-inplace-luks1
64015d
+++ b/src/luks/tests/regen-inplace-luks1
64015d
@@ -76,7 +76,7 @@ fi
64015d
 old_key=$(clevis luks pass -d "${DEV}" -s "${SLT}")
64015d
 
64015d
 # Now let's try regen.
64015d
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
64015d
+if ! clevis luks regen -d "${DEV}" -s "${SLT}"; then
64015d
     error "${TEST}: clevis luks regen failed"
64015d
 fi
64015d
 
64015d
diff --git a/src/luks/tests/regen-inplace-luks2 b/src/luks/tests/regen-inplace-luks2
64015d
index 1cb7a29..d1bc182 100755
64015d
--- a/src/luks/tests/regen-inplace-luks2
64015d
+++ b/src/luks/tests/regen-inplace-luks2
64015d
@@ -77,7 +77,7 @@ fi
64015d
 old_key=$(clevis luks pass -d "${DEV}" -s "${SLT}")
64015d
 
64015d
 # Now let's try regen.
64015d
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
64015d
+if ! clevis luks regen -d "${DEV}" -s "${SLT}"; then
64015d
     error "${TEST}: clevis luks regen failed"
64015d
 fi
64015d
 
64015d
diff --git a/src/luks/tests/regen-not-inplace-luks1 b/src/luks/tests/regen-not-inplace-luks1
64015d
index 1b65ca7..fba61dc 100755
64015d
--- a/src/luks/tests/regen-not-inplace-luks1
64015d
+++ b/src/luks/tests/regen-not-inplace-luks1
64015d
@@ -79,7 +79,7 @@ if [ "${enabled}" -ne 2 ]; then
64015d
 fi
64015d
 
64015d
 # Now let's try regen.
64015d
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
64015d
+if ! clevis luks regen -d "${DEV}" -s "${SLT}" <<< "${DEFAULT_PASS}"; then
64015d
     error "${TEST}: clevis luks regen failed"
64015d
 fi
64015d
 
64015d
diff --git a/src/luks/tests/regen-not-inplace-luks2 b/src/luks/tests/regen-not-inplace-luks2
64015d
index dc91449..6e3b012 100755
64015d
--- a/src/luks/tests/regen-not-inplace-luks2
64015d
+++ b/src/luks/tests/regen-not-inplace-luks2
64015d
@@ -80,7 +80,7 @@ if [ "${enabled}" -ne 2 ]; then
64015d
 fi
64015d
 
64015d
 # Now let's try regen.
64015d
-if ! clevis_regen "${DEV}" "${SLT}" "${DEFAULT_PASS}"; then
64015d
+if ! clevis luks regen -d "${DEV}" -s "${SLT}" <<< "${DEFAULT_PASS}"; then
64015d
     error "${TEST}: clevis luks regen failed"
64015d
 fi
64015d
 
64015d
diff --git a/src/luks/tests/tests-common-functions.in b/src/luks/tests/tests-common-functions.in
64015d
index 6101f28..7b3fdad 100755
64015d
--- a/src/luks/tests/tests-common-functions.in
64015d
+++ b/src/luks/tests/tests-common-functions.in
64015d
@@ -229,31 +229,5 @@ tang_get_adv() {
64015d
     curl -o "${adv}" http://"${TANG_HOST}":"${port}"/adv
64015d
 }
64015d
 
64015d
-# Regenerate binding.
64015d
-clevis_regen() {
64015d
-    local DEV="${1}"
64015d
-    local SLT="${2}"
64015d
-    local PASS="${3}"
64015d
-
64015d
-    expect -d << CLEVIS_REGEN
64015d
-        set timeout 120
64015d
-        spawn sh -c "clevis luks regen -d $DEV -s $SLT"
64015d
-        expect {
64015d
-            "Enter existing LUKS password" {
64015d
-                send "$PASS\r"
64015d
-                exp_continue
64015d
-            }
64015d
-            "Do you wish to trust these keys" {
64015d
-                send "y\r"
64015d
-                exp_continue
64015d
-            }
64015d
-            expect eof
64015d
-            wait
64015d
-        }
64015d
-CLEVIS_REGEN
64015d
-    ret=$?
64015d
-    return "${ret}"
64015d
-}
64015d
-
64015d
 export TANG_HOST=127.0.0.1
64015d
 export DEFAULT_PASS='just-some-test-password-here'
64015d
diff --git a/src/luks/tests/unlock-tang-luks1 b/src/luks/tests/unlock-tang-luks1
64015d
index 841ba01..6ede47b 100755
64015d
--- a/src/luks/tests/unlock-tang-luks1
64015d
+++ b/src/luks/tests/unlock-tang-luks1
64015d
@@ -31,6 +31,10 @@ on_exit() {
64015d
 trap 'on_exit' EXIT
64015d
 trap 'on_exit' ERR
64015d
 
64015d
+# LUKS1.
64015d
+DEV="${TMP}/luks1-device"
64015d
+new_device "luks1" "${DEV}"
64015d
+
64015d
 TMP="$(mktemp -d)"
64015d
 
64015d
 port=$(get_random_port)
64015d
@@ -43,9 +47,6 @@ tang_get_adv "${port}" "${adv}"
64015d
 
64015d
 cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
64015d
 
64015d
-# LUKS1.
64015d
-DEV="${TMP}/luks1-device"
64015d
-new_device "luks1" "${DEV}"
64015d
 
64015d
 if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
     error "${TEST}: Bind should have succeeded."
64015d
diff --git a/src/luks/tests/unlock-tang-luks2 b/src/luks/tests/unlock-tang-luks2
64015d
index 81822fb..b32c5aa 100755
64015d
--- a/src/luks/tests/unlock-tang-luks2
64015d
+++ b/src/luks/tests/unlock-tang-luks2
64015d
@@ -31,6 +31,10 @@ on_exit() {
64015d
 trap 'on_exit' EXIT
64015d
 trap 'on_exit' ERR
64015d
 
64015d
+# LUKS2.
64015d
+DEV="${TMP}/luks2-device"
64015d
+new_device "luks2" "${DEV}"
64015d
+
64015d
 TMP="$(mktemp -d)"
64015d
 
64015d
 port=$(get_random_port)
64015d
@@ -43,10 +47,6 @@ tang_get_adv "${port}" "${adv}"
64015d
 
64015d
 cfg=$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")
64015d
 
64015d
-# LUKS2.
64015d
-DEV="${TMP}/luks2-device"
64015d
-new_device "luks2" "${DEV}"
64015d
-
64015d
 if ! clevis luks bind -f -d "${DEV}" tang "${cfg}" <<< "${DEFAULT_PASS}"; then
64015d
     error "${TEST}: Bind should have succeeded."
64015d
 fi
64015d
-- 
64015d
2.18.4
64015d