[backend/federation] Fix sporadic key fetch failures (ISH-194)

This commit is contained in:
Laura Hausmann 2024-03-16 17:17:43 +01:00
parent 4c1e03ccc1
commit 4878d82463
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 21 additions and 0 deletions

View file

@ -67,6 +67,9 @@ public class AuthorizedFetchMiddleware(
var user = await userResolver.ResolveAsync(sig.KeyId).WaitAsync(ct); var user = await userResolver.ResolveAsync(sig.KeyId).WaitAsync(ct);
key = await db.UserPublickeys.Include(p => p.User) key = await db.UserPublickeys.Include(p => p.User)
.FirstOrDefaultAsync(p => p.User == user, ct); .FirstOrDefaultAsync(p => p.User == user, ct);
// If the key is still null here, we have a data consistency issue and need to update the key manually
key ??= await userSvc.UpdateUserPublicKeyAsync(user).WaitAsync(ct);
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -432,6 +432,24 @@ public class UserService(
return key; return key;
} }
public async Task<UserPublickey> UpdateUserPublicKeyAsync(User user)
{
var uri = user.Uri ?? throw new Exception("Can't update public key of user without Uri");
var actor = await fetchSvc.FetchActorAsync(uri);
if (actor.PublicKey?.PublicKey == null)
throw new Exception("Failed to update user public key: Invalid or missing public key");
var key = await db.UserPublickeys.FirstOrDefaultAsync(p => p.User == user) ?? new UserPublickey { User = user };
key.KeyId = actor.PublicKey.Id;
key.KeyPem = actor.PublicKey.PublicKey;
db.Update(key);
await db.SaveChangesAsync();
return key;
}
public async Task DeleteUserAsync(ASActor actor) public async Task DeleteUserAsync(ASActor actor)
{ {
var user = await db.Users var user = await db.Users