@@ -336,6 +336,7 @@ func (uc *CASBackendUseCase) Update(ctx context.Context, orgID, id, description
336336 }
337337
338338 var secretName string
339+ credentialsUpdated := creds != nil
339340 // We want to rotate credentials
340341 if creds != nil {
341342 secretName , err = uc .credsRW .SaveCredentials (ctx , orgID , creds )
@@ -344,18 +345,46 @@ func (uc *CASBackendUseCase) Update(ctx context.Context, orgID, id, description
344345 }
345346 }
346347
347- after , err := uc .repo .Update (ctx , & CASBackendUpdateOpts {
348+ // Update the backend without modifying validation status directly
349+ // The validation status will be updated through PerformValidation if needed
350+ // Don't set validation status here - let PerformValidation handle it
351+ updateOpts := & CASBackendUpdateOpts {
348352 ID : uuid ,
349353 CASBackendOpts : & CASBackendOpts {
350- SecretName : secretName , Default : defaultB , Description : description , OrgID : orgUUID ,
351- ValidationStatus : CASBackendValidationOK ,
352- ValidationError : ToPtr ("" ),
354+ SecretName : secretName ,
355+ Default : defaultB ,
356+ Description : description ,
357+ OrgID : orgUUID ,
353358 },
354- })
359+ }
360+
361+ // If we're not updating credentials, preserve the current validation status
362+ if ! credentialsUpdated {
363+ updateOpts .ValidationStatus = before .ValidationStatus
364+ updateOpts .ValidationError = before .ValidationError
365+ }
366+
367+ after , err := uc .repo .Update (ctx , updateOpts )
355368 if err != nil {
356369 return nil , err
357370 }
358371
372+ // If credentials were updated, perform validation to check if they work
373+ // This will properly update validation status and send events
374+ if credentialsUpdated {
375+ if err := uc .PerformValidation (ctx , id ); err != nil {
376+ // Log the validation error but don't fail the update operation
377+ // The validation status will be updated by PerformValidation
378+ uc .logger .Warnw ("msg" , "validation failed after credential update" , "ID" , id , "error" , err )
379+ }
380+
381+ // Reload the backend to get the updated validation status
382+ after , err = uc .repo .FindByIDInOrg (ctx , orgUUID , uuid )
383+ if err != nil {
384+ return nil , fmt .Errorf ("reloading backend after validation: %w" , err )
385+ }
386+ }
387+
359388 // If we just updated the backend from default=true => default=false, we need to set up the fallback as default
360389 if before .Default && ! after .Default {
361390 if _ , err := uc .defaultFallbackBackend (ctx , orgID ); err != nil {
@@ -374,7 +403,7 @@ func (uc *CASBackendUseCase) Update(ctx context.Context, orgID, id, description
374403 Default : after .Default ,
375404 },
376405 NewDescription : & description ,
377- CredentialsChanged : creds != nil ,
406+ CredentialsChanged : credentialsUpdated ,
378407 PreviousDefault : before .Default ,
379408 }, & orgUUID )
380409 }
0 commit comments