refactor(core): Port Public API config (no-changelog) (#10082)

This commit is contained in:
Iván Ovejero 2024-07-17 15:36:40 +02:00 committed by GitHub
parent b0abee7eb6
commit 8a53d6127e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 35 additions and 34 deletions

View file

@ -0,0 +1,16 @@
import { Config, Env } from '../decorators';
@Config
export class PublicApiConfig {
/** Whether to disable the Public API */
@Env('N8N_PUBLIC_API_DISABLED')
readonly disabled: boolean = false;
/** Path segment for the Public API */
@Env('N8N_PUBLIC_API_ENDPOINT')
readonly path: string = 'api';
/** Whether to disable the Swagger UI for the Public API */
@Env('N8N_PUBLIC_API_SWAGGERUI_DISABLED')
readonly swaggerUiDisabled: boolean = false;
}

View file

@ -2,6 +2,7 @@ import { Config, Nested } from './decorators';
import { CredentialsConfig } from './configs/credentials'; import { CredentialsConfig } from './configs/credentials';
import { DatabaseConfig } from './configs/database'; import { DatabaseConfig } from './configs/database';
import { EmailConfig } from './configs/email'; import { EmailConfig } from './configs/email';
import { PublicApiConfig } from './configs/public-api';
@Config @Config
class UserManagementConfig { class UserManagementConfig {
@ -19,4 +20,7 @@ export class GlobalConfig {
@Nested @Nested
userManagement: UserManagementConfig; userManagement: UserManagementConfig;
@Nested
publicApi: PublicApiConfig;
} }

View file

@ -17,6 +17,7 @@ import { License } from '@/License';
import { UserRepository } from '@db/repositories/user.repository'; import { UserRepository } from '@db/repositories/user.repository';
import { UrlService } from '@/services/url.service'; import { UrlService } from '@/services/url.service';
import type { AuthenticatedRequest } from '@/requests'; import type { AuthenticatedRequest } from '@/requests';
import { GlobalConfig } from '@n8n/config';
async function createApiRouter( async function createApiRouter(
version: string, version: string,
@ -35,7 +36,7 @@ async function createApiRouter(
]; ];
const apiController = express.Router(); const apiController = express.Router();
if (!config.getEnv('publicApi.swaggerUi.disabled')) { if (!Container.get(GlobalConfig).publicApi.swaggerUiDisabled) {
const { serveFiles, setup } = await import('swagger-ui-express'); const { serveFiles, setup } = await import('swagger-ui-express');
const swaggerThemePath = path.join(__dirname, 'swaggerTheme.css'); const swaggerThemePath = path.join(__dirname, 'swaggerTheme.css');
const swaggerThemeCss = await fs.readFile(swaggerThemePath, { encoding: 'utf-8' }); const swaggerThemeCss = await fs.readFile(swaggerThemePath, { encoding: 'utf-8' });
@ -153,5 +154,5 @@ export const loadPublicApiVersions = async (
}; };
export function isApiEnabled(): boolean { export function isApiEnabled(): boolean {
return !config.get('publicApi.disabled') && !Container.get(License).isAPIDisabled(); return !Container.get(GlobalConfig).publicApi.disabled && !Container.get(License).isAPIDisabled();
} }

View file

@ -81,6 +81,7 @@ export class Server extends AbstractServer {
private readonly loadNodesAndCredentials: LoadNodesAndCredentials, private readonly loadNodesAndCredentials: LoadNodesAndCredentials,
private readonly orchestrationService: OrchestrationService, private readonly orchestrationService: OrchestrationService,
private readonly postHogClient: PostHogClient, private readonly postHogClient: PostHogClient,
private readonly globalConfig: GlobalConfig,
) { ) {
super('main'); super('main');
@ -96,8 +97,7 @@ export class Server extends AbstractServer {
this.presetCredentialsLoaded = false; this.presetCredentialsLoaded = false;
const globalConfig = Container.get(GlobalConfig); this.endpointPresetCredentials = this.globalConfig.credentials.overwrite.endpoint;
this.endpointPresetCredentials = globalConfig.credentials.overwrite.endpoint;
await super.start(); await super.start();
this.logger.debug(`Server ID: ${this.uniqueInstanceId}`); this.logger.debug(`Server ID: ${this.uniqueInstanceId}`);
@ -185,7 +185,7 @@ export class Server extends AbstractServer {
await this.postHogClient.init(); await this.postHogClient.init();
const publicApiEndpoint = config.getEnv('publicApi.path'); const publicApiEndpoint = this.globalConfig.publicApi.path;
// ---------------------------------------- // ----------------------------------------
// Public API // Public API

View file

@ -562,29 +562,6 @@ export const schema = {
}, },
}, },
publicApi: {
disabled: {
format: Boolean,
default: false,
env: 'N8N_PUBLIC_API_DISABLED',
doc: 'Whether to disable the Public API',
},
path: {
format: String,
default: 'api',
env: 'N8N_PUBLIC_API_ENDPOINT',
doc: 'Path for the public api endpoints',
},
swaggerUi: {
disabled: {
format: Boolean,
default: false,
env: 'N8N_PUBLIC_API_SWAGGERUI_DISABLED',
doc: 'Whether to disable the Swagger UI for the Public API',
},
},
},
workflowTagsDisabled: { workflowTagsDisabled: {
format: Boolean, format: Boolean,
default: false, default: false,

View file

@ -152,9 +152,9 @@ export class FrontendService {
publicApi: { publicApi: {
enabled: isApiEnabled(), enabled: isApiEnabled(),
latestVersion: 1, latestVersion: 1,
path: config.getEnv('publicApi.path'), path: this.globalConfig.publicApi.path,
swaggerUi: { swaggerUi: {
enabled: !config.getEnv('publicApi.swaggerUi.disabled'), enabled: !Container.get(GlobalConfig).publicApi.swaggerUiDisabled,
}, },
}, },
workflowTagsDisabled: config.getEnv('workflowTagsDisabled'), workflowTagsDisabled: config.getEnv('workflowTagsDisabled'),

View file

@ -3,7 +3,6 @@ import { IsNull } from '@n8n/typeorm';
import validator from 'validator'; import validator from 'validator';
import { randomString } from 'n8n-workflow'; import { randomString } from 'n8n-workflow';
import config from '@/config';
import type { User } from '@db/entities/User'; import type { User } from '@db/entities/User';
import { UserRepository } from '@db/repositories/user.repository'; import { UserRepository } from '@db/repositories/user.repository';
import { ProjectRepository } from '@db/repositories/project.repository'; import { ProjectRepository } from '@db/repositories/project.repository';
@ -14,12 +13,14 @@ import * as testDb from './shared/testDb';
import * as utils from './shared/utils/'; import * as utils from './shared/utils/';
import { addApiKey, createOwner, createUser, createUserShell } from './shared/db/users'; import { addApiKey, createOwner, createUser, createUserShell } from './shared/db/users';
import type { SuperAgentTest } from './shared/types'; import type { SuperAgentTest } from './shared/types';
import { mockInstance } from '@test/mocking';
import { GlobalConfig } from '@n8n/config';
const testServer = utils.setupTestServer({ endpointGroups: ['me'] }); const testServer = utils.setupTestServer({ endpointGroups: ['me'] });
beforeEach(async () => { beforeEach(async () => {
await testDb.truncate(['User']); await testDb.truncate(['User']);
config.set('publicApi.disabled', false); mockInstance(GlobalConfig, { publicApi: { disabled: false } });
}); });
describe('When public API is disabled', () => { describe('When public API is disabled', () => {
@ -30,7 +31,7 @@ describe('When public API is disabled', () => {
owner = await createOwner(); owner = await createOwner();
await addApiKey(owner); await addApiKey(owner);
authAgent = testServer.authAgentFor(owner); authAgent = testServer.authAgentFor(owner);
config.set('publicApi.disabled', true); mockInstance(GlobalConfig, { publicApi: { disabled: true } });
}); });
test('POST /me/api-key should 404', async () => { test('POST /me/api-key should 404', async () => {

View file

@ -1,8 +1,10 @@
import config from '@/config'; import config from '@/config';
import { GlobalConfig } from '@n8n/config';
import Container from 'typedi';
export const REST_PATH_SEGMENT = config.getEnv('endpoints.rest'); export const REST_PATH_SEGMENT = config.getEnv('endpoints.rest');
export const PUBLIC_API_REST_PATH_SEGMENT = config.getEnv('publicApi.path'); export const PUBLIC_API_REST_PATH_SEGMENT = Container.get(GlobalConfig).publicApi.path;
export const SUCCESS_RESPONSE_BODY = { export const SUCCESS_RESPONSE_BODY = {
data: { data: {