-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add support for dynamic access group configuration and synchron… #2226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Darkroom4364
wants to merge
14
commits into
dev
Choose a base branch
from
1911-auth-add-support-to-change-access_configjson-on-an-existing-instance
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b8f3152
feat: add support for dynamic access group configuration and synchron…
Darkroom4364 776ce32
feat: add support for dynamic access configuration and sync access gr…
Darkroom4364 2c25305
refactor: optimize access group UUID handling using Set for improved …
Darkroom4364 aef4a3f
fix: remove test for skipping users without email addresses
Darkroom4364 862d339
fix: correct email matching logic by adding '@' prefix in access grou…
Darkroom4364 b26956c
feat: validate access_config.json structure and use softRemove for st…
Darkroom4364 f97ea5d
feat: enhance access configuration validation and restore deleted acc…
Darkroom4364 69cd456
refactor: remove rights property from access_groups in configuration …
Darkroom4364 3d2bdfd
feat: add rights property to access groups in configuration and tests
Darkroom4364 820fd57
feat: add rights property to access groups in configuration and updat…
Darkroom4364 af89569
cosmetic fix for prettier
Darkroom4364 c330ac5
feat: add support for restoring deleted access groups in syncAccessGr…
Darkroom4364 b1df0fa
fix: update logic for handling deleted access groups in syncAccessGro…
Darkroom4364 5782890
feat: add error handling for access config file reading and update ac…
Darkroom4364 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,10 @@ export class AuthService implements OnModuleInit { | |
|
|
||
| async onModuleInit(): Promise<void> { | ||
| await this.affiliationGroupService.createAccessGroups(this.config); | ||
| await this.affiliationGroupService.syncAccessGroups( | ||
| this.config, | ||
| this.userRepository, | ||
| ); | ||
|
Comment on lines
54
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis is a comment left during a code review.
Path: backend/src/services/auth.service.ts
Line: 54-59
Comment:
**`createAccessGroups` is redundant before `syncAccessGroups`**
`syncAccessGroups` already upserts every group from the config in its step 1, so the preceding `createAccessGroups` call performs duplicate lookups for every configured group. Removing it avoids the unnecessary round-trips.
How can I resolve this? If you propose a fix, please make it concise. |
||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
|
|
||
333 changes: 333 additions & 0 deletions
333
backend/tests/auth/access-groups/affiliation-sync-on-reboot.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,333 @@ | ||
| import { createNewUser } from '@/services/auth.service'; | ||
| import { | ||
| AccessGroupEntity, | ||
| AccountEntity, | ||
| AffiliationGroupService, | ||
| GroupMembershipEntity, | ||
| UserEntity, | ||
| } from '@kleinkram/backend-common'; | ||
| import { | ||
| AccessGroupConfig, | ||
| AccessGroupType, | ||
| Providers, | ||
| } from '@kleinkram/shared'; | ||
| import { database } from '../../utils/database-utilities'; | ||
| import { setupDatabaseHooks } from '../../utils/test-helpers'; | ||
|
|
||
| describe('Affiliation Group Sync on Reboot', () => { | ||
| setupDatabaseHooks(); | ||
|
|
||
| const GROUP_A_UUID = '00000000-aaaa-aaaa-aaaa-aaaaaaaaaaaa'; | ||
| const GROUP_B_UUID = '00000000-bbbb-bbbb-bbbb-bbbbbbbbbbbb'; | ||
|
|
||
| let affiliationGroupService: AffiliationGroupService; | ||
|
|
||
| beforeAll(() => { | ||
| const accessGroupRepository = database.getRepository(AccessGroupEntity); | ||
| const groupMembershipRepository = database.getRepository( | ||
| GroupMembershipEntity, | ||
| ); | ||
| affiliationGroupService = new AffiliationGroupService( | ||
| accessGroupRepository, | ||
| groupMembershipRepository, | ||
| ); | ||
| }); | ||
|
|
||
| const createUser = async (email: string, config: AccessGroupConfig) => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
| const accountRepository = database.getRepository(AccountEntity); | ||
| await createNewUser( | ||
| config, | ||
| userRepository, | ||
| accountRepository, | ||
| affiliationGroupService, | ||
| { | ||
| oauthID: `oauth-${email}`, | ||
| provider: Providers.FakeOAuth, | ||
| email, | ||
| username: email.split('@')[0], | ||
| picture: '', | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| test('should add users to a new affiliation group on sync', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
|
|
||
| const initialConfig: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.createAccessGroups(initialConfig); | ||
| await createUser('alice@kleinkram.dev', initialConfig); | ||
|
|
||
| // Now add Group B to config and sync | ||
| const updatedConfig: AccessGroupConfig = { | ||
| emails: [ | ||
| { | ||
| email: 'kleinkram.dev', | ||
| access_groups: [GROUP_A_UUID, GROUP_B_UUID], | ||
| }, | ||
| ], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| { name: 'Group B', uuid: GROUP_B_UUID, rights: 5 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.syncAccessGroups( | ||
| updatedConfig, | ||
| userRepository, | ||
| ); | ||
|
|
||
| const user = await userRepository.findOneOrFail({ | ||
| where: { email: 'alice@kleinkram.dev' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
|
|
||
| const groupUuids = | ||
| user.memberships?.map((m) => m.accessGroup?.uuid) ?? []; | ||
| expect(groupUuids).toContain(GROUP_A_UUID); | ||
| expect(groupUuids).toContain(GROUP_B_UUID); | ||
| }); | ||
|
|
||
| test('should remove memberships when group is removed from config', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
|
|
||
| const initialConfig: AccessGroupConfig = { | ||
| emails: [ | ||
| { | ||
| email: 'kleinkram.dev', | ||
| access_groups: [GROUP_A_UUID, GROUP_B_UUID], | ||
| }, | ||
| ], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| { name: 'Group B', uuid: GROUP_B_UUID, rights: 5 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.createAccessGroups(initialConfig); | ||
| await createUser('bob@kleinkram.dev', initialConfig); | ||
|
|
||
| // Remove Group B from config | ||
| const updatedConfig: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.syncAccessGroups( | ||
| updatedConfig, | ||
| userRepository, | ||
| ); | ||
|
|
||
| const user = await userRepository.findOneOrFail({ | ||
| where: { email: 'bob@kleinkram.dev' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
|
|
||
| const groupUuids = | ||
| user.memberships?.map((m) => m.accessGroup?.uuid) ?? []; | ||
| expect(groupUuids).toContain(GROUP_A_UUID); | ||
| expect(groupUuids).not.toContain(GROUP_B_UUID); | ||
|
|
||
| // Group B should be soft-deleted | ||
| const accessGroupRepository = database.getRepository(AccessGroupEntity); | ||
| const groupB = await accessGroupRepository.findOne({ | ||
| where: { uuid: GROUP_B_UUID }, | ||
| }); | ||
| expect(groupB).toBeNull(); | ||
| }); | ||
|
|
||
| test('should update group name when changed in config', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
|
|
||
| const initialConfig: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.syncAccessGroups( | ||
| initialConfig, | ||
| userRepository, | ||
| ); | ||
|
|
||
| const updatedConfig: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A Renamed', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.syncAccessGroups( | ||
| updatedConfig, | ||
| userRepository, | ||
| ); | ||
|
|
||
| const accessGroupRepository = database.getRepository(AccessGroupEntity); | ||
| const group = await accessGroupRepository.findOneOrFail({ | ||
| where: { uuid: GROUP_A_UUID }, | ||
| }); | ||
| expect(group.name).toBe('Group A Renamed'); | ||
| }); | ||
|
|
||
| test('should remove memberships when email pattern changes', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
|
|
||
| const initialConfig: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.createAccessGroups(initialConfig); | ||
| await createUser('carol@kleinkram.dev', initialConfig); | ||
|
|
||
| // Change email pattern so carol no longer matches | ||
| const updatedConfig: AccessGroupConfig = { | ||
| emails: [ | ||
| { email: 'other-domain.com', access_groups: [GROUP_A_UUID] }, | ||
| ], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.syncAccessGroups( | ||
| updatedConfig, | ||
| userRepository, | ||
| ); | ||
|
|
||
| const user = await userRepository.findOneOrFail({ | ||
| where: { email: 'carol@kleinkram.dev' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
|
|
||
| const affiliationMemberships = (user.memberships ?? []).filter( | ||
| (m) => m.accessGroup?.type === AccessGroupType.AFFILIATION, | ||
| ); | ||
| expect(affiliationMemberships).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should be idempotent when called twice with same config', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
|
|
||
| const config: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.createAccessGroups(config); | ||
| await createUser('dave@kleinkram.dev', config); | ||
|
|
||
| await affiliationGroupService.syncAccessGroups(config, userRepository); | ||
| await affiliationGroupService.syncAccessGroups(config, userRepository); | ||
|
|
||
| const user = await userRepository.findOneOrFail({ | ||
| where: { email: 'dave@kleinkram.dev' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
|
|
||
| const affiliationMemberships = (user.memberships ?? []).filter( | ||
| (m) => m.accessGroup?.type === AccessGroupType.AFFILIATION, | ||
| ); | ||
| expect(affiliationMemberships).toHaveLength(1); | ||
| expect(affiliationMemberships[0].accessGroup?.uuid).toBe(GROUP_A_UUID); | ||
| }); | ||
|
|
||
| test('should not touch primary or custom groups', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
|
|
||
| const config: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.createAccessGroups(config); | ||
| await createUser('eve@kleinkram.dev', config); | ||
|
|
||
| // Verify user has a primary group | ||
| const userBefore = await userRepository.findOneOrFail({ | ||
| where: { email: 'eve@kleinkram.dev' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
| const primaryBefore = (userBefore.memberships ?? []).filter( | ||
| (m) => m.accessGroup?.type === AccessGroupType.PRIMARY, | ||
| ); | ||
| expect(primaryBefore.length).toBeGreaterThan(0); | ||
|
|
||
| // Sync with empty config (no affiliation groups) | ||
| const emptyConfig: AccessGroupConfig = { | ||
| emails: [], | ||
| access_groups: [], | ||
| }; | ||
|
|
||
| await affiliationGroupService.syncAccessGroups( | ||
| emptyConfig, | ||
| userRepository, | ||
| ); | ||
|
|
||
| // Primary group should still exist | ||
| const userAfter = await userRepository.findOneOrFail({ | ||
| where: { email: 'eve@kleinkram.dev' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
| const primaryAfter = (userAfter.memberships ?? []).filter( | ||
| (m) => m.accessGroup?.type === AccessGroupType.PRIMARY, | ||
| ); | ||
| expect(primaryAfter).toHaveLength(primaryBefore.length); | ||
| }); | ||
|
|
||
| test('should remove manually-added affiliation membership when user does not match pattern', async () => { | ||
| const userRepository = database.getRepository(UserEntity); | ||
| const groupMembershipRepository = database.getRepository( | ||
| GroupMembershipEntity, | ||
| ); | ||
|
|
||
| const config: AccessGroupConfig = { | ||
| emails: [{ email: 'kleinkram.dev', access_groups: [GROUP_A_UUID] }], | ||
| access_groups: [ | ||
| { name: 'Group A', uuid: GROUP_A_UUID, rights: 10 }, | ||
| ], | ||
| }; | ||
|
|
||
| await affiliationGroupService.createAccessGroups(config); | ||
|
|
||
| // Create an external user (non-matching email) | ||
| await createUser('frank@external.com', config); | ||
|
|
||
| // Manually add the external user to the affiliation group | ||
| const frank = await userRepository.findOneOrFail({ | ||
| where: { email: 'frank@external.com' }, | ||
| }); | ||
| const manualMembership = groupMembershipRepository.create({ | ||
| user: { uuid: frank.uuid }, | ||
| accessGroup: { uuid: GROUP_A_UUID }, | ||
| }); | ||
| await groupMembershipRepository.save(manualMembership); | ||
|
|
||
| // Sync should remove the manual membership | ||
| await affiliationGroupService.syncAccessGroups(config, userRepository); | ||
|
|
||
| const updatedFrank = await userRepository.findOneOrFail({ | ||
| where: { email: 'frank@external.com' }, | ||
| relations: ['memberships', 'memberships.accessGroup'], | ||
| }); | ||
| const affiliationMemberships = (updatedFrank.memberships ?? []).filter( | ||
| (m) => m.accessGroup?.type === AccessGroupType.AFFILIATION, | ||
| ); | ||
| expect(affiliationMemberships).toHaveLength(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AuthService.onModuleInit() now calls syncAccessGroups(), which performs potentially heavy DB writes/reads across all users. This makes application startup time/data-plane health dependent on completing a full sync. If this is intended, consider moving to a dedicated bootstrap job with logging/metrics and (at minimum) ensuring it is resilient to partial failures (e.g., wrap and log errors rather than failing module init).