@@ -430,18 +430,35 @@ func (l *LocalMembership) registerLocalIdentity(ctx context.Context, identityCon
430430 l .logger .DebugfContext (ctx , "try to load identity with [%d] key managers [%v]" , len (l .KeyManagerProviders ), l .KeyManagerProviders )
431431 for i , p := range l .KeyManagerProviders {
432432 var err error
433- keyManager , err = p .Get (ctx , identityConfig )
434- if err == nil && keyManager != nil && len (keyManager .EnrollmentID ()) != 0 {
435- priority = i
436- break
433+ var km KeyManager
434+ km , err = p .Get (ctx , identityConfig )
435+ if err != nil {
436+ errs = append (errs , err )
437+ continue
438+ }
439+
440+ if len (km .EnrollmentID ()) == 0 {
441+ errs = append (errs , errors .Errorf ("no enrollment id found for identity [%s]" , identityConfig .ID ))
442+ continue
437443 }
438- keyManager = nil
439- errs = append (errs , err )
444+
445+ // only assign keyManager if the provider returned a valid enrollment id
446+ keyManager = km
447+ priority = i
448+ break
440449 }
441450 if keyManager == nil {
442- return errors .Wrapf (
443- errors .Join (errs ... ),
444- "failed to get a key manager for the passed identity config for [%s:%s]" ,
451+ logger .Errorf ("no key manager found for identity [%s], err [%+v]" , identityConfig .ID , errs )
452+ err := errors .Join (errs ... )
453+ if err != nil {
454+ return errors .Wrapf (err ,
455+ "failed to get a key manager for the passed identity config for [%s:%s]" ,
456+ identityConfig .ID ,
457+ identityConfig .URL ,
458+ )
459+ }
460+ return errors .Errorf (
461+ "no key manager found for [%s:%s]" ,
445462 identityConfig .ID ,
446463 identityConfig .URL ,
447464 )
@@ -467,21 +484,29 @@ func (l *LocalMembership) registerLocalIdentity(ctx context.Context, identityCon
467484func (l * LocalMembership ) registerIdentityConfiguration (ctx context.Context , identity * IdentityConfiguration , defaultIdentity bool ) error {
468485 // Try to register the local identity
469486 identity .URL = l .config .TranslatePath (identity .URL )
470- if err := l .registerLocalIdentity (ctx , identity , defaultIdentity ); err != nil {
471- l .logger .Warnf ("failed to load local identity at [%s]:[%s]" , identity .URL , err )
487+ err1 := l .registerLocalIdentity (ctx , identity , defaultIdentity )
488+ if err1 == nil {
489+ // nothing else needs to be done
490+ return nil
491+ }
492+
493+ // second chance, load the path as folder
494+ {
495+ l .logger .Warnf ("failed to load local identity at [%s]:[%s]" , identity .URL , err1 )
472496 // Does path correspond to a folder containing multiple identities?
473- if err := l .registerLocalIdentities (ctx , identity ); err != nil {
474- return errors .WithMessagef (err , "failed to register local identity" )
497+ err2 := l .registerLocalIdentities (ctx , identity )
498+ if err2 != nil {
499+ return errors .Wrap (errors .Join (err1 , err2 ), "failed to register local identity" )
475500 }
476501 }
502+
477503 return nil
478504}
479505
480506func (l * LocalMembership ) registerLocalIdentities (ctx context.Context , configuration * IdentityConfiguration ) error {
481507 entries , err := os .ReadDir (configuration .URL )
482508 if err != nil {
483- l .logger .Warnf ("failed reading from [%s]: [%s]" , configuration .URL , err )
484- return nil
509+ return errors .Wrapf (err , "no valid identities found in [%s]" , configuration .URL )
485510 }
486511 found := 0
487512 var errs []error
@@ -502,7 +527,7 @@ func (l *LocalMembership) registerLocalIdentities(ctx context.Context, configura
502527 found ++
503528 }
504529 if found == 0 {
505- return errors .Errorf ( "no valid identities found in [%s], errs [%v] " , configuration .URL , errs )
530+ return errors .Wrapf ( errors . Join ( errs ... ), "no valid identities found in [%s]" , configuration .URL )
506531 }
507532 return nil
508533}
0 commit comments