Skip to content

Commit

Permalink
Merge pull request #3602 from akutz/feature/include-crypto-key-in-dis…
Browse files Browse the repository at this point in the history
…k-info

api: Fetch crypto key in GetVirtualDiskInfoByUUID
  • Loading branch information
akutz authored Oct 24, 2024
2 parents 499b304 + 6e84b72 commit 6df5289
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 5 deletions.
27 changes: 23 additions & 4 deletions vmdk/disk_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ import (
"github.com/vmware/govmomi/vim25/types"
)

type VirtualDiskCryptoKey struct {
KeyID string
ProviderID string
}

type VirtualDiskInfo struct {
CapacityInBytes int64
DeviceKey int32
FileName string
Size int64
UniqueSize int64
CryptoKey VirtualDiskCryptoKey
}

// GetVirtualDiskInfoByUUID returns information about a virtual disk identified
Expand Down Expand Up @@ -87,8 +93,9 @@ func GetVirtualDiskInfoByUUID(
// Find the disk by UUID by inspecting all of the disk backing types that
// can have an associated UUID.
var (
disk *types.VirtualDisk
fileName string
disk *types.VirtualDisk
fileName string
cryptoKey *types.CryptoKeyId
)
for i := range mo.Config.Hardware.Device {
switch tvd := mo.Config.Hardware.Device[i].(type) {
Expand All @@ -98,11 +105,13 @@ func GetVirtualDiskInfoByUUID(
if tb.Uuid == diskUUID {
disk = tvd
fileName = tb.FileName
cryptoKey = tb.KeyId
}
case *types.VirtualDiskSeSparseBackingInfo:
if tb.Uuid == diskUUID {
disk = tvd
fileName = tb.FileName
cryptoKey = tb.KeyId
}
case *types.VirtualDiskRawDiskMappingVer1BackingInfo:
if tb.Uuid == diskUUID {
Expand All @@ -113,6 +122,7 @@ func GetVirtualDiskInfoByUUID(
if tb.Uuid == diskUUID {
disk = tvd
fileName = tb.FileName
cryptoKey = tb.KeyId
}
case *types.VirtualDiskRawDiskVer2BackingInfo:
if tb.Uuid == diskUUID {
Expand Down Expand Up @@ -154,11 +164,20 @@ func GetVirtualDiskInfoByUUID(
}
}

return VirtualDiskInfo{
di := VirtualDiskInfo{
CapacityInBytes: disk.CapacityInBytes,
DeviceKey: disk.Key,
FileName: fileName,
Size: size,
UniqueSize: uniqueSize,
}, nil
}

if ck := cryptoKey; ck != nil {
di.CryptoKey.KeyID = ck.KeyId
if pid := ck.ProviderId; pid != nil {
di.CryptoKey.ProviderID = pid.Id
}
}

return di, nil
}
95 changes: 94 additions & 1 deletion vmdk/disk_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
}
}

getEncryptedDiskInfo := func(pid, kid string) vmdk.VirtualDiskInfo {
return vmdk.VirtualDiskInfo{
CapacityInBytes: tenGiBInBytes,
DeviceKey: deviceKey,
FileName: fileName,
Size: (1 * 1024 * 1024 * 1024) + 950,
UniqueSize: (5 * 1024 * 1024) + 100,
CryptoKey: vmdk.VirtualDiskCryptoKey{
KeyID: kid,
ProviderID: pid,
},
}
}

getLayoutEx := func() *types.VirtualMachineFileLayoutEx {
return &types.VirtualMachineFileLayoutEx{
Disk: []types.VirtualMachineFileLayoutExDiskLayout{
Expand Down Expand Up @@ -150,6 +164,32 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
diskUUID: diskUUID,
diskInfo: getDiskInfo(),
},
{
name: "one encrypted disk w VirtualDiskFlatVer2BackingInfo",
mo: mo.VirtualMachine{
Config: &types.VirtualMachineConfigInfo{
Hardware: types.VirtualHardware{
Device: []types.BaseVirtualDevice{
getDisk(&types.VirtualDiskFlatVer2BackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: fileName,
},
Uuid: diskUUID,
KeyId: &types.CryptoKeyId{
KeyId: "my-key-id",
ProviderId: &types.KeyProviderId{
Id: "my-provider-id",
},
},
}),
},
},
},
LayoutEx: getLayoutEx(),
},
diskUUID: diskUUID,
diskInfo: getEncryptedDiskInfo("my-provider-id", "my-key-id"),
},
{
name: "one disk w VirtualDiskSeSparseBackingInfo",
mo: mo.VirtualMachine{
Expand All @@ -170,6 +210,32 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
diskUUID: diskUUID,
diskInfo: getDiskInfo(),
},
{
name: "one encrypted disk w VirtualDiskSeSparseBackingInfo",
mo: mo.VirtualMachine{
Config: &types.VirtualMachineConfigInfo{
Hardware: types.VirtualHardware{
Device: []types.BaseVirtualDevice{
getDisk(&types.VirtualDiskSeSparseBackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: fileName,
},
Uuid: diskUUID,
KeyId: &types.CryptoKeyId{
KeyId: "my-key-id",
ProviderId: &types.KeyProviderId{
Id: "my-provider-id",
},
},
}),
},
},
},
LayoutEx: getLayoutEx(),
},
diskUUID: diskUUID,
diskInfo: getEncryptedDiskInfo("my-provider-id", "my-key-id"),
},
{
name: "one disk w VirtualDiskRawDiskMappingVer1BackingInfo",
mo: mo.VirtualMachine{
Expand Down Expand Up @@ -210,6 +276,32 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
diskUUID: diskUUID,
diskInfo: getDiskInfo(),
},
{
name: "one encrypted disk w VirtualDiskSparseVer2BackingInfo",
mo: mo.VirtualMachine{
Config: &types.VirtualMachineConfigInfo{
Hardware: types.VirtualHardware{
Device: []types.BaseVirtualDevice{
getDisk(&types.VirtualDiskSparseVer2BackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: fileName,
},
Uuid: diskUUID,
KeyId: &types.CryptoKeyId{
KeyId: "my-key-id",
ProviderId: &types.KeyProviderId{
Id: "my-provider-id",
},
},
}),
},
},
},
LayoutEx: getLayoutEx(),
},
diskUUID: diskUUID,
diskInfo: getEncryptedDiskInfo("my-provider-id", "my-key-id"),
},
{
name: "one disk w VirtualDiskRawDiskVer2BackingInfo",
mo: mo.VirtualMachine{
Expand Down Expand Up @@ -311,8 +403,9 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
for i := range testCases {
tc := testCases[i]
t.Run(tc.name, func(t *testing.T) {
var ctx context.Context
dii, err := vmdk.GetVirtualDiskInfoByUUID(
nil, nil, tc.mo, false, tc.diskUUID)
ctx, nil, tc.mo, false, tc.diskUUID)

if tc.err != "" {
assert.EqualError(t, err, tc.err)
Expand Down

0 comments on commit 6df5289

Please sign in to comment.