diff --git a/providers/azure/connection/azureinstancesnapshot/provider.go b/providers/azure/connection/azureinstancesnapshot/provider.go index 2dc4221355..e02f79fbaa 100644 --- a/providers/azure/connection/azureinstancesnapshot/provider.go +++ b/providers/azure/connection/azureinstancesnapshot/provider.go @@ -36,7 +36,11 @@ const ( // the instance from which we're performing the scan type azureScannerInstance struct { - instanceInfo + subscriptionId string + resourceGroup string + name string + // holds extra information about the instance, fetched via the Azure API + instanceInfo *instanceInfo } type assetInfo struct { @@ -94,12 +98,10 @@ func determineScannerInstanceInfo(localConn *local.LocalConnection, token azcore return nil, err } - instanceInfo, err := InstanceInfo(resourceGrp, instanceName, subId, token) - if err != nil { - return nil, err - } return &azureScannerInstance{ - instanceInfo: instanceInfo, + subscriptionId: subId, + resourceGroup: resourceGrp, + name: instanceName, }, nil } @@ -167,6 +169,11 @@ func NewAzureSnapshotConnection(id uint32, conf *inventory.Config, asset *invent // 3. we either clone the target disk/snapshot and mount it // or we skip the setup and expect the disk to be already attached if !c.skipSetup() { + instanceInfo, err := InstanceInfo(scanner.resourceGroup, scanner.name, scanner.subscriptionId, token) + if err != nil { + return nil, err + } + c.scanner.instanceInfo = &instanceInfo scsiDevices, err := c.listScsiDevices() if err != nil { c.Close() @@ -327,4 +334,4 @@ func (c *AzureSnapshotConnection) Type() shared.ConnectionType { func (c *AzureSnapshotConnection) Config() *inventory.Config { return c.FileSystemConnection.Conf -} \ No newline at end of file +} diff --git a/providers/azure/connection/azureinstancesnapshot/setup.go b/providers/azure/connection/azureinstancesnapshot/setup.go index f35434d413..837f0770ae 100644 --- a/providers/azure/connection/azureinstancesnapshot/setup.go +++ b/providers/azure/connection/azureinstancesnapshot/setup.go @@ -55,6 +55,9 @@ func (c *AzureSnapshotConnection) setupDiskAndMount(target scanTarget, lun int32 } func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo, assetInfo, error) { + if c.scanner.instanceInfo == nil { + return mountedDiskInfo{}, assetInfo{}, errors.New("cannot setup disk, instance info not found") + } mi := mountedDiskInfo{} ai := assetInfo{} h := sha256.New() @@ -79,7 +82,7 @@ func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo, } log.Debug().Str("boot disk", instanceInfo.bootDiskId).Msg("found boot disk for instance, cloning") - disk, err := c.snapshotCreator.cloneDisk(instanceInfo.bootDiskId, c.scanner.resourceGroup, diskName, c.scanner.location, c.scanner.vm.Zones) + disk, err := c.snapshotCreator.cloneDisk(instanceInfo.bootDiskId, c.scanner.resourceGroup, diskName, c.scanner.instanceInfo.location, c.scanner.instanceInfo.vm.Zones) if err != nil { log.Error().Err(err).Msg("could not complete disk cloning") return mountedDiskInfo{}, assetInfo{}, errors.Wrap(err, "could not complete disk cloning") @@ -95,7 +98,7 @@ func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo, return mountedDiskInfo{}, assetInfo{}, err } - disk, err := c.snapshotCreator.createSnapshotDisk(snapshotInfo.snapshotId, c.scanner.resourceGroup, diskName, c.scanner.location, c.scanner.vm.Zones) + disk, err := c.snapshotCreator.createSnapshotDisk(snapshotInfo.snapshotId, c.scanner.resourceGroup, diskName, c.scanner.instanceInfo.location, c.scanner.instanceInfo.vm.Zones) if err != nil { log.Error().Err(err).Msg("could not complete snapshot disk creation") return mountedDiskInfo{}, assetInfo{}, errors.Wrap(err, "could not create disk from snapshot") @@ -111,7 +114,7 @@ func (c *AzureSnapshotConnection) setupDisk(target scanTarget) (mountedDiskInfo, return mountedDiskInfo{}, assetInfo{}, err } - disk, err := c.snapshotCreator.cloneDisk(diskInfo.diskId, c.scanner.resourceGroup, diskName, c.scanner.location, c.scanner.vm.Zones) + disk, err := c.snapshotCreator.cloneDisk(diskInfo.diskId, c.scanner.resourceGroup, diskName, c.scanner.instanceInfo.location, c.scanner.instanceInfo.vm.Zones) if err != nil { log.Error().Err(err).Msg("could not complete disk cloning") return mountedDiskInfo{}, assetInfo{}, errors.Wrap(err, "could not complete disk cloning") diff --git a/providers/azure/connection/azureinstancesnapshot/snapshot.go b/providers/azure/connection/azureinstancesnapshot/snapshot.go index 14342b52b1..069454b0b4 100644 --- a/providers/azure/connection/azureinstancesnapshot/snapshot.go +++ b/providers/azure/connection/azureinstancesnapshot/snapshot.go @@ -5,6 +5,7 @@ package azureinstancesnapshot import ( "context" + "errors" "time" "github.com/rs/zerolog/log" @@ -207,7 +208,10 @@ func (sc *snapshotCreator) cloneDisk(sourceDiskId, resourceGroupName, diskName s } // attachDisk attaches a disk to an instance -func (sc *snapshotCreator) attachDisk(targetInstance instanceInfo, diskName, diskId string, lun int32) error { +func (sc *snapshotCreator) attachDisk(targetInstance *instanceInfo, diskName, diskId string, lun int32) error { + if targetInstance == nil { + return errors.New("targetInstance is nil, cannot attach disk") + } ctx := context.Background() log.Debug().Str("disk-name", diskName).Int32("LUN", lun).Msg("attach disk") computeSvc, err := sc.computeClient() @@ -257,7 +261,10 @@ func (sc *snapshotCreator) attachDisk(targetInstance instanceInfo, diskName, dis return err } -func (sc *snapshotCreator) detachDisk(diskName string, targetInstance instanceInfo) error { +func (sc *snapshotCreator) detachDisk(diskName string, targetInstance *instanceInfo) error { + if targetInstance == nil { + return errors.New("targetInstance is nil, cannot detach disk") + } ctx := context.Background() log.Debug().Str("instance-name", targetInstance.instanceName).Msg("detach disk from instance") computeSvc, err := sc.computeClient()