Fix error joining issues in KeyStore.

This commit is contained in:
Captain ALM 2024-06-09 21:15:28 +01:00
parent a94ed7a2e5
commit fe2d905236
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -40,6 +40,7 @@ func NewMJwtKeyStoreFromDirectory(directory, keyPrvExt, keyPubExt string) (KeySt
if err != nil { if err != nil {
return nil, err return nil, err
} }
errs := make([]error, 0, len(dirEntries)/2)
// Import keys from files, based on extension // Import keys from files, based on extension
for _, entry := range dirEntries { for _, entry := range dirEntries {
if entry.IsDir() { if entry.IsDir() {
@ -55,9 +56,8 @@ func NewMJwtKeyStoreFromDirectory(directory, keyPrvExt, keyPubExt string) (KeySt
if err2 == nil { if err2 == nil {
ks.store[kID] = key ks.store[kID] = key
ks.storePub[kID] = &key.PublicKey ks.storePub[kID] = &key.PublicKey
} else {
err = err2
} }
errs = append(errs, err2)
} else if path.Ext(entry.Name()) == "."+keyPubExt { } else if path.Ext(entry.Name()) == "."+keyPubExt {
// Load rsa public key with the file name as the kID (Up to the first .) // Load rsa public key with the file name as the kID (Up to the first .)
key, err2 := rsapublic.Read(path.Join(directory, entry.Name())) key, err2 := rsapublic.Read(path.Join(directory, entry.Name()))
@ -67,11 +67,11 @@ func NewMJwtKeyStoreFromDirectory(directory, keyPrvExt, keyPubExt string) (KeySt
ks.store[kID] = nil ks.store[kID] = nil
} }
ks.storePub[kID] = key ks.storePub[kID] = key
} else {
err = err2
} }
errs = append(errs, err2)
} }
} }
err = errors.Join(errs...)
return ks, err return ks, err
} }
@ -88,23 +88,21 @@ func ExportKeyStore(ks KeyStore, directory, keyPrvExt, keyPubExt string) error {
return err return err
} }
errs := make([]error, 0, len(ks.ListKeys())/2)
// Export all keys // Export all keys
for _, kID := range ks.ListKeys() { for _, kID := range ks.ListKeys() {
kPrv := ks.GetKey(kID) kPrv := ks.GetKey(kID)
if kPrv != nil { if kPrv != nil {
err2 := rsaprivate.Write(path.Join(directory, kID+"."+keyPrvExt), kPrv) err2 := rsaprivate.Write(path.Join(directory, kID+"."+keyPrvExt), kPrv)
if err2 != nil { errs = append(errs, err2)
err = err2
}
} }
kPub := ks.GetKeyPublic(kID) kPub := ks.GetKeyPublic(kID)
if kPub != nil { if kPub != nil {
err2 := rsapublic.Write(path.Join(directory, kID+"."+keyPubExt), kPub) err2 := rsapublic.Write(path.Join(directory, kID+"."+keyPubExt), kPub)
if err2 != nil { errs = append(errs, err2)
err = err2
}
} }
} }
err = errors.Join(errs...)
return err return err
} }