get.porter.sh/porter@v1.3.0/pkg/grpc/portergrpc/portergrpc.go (about)

     1  package portergrpc
     2  
     3  import (
     4  	"context"
     5  
     6  	pGRPC "get.porter.sh/porter/gen/proto/go/porterapis/porter/v1alpha1"
     7  	"get.porter.sh/porter/pkg/config"
     8  	"get.porter.sh/porter/pkg/porter"
     9  	"get.porter.sh/porter/pkg/secrets"
    10  	secretsplugin "get.porter.sh/porter/pkg/secrets/pluginstore"
    11  	"get.porter.sh/porter/pkg/signing"
    12  	signingplugin "get.porter.sh/porter/pkg/signing/pluginstore"
    13  	"get.porter.sh/porter/pkg/storage"
    14  	storageplugin "get.porter.sh/porter/pkg/storage/pluginstore"
    15  	"google.golang.org/grpc"
    16  )
    17  
    18  // PorterServer defines the struct for managing a porter GRPC server
    19  type PorterServer struct {
    20  	PorterConfig *config.Config
    21  	pGRPC.UnimplementedPorterServer
    22  }
    23  
    24  // NewPorterServer creates a new instance of the PorterServer for a config
    25  func NewPorterServer(cfg *config.Config) (*PorterServer, error) {
    26  	return &PorterServer{PorterConfig: cfg}, nil
    27  }
    28  
    29  // NewConnectionInterceptor creates a middleware interceptor for the GRPC server that manages creating a porter connection for each requested RPC stream.
    30  // If the connection is unable to be created for the RPC then the RPC fails, otherwise the connection is added to the RPC context and the next handler in the
    31  // chain is called
    32  func (s *PorterServer) NewConnectionInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    33  	storage := storage.NewPluginAdapter(storageplugin.NewStore(s.PorterConfig))
    34  	secretStorage := secrets.NewPluginAdapter(secretsplugin.NewStore(s.PorterConfig))
    35  	signer := signing.NewPluginAdapter(signingplugin.NewSigner(s.PorterConfig))
    36  	p := porter.NewFor(s.PorterConfig, storage, secretStorage, signer)
    37  	if _, err := p.Connect(ctx); err != nil {
    38  		return nil, err
    39  	}
    40  	defer p.Close()
    41  
    42  	ctx = AddPorterConnectionToContext(p, ctx)
    43  	return handler(ctx, req)
    44  }