Skip to content
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

fix(keycloak): fix #591: Cleanup some small code smells in Keycloak plugin #1022

Merged
merged 1 commit into from
Dec 21, 2023
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
110 changes: 55 additions & 55 deletions plugins/keycloak-backend/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,60 @@ export type KeycloakProviderConfig = {
groupQuerySize?: number;
};

const readProviderConfig = (
id: string,
providerConfigInstance: Config,
): KeycloakProviderConfig => {
const baseUrl = providerConfigInstance.getString('baseUrl');
const realm = providerConfigInstance.getOptionalString('realm') ?? 'master';
const loginRealm =
providerConfigInstance.getOptionalString('loginRealm') ?? 'master';
const username = providerConfigInstance.getOptionalString('username');
const password = providerConfigInstance.getOptionalString('password');
const clientId = providerConfigInstance.getOptionalString('clientId');
const clientSecret = providerConfigInstance.getOptionalString('clientSecret');
const userQuerySize =
providerConfigInstance.getOptionalNumber('userQuerySize');
const groupQuerySize =
providerConfigInstance.getOptionalNumber('groupQuerySize');

if (clientId && !clientSecret) {
throw new Error(`clientSecret must be provided when clientId is defined.`);
}

if (clientSecret && !clientId) {
throw new Error(`clientId must be provided when clientSecret is defined.`);
}

if (username && !password) {
throw new Error(`password must be provided when username is defined.`);
}

if (password && !username) {
throw new Error(`username must be provided when password is defined.`);
}

const schedule = providerConfigInstance.has('schedule')
? readTaskScheduleDefinitionFromConfig(
providerConfigInstance.getConfig('schedule'),
)
: undefined;

return {
id,
baseUrl,
loginRealm,
realm,
username,
password,
clientId,
clientSecret,
schedule,
userQuerySize,
groupQuerySize,
};
};

export const readProviderConfigs = (
config: Config,
): KeycloakProviderConfig[] => {
Expand All @@ -103,62 +157,8 @@ export const readProviderConfigs = (
if (!providersConfig) {
return [];
}

return providersConfig.keys().map(id => {
const providerConfigInstance = providersConfig.getConfig(id);

const baseUrl = providerConfigInstance.getString('baseUrl');
const realm = providerConfigInstance.getOptionalString('realm') ?? 'master';
const loginRealm =
providerConfigInstance.getOptionalString('loginRealm') ?? 'master';
const username = providerConfigInstance.getOptionalString('username');
const password = providerConfigInstance.getOptionalString('password');
const clientId = providerConfigInstance.getOptionalString('clientId');
const clientSecret =
providerConfigInstance.getOptionalString('clientSecret');
const userQuerySize =
providerConfigInstance.getOptionalNumber('userQuerySize');
const groupQuerySize =
providerConfigInstance.getOptionalNumber('groupQuerySize');

if (clientId && !clientSecret) {
throw new Error(
`clientSecret must be provided when clientId is defined.`,
);
}

if (clientSecret && !clientId) {
throw new Error(
`clientId must be provided when clientSecret is defined.`,
);
}

if (username && !password) {
throw new Error(`password must be provided when username is defined.`);
}

if (password && !username) {
throw new Error(`username must be provided when password is defined.`);
}

const schedule = providerConfigInstance.has('schedule')
? readTaskScheduleDefinitionFromConfig(
providerConfigInstance.getConfig('schedule'),
)
: undefined;

return {
id,
baseUrl,
loginRealm,
realm,
username,
password,
clientId,
clientSecret,
schedule,
userQuerySize,
groupQuerySize,
};
return readProviderConfig(id, providerConfigInstance);
});
};
7 changes: 5 additions & 2 deletions plugins/keycloak-backend/src/lib/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ describe('parseGroup', () => {
};
const entity = await parseGroup(groupsFixture[0], 'test', transformer);

expect(entity!.metadata.name).toEqual('biggroup_test');
expect(entity).toBeDefined();
expect(entity?.metadata.name).toEqual('biggroup_test');
});
});

Expand Down Expand Up @@ -134,7 +135,8 @@ describe('parseUser', () => {
};
const entity = await parseUser(usersFixture[0], 'test', [], transformer);

expect(entity!.metadata.name).toEqual('jamesdoe_test');
expect(entity).toBeDefined();
expect(entity?.metadata.name).toEqual('jamesdoe_test');
});
});

Expand All @@ -150,6 +152,7 @@ describe('getEntities', () => {

expect(users).toHaveLength(3);
});

it('should fetch all users with pagination', async () => {
const client = new KeycloakAdminClientMock() as unknown as KcAdminClient;

Expand Down
10 changes: 5 additions & 5 deletions plugins/keycloak-backend/src/lib/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const parseGroup = async (
realm: string,
groupTransformer?: GroupTransformer,
): Promise<GroupEntity | undefined> => {
const transformer = groupTransformer || noopGroupTransformer;
const transformer = groupTransformer ?? noopGroupTransformer;
const entity: GroupEntity = {
apiVersion: 'backstage.io/v1beta1',
kind: 'Group',
Expand All @@ -59,7 +59,7 @@ export const parseGroup = async (
displayName: keycloakGroup.name!,
},
// children, parent and members are updated again after all group and user transformers applied.
children: keycloakGroup.subGroups?.map(g => g.name!) || [],
children: keycloakGroup.subGroups?.map(g => g.name!) ?? [],
parent: keycloakGroup.parent,
members: keycloakGroup.members,
},
Expand All @@ -75,7 +75,7 @@ export const parseUser = async (

userTransformer?: UserTransformer,
): Promise<UserEntity | undefined> => {
const transformer = userTransformer || noopUserTransformer;
const transformer = userTransformer ?? noopUserTransformer;
const entity: UserEntity = {
apiVersion: 'backstage.io/v1beta1',
kind: 'User',
Expand Down Expand Up @@ -230,11 +230,11 @@ export const readKeycloakRealm = async (
entity.spec.members =
g.entity.spec.members?.map(
m => parsedUsers.find(p => p.username === m)?.entity.metadata.name!,
) || [];
) ?? [];
entity.spec.children =
g.entity.spec.children?.map(
c => parsedGroups.find(p => p.name === c)?.entity.metadata.name!,
) || [];
) ?? [];
entity.spec.parent = parsedGroups.find(p => p.name === entity.spec.parent)
?.entity.metadata.name;
return entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,16 @@ export class KeycloakOrgEntityProvider implements EntityProvider {
username: provider.username,
password: provider.password,
};
} else {
} else if (provider.clientId && provider.clientSecret) {
credentials = {
grantType: 'client_credentials',
clientId: provider.clientId!,
clientId: provider.clientId,
clientSecret: provider.clientSecret,
};
} else {
throw new Error(
`username and password or clientId and clientSecret must be provided.`,
);
}

await kcAdminClient.auth(credentials);
Expand Down