kms.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Package kms defines the implementation for kms plugins.
  2. // KMS plugins allow to encrypt/decrypt sensitive data.
  3. package kms
  4. import (
  5. "context"
  6. "github.com/hashicorp/go-plugin"
  7. "google.golang.org/grpc"
  8. "github.com/drakkan/sftpgo/v2/sdk/plugin/kms/proto"
  9. )
  10. const (
  11. // PluginName defines the name for a kms plugin
  12. PluginName = "kms"
  13. )
  14. // Handshake is a common handshake that is shared by plugin and host.
  15. var Handshake = plugin.HandshakeConfig{
  16. ProtocolVersion: 1,
  17. MagicCookieKey: "SFTPGO_PLUGIN_KMS",
  18. MagicCookieValue: "223e3571-7ed2-4b96-b4b3-c7eb87d7ca1d",
  19. }
  20. // PluginMap is the map of plugins we can dispense.
  21. var PluginMap = map[string]plugin.Plugin{
  22. PluginName: &Plugin{},
  23. }
  24. // Service defines the interface for kms plugins
  25. type Service interface {
  26. Encrypt(payload, additionalData, URL, masterKey string) (string, string, int32, error)
  27. Decrypt(payload, key, additionalData string, mode int, URL, masterKey string) (string, error)
  28. }
  29. // Plugin defines the implementation to serve/connect to a notifier plugin
  30. type Plugin struct {
  31. plugin.Plugin
  32. Impl Service
  33. }
  34. // GRPCServer defines the GRPC server implementation for this plugin
  35. func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  36. proto.RegisterKMSServer(s, &GRPCServer{
  37. Impl: p.Impl,
  38. })
  39. return nil
  40. }
  41. // GRPCClient defines the GRPC client implementation for this plugin
  42. func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  43. return &GRPCClient{
  44. client: proto.NewKMSClient(c),
  45. }, nil
  46. }