go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/plugin/interface.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  // Interface to use cnquery as a plugin
     5  package plugin
     6  
     7  import (
     8  	"github.com/hashicorp/go-plugin"
     9  	"golang.org/x/net/context"
    10  	"google.golang.org/grpc"
    11  )
    12  
    13  // Handshake is a common handshake that is shared by plugin and host.
    14  var Handshake = plugin.HandshakeConfig{
    15  	ProtocolVersion:  1,
    16  	MagicCookieKey:   "BASIC_PLUGIN",
    17  	MagicCookieValue: "ifenohXaoHoh3Iequeg0iuph2gaeth",
    18  }
    19  
    20  // PluginMap is the map of plugins we can dispense.
    21  var PluginMap = map[string]plugin.Plugin{
    22  	"provider": &ProviderPluginImpl{},
    23  }
    24  
    25  type ProviderCallback interface {
    26  	Collect(req *DataRes) error
    27  	GetRecording(req *DataReq) (*ResourceData, error)
    28  	GetData(req *DataReq) (*DataRes, error)
    29  }
    30  
    31  // ProviderPlugin is the interface that we're exposing as a plugin.
    32  type ProviderPlugin interface {
    33  	ParseCLI(req *ParseCLIReq) (*ParseCLIRes, error)
    34  	Connect(req *ConnectReq, callback ProviderCallback) (*ConnectRes, error)
    35  	MockConnect(req *ConnectReq, callback ProviderCallback) (*ConnectRes, error)
    36  	Shutdown(req *ShutdownReq) (*ShutdownRes, error)
    37  	GetData(req *DataReq) (*DataRes, error)
    38  	StoreData(req *StoreReq) (*StoreRes, error)
    39  }
    40  
    41  // This is the implementation of plugin.Plugin so we can serve/consume this.
    42  // We also implement GRPCPlugin so that this plugin can be served over
    43  // gRPC.
    44  type ProviderPluginImpl struct {
    45  	plugin.NetRPCUnsupportedPlugin
    46  	// Concrete implementation, written in Go. This is only used for plugins
    47  	// that are written in Go.
    48  	Impl ProviderPlugin
    49  }
    50  
    51  func (p *ProviderPluginImpl) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
    52  	RegisterProviderPluginServer(s, &GRPCServer{
    53  		Impl:   p.Impl,
    54  		broker: broker,
    55  	})
    56  	return nil
    57  }
    58  
    59  func (p *ProviderPluginImpl) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
    60  	return &GRPCClient{
    61  		client: NewProviderPluginClient(c),
    62  		broker: broker,
    63  	}, nil
    64  }
    65  
    66  var _ plugin.GRPCPlugin = &ProviderPluginImpl{}