go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/_motor/providers/resolver/connect.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package resolver 5 6 import ( 7 "context" 8 9 "github.com/cockroachdb/errors" 10 "github.com/rs/zerolog/log" 11 "go.mondoo.com/cnquery/motor" 12 v1 "go.mondoo.com/cnquery/motor/inventory/v1" 13 "go.mondoo.com/cnquery/motor/vault" 14 ) 15 16 func EstablishConnection(ctx context.Context, tc *v1.Config, credsResolver vault.Resolver, insecure bool, record bool) (*motor.Motor, error) { 17 log.Debug().Str("connection", tc.ToUrl()).Bool("insecure", insecure).Msg("establish connection to asset") 18 // overwrite connection specific insecure with global insecure 19 if insecure { 20 tc.Insecure = insecure 21 } 22 23 if record { 24 tc.Record = true 25 } 26 27 return NewMotorConnection(ctx, tc, credsResolver) 28 } 29 30 func OpenAssetConnection(ctx context.Context, assetInfo *v1.Asset, credsResolver vault.Resolver, record bool) (*motor.Motor, error) { 31 if assetInfo == nil { 32 return nil, errors.New("asset is not defined") 33 } 34 35 // connect to the platform 36 if len(assetInfo.Connections) == 0 { 37 return nil, errors.New("no connection provided for asset " + assetInfo.Name) 38 } 39 40 // TODO: we may want to allow multiple connection trials later 41 pCfg := assetInfo.Connections[0] 42 43 // use connection host as default 44 if assetInfo.Name == "" { 45 assetInfo.Name = pCfg.Host 46 } 47 48 // some transports have their own kind/runtime information already 49 // NOTE: going forward we may want to enforce that assets have at least kind and runtime information 50 if assetInfo.Platform != nil { 51 pCfg.Runtime = assetInfo.Platform.Runtime 52 if pCfg.Options == nil { 53 pCfg.Options = map[string]string{} 54 } 55 // set platform name override to ensure we get the correct platform at policy execution time 56 pCfg.Options["platform-override"] = assetInfo.Platform.Name 57 } 58 59 // parse reference id and restore options 60 if len(assetInfo.PlatformIds) > 0 { 61 pCfg.PlatformId = assetInfo.PlatformIds[0] 62 } 63 64 m, err := EstablishConnection(ctx, pCfg, credsResolver, pCfg.Insecure, record) 65 if err != nil { 66 return nil, err 67 } 68 69 m.SetAsset(assetInfo) 70 71 return m, nil 72 } 73 74 func OpenAssetConnections(ctx context.Context, assetInfo *v1.Asset, credsResolver vault.Resolver, record bool) ([]*motor.Motor, error) { 75 if assetInfo == nil { 76 return nil, errors.New("asset is not defined") 77 } 78 79 // connect to the platform 80 if len(assetInfo.Connections) == 0 { 81 return nil, errors.New("no connection provided for asset " + assetInfo.Name) 82 } 83 84 // TODO: we may want to allow multiple connection trials later 85 connections := []*motor.Motor{} 86 for ci := range assetInfo.Connections { 87 pCfg := assetInfo.Connections[ci] 88 89 // use connection host as default 90 if assetInfo.Name == "" { 91 assetInfo.Name = pCfg.Host 92 } 93 94 // some transports have their own kind/runtime information already 95 // NOTE: going forward we may want to enforce that assets have at least kind and runtime information 96 if assetInfo.Platform != nil { 97 pCfg.Runtime = assetInfo.Platform.Runtime 98 if pCfg.Options == nil { 99 pCfg.Options = map[string]string{} 100 } 101 // set platform name override to ensure we get the correct platform at policy execution time 102 pCfg.Options["platform-override"] = assetInfo.Platform.Name 103 } 104 105 // parse reference id and restore options 106 if len(assetInfo.PlatformIds) > 0 { 107 pCfg.PlatformId = assetInfo.PlatformIds[0] 108 } 109 110 m, err := EstablishConnection(ctx, pCfg, credsResolver, pCfg.Insecure, record) 111 if err != nil { 112 return nil, err 113 } 114 115 m.SetAsset(assetInfo) 116 connections = append(connections, m) 117 } 118 return connections, nil 119 }