ExtSvcAuth: Clean up orphaned external services on start up (#77951)
* Plugin: Remove external service on plugin removal * Early exit no service account * Add log * WIP * Cable OAuth2Server client removal * Move function lower * Add function to test removal * Add test to RemoveExternalService * Test RemoveExtSvcAccount * remove apostrophy in comment * Add cfg to plugin installer to check features * Add feature flag check in the service registration service * Comments * Move metrics Inc * Initialize map * Reorder * Initialize mutex as well * Add HasExternalService as suggested * WIP: CleanUpOrphanedExternalServices * 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> * oauthserver return names * Name is not Slug * Use plugin ID not slug * Add background job * remove negation on feature check * Add test to the CleanUp function * Test GetExternalServiceNames * rename test * Add test for ExtSvcAccountsService_GetExternalServiceNames * Add a todo * Add todo * Option based on mix * Rewrite a bit the comment * Opinionated choice use slugs instead of names everywhere * Nit. * Comments and re-ordering * Comment * Add log * Add context --------- Co-authored-by: linoman <2051016+linoman@users.noreply.github.com>
This commit is contained in:
co-authored by
linoman
parent
6b6b209e1c
commit
ba717454e1
@@ -193,6 +193,17 @@ func (s *OAuth2ServiceImpl) setClientUser(ctx context.Context, client *oauthserv
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetExternalServiceNames get the names of External Service in store
|
||||
func (s *OAuth2ServiceImpl) GetExternalServiceNames(ctx context.Context) ([]string, error) {
|
||||
s.logger.Debug("Get external service names from store")
|
||||
res, err := s.sqlstore.GetExternalServiceNames(ctx)
|
||||
if err != nil {
|
||||
s.logger.Error("Could not fetch clients from store", "error", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *OAuth2ServiceImpl) RemoveExternalService(ctx context.Context, name string) error {
|
||||
s.logger.Info("Remove external service", "service", name)
|
||||
|
||||
@@ -228,19 +239,20 @@ func (s *OAuth2ServiceImpl) SaveExternalService(ctx context.Context, registratio
|
||||
s.logger.Warn("RegisterExternalService called without registration")
|
||||
return nil, nil
|
||||
}
|
||||
s.logger.Info("Registering external service", "external service name", registration.Name)
|
||||
slug := registration.Name
|
||||
s.logger.Info("Registering external service", "external service", slug)
|
||||
|
||||
// Check if the client already exists in store
|
||||
client, errFetchExtSvc := s.sqlstore.GetExternalServiceByName(ctx, registration.Name)
|
||||
client, errFetchExtSvc := s.sqlstore.GetExternalServiceByName(ctx, slug)
|
||||
if errFetchExtSvc != nil && !errors.Is(errFetchExtSvc, oauthserver.ErrClientNotFound) {
|
||||
s.logger.Error("Error fetching service", "external service", registration.Name, "error", errFetchExtSvc)
|
||||
s.logger.Error("Error fetching service", "external service", slug, "error", errFetchExtSvc)
|
||||
return nil, errFetchExtSvc
|
||||
}
|
||||
// Otherwise, create a new client
|
||||
if client == nil {
|
||||
s.logger.Debug("External service does not yet exist", "external service name", registration.Name)
|
||||
s.logger.Debug("External service does not yet exist", "external service", slug)
|
||||
client = &oauthserver.OAuthExternalService{
|
||||
Name: registration.Name,
|
||||
Name: slug,
|
||||
ServiceAccountID: oauthserver.NoServiceAccountID,
|
||||
Audiences: s.cfg.AppURL,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user