go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/mock.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package providers 5 6 import ( 7 "github.com/cockroachdb/errors" 8 "go.mondoo.com/cnquery/providers-sdk/v1/inventory" 9 "go.mondoo.com/cnquery/providers-sdk/v1/plugin" 10 ) 11 12 var mockProvider = Provider{ 13 Provider: &plugin.Provider{ 14 Name: "mock", 15 ID: "go.mondoo.com/cnquery/providers/mock", 16 Version: "9.0.0", 17 Connectors: []plugin.Connector{{ 18 Name: "mock", 19 Use: "mock", 20 Short: "use a recording without an active connection", 21 }}, 22 }, 23 } 24 25 type mockProviderService struct { 26 coordinator *coordinator 27 initialized bool 28 runtime *Runtime 29 } 30 31 func (s *mockProviderService) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) { 32 return &plugin.ParseCLIRes{ 33 Asset: &inventory.Asset{ 34 Connections: []*inventory.Config{{ 35 Type: "mock", 36 }}, 37 }, 38 }, nil 39 } 40 41 func (s *mockProviderService) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) { 42 // initialize all other providers from all asset connections in the recording 43 recording := s.runtime.Recording 44 if recording == nil { 45 return nil, errors.New("cannot find recording for mock provider") 46 } 47 48 base := baseRecording(recording) 49 if base == nil { 50 return nil, errors.New("cannot find base recording for mock provider") 51 } 52 53 if len(base.Assets) == 0 { 54 return nil, errors.New("no assets found in recording") 55 } 56 asset := base.Assets[0] 57 58 if len(asset.Connections) == 0 { 59 return nil, errors.New("no connections found in asset") 60 } 61 62 var res *plugin.ConnectRes 63 for i := range asset.Connections { 64 conf := asset.Connections[i] 65 66 provider, err := s.runtime.addProvider(conf.ProviderID, false) 67 if err != nil { 68 return nil, errors.Wrap(err, "failed to init provider for connection in recording") 69 } 70 71 conn, err := provider.Instance.Plugin.MockConnect(&plugin.ConnectReq{ 72 Asset: asset.Asset.ToInventory(), 73 Features: req.Features, 74 Upstream: req.Upstream, 75 }, callback) 76 if err != nil { 77 return nil, errors.Wrap(err, "failed to init referenced provider") 78 } 79 80 provider.Connection = conn 81 if i == 0 { 82 res = conn 83 } 84 } 85 86 return res, nil 87 } 88 89 func (s *mockProviderService) MockConnect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) { 90 // Should never happen: the mock provider should not be called with MockConnect. 91 // It is the only thing that should ever call MockConnect to other providers 92 // (outside of tests). 93 return nil, errors.New("the mock provider does not support the mock connect call, this is an internal error") 94 } 95 96 func (s *mockProviderService) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error) { 97 // Nothing to do yet... 98 return nil, nil 99 } 100 101 func (s *mockProviderService) GetData(req *plugin.DataReq) (*plugin.DataRes, error) { 102 panic("NO") 103 } 104 105 func (s *mockProviderService) StoreData(req *plugin.StoreReq) (*plugin.StoreRes, error) { 106 panic("NO") 107 } 108 109 func (s *mockProviderService) Init(running *RunningProvider) { 110 if s.initialized { 111 return 112 } 113 s.initialized = true 114 115 rt := s.coordinator.NewRuntime() 116 rt.schema.loadAllSchemas() 117 running.Schema = &rt.schema.Schema 118 }