| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | package grpcplugin | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os/exec" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend/grpcplugin" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin" | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2" | 
					
						
							| 
									
										
										
										
											2022-06-10 01:19:27 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin/secretsmanagerplugin" | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 	goplugin "github.com/hashicorp/go-plugin" | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Handshake is the HandshakeConfig used to configure clients and servers.
 | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | var handshake = goplugin.HandshakeConfig{ | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 	// The ProtocolVersion is the version that must match between Grafana core
 | 
					
						
							|  |  |  | 	// and Grafana plugins. This should be bumped whenever a (breaking) change
 | 
					
						
							|  |  |  | 	// happens in one or the other that makes it so that they can't safely communicate.
 | 
					
						
							| 
									
										
										
										
											2021-05-19 14:57:48 +08:00
										 |  |  | 	ProtocolVersion: grpcplugin.ProtocolVersion, | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// The magic cookie values should NEVER be changed.
 | 
					
						
							| 
									
										
										
										
											2020-11-05 22:37:11 +08:00
										 |  |  | 	MagicCookieKey:   grpcplugin.MagicCookieKey, | 
					
						
							|  |  |  | 	MagicCookieValue: grpcplugin.MagicCookieValue, | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 16:32:06 +08:00
										 |  |  | func newClientConfig(executablePath string, env []string, logger log.Logger, | 
					
						
							|  |  |  | 	versionedPlugins map[int]goplugin.PluginSet) *goplugin.ClientConfig { | 
					
						
							|  |  |  | 	// We can ignore gosec G201 here, since the dynamic part of executablePath comes from the plugin definition
 | 
					
						
							|  |  |  | 	// nolint:gosec
 | 
					
						
							| 
									
										
										
										
											2020-04-15 00:04:27 +08:00
										 |  |  | 	cmd := exec.Command(executablePath) | 
					
						
							|  |  |  | 	cmd.Env = env | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 	return &goplugin.ClientConfig{ | 
					
						
							| 
									
										
										
										
											2020-04-15 00:04:27 +08:00
										 |  |  | 		Cmd:              cmd, | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 		HandshakeConfig:  handshake, | 
					
						
							|  |  |  | 		VersionedPlugins: versionedPlugins, | 
					
						
							|  |  |  | 		Logger:           logWrapper{Logger: logger}, | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 		AllowedProtocols: []goplugin.Protocol{goplugin.ProtocolGRPC}, | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 20:16:58 +08:00
										 |  |  | // StartRendererFunc callback function called when a renderer plugin is started.
 | 
					
						
							|  |  |  | type StartRendererFunc func(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error | 
					
						
							| 
									
										
										
										
											2020-01-14 00:13:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 01:19:27 +08:00
										 |  |  | // StartSecretsManagerFunc callback function called when a secrets manager plugin is started.
 | 
					
						
							|  |  |  | type StartSecretsManagerFunc func(pluginID string, secretsmanager secretsmanagerplugin.SecretsManagerPlugin, logger log.Logger) error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 21:48:24 +08:00
										 |  |  | // PluginDescriptor is a descriptor used for registering backend plugins.
 | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | type PluginDescriptor struct { | 
					
						
							| 
									
										
										
										
											2022-06-10 01:19:27 +08:00
										 |  |  | 	pluginID              string | 
					
						
							|  |  |  | 	executablePath        string | 
					
						
							|  |  |  | 	managed               bool | 
					
						
							|  |  |  | 	versionedPlugins      map[int]goplugin.PluginSet | 
					
						
							|  |  |  | 	startRendererFn       StartRendererFunc | 
					
						
							|  |  |  | 	startSecretsManagerFn StartSecretsManagerFunc | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | // getV2PluginSet returns list of plugins supported on v2.
 | 
					
						
							|  |  |  | func getV2PluginSet() goplugin.PluginSet { | 
					
						
							|  |  |  | 	return goplugin.PluginSet{ | 
					
						
							| 
									
										
										
										
											2022-06-10 01:19:27 +08:00
										 |  |  | 		"diagnostics":    &grpcplugin.DiagnosticsGRPCPlugin{}, | 
					
						
							|  |  |  | 		"resource":       &grpcplugin.ResourceGRPCPlugin{}, | 
					
						
							|  |  |  | 		"data":           &grpcplugin.DataGRPCPlugin{}, | 
					
						
							|  |  |  | 		"stream":         &grpcplugin.StreamGRPCPlugin{}, | 
					
						
							|  |  |  | 		"renderer":       &pluginextensionv2.RendererGRPCPlugin{}, | 
					
						
							|  |  |  | 		"secretsmanager": &secretsmanagerplugin.SecretsManagerGRPCPlugin{}, | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | // NewBackendPlugin creates a new backend plugin factory used for registering a backend plugin.
 | 
					
						
							| 
									
										
										
										
											2021-06-03 20:16:58 +08:00
										 |  |  | func NewBackendPlugin(pluginID, executablePath string) backendplugin.PluginFactoryFunc { | 
					
						
							| 
									
										
										
										
											2020-12-15 16:32:06 +08:00
										 |  |  | 	return newPlugin(PluginDescriptor{ | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 		pluginID:       pluginID, | 
					
						
							|  |  |  | 		executablePath: executablePath, | 
					
						
							|  |  |  | 		managed:        true, | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 		versionedPlugins: map[int]goplugin.PluginSet{ | 
					
						
							| 
									
										
										
										
											2020-11-05 22:37:11 +08:00
										 |  |  | 			grpcplugin.ProtocolVersion: getV2PluginSet(), | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | // NewRendererPlugin creates a new renderer plugin factory used for registering a backend renderer plugin.
 | 
					
						
							| 
									
										
										
										
											2021-06-03 20:16:58 +08:00
										 |  |  | func NewRendererPlugin(pluginID, executablePath string, startFn StartRendererFunc) backendplugin.PluginFactoryFunc { | 
					
						
							| 
									
										
										
										
											2020-12-15 16:32:06 +08:00
										 |  |  | 	return newPlugin(PluginDescriptor{ | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 		pluginID:       pluginID, | 
					
						
							|  |  |  | 		executablePath: executablePath, | 
					
						
							|  |  |  | 		managed:        false, | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 		versionedPlugins: map[int]goplugin.PluginSet{ | 
					
						
							| 
									
										
										
										
											2020-11-05 22:37:11 +08:00
										 |  |  | 			grpcplugin.ProtocolVersion: getV2PluginSet(), | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2021-06-03 20:16:58 +08:00
										 |  |  | 		startRendererFn: startFn, | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-06-10 01:19:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // NewSecetsManagerPlugin creates a new secrets manager plugin factory used for registering a backend secrets manager plugin.
 | 
					
						
							|  |  |  | func NewSecretsManagerPlugin(pluginID, executablePath string, startFn StartSecretsManagerFunc) backendplugin.PluginFactoryFunc { | 
					
						
							|  |  |  | 	return newPlugin(PluginDescriptor{ | 
					
						
							|  |  |  | 		pluginID:       pluginID, | 
					
						
							|  |  |  | 		executablePath: executablePath, | 
					
						
							|  |  |  | 		managed:        false, | 
					
						
							|  |  |  | 		versionedPlugins: map[int]goplugin.PluginSet{ | 
					
						
							|  |  |  | 			grpcplugin.ProtocolVersion: getV2PluginSet(), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		startSecretsManagerFn: startFn, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |