Plugin: Remove external service on plugin removal (#77712)
* Plugin: Remove external service on plugin removal * Add feature flag check in the service registration service * Initialize map * Add HasExternalService as suggested * Commit suggestion Co-authored-by: linoman <2051016+linoman@users.noreply.github.com> * Nit on test. Co-authored-by: linoman <2051016+linoman@users.noreply.github.com> --------- Co-authored-by: linoman <2051016+linoman@users.noreply.github.com>
This commit is contained in:
co-authored by
linoman
parent
7169bfdb30
commit
20a2840046
@@ -39,8 +39,7 @@ func newExternalServiceRegistration(cfg *config.Cfg, serviceRegistry auth.Extern
|
||||
|
||||
// Register registers the external service with the external service registry, if the feature is enabled.
|
||||
func (r *ExternalServiceRegistration) Register(ctx context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
if p.ExternalServiceRegistration != nil &&
|
||||
(r.cfg.Features.IsEnabled(featuremgmt.FlagExternalServiceAuth) || r.cfg.Features.IsEnabled(featuremgmt.FlagExternalServiceAccounts)) {
|
||||
if p.ExternalServiceRegistration != nil {
|
||||
s, err := r.externalServiceRegistry.RegisterExternalService(ctx, p.ID, plugindef.Type(p.Type), p.ExternalServiceRegistration)
|
||||
if err != nil {
|
||||
r.log.Error("Could not register an external service. Initialization skipped", "pluginId", p.ID, "error", err)
|
||||
|
||||
@@ -5,32 +5,53 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins/auth"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/plugindef"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/extsvcauth"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
reg extsvcauth.ExternalServiceRegistry
|
||||
settingsSvc pluginsettings.Service
|
||||
featureEnabled bool
|
||||
log log.Logger
|
||||
reg extsvcauth.ExternalServiceRegistry
|
||||
settingsSvc pluginsettings.Service
|
||||
}
|
||||
|
||||
func ProvideService(reg extsvcauth.ExternalServiceRegistry, settingsSvc pluginsettings.Service) *Service {
|
||||
func ProvideService(cfg *config.Cfg, reg extsvcauth.ExternalServiceRegistry, settingsSvc pluginsettings.Service) *Service {
|
||||
s := &Service{
|
||||
reg: reg,
|
||||
settingsSvc: settingsSvc,
|
||||
featureEnabled: cfg.Features.IsEnabled(featuremgmt.FlagExternalServiceAuth) || cfg.Features.IsEnabled(featuremgmt.FlagExternalServiceAccounts),
|
||||
log: log.New("plugins.external.registration"),
|
||||
reg: reg,
|
||||
settingsSvc: settingsSvc,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Service) HasExternalService(ctx context.Context, pluginID string) bool {
|
||||
if !s.featureEnabled {
|
||||
s.log.Debug("Skipping HasExternalService call. The feature is behind a feature toggle and needs to be enabled.")
|
||||
return false
|
||||
}
|
||||
|
||||
return s.reg.HasExternalService(ctx, pluginID)
|
||||
}
|
||||
|
||||
// RegisterExternalService is a simplified wrapper around SaveExternalService for the plugin use case.
|
||||
func (s *Service) RegisterExternalService(ctx context.Context, svcName string, pType plugindef.Type, svc *plugindef.ExternalServiceRegistration) (*auth.ExternalService, error) {
|
||||
func (s *Service) RegisterExternalService(ctx context.Context, pluginID string, pType plugindef.Type, svc *plugindef.ExternalServiceRegistration) (*auth.ExternalService, error) {
|
||||
if !s.featureEnabled {
|
||||
s.log.Warn("Skipping External Service Registration. The feature is behind a feature toggle and needs to be enabled.")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Datasource plugins can only be enabled
|
||||
enabled := true
|
||||
// App plugins can be disabled
|
||||
if pType == plugindef.TypeApp {
|
||||
settings, err := s.settingsSvc.GetPluginSettingByPluginID(ctx, &pluginsettings.GetByPluginIDArgs{PluginID: svcName})
|
||||
settings, err := s.settingsSvc.GetPluginSettingByPluginID(ctx, &pluginsettings.GetByPluginIDArgs{PluginID: pluginID})
|
||||
if err != nil && !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
@@ -56,7 +77,7 @@ func (s *Service) RegisterExternalService(ctx context.Context, svcName string, p
|
||||
}
|
||||
|
||||
registration := &extsvcauth.ExternalServiceRegistration{
|
||||
Name: svcName,
|
||||
Name: pluginID,
|
||||
Impersonation: impersonation,
|
||||
Self: self,
|
||||
}
|
||||
@@ -98,3 +119,13 @@ func toAccessControlPermissions(ps []plugindef.Permission) []accesscontrol.Permi
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// RemoveExternalService removes the external service account associated to a plugin
|
||||
func (s *Service) RemoveExternalService(ctx context.Context, pluginID string) error {
|
||||
if !s.featureEnabled {
|
||||
s.log.Debug("Skipping External Service Removal. The feature is behind a feature toggle and needs to be enabled.")
|
||||
return nil
|
||||
}
|
||||
|
||||
return s.reg.RemoveExternalService(ctx, pluginID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user