Browse Source

fix: asynchronous ssl config read

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
2e5ec1ed18
  1. 2
      packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts
  2. 8
      packages/nocodb/src/lib/utils/common/BaseApiBuilder.ts
  3. 25
      packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts
  4. 14
      packages/nocodb/src/lib/utils/common/XcProcedure.ts
  5. 2
      packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts
  6. 2
      packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

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

@ -299,7 +299,7 @@ export default class SqlMgr {
this.currentProjectConnections[connectionKey] = this.currentProjectConnections[connectionKey] =
SqlClientFactory.create({ SqlClientFactory.create({
...connectionConfig, ...connectionConfig,
knex: NcConnectionMgr.get({ knex: await NcConnectionMgr.get({
dbAlias: this.currentProjectJson.envs[env].db[i].meta.dbAlias, dbAlias: this.currentProjectJson.envs[env].db[i].meta.dbAlias,
env: env, env: env,
config: args, config: args,

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

@ -1291,8 +1291,8 @@ export default abstract class BaseApiBuilder<T extends Noco>
this.models[viewName] = this.getBaseModel(newMeta); this.models[viewName] = this.getBaseModel(newMeta);
} }
public getDbDriver(): XKnex { public async getDbDriver(): Promise<XKnex> {
this.initDbDriver(); await this.initDbDriver();
return this.dbDriver; return this.dbDriver;
} }
@ -1669,8 +1669,8 @@ export default abstract class BaseApiBuilder<T extends Noco>
await this.cronJob.init(); await this.cronJob.init();
} }
protected initDbDriver(): void { protected async initDbDriver(): Promise<void> {
this.dbDriver = NcConnectionMgr.get({ this.dbDriver = await NcConnectionMgr.get({
dbAlias: this.dbAlias, dbAlias: this.dbAlias,
env: this.config.env, env: this.config.env,
config: this.config, config: this.config,

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

@ -2,11 +2,14 @@ import SqlClientFactory from '../../db/sql-client/lib/SqlClientFactory';
import { XKnex } from '../../db/sql-data-mapper'; import { XKnex } from '../../db/sql-data-mapper';
import { NcConfig } from '../../../interface/config'; import { NcConfig } from '../../../interface/config';
import fs from 'fs'; import fs from 'fs';
import { promisify } from 'util';
import { Knex } from 'knex'; import { Knex } from 'knex';
import NcMetaIO from '../../meta/NcMetaIO'; import NcMetaIO from '../../meta/NcMetaIO';
import { defaultConnectionConfig } from '../NcConfigFactory'; import { defaultConnectionConfig } from '../NcConfigFactory';
const readFileAsync = promisify(fs.readFile);
export default class NcConnectionMgr { export default class NcConnectionMgr {
private static connectionRefs: { private static connectionRefs: {
[projectId: string]: { [projectId: string]: {
@ -43,7 +46,7 @@ export default class NcConnectionMgr {
} }
} }
public static get({ public static async get({
dbAlias = 'db', dbAlias = 'db',
env = '_noco', env = '_noco',
config, config,
@ -53,7 +56,7 @@ export default class NcConnectionMgr {
env: string; env: string;
config: NcConfig; config: NcConfig;
projectId: string; projectId: string;
}): XKnex { }): Promise<XKnex> {
if (this.connectionRefs?.[projectId]?.[env]?.[dbAlias]) { if (this.connectionRefs?.[projectId]?.[env]?.[dbAlias]) {
return this.connectionRefs?.[projectId]?.[env]?.[dbAlias]; return this.connectionRefs?.[projectId]?.[env]?.[dbAlias];
} }
@ -73,25 +76,25 @@ export default class NcConnectionMgr {
connectionConfig.connection.ssl.caFilePath && connectionConfig.connection.ssl.caFilePath &&
!connectionConfig.connection.ssl.ca !connectionConfig.connection.ssl.ca
) { ) {
connectionConfig.connection.ssl.ca = fs connectionConfig.connection.ssl.ca = await readFileAsync(
.readFileSync(connectionConfig.connection.ssl.caFilePath) connectionConfig.connection.ssl.caFilePath
.toString(); ).toString();
} }
if ( if (
connectionConfig.connection.ssl.keyFilePath && connectionConfig.connection.ssl.keyFilePath &&
!connectionConfig.connection.ssl.key !connectionConfig.connection.ssl.key
) { ) {
connectionConfig.connection.ssl.key = fs connectionConfig.connection.ssl.key = await readFileAsync(
.readFileSync(connectionConfig.connection.ssl.keyFilePath) connectionConfig.connection.ssl.keyFilePath
.toString(); ).toString();
} }
if ( if (
connectionConfig.connection.ssl.certFilePath && connectionConfig.connection.ssl.certFilePath &&
!connectionConfig.connection.ssl.cert !connectionConfig.connection.ssl.cert
) { ) {
connectionConfig.connection.ssl.cert = fs connectionConfig.connection.ssl.cert = await readFileAsync(
.readFileSync(connectionConfig.connection.ssl.certFilePath) connectionConfig.connection.ssl.certFilePath
.toString(); ).toString();
} }
} }

14
packages/nocodb/src/lib/utils/common/XcProcedure.ts

@ -10,8 +10,8 @@ export default class XcProcedure {
public async callFunction(name: string, args: any[]) { public async callFunction(name: string, args: any[]) {
try { try {
if (this.builder.getDbType() === 'mssql') { if (this.builder.getDbType() === 'mssql') {
const result = await this.builder const result = await (await this.builder
.getDbDriver() .getDbDriver())
.raw( .raw(
`select dbo.??(${new Array(args.length) `select dbo.??(${new Array(args.length)
.fill('?') .fill('?')
@ -20,8 +20,8 @@ export default class XcProcedure {
); );
return result[0]; return result[0];
} else { } else {
const result = await this.builder const result = await (await this.builder
.getDbDriver() .getDbDriver())
.raw( .raw(
`select ??(${new Array(args.length).fill('?').join(',')}) as ??`, `select ??(${new Array(args.length).fill('?').join(',')}) as ??`,
[name, ...args, name] [name, ...args, name]
@ -65,7 +65,7 @@ export default class XcProcedure {
) { ) {
const knexRef = args.reduce( const knexRef = args.reduce(
(knex, val, i) => knex.raw(`SET @var${i}=?`, [val]), (knex, val, i) => knex.raw(`SET @var${i}=?`, [val]),
this.builder.getDbDriver().schema (await this.builder.getDbDriver()).schema
); );
const count = args.length; const count = args.length;
const result = await knexRef.raw( const result = await knexRef.raw(
@ -76,8 +76,8 @@ export default class XcProcedure {
); );
return [result[count][0][0]]; return [result[count][0][0]];
} else if (this.builder.getDbType() === 'pg') { } else if (this.builder.getDbType() === 'pg') {
const result = await this.builder const result = await (await this.builder
.getDbDriver() .getDbDriver())
.raw(`Call ??(${new Array(args.length).fill('?').join(',')})`, [ .raw(`Call ??(${new Array(args.length).fill('?').join(',')})`, [
name, name,
...args, ...args,

2
packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts

@ -249,7 +249,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
const t = process.hrtime(); const t = process.hrtime();
this.initDbDriver(); await this.initDbDriver();
// todo: change condition // todo: change condition
if (this.connectionConfig.meta.reset) { if (this.connectionConfig.meta.reset) {

2
packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

@ -86,7 +86,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.log('loadRoutes'); this.log('loadRoutes');
const t = process.hrtime(); const t = process.hrtime();
this.initDbDriver(); await this.initDbDriver();
// todo: change condition // todo: change condition
if (this.connectionConfig.meta.reset) { if (this.connectionConfig.meta.reset) {

Loading…
Cancel
Save