go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/id/gcp/gcp.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package gcp 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/gce" 14 "go.mondoo.com/cnquery/providers/os/resources/smbios" 15 ) 16 17 const ( 18 gceIdentifierFileLinux = "/sys/class/dmi/id/product_name" 19 ) 20 21 func Detect(conn shared.Connection, p *inventory.Platform) (string, string, []string) { 22 productName := "" 23 if p.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 one file 29 content, err := afero.ReadFile(conn.FileSystem(), gceIdentifierFileLinux) 30 if err != nil { 31 log.Debug().Err(err).Msgf("unable to read %s", gceIdentifierFileLinux) 32 return "", "", nil 33 } 34 productName = string(content) 35 } else { 36 mgr, err := smbios.ResolveManager(conn, p) 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 productName = info.SysInfo.Model 46 } 47 48 if strings.Contains(productName, "Google Compute Engine") { 49 mdsvc, err := gce.Resolve(conn, p) 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", p.GetFamily()). 58 Msg("failed to get GCE platform id") 59 return "", "", nil 60 } 61 return id.InstanceID, "", []string{id.ProjectID} 62 } 63 return "", "", nil 64 }