go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/id/azure/azure.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package azure
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/rs/zerolog/log"
    10  	"github.com/spf13/afero"
    11  	"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
    12  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    13  	"go.mondoo.com/cnquery/providers/os/id/azcompute"
    14  	"go.mondoo.com/cnquery/providers/os/resources/smbios"
    15  )
    16  
    17  const (
    18  	azureIdentifierFileLinux = "/sys/class/dmi/id/sys_vendor"
    19  )
    20  
    21  func Detect(conn shared.Connection, pf *inventory.Platform) (string, string, []string) {
    22  	sysVendor := ""
    23  	if pf.IsFamily("linux") {
    24  		// Fetching the product version from the smbios manager is slow
    25  		// because it iterates through files we don't need to check. This
    26  		// is an optimization for our sshfs. Also, be aware that on linux,
    27  		// you may not have access to all the smbios things under /sys, so
    28  		// you want to make sure to only check the product_version file
    29  		content, err := afero.ReadFile(conn.FileSystem(), azureIdentifierFileLinux)
    30  		if err != nil {
    31  			log.Debug().Err(err).Msgf("unable to read %s", azureIdentifierFileLinux)
    32  			return "", "", nil
    33  		}
    34  		sysVendor = string(content)
    35  	} else {
    36  		mgr, err := smbios.ResolveManager(conn, pf)
    37  		if err != nil {
    38  			return "", "", nil
    39  		}
    40  		info, err := mgr.Info()
    41  		if err != nil {
    42  			log.Debug().Err(err).Msg("failed to query smbios")
    43  			return "", "", nil
    44  		}
    45  		sysVendor = info.SysInfo.Vendor
    46  	}
    47  
    48  	if strings.Contains(sysVendor, "Microsoft Corporation") {
    49  		mdsvc, err := azcompute.Resolve(conn, pf)
    50  		if err != nil {
    51  			log.Debug().Err(err).Msg("failed to get metadata resolver")
    52  			return "", "", nil
    53  		}
    54  		id, err := mdsvc.Identify()
    55  		if err != nil {
    56  			log.Debug().Err(err).
    57  				Strs("platform", pf.GetFamily()).
    58  				Msg("failed to get Azure platform id")
    59  			return "", "", nil
    60  		}
    61  		return id.InstanceID, "", []string{id.AccountID}
    62  	}
    63  
    64  	return "", "", nil
    65  }