go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/id/clouddetect/clouddetect.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package clouddetect 5 6 import ( 7 "sync" 8 9 "github.com/rs/zerolog/log" 10 "go.mondoo.com/cnquery/providers-sdk/v1/inventory" 11 "go.mondoo.com/cnquery/providers/os/connection/shared" 12 "go.mondoo.com/cnquery/providers/os/id/aws" 13 "go.mondoo.com/cnquery/providers/os/id/azure" 14 "go.mondoo.com/cnquery/providers/os/id/gcp" 15 ) 16 17 type ( 18 RelatedPlatformID = string 19 PlatformName = string 20 PlatformID = string 21 ) 22 23 type detectorFunc func(conn shared.Connection, p *inventory.Platform) (PlatformID, PlatformName, []RelatedPlatformID) 24 25 var detectors = []detectorFunc{ 26 aws.Detect, 27 azure.Detect, 28 gcp.Detect, 29 } 30 31 type detectResult struct { 32 platformId string 33 platformName string 34 relatedPlatformIds []string 35 } 36 37 func Detect(conn shared.Connection, p *inventory.Platform) (PlatformID, PlatformName, []RelatedPlatformID) { 38 wg := sync.WaitGroup{} 39 wg.Add(len(detectors)) 40 41 valChan := make(chan detectResult, len(detectors)) 42 for i := range detectors { 43 go func(f detectorFunc) { 44 defer wg.Done() 45 46 v, name, related := f(conn, p) 47 if v != "" { 48 valChan <- detectResult{ 49 platformName: name, 50 platformId: v, 51 relatedPlatformIds: related, 52 } 53 } 54 }(detectors[i]) 55 } 56 57 wg.Wait() 58 close(valChan) 59 60 platformIds := []string{} 61 relatedPlatformIds := []string{} 62 var name string 63 for v := range valChan { 64 platformIds = append(platformIds, v.platformId) 65 name = v.platformName 66 relatedPlatformIds = append(relatedPlatformIds, v.relatedPlatformIds...) 67 } 68 69 if len(platformIds) == 0 { 70 return "", "", nil 71 } else if len(platformIds) > 1 { 72 log.Error().Strs("detected", platformIds).Msg("multiple cloud platform ids detected") 73 return "", "", nil 74 } 75 76 return platformIds[0], name, relatedPlatformIds 77 }