fix: Handle reading huge amounts of users without crashing

Signed-off-by: Gustaf Räntilä <g.rantila@gmail.com>
This commit is contained in:
Gustaf Räntilä
2024-04-26 09:04:19 +02:00
parent 49eab297a3
commit 6e370e62e9
3 changed files with 54 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-msgraph': patch
---
Handle fetching huge amounts of users from Azure without crashing
@@ -1064,5 +1064,51 @@ describe('read microsoft graph', () => {
);
expect(client.getUserPhotoWithSizeLimit).toHaveBeenCalledTimes(1);
});
it('should handle loading huge amounts of users', async () => {
client.getOrganization.mockResolvedValue(getExampleOrg());
const userCount = 200_000;
async function* getHugeAmountsOfExampleUsers() {
for (let i = 0; i < userCount; ++i) {
yield {
id: `userid-${i}`,
displayName: 'User Name',
mail: 'user.name@example.com',
};
}
}
client.getUsers.mockImplementation(getHugeAmountsOfExampleUsers);
client.getGroups.mockImplementation(getExampleGroups);
client.getGroupMembers.mockImplementation(getExampleGroupMembers);
client.getGroupPhotoWithSizeLimit.mockResolvedValue(
'data:image/jpeg;base64,...',
);
const { users } = await readMicrosoftGraphOrg(client, 'tenantid', {
logger: getVoidLogger(),
loadUserPhotos: false,
});
expect(users.length).toBe(userCount);
expect(client.getUsers).toHaveBeenCalledTimes(1);
expect(client.getUsers).toHaveBeenCalledWith(
{
top: 999,
},
undefined,
);
expect(client.getGroups).toHaveBeenCalledTimes(1);
expect(client.getGroups).toHaveBeenCalledWith(
{
top: 999,
},
undefined,
);
});
});
});
@@ -384,7 +384,7 @@ export async function readMicrosoftGraphOrg(
logger: LoggerService;
},
): Promise<{ users: UserEntity[]; groups: GroupEntity[] }> {
const users: UserEntity[] = [];
let users: UserEntity[] = [];
if (options.userGroupMemberFilter || options.userGroupMemberSearch) {
const { users: usersInGroups } = await readMicrosoftGraphUsersInGroups(
@@ -401,7 +401,7 @@ export async function readMicrosoftGraphOrg(
logger: options.logger,
},
);
users.push(...usersInGroups);
users = usersInGroups;
} else {
const { users: usersWithFilter } = await readMicrosoftGraphUsers(client, {
queryMode: options.queryMode,
@@ -412,7 +412,7 @@ export async function readMicrosoftGraphOrg(
transformer: options.userTransformer,
logger: options.logger,
});
users.push(...usersWithFilter);
users = usersWithFilter;
}
const { groups, rootGroup, groupMember, groupMemberOf } =
await readMicrosoftGraphGroups(client, tenantId, {