ToolJet/server/data-migrations/1706024347284-AddInstanceLevelSSOInSSOConfigs.ts

77 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-02-25 06:52:50 +00:00
import { MigrationInterface, QueryRunner } from 'typeorm';
import { ConfigScope, SSOConfigs, SSOType } from '@entities/sso_config.entity';
2025-08-03 07:09:18 +00:00
import { NestFactory } from '@nestjs/core';
import { AppModule } from '@modules/app/module';
import { getTooljetEdition } from '@helpers/utils.helper';
import { getImportPath, TOOLJET_EDITIONS } from '@modules/app/constants';
import { getEnvVars } from 'scripts/database-config-utils';
2025-02-25 06:52:50 +00:00
export class AddInstanceLevelSSOInSSOConfigs1706024347284 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const entityManager = queryRunner.manager;
2025-08-03 07:09:18 +00:00
const nestApp = await NestFactory.createApplicationContext(await AppModule.register({ IS_GET_CONTEXT: true }));
const edition = getTooljetEdition() as TOOLJET_EDITIONS;
const { EncryptionService } = await import(`${await getImportPath(true, edition)}/encryption/service`);
const encryptionService = nestApp.get(EncryptionService);
const envVars = getEnvVars();
2025-02-25 06:52:50 +00:00
const ssoConfigs: Partial<SSOConfigs>[] = [
{
configScope: ConfigScope.INSTANCE,
sso: SSOType.GOOGLE,
2025-08-03 07:09:18 +00:00
enabled: !!envVars?.SSO_GOOGLE_OAUTH2_CLIENT_ID,
2025-02-25 06:52:50 +00:00
configs: {
2025-08-03 07:09:18 +00:00
clientId: envVars?.SSO_GOOGLE_OAUTH2_CLIENT_ID || '',
2025-02-25 06:52:50 +00:00
},
},
{
configScope: ConfigScope.INSTANCE,
sso: SSOType.GIT,
2025-08-03 07:09:18 +00:00
enabled: !!envVars?.SSO_GIT_OAUTH2_CLIENT_ID,
2025-02-25 06:52:50 +00:00
configs: {
2025-08-03 07:09:18 +00:00
clientId: envVars?.SSO_GIT_OAUTH2_CLIENT_ID || '',
hostName: envVars?.SSO_GIT_OAUTH2_HOST || '',
2025-02-25 06:52:50 +00:00
clientSecret:
2025-08-03 07:09:18 +00:00
(envVars?.SSO_GIT_OAUTH2_CLIENT_SECRET &&
2025-02-25 06:52:50 +00:00
(await encryptionService.encryptColumnValue(
'ssoConfigs',
'clientSecret',
2025-08-03 07:09:18 +00:00
envVars.SSO_GIT_OAUTH2_CLIENT_SECRET
2025-02-25 06:52:50 +00:00
))) ||
'',
},
},
{
configScope: ConfigScope.INSTANCE,
sso: SSOType.OPENID,
2025-08-03 07:09:18 +00:00
enabled: !!envVars?.SSO_OPENID_CLIENT_ID,
2025-02-25 06:52:50 +00:00
configs: {
2025-08-03 07:09:18 +00:00
clientId: envVars?.SSO_OPENID_CLIENT_ID || '',
name: envVars?.SSO_OPENID_NAME || '',
2025-02-25 06:52:50 +00:00
clientSecret:
2025-08-03 07:09:18 +00:00
(envVars?.SSO_OPENID_CLIENT_SECRET &&
2025-02-25 06:52:50 +00:00
(await encryptionService.encryptColumnValue(
'ssoConfigs',
'clientSecret',
2025-08-03 07:09:18 +00:00
envVars.SSO_OPENID_CLIENT_SECRET
2025-02-25 06:52:50 +00:00
))) ||
'',
2025-08-03 07:09:18 +00:00
wellKnownUrl: envVars?.SSO_OPENID_WELL_KNOWN_URL || '',
2025-02-25 06:52:50 +00:00
},
},
{
configScope: ConfigScope.INSTANCE,
sso: SSOType.FORM,
enabled: true,
},
];
for (const config of ssoConfigs) {
await entityManager.insert(SSOConfigs, config);
}
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}