Browse Source

fix: change logic for ssl certificate read on KnexClient

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
c19d3ac25c
  1. 29
      packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts
  2. 28
      packages/nocodb/src/lib/db/sql-client/lib/SqlClientFactory.ts
  3. 6
      packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts
  4. 12
      packages/nocodb/src/lib/db/sql-mgr/v2/SqlMgrv2.ts
  5. 8
      packages/nocodb/src/lib/db/sql-mgr/v2/SqlMgrv2Trans.ts
  6. 8
      packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigrator.ts
  7. 12
      packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigratorv2.ts
  8. 4
      packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigratorv2Tans.ts
  9. 14
      packages/nocodb/src/lib/meta/NcMetaMgr.ts
  10. 2
      packages/nocodb/src/lib/meta/api/baseApis.ts
  11. 2
      packages/nocodb/src/lib/meta/api/columnApis.ts
  12. 8
      packages/nocodb/src/lib/meta/api/metaDiffApis.ts
  13. 4
      packages/nocodb/src/lib/meta/api/projectApis.ts
  14. 4
      packages/nocodb/src/lib/meta/api/tableApis.ts
  15. 4
      packages/nocodb/src/lib/meta/api/utilApis.ts
  16. 2
      packages/nocodb/src/lib/meta/handlers/xcMetaDiff.ts
  17. 4
      packages/nocodb/src/lib/utils/NcConfigFactory.ts
  18. 8
      packages/nocodb/src/lib/utils/common/BaseApiBuilder.ts
  19. 6
      packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts
  20. 4
      packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts
  21. 6
      packages/nocodb/src/lib/utils/common/handlers/xcMetaDiffSync.ts
  22. 4
      packages/nocodb/src/lib/v1-legacy/NcProjectBuilder.ts

29
packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts

@ -586,27 +586,6 @@ class KnexClient extends SqlClient {
if (connectionConfig.knex) {
this.sqlClient = connectionConfig.knex;
} else {
// console.log('KnexClient',this.connectionConfig);
if (
this.connectionConfig.connection.ssl &&
typeof this.connectionConfig.connection.ssl === 'object'
) {
if (this.connectionConfig.connection.ssl.caFilePath) {
this.connectionConfig.connection.ssl.ca = fs
.readFileSync(this.connectionConfig.connection.ssl.caFilePath)
.toString();
}
if (this.connectionConfig.connection.ssl.keyFilePath) {
this.connectionConfig.connection.ssl.key = fs
.readFileSync(this.connectionConfig.connection.ssl.keyFilePath)
.toString();
}
if (this.connectionConfig.connection.ssl.certFilePath) {
this.connectionConfig.connection.ssl.cert = fs
.readFileSync(this.connectionConfig.connection.ssl.certFilePath)
.toString();
}
}
const tmpConnectionConfig =
connectionConfig.client === 'sqlite3'
? connectionConfig.connection
@ -620,10 +599,10 @@ class KnexClient extends SqlClient {
this.evt = new Emit();
}
_validateInput() {
async _validateInput() {
try {
const packageJson = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')
await promisify(fs.readFile)(path.join(process.cwd(), 'package.json'), 'utf8')
);
return (
packageJson.name === 'nocodb' || 'nocodb' in packageJson.dependencies
@ -632,10 +611,10 @@ class KnexClient extends SqlClient {
return true;
}
validateInput() {
async validateInput() {
try {
if (!('___ext' in KnexClient)) {
KnexClient.___ext = this._validateInput();
KnexClient.___ext = await this._validateInput();
}
if (!KnexClient.___ext) {
Tele.emit('evt', {

28
packages/nocodb/src/lib/db/sql-client/lib/SqlClientFactory.ts

@ -9,6 +9,11 @@ import YugabyteClient from './pg/YugabyteClient';
import TidbClient from './mysql/TidbClient';
import VitessClient from './mysql/VitessClient';
import fs from 'fs';
import { promisify } from 'util';
const readFileAsync = promisify(fs.readFile);
export class SqlClientFactory {
static create(connectionConfig) {
connectionConfig.meta = connectionConfig.meta || {};
@ -40,7 +45,28 @@ export class SqlClientFactory {
}
export default class {
static create(connectionConfig) {
static async create(connectionConfig) {
if (
connectionConfig.connection.ssl &&
typeof connectionConfig.connection.ssl === 'object'
) {
if (connectionConfig.connection.ssl.caFilePath) {
connectionConfig.connection.ssl.ca = await readFileAsync(
connectionConfig.connection.ssl.caFilePath
).toString();
}
if (connectionConfig.connection.ssl.keyFilePath) {
connectionConfig.connection.ssl.key = await readFileAsync(
connectionConfig.connection.ssl.keyFilePath
).toString();
}
if (connectionConfig.connection.ssl.certFilePath) {
connectionConfig.connection.ssl.cert = await readFileAsync(
connectionConfig.connection.ssl.certFilePath
).toString();
}
}
if (Noco.isEE()) {
return SqlClientFactoryEE.create(connectionConfig);
}

6
packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts

@ -152,7 +152,7 @@ export default class SqlMgr {
// return this._project;
// }
public async testConnection(args = {}) {
const client = SqlClientFactory.create(args);
const client = await SqlClientFactory.create(args);
return client.testConnection();
}
@ -301,7 +301,7 @@ export default class SqlMgr {
const connectionKey = `${env}_${this.currentProjectJson.envs[env].db[i].meta.dbAlias}`;
this.currentProjectConnections[connectionKey] =
SqlClientFactory.create({
await SqlClientFactory.create({
...connectionConfig,
knex: await NcConnectionMgr.get({
dbAlias: this.currentProjectJson.envs[env].db[i].meta.dbAlias,
@ -361,7 +361,7 @@ export default class SqlMgr {
}
if ('client' in connectionConfig) {
const data = SqlClientFactory.create(connectionConfig);
const data = await SqlClientFactory.create(connectionConfig);
this.currentProjectConnections[connectionKey] = data;
// console.log(data);
return data;

12
packages/nocodb/src/lib/db/sql-mgr/v2/SqlMgrv2.ts

@ -29,11 +29,11 @@ export default class SqlMgrv2 {
return this;
}
public migrator(_base: Base) {
public async migrator(_base: Base) {
return this._migrator;
}
public static async testConnection(args = {}) {
const client = SqlClientFactory.create(args);
const client = await SqlClientFactory.create(args);
return client.testConnection();
}
@ -52,7 +52,7 @@ export default class SqlMgrv2 {
log.api(`${func}:args:`, base, op, opArgs);
// create sql client for this operation
const client = this.getSqlClient(base);
const client = await this.getSqlClient(base);
// do sql operation
const data = await client[op](opArgs);
@ -75,7 +75,7 @@ export default class SqlMgrv2 {
log.api(`${func}:args:`, base, op, opArgs);
// create sql client for this operation
const sqlClient = this.getSqlClient(base); //await this.projectGetSqlClient(args);
const sqlClient = await this.getSqlClient(base); //await this.projectGetSqlClient(args);
// do sql operation
const sqlMigrationStatements = await sqlClient[op](opArgs);
@ -118,7 +118,7 @@ export default class SqlMgrv2 {
return sqlMigrationStatements;
}
protected getSqlClient(base: Base) {
return NcConnectionMgrv2.getSqlClient(base);
protected async getSqlClient(base: Base): Promise<any> {
return await NcConnectionMgrv2.getSqlClient(base);
}
}

8
packages/nocodb/src/lib/db/sql-mgr/v2/SqlMgrv2Trans.ts

@ -25,10 +25,10 @@ export default class SqlMgrv2Trans extends SqlMgrv2 {
this.base = base;
}
public migrator() {
public async migrator() {
return new KnexMigratorv2Tans(
{ id: this.projectId },
this.getSqlClient(this.base),
await this.getSqlClient(this.base),
this.ncMeta
);
}
@ -52,8 +52,8 @@ export default class SqlMgrv2Trans extends SqlMgrv2 {
}
}
protected getSqlClient(base: Base) {
return NcConnectionMgrv2.getSqlClient(base, this.trx);
protected async getSqlClient(base: Base): Promise<any> {
return await NcConnectionMgrv2.getSqlClient(base, this.trx);
}
public async sqlOp(base: Base, op, opArgs): Promise<any> {

8
packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigrator.ts

@ -381,7 +381,7 @@ export default class KnexMigrator extends SqlMigrator {
async _initDbWithSql(connectionConfig) {
const sqlClient =
connectionConfig.sqlClient || SqlClientFactory.create(connectionConfig);
connectionConfig.sqlClient || await SqlClientFactory.create(connectionConfig);
if (connectionConfig.client === 'oracledb') {
this.emit(
`${connectionConfig.client}: Creating DB if not exists ${connectionConfig.connection.user}`
@ -427,7 +427,7 @@ export default class KnexMigrator extends SqlMigrator {
}
async _cleanDbWithSql(connectionConfig) {
const sqlClient = SqlClientFactory.create(connectionConfig);
const sqlClient = await SqlClientFactory.create(connectionConfig);
if (connectionConfig.client === 'oracledb') {
this.emit(`Dropping DB : ${connectionConfig.connection.user}`);
await sqlClient.dropDatabase({
@ -567,7 +567,7 @@ export default class KnexMigrator extends SqlMigrator {
args.dbAlias,
args.env
);
const sqlClient = args.sqlClient || SqlClientFactory.create(connection);
const sqlClient = args.sqlClient || await SqlClientFactory.create(connection);
let migrations = await sqlClient.selectAll(
sqlClient.getTnPath(connection.meta.tn)
@ -820,7 +820,7 @@ export default class KnexMigrator extends SqlMigrator {
args.dbAlias,
args.env
);
const sqlClient = SqlClientFactory.create(connection);
const sqlClient = await SqlClientFactory.create(connection);
const migrations = await sqlClient.selectAll(
sqlClient.getTnPath(connection.meta.tn)
);

12
packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigratorv2.ts

@ -382,7 +382,7 @@ export default class KnexMigratorv2 {
// }
async _initDbWithSql(base: Base) {
const sqlClient = this.getSqlClient(base);
const sqlClient = await this.getSqlClient(base);
const connectionConfig = base.getConnectionConfig();
if (connectionConfig.client === 'oracledb') {
this.emit(
@ -428,12 +428,12 @@ export default class KnexMigratorv2 {
// }
}
protected getSqlClient(base: Base) {
return NcConnectionMgrv2.getSqlClient(base);
protected async getSqlClient(base: Base): Promise<any> {
return await NcConnectionMgrv2.getSqlClient(base);
}
async _cleanDbWithSql(connectionConfig) {
const sqlClient = SqlClientFactory.create(connectionConfig);
const sqlClient = await SqlClientFactory.create(connectionConfig);
if (connectionConfig.client === 'oracledb') {
this.emit(`Dropping DB : ${connectionConfig.connection.user}`);
await sqlClient.dropDatabase({
@ -584,7 +584,7 @@ export default class KnexMigratorv2 {
// args.dbAlias,
// args.env
// );
const sqlClient = this.getSqlClient(base);
const sqlClient = await this.getSqlClient(base);
let migrations = await sqlClient.selectAll(
// todo: replace
@ -849,7 +849,7 @@ export default class KnexMigratorv2 {
// args.dbAlias,
// args.env
// );
const sqlClient = this.getSqlClient(base); // SqlClientFactory.create(connection);
const sqlClient = await this.getSqlClient(base); // SqlClientFactory.create(connection);
const migrations = await sqlClient.selectAll(
sqlClient.getTnPath('nc_evolutions')
);

4
packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigratorv2Tans.ts

@ -24,7 +24,7 @@ export default class KnexMigratorv2Tans extends KnexMigratorv2 {
protected get metaDb(): XKnex {
return this.ncMeta.knex || Noco.ncMeta.knex;
}
protected getSqlClient(base: Base) {
return this.sqlClient || NcConnectionMgrv2.getSqlClient(base);
protected async getSqlClient(base: Base): Promise<any> {
return this.sqlClient || await NcConnectionMgrv2.getSqlClient(base);
}
}

14
packages/nocodb/src/lib/meta/NcMetaMgr.ts

@ -1554,7 +1554,7 @@ export default class NcMetaMgr {
break;
case 'testConnection':
result = await SqlClientFactory.create(args.args).testConnection();
result = await (await SqlClientFactory.create(args.args)).testConnection();
break;
case 'xcProjectGetConfig':
result = await this.xcMeta.projectGetById(this.getProjectId(args));
@ -2907,9 +2907,9 @@ export default class NcMetaMgr {
}
}
protected projectGetSqlClient(args) {
protected async projectGetSqlClient(args) {
const builder = this.getBuilder(args);
return builder?.getSqlClient();
return await builder?.getSqlClient();
}
protected getBuilder(args): RestApiBuilder | GqlApiBuilder {
@ -3443,8 +3443,8 @@ export default class NcMetaMgr {
}
// @ts-ignore
protected getSqlClient(project_id: string, dbAlias: string) {
return this.app?.projectBuilders
protected async getSqlClient(project_id: string, dbAlias: string) {
return await this.app?.projectBuilders
?.find((pb) => pb?.id === project_id)
?.apiBuilders?.find((builder) => builder.dbAlias === dbAlias)
?.getSqlClient();
@ -4253,7 +4253,7 @@ export default class NcMetaMgr {
return { data: { list: columns } };
}
return this.projectGetSqlClient(args).columnList(args.args);
return (await this.projectGetSqlClient(args)).columnList(args.args);
} catch (e) {
throw e;
}
@ -4598,7 +4598,7 @@ export default class NcMetaMgr {
{}
);
const sqlClient = this.projectGetSqlClient(args);
const sqlClient = await this.projectGetSqlClient(args);
switch (args.args.type) {
case 'table':

2
packages/nocodb/src/lib/meta/api/baseApis.ts

@ -119,7 +119,7 @@ async function populateMeta(base: Base, project: Project): Promise<any> {
};
const t = process.hrtime();
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
let order = 1;
const models2: { [tableName: string]: Model } = {};

2
packages/nocodb/src/lib/meta/api/columnApis.ts

@ -674,7 +674,7 @@ export async function columnAdd(
],
};
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
const sqlMgr = await ProjectMgrv2.getSqlMgr({ id: base.project_id });
await sqlMgr.sqlOpPlus(base, 'tableUpdate', tableUpdateBody);

8
packages/nocodb/src/lib/meta/api/metaDiffApis.ts

@ -548,7 +548,7 @@ export async function metaDiff(req, res) {
for (const base of project.bases) {
try {
// @ts-ignore
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
changes = changes.concat(await getMetaDiff(sqlClient, project, base));
} catch (e) {
console.log(e);
@ -563,7 +563,7 @@ export async function baseMetaDiff(req, res) {
const base = await Base.get(req.params.baseId);
let changes = [];
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
changes = await getMetaDiff(sqlClient, project, base);
res.json(changes);
@ -575,7 +575,7 @@ export async function metaDiffSync(req, res) {
const virtualColumnInsert: Array<() => Promise<void>> = [];
// @ts-ignore
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
const changes = await getMetaDiff(sqlClient, project, base);
/* Get all relations */
@ -778,7 +778,7 @@ export async function baseMetaDiffSync(req, res) {
const virtualColumnInsert: Array<() => Promise<void>> = [];
// @ts-ignore
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
const changes = await getMetaDiff(sqlClient, project, base);
/* Get all relations */

4
packages/nocodb/src/lib/meta/api/projectApis.ts

@ -213,7 +213,7 @@ async function populateMeta(base: Base, project: Project): Promise<any> {
};
const t = process.hrtime();
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
let order = 1;
const models2: { [tableName: string]: Model } = {};
@ -467,7 +467,7 @@ export async function projectCost(req, res) {
const project = await Project.getWithInfo(req.params.projectId);
for (const base of project.bases) {
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
const userCount = await ProjectUser.getUsersCount(req.query);
const recordCount = (await sqlClient.totalRecords())?.data.TotalRecords;

4
packages/nocodb/src/lib/meta/api/tableApis.ts

@ -148,7 +148,7 @@ export async function tableCreate(req: Request<any, any, TableReqType>, res) {
}
const sqlMgr = await ProjectMgrv2.getSqlMgr(project);
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
let tableNameLengthLimit = 255;
const sqlClientType = sqlClient.clientType;
@ -295,7 +295,7 @@ export async function tableUpdate(req: Request<any, any>, res) {
}
const sqlMgr = await ProjectMgrv2.getSqlMgr(project);
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
let tableNameLengthLimit = 255;
const sqlClientType = sqlClient.clientType;

4
packages/nocodb/src/lib/meta/api/utilApis.ts

@ -320,8 +320,8 @@ export async function aggregatedMetaInfo(_req: Request, res: Response) {
project.getBases().then(async (bases) => {
return extractResultOrNull(
await Promise.allSettled(
bases.map((base) =>
NcConnectionMgrv2.getSqlClient(base)
bases.map(async (base) =>
(await NcConnectionMgrv2.getSqlClient(base))
.totalRecords?.()
?.then((result) => result?.data)
)

2
packages/nocodb/src/lib/meta/handlers/xcMetaDiff.ts

@ -42,7 +42,7 @@ export default async function (
const builder = this.getBuilder(args);
// @ts-ignore
const sqlClient = this.projectGetSqlClient(args);
const sqlClient = await this.projectGetSqlClient(args);
// @ts-ignore
const tableList = (await sqlClient.tableList())?.data?.list?.filter((t) => {

4
packages/nocodb/src/lib/utils/NcConfigFactory.ts

@ -584,7 +584,7 @@ export default class NcConfigFactory implements NcConfig {
public static async metaDbCreateIfNotExist(args: NcConfig) {
if (args.meta?.db?.client === 'sqlite3') {
const metaSqlClient = SqlClientFactory.create({
const metaSqlClient = await SqlClientFactory.create({
...args.meta.db,
connection: args.meta.db,
});
@ -592,7 +592,7 @@ export default class NcConfigFactory implements NcConfig {
database: args.meta.db?.connection?.filename,
});
} else {
const metaSqlClient = SqlClientFactory.create(args.meta.db);
const metaSqlClient = await SqlClientFactory.create(args.meta.db);
await metaSqlClient.createDatabaseIfNotExists(args.meta.db?.connection);
await metaSqlClient.knex.destroy();
}

8
packages/nocodb/src/lib/utils/common/BaseApiBuilder.ts

@ -187,8 +187,8 @@ export default abstract class BaseApiBuilder<T extends Noco>
return this.connectionConfig?.meta?.dbAlias;
}
public getSqlClient(): any {
return NcConnectionMgr.getSqlClient({
public async getSqlClient(): Promise<any> {
return await NcConnectionMgr.getSqlClient({
dbAlias: this.dbAlias,
env: this.config.env,
config: this.config,
@ -1676,7 +1676,7 @@ export default abstract class BaseApiBuilder<T extends Noco>
config: this.config,
projectId: this.projectId,
});
this.sqlClient = NcConnectionMgr.getSqlClient({
this.sqlClient = await NcConnectionMgr.getSqlClient({
dbAlias: this.dbAlias,
env: this.config.env,
config: this.config,
@ -3064,7 +3064,7 @@ export default abstract class BaseApiBuilder<T extends Noco>
);
const colListRef = {};
const tableList =
(await this.getSqlClient()?.tableList())?.data?.list || [];
(await (await this.getSqlClient())?.tableList())?.data?.list || [];
colListRef[tableName] = await this.getColumnList(tableName);

6
packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts

@ -141,7 +141,7 @@ export default class NcConnectionMgr {
return config?.envs?.[env]?.db?.find((db) => db?.meta?.dbAlias === dbAlias);
}
public static getSqlClient({
public static async getSqlClient({
projectId,
dbAlias = 'db',
env = '_noco',
@ -151,14 +151,14 @@ export default class NcConnectionMgr {
env: string;
config: NcConfig;
projectId: string;
}): any {
}): Promise<any> {
const knex = this.get({
dbAlias,
env,
config,
projectId,
});
return SqlClientFactory.create({
return await SqlClientFactory.create({
knex,
...this.getConnectionConfig(config, env, dbAlias),
});

4
packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts

@ -149,9 +149,9 @@ export default class NcConnectionMgrv2 {
// return config?.envs?.[env]?.db?.find(db => db?.meta?.dbAlias === dbAlias);
// }
public static getSqlClient(base: Base, _knex = null): any {
public static async getSqlClient(base: Base, _knex = null): Promise<any> {
const knex = _knex || this.get(base);
return SqlClientFactory.create({
return await SqlClientFactory.create({
knex,
...base.getConnectionConfig(),
});

6
packages/nocodb/src/lib/utils/common/handlers/xcMetaDiffSync.ts

@ -10,8 +10,8 @@ import { GqlApiBuilder } from '../../../v1-legacy/gql/GqlApiBuilder';
export default async function (this: BaseApiBuilder<any> | any) {
const changes: Array<NcMetaDiffType> = await xcMetaDiff.call(
{
projectGetSqlClient: () => {
return this.getSqlClient();
projectGetSqlClient: async () => {
return await this.getSqlClient();
},
getProjectId: () => this.getProjectId(),
getDbAlias: () => this.getDbAlias(),
@ -57,7 +57,7 @@ export default async function (this: BaseApiBuilder<any> | any) {
return true;
});
// @ts-ignore
const relationList = (await this.getSqlClient().tableList())?.data?.list;
const relationList = (await (await this.getSqlClient()).tableList())?.data?.list;
const oldModels = await this.xcMeta.metaList(
this.getProjectId(),

4
packages/nocodb/src/lib/v1-legacy/NcProjectBuilder.ts

@ -626,7 +626,7 @@ export default class NcProjectBuilder {
i++;
} else if (db.meta?.allSchemas) {
/* get all schemas and create APIs for all of them */
const sqlClient = SqlClientFactory.create({
const sqlClient = await SqlClientFactory.create({
...db,
connection: { ...db.connection, database: undefined },
});
@ -697,7 +697,7 @@ export default class NcProjectBuilder {
for (const connectionConfig of dbs) {
try {
const sqlClient = NcConnectionMgr.getSqlClient({
const sqlClient = await NcConnectionMgr.getSqlClient({
dbAlias: connectionConfig?.meta?.dbAlias,
env: this.config.env,
config: this.config,

Loading…
Cancel
Save