tape: fix regression in restoring key from medium

Since commit 1343dcaf we automatically try to load the key into the
drive after reading the media-set label, this cannot work for the case
where we actually restore the key from the tape itself.

To address this special case while preserving the automatic key
loading, everything except the setup of the key has been separated
from the 'read_label' method into a new function named
'read_label_without_loading_key'. Consequently, the 'restore-key' API
endpoint can be switched to utilize this new method, thereby avoiding
the issue.

Fixes: 1343dcaf ("tape: move 'set_encryption' calls to the TapeDriver")
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
 [ TL: reword and shorten commit message ]
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Dominik Csapak 2024-01-31 14:42:33 +01:00 committed by Thomas Lamprecht
parent 1565ff951e
commit a33d795741
2 changed files with 32 additions and 14 deletions

View File

@ -630,7 +630,7 @@ pub async fn restore_key(drive: String, password: String) -> Result<(), Error> {
run_drive_blocking_task(drive.clone(), "restore key".to_string(), move |config| {
let mut drive = open_drive(&config, &drive)?;
let (_media_id, key_config) = drive.read_label()?;
let (_media_id, key_config) = drive.read_label_without_loading_key()?;
if let Some(key_config) = key_config {
let password_fn = || Ok(password.as_bytes().to_vec());

View File

@ -105,11 +105,13 @@ pub trait TapeDriver {
key_config: Option<&KeyConfig>,
) -> Result<(), Error>;
/// Read the media label
/// Read the media label without setting the encryption key
///
/// This tries to read both media labels (label and
/// media_set_label). Also returns the optional encryption key configuration.
fn read_label(&mut self) -> Result<(Option<MediaId>, Option<KeyConfig>), Error> {
/// This is used internally by 'read_label' and when restoring the encryption
/// key from the drive. Should not be used or overwritten otherwise!
fn read_label_without_loading_key(
&mut self,
) -> Result<(Option<MediaId>, Option<KeyConfig>), Error> {
self.rewind()?;
let label = {
@ -182,20 +184,36 @@ pub trait TapeDriver {
bail!("got unexpected data after media set label");
}
drop(reader);
let encrypt_fingerprint = media_set_label
.encryption_key_fingerprint
.clone()
.map(|fp| (fp, media_set_label.uuid.clone()));
self.set_encryption(encrypt_fingerprint)?;
media_id.media_set_label = Some(media_set_label);
Ok((Some(media_id), key_config))
}
/// Read the media label
///
/// This tries to read both media labels (label and
/// media_set_label). Also returns the optional encryption key configuration.
///
/// Automatically sets the encryption key on the drive
fn read_label(&mut self) -> Result<(Option<MediaId>, Option<KeyConfig>), Error> {
let (media_id, key_config) = self.read_label_without_loading_key()?;
let encrypt_fingerprint = if let Some(media_set_label) =
media_id.as_ref().and_then(|id| id.media_set_label.clone())
{
media_set_label
.encryption_key_fingerprint
.clone()
.map(|fp| (fp, media_set_label.uuid.clone()))
} else {
None
};
self.set_encryption(encrypt_fingerprint)?;
Ok((media_id, key_config))
}
/// Eject media
fn eject_media(&mut self) -> Result<(), Error>;