disk_crypt/
resolver.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Resource resolver for the encrypted disk device.

use crate::CryptDisk;
use async_trait::async_trait;
use disk_backend::resolve::ResolveDiskParameters;
use disk_backend::resolve::ResolvedDisk;
use disk_crypt_resources::DiskCryptHandle;
use thiserror::Error;
use vm_resource::AsyncResolveResource;
use vm_resource::ResolveError;
use vm_resource::ResourceResolver;
use vm_resource::declare_static_async_resolver;
use vm_resource::kind::DiskHandleKind;

declare_static_async_resolver! {
    DiskCryptResolver,
    (DiskHandleKind, DiskCryptHandle),
}

/// The resolver for [`DiskCryptHandle`].
pub struct DiskCryptResolver;

/// An error that occurred while resolving a [`DiskCryptHandle`].
#[derive(Debug, Error)]
pub enum DiskResolveError {
    /// Failed to resolve the inner disk.
    #[error("failed to resolve inner disk")]
    ResolveInner(#[source] ResolveError),
    /// Failed to create the disk.
    #[error("failed to create disk")]
    NewDisk(#[source] crate::NewDiskError),
    /// The disk is invalid.
    #[error("invalid disk")]
    InvalidDisk(#[source] disk_backend::InvalidDisk),
}

#[async_trait]
impl AsyncResolveResource<DiskHandleKind, DiskCryptHandle> for DiskCryptResolver {
    type Output = ResolvedDisk;
    type Error = DiskResolveError;

    async fn resolve(
        &self,
        resolver: &ResourceResolver,
        resource: DiskCryptHandle,
        input: ResolveDiskParameters<'_>,
    ) -> Result<Self::Output, Self::Error> {
        let inner = resolver
            .resolve(
                resource.disk,
                ResolveDiskParameters {
                    read_only: input.read_only,
                    _async_trait_workaround: &(),
                },
            )
            .await
            .map_err(DiskResolveError::ResolveInner)?;

        let disk = CryptDisk::new(resource.cipher, &resource.key, inner.0)
            .map_err(DiskResolveError::NewDisk)?;
        ResolvedDisk::new(disk).map_err(DiskResolveError::InvalidDisk)
    }
}