Skip to content

Commit 327f0f9

Browse files
authored
feat: Add script to add platform admin to keycloak and create user org roles (#1538)
* feat/add script to add platform admin keycloak and role Signed-off-by: sujitaw <sujit.sutar@ayanworks.com> * fix/eslint issue Signed-off-by: sujitaw <sujit.sutar@ayanworks.com> * fix/coderabbit comments Signed-off-by: sujitaw <sujit.sutar@ayanworks.com> * fix/changes to fetch value from db Signed-off-by: sujitaw <sujit.sutar@ayanworks.com> * fix pr comments Signed-off-by: sujitaw <sujit.sutar@ayanworks.com> * fix pr comments Signed-off-by: sujitaw <sujit.sutar@ayanworks.com> --------- Signed-off-by: sujitaw <sujit.sutar@ayanworks.com>
1 parent 788869f commit 327f0f9

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

.env.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ CLUSTER_NAME=CREDO-CONTROLLER-CLUSTER # ECS cluster name for credo controller
204204
TASKDEFINITION_FAMILY=CREDO-CONTROLLER-TASKDEFINITION # ECS taskdefinition name for credo controller
205205
PROTOCOL=http
206206

207+
#Platform admin setup
208+
PLATFORM_ADMIN_KEYCLOAK_ID=adminClient #Create a new keycloak client for platform admin (admin console) with adminClient name and add its client id
209+
PLATFORM_ADMIN_KEYCLOAK_SECRET=xxxxxxxxxxxx #Add the secret of keycloak client created for the above platform admin (admin console) id
210+
PLATFORM_ADMIN_OLD_CLIENT_ID=adminClient #Add incase the keycloak id of the old platform client is changed and want to update all the users in database with the new admin client
211+
#Note : the above is for platform client and not platform admin client
212+
207213
# To add more client add the following variables for each additional client.
208214
# Replace the `CLIENT-NAME` with the appropriate client name as added in `SUPPORTED_SSO_CLIENTS`
209215
# Default client will not need the following details

libs/prisma-service/prisma/data/credebl-master-table/credebl-master-table.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"createdBy": "",
2727
"lastChangedBy": ""
2828
},
29+
"platformAdminKeycloakPassword":"Admin@123",
2930
"orgRoleData": [
3031
{
3132
"name": "owner",

libs/prisma-service/prisma/seed.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable camelcase */
21
import * as CryptoJS from 'crypto-js';
32
import * as fs from 'fs';
43
import * as util from 'util';
@@ -521,8 +520,11 @@ export const updateClientId = async (): Promise<void> => {
521520
throw new Error('Missing required environment variables');
522521
}
523522

524-
const OLD_CLIENT_ID = 'adminClient';
525-
523+
const OLD_CLIENT_ID = process.env.PLATFORM_ADMIN_OLD_CLIENT_ID;
524+
if (!OLD_CLIENT_ID) {
525+
logger.log('Skipping updateClientId script requires PLATFORM_ADMIN_OLD_CLIENT_ID');
526+
return;
527+
}
526528
// Encrypt once
527529
const newEncryptedClientId = CryptoJS.AES.encrypt(
528530
JSON.stringify(KEYCLOAK_MANAGEMENT_CLIENT_ID),
@@ -661,17 +663,23 @@ export async function getKeycloakToken(): Promise<string> {
661663

662664
export async function createKeycloakUser(): Promise<void> {
663665
logger.log(`✅ Creating keycloak user for platform admin`);
666+
const { platformAdminData } = JSON.parse(configData);
667+
if (!platformAdminData?.password) {
668+
throw new Error('platformAdminData password is missing from credebl-master-table.json');
669+
}
670+
if (!cachedConfig) {
671+
throw new Error('failed to load platform config data from db');
672+
}
673+
664674
const {
665675
KEYCLOAK_DOMAIN,
666676
KEYCLOAK_REALM,
667-
PLATFORM_ADMIN_USER_PASSWORD,
668677
PLATFORM_ADMIN_KEYCLOAK_ID,
669678
PLATFORM_ADMIN_KEYCLOAK_SECRET,
670679
CRYPTO_PRIVATE_KEY
671680
} = process.env;
672681

673682
if (
674-
!PLATFORM_ADMIN_USER_PASSWORD ||
675683
!KEYCLOAK_DOMAIN ||
676684
!KEYCLOAK_REALM ||
677685
!PLATFORM_ADMIN_KEYCLOAK_ID ||
@@ -682,14 +690,14 @@ export async function createKeycloakUser(): Promise<void> {
682690
'Missing required environment variables for either PLATFORM_ADMIN_USER_PASSWORD or KEYCLOAK_DOMAIN or KEYCLOAK_REALM or PLATFORM_ADMIN_KEYCLOAK_ID or PLATFORM_ADMIN_KEYCLOAK_SECRET or CRYPTO_PRIVATE_KEY'
683691
);
684692
}
685-
693+
const decryptedPassword = CryptoJS.AES.decrypt(platformAdminData.password, CRYPTO_PRIVATE_KEY);
686694
const token = await getKeycloakToken();
687695
const user = {
688696
username: cachedConfig.platformEmail,
689697
email: cachedConfig.platformEmail,
690698
firstName: cachedConfig.platformName,
691699
lastName: cachedConfig.platformName,
692-
password: PLATFORM_ADMIN_USER_PASSWORD
700+
password: decryptedPassword.toString(CryptoJS.enc.Utf8)
693701
};
694702
const res = await fetch(`${KEYCLOAK_DOMAIN}admin/realms/${KEYCLOAK_REALM}/users`, {
695703
method: 'POST',

0 commit comments

Comments
 (0)