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

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package aws
     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/awsec2"
    14  	"go.mondoo.com/cnquery/providers/os/id/awsecs"
    15  	"go.mondoo.com/cnquery/providers/os/resources/smbios"
    16  )
    17  
    18  func readValue(conn shared.Connection, fPath string) string {
    19  	content, err := afero.ReadFile(conn.FileSystem(), fPath)
    20  	if err != nil {
    21  		log.Debug().Err(err).Msgf("unable to read %s", fPath)
    22  		return ""
    23  	}
    24  	return string(content)
    25  }
    26  
    27  func Detect(conn shared.Connection, p *inventory.Platform) (string, string, []string) {
    28  	var values []string
    29  	if p.IsFamily("linux") {
    30  		// Fetching the data from the smbios manager is slow for some transports
    31  		// because it iterates through files we don't need to check. This
    32  		// is an optimization for our sshfs. Also, be aware that on linux,
    33  		// you may not have access to all the smbios things under /sys, so
    34  		// you want to make sure to only check the files we actually look at
    35  
    36  		values = []string{
    37  			readValue(conn, "/sys/class/dmi/id/product_version"),
    38  			readValue(conn, "/sys/class/dmi/id/bios_vendor"),
    39  		}
    40  	} else {
    41  		mgr, err := smbios.ResolveManager(conn, p)
    42  		if err != nil {
    43  			return "", "", nil
    44  		}
    45  		info, err := mgr.Info()
    46  		if err != nil {
    47  			log.Debug().Err(err).Msg("failed to query smbios")
    48  			return "", "", nil
    49  		}
    50  		values = []string{
    51  			info.SysInfo.Version,
    52  			info.BIOS.Vendor,
    53  		}
    54  	}
    55  
    56  	for _, v := range values {
    57  		if strings.Contains(strings.ToLower(v), "amazon") {
    58  			mdsvc, err := awsec2.Resolve(conn, p)
    59  			if err != nil {
    60  				log.Debug().Err(err).Msg("failed to get metadata resolver")
    61  				return "", "", nil
    62  			}
    63  			id, err := mdsvc.Identify()
    64  			if err == nil {
    65  				return id.InstanceID, id.InstanceName, []string{id.AccountID}
    66  			}
    67  			log.Debug().Err(err).
    68  				Strs("platform", p.GetFamily()).
    69  				Msg("failed to get AWS platform id")
    70  			// try ecs
    71  			mdsvcEcs, err := awsecs.Resolve(conn, p)
    72  			if err != nil {
    73  				log.Debug().Err(err).Msg("failed to get metadata resolver")
    74  				return "", "", nil
    75  			}
    76  			idEcs, err := mdsvcEcs.Identify()
    77  			if err == nil {
    78  				return idEcs.PlatformIds[0], idEcs.Name, []string{idEcs.AccountPlatformID}
    79  			} else {
    80  				log.Debug().Err(err).
    81  					Strs("platform", p.GetFamily()).
    82  					Msg("failed to get AWS platform id")
    83  			}
    84  		}
    85  	}
    86  
    87  	return "", "", nil
    88  }