PR feedback: simplified fallback implementation

Co-Authored-By: Gabriel MABILLE <gamab@users.noreply.github.com>
pull/91665/head
Claudiu Dragalina-Paraipan 9 months ago
parent b5209dba64
commit e9e2b11ba2
  1. 10
      pkg/services/authn/grpcutils/grpc_authenticator.go

@ -67,7 +67,6 @@ func NewInProcGrpcAuthenticator() *authnlib.GrpcAuthenticator {
type AuthenticatorWithFallback struct { type AuthenticatorWithFallback struct {
authenticator *authnlib.GrpcAuthenticator authenticator *authnlib.GrpcAuthenticator
legacyAuthenticator *grpc.Authenticator legacyAuthenticator *grpc.Authenticator
fallbackEnabled bool
metrics *metrics metrics *metrics
} }
@ -82,12 +81,15 @@ func NewGrpcAuthenticatorWithFallback(cfg *setting.Cfg, reg prometheus.Registere
return nil, err return nil, err
} }
if !authCfg.LegacyFallback {
return authenticator, nil
}
legacyAuthenticator := &grpc.Authenticator{} legacyAuthenticator := &grpc.Authenticator{}
return &AuthenticatorWithFallback{ return &AuthenticatorWithFallback{
authenticator: authenticator, authenticator: authenticator,
legacyAuthenticator: legacyAuthenticator, legacyAuthenticator: legacyAuthenticator,
fallbackEnabled: authCfg.LegacyFallback,
metrics: newMetrics(reg), metrics: newMetrics(reg),
}, nil }, nil
} }
@ -95,8 +97,8 @@ func NewGrpcAuthenticatorWithFallback(cfg *setting.Cfg, reg prometheus.Registere
func (f *AuthenticatorWithFallback) Authenticate(ctx context.Context) (context.Context, error) { func (f *AuthenticatorWithFallback) Authenticate(ctx context.Context) (context.Context, error) {
// Try to authenticate with the new authenticator first // Try to authenticate with the new authenticator first
newCtx, err := f.authenticator.Authenticate(ctx) newCtx, err := f.authenticator.Authenticate(ctx)
// If allowed fallback to the legacy authenticator if err != nil {
if err != nil && f.fallbackEnabled { // In case of error, fallback to the legacy authenticator
newCtx, err = f.legacyAuthenticator.Authenticate(ctx) newCtx, err = f.legacyAuthenticator.Authenticate(ctx)
f.metrics.fallbackCounter.WithLabelValues(fmt.Sprintf("%t", err == nil)).Inc() f.metrics.fallbackCounter.WithLabelValues(fmt.Sprintf("%t", err == nil)).Inc()
} }

Loading…
Cancel
Save