Skip to content

fix: change order of managing groups and rolemapping #240

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

Merged
merged 7 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/operators/keycloak/keycloak.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as keycloak from './keycloak'
import { updateUserGroups } from './keycloak'

describe('Keycloak User Group Management', () => {
Expand Down Expand Up @@ -87,4 +88,62 @@ describe('Keycloak User Group Management', () => {
)
})
})
describe('IDPManager', () => {
let api: any

beforeEach(() => {
jest.mock('./keycloak', () => ({
internalIDP: jest.fn(),
externalIDP: jest.fn(),
manageUsers: jest.fn(),
IDPManager: jest.fn(async (api, externalIdp) => {
if (externalIdp) {
await keycloak.externalIDP(api)
} else {
await keycloak.internalIDP(api)
await keycloak.manageUsers(api, [])
}
},
)}))

api = {
providers: {},
clientScope: {},
roles: {},
clientRoleMappings: {},
roleMapper: {},
clients: {},
protocols: {},
realms: {},
users: {},
groups: {},
}
})

afterEach(() => {
jest.restoreAllMocks()
})

it('should call externalIDP when FEAT_EXTERNAL_IDP is "true"', async () => {
const { IDPManager } = await import('./keycloak')

jest.spyOn(keycloak, 'externalIDP').mockImplementation(jest.fn())

await IDPManager(api, true)

expect(keycloak.externalIDP).toHaveBeenCalled()
})

it('should call internalIdp and manageUsers when FEAT_EXTERNAL_IDP is not "true"', async () => {
const { IDPManager } = await import('./keycloak')

jest.spyOn(keycloak, 'internalIDP').mockImplementation(jest.fn())
jest.spyOn(keycloak, 'manageUsers').mockImplementation(jest.fn())

await IDPManager(api, false)

expect(keycloak.internalIDP).toHaveBeenCalled()
expect(keycloak.manageUsers).toHaveBeenCalled()
})
})
})
22 changes: 13 additions & 9 deletions src/operators/keycloak/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ async function runKeycloakUpdater() {
const api = setupKeycloakApi(connection)
await keycloakConfigMapChanges(api)
await manageGroups(api)
if (!JSON.parse(env.FEAT_EXTERNAL_IDP)) {
await manageUsers(api, env.USERS as Record<string, any>[])
}
await IDPManager(api, env.FEAT_EXTERNAL_IDP === 'true')
}, 'update from config')
console.info('Updated Config')
}
Expand Down Expand Up @@ -330,8 +328,6 @@ if (typeof require !== 'undefined' && require.main === module) {
}
async function keycloakConfigMapChanges(api: KeycloakApi) {
await keycloakRealmProviderConfigurer(api)
if (env.FEAT_EXTERNAL_IDP === 'true') await externalIDP(api)
else await internalIdp(api)
}

async function createKeycloakConnection(): Promise<KeycloakConnection> {
Expand Down Expand Up @@ -485,7 +481,7 @@ async function keycloakRealmProviderConfigurer(api: KeycloakApi) {
await api.realms.adminRealmsRealmPut(env.KEYCLOAK_REALM, createLoginThemeConfig('APL'))
}

async function externalIDP(api: KeycloakApi) {
export async function externalIDP(api: KeycloakApi) {
// Keycloak acts as broker
// Create Identity Provider
const idp = await createIdProvider(env.IDP_CLIENT_ID, env.IDP_ALIAS, env.IDP_CLIENT_SECRET, env.IDP_OIDC_URL)
Expand Down Expand Up @@ -547,7 +543,7 @@ async function externalIDP(api: KeycloakApi) {
}
}

async function internalIdp(api: KeycloakApi) {
export async function internalIDP(api: KeycloakApi) {
// IDP instead of broker
console.info('Getting realm groups')
const updatedExistingGroups = (await api.groups.adminRealmsRealmGroupsGet(keycloakRealm)).body
Expand Down Expand Up @@ -625,7 +621,15 @@ async function internalIdp(api: KeycloakApi) {
await createUpdateUser(api, createAdminUser(env.KEYCLOAK_ADMIN, env.KEYCLOAK_ADMIN_PASSWORD))
}

async function manageGroups(api: KeycloakApi) {
export async function IDPManager(api: KeycloakApi, isExternalIdp: boolean) {
if (isExternalIdp) await externalIDP(api)
else {
await internalIDP(api)
await manageUsers(api, env.USERS as Record<string, any>[])
}
}

export async function manageGroups(api: KeycloakApi) {
const teamGroups = createGroups(env.TEAM_IDS)
console.info('Getting realm groups')
try {
Expand Down Expand Up @@ -744,7 +748,7 @@ async function deleteUsers(api: any, users: any[]) {
)
}

async function manageUsers(api: KeycloakApi, users: Record<string, any>[]) {
export async function manageUsers(api: KeycloakApi, users: Record<string, any>[]) {
// Create/Update users in realm 'otomi'
await Promise.all(
users.map((user) =>
Expand Down
Loading