go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/shared/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 shared
     6  
     7  import (
     8  	"github.com/hashicorp/go-plugin"
     9  	"go.mondoo.com/cnquery/providers"
    10  	"go.mondoo.com/cnquery/shared/proto"
    11  	"golang.org/x/net/context"
    12  	"google.golang.org/grpc"
    13  )
    14  
    15  // Handshake is a common handshake that is shared by plugin and host.
    16  var Handshake = plugin.HandshakeConfig{
    17  	ProtocolVersion:  1,
    18  	MagicCookieKey:   "BASIC_PLUGIN",
    19  	MagicCookieValue: "ifenohXaoHoh3Iequeg0iuph2gaeth",
    20  }
    21  
    22  // PluginMap is the map of plugins we can dispense.
    23  var PluginMap = map[string]plugin.Plugin{
    24  	"cnquery": &CNQueryPlugin{},
    25  }
    26  
    27  type OutputHelper interface {
    28  	WriteString(string) error
    29  	Write([]byte) (int, error)
    30  }
    31  
    32  // CNQuery is the interface that we're exposing as a plugin.
    33  type CNQuery interface {
    34  	RunQuery(conf *proto.RunQueryConfig, runtime *providers.Runtime, out OutputHelper) error
    35  }
    36  
    37  // This is the implementation of plugin.Plugin so we can serve/consume this.
    38  // We also implement GRPCPlugin so that this plugin can be served over
    39  // gRPC.
    40  type CNQueryPlugin struct {
    41  	plugin.NetRPCUnsupportedPlugin
    42  	// Concrete implementation, written in Go. This is only used for plugins
    43  	// that are written in Go.
    44  	Impl CNQuery
    45  }
    46  
    47  func (p *CNQueryPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
    48  	proto.RegisterCNQueryServer(s, &GRPCServer{
    49  		Impl:   p.Impl,
    50  		broker: broker,
    51  	})
    52  	return nil
    53  }
    54  
    55  func (p *CNQueryPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
    56  	return &GRPCClient{
    57  		client: proto.NewCNQueryClient(c),
    58  		broker: broker,
    59  	}, nil
    60  }
    61  
    62  var _ plugin.GRPCPlugin = &CNQueryPlugin{}