refactor(core): Port external storage config (no-changelog) (#10169)

This commit is contained in:
Iván Ovejero 2024-07-24 13:08:20 +02:00 committed by GitHub
parent f1a3791abc
commit 66c49bb6a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 71 additions and 60 deletions

View file

@ -0,0 +1,42 @@
import { Config, Env, Nested } from '../decorators';
@Config
class S3BucketConfig {
/** Name of the n8n bucket in S3-compatible external storage */
@Env('N8N_EXTERNAL_STORAGE_S3_BUCKET_NAME')
readonly name: string = '';
/** Region of the n8n bucket in S3-compatible external storage @example "us-east-1" */
@Env('N8N_EXTERNAL_STORAGE_S3_BUCKET_REGION')
readonly region: string = '';
}
@Config
class S3CredentialsConfig {
/** Access key in S3-compatible external storage */
@Env('N8N_EXTERNAL_STORAGE_S3_ACCESS_KEY')
readonly accessKey: string = '';
/** Access secret in S3-compatible external storage */
@Env('N8N_EXTERNAL_STORAGE_S3_ACCESS_SECRET')
readonly accessSecret: string = '';
}
@Config
class S3Config {
/** Host of the n8n bucket in S3-compatible external storage @example "s3.us-east-1.amazonaws.com" */
@Env('N8N_EXTERNAL_STORAGE_S3_HOST')
readonly host: string = '';
@Nested
readonly bucket: S3BucketConfig;
@Nested
readonly credentials: S3CredentialsConfig;
}
@Config
export class ExternalStorageConfig {
@Nested
readonly s3: S3Config;
}

View file

@ -8,6 +8,7 @@ import { ExternalSecretsConfig } from './configs/external-secrets';
import { TemplatesConfig } from './configs/templates'; import { TemplatesConfig } from './configs/templates';
import { EventBusConfig } from './configs/event-bus'; import { EventBusConfig } from './configs/event-bus';
import { NodesConfig } from './configs/nodes'; import { NodesConfig } from './configs/nodes';
import { ExternalStorageConfig } from './configs/external-storage';
@Config @Config
class UserManagementConfig { class UserManagementConfig {
@ -18,29 +19,32 @@ class UserManagementConfig {
@Config @Config
export class GlobalConfig { export class GlobalConfig {
@Nested @Nested
database: DatabaseConfig; readonly database: DatabaseConfig;
@Nested @Nested
credentials: CredentialsConfig; readonly credentials: CredentialsConfig;
@Nested @Nested
userManagement: UserManagementConfig; readonly userManagement: UserManagementConfig;
@Nested @Nested
versionNotifications: VersionNotificationsConfig; readonly versionNotifications: VersionNotificationsConfig;
@Nested @Nested
publicApi: PublicApiConfig; readonly publicApi: PublicApiConfig;
@Nested @Nested
externalSecrets: ExternalSecretsConfig; readonly externalSecrets: ExternalSecretsConfig;
@Nested @Nested
templates: TemplatesConfig; readonly templates: TemplatesConfig;
@Nested @Nested
eventBus: EventBusConfig; readonly eventBus: EventBusConfig;
@Nested @Nested
readonly nodes: NodesConfig; readonly nodes: NodesConfig;
@Nested
readonly externalStorage: ExternalStorageConfig;
} }

View file

@ -122,6 +122,19 @@ describe('GlobalConfig', () => {
endpoint: 'https://api.n8n.io/api/versions/', endpoint: 'https://api.n8n.io/api/versions/',
infoUrl: 'https://docs.n8n.io/hosting/installation/updating/', infoUrl: 'https://docs.n8n.io/hosting/installation/updating/',
}, },
externalStorage: {
s3: {
host: '',
bucket: {
name: '',
region: '',
},
credentials: {
accessKey: '',
accessSecret: '',
},
},
},
}; };
it('should use all default values when no env variables are defined', () => { it('should use all default values when no env variables are defined', () => {

View file

@ -44,6 +44,8 @@ export abstract class BaseCommand extends Command {
protected license: License; protected license: License;
private globalConfig = Container.get(GlobalConfig);
/** /**
* How long to wait for graceful shutdown before force killing the process. * How long to wait for graceful shutdown before force killing the process.
*/ */
@ -79,8 +81,7 @@ export abstract class BaseCommand extends Command {
await this.exitWithCrash('There was an error running database migrations', error), await this.exitWithCrash('There was an error running database migrations', error),
); );
const globalConfig = Container.get(GlobalConfig); const { type: dbType } = this.globalConfig.database;
const { type: dbType } = globalConfig.database;
if (['mysqldb', 'mariadb'].includes(dbType)) { if (['mysqldb', 'mariadb'].includes(dbType)) {
this.logger.warn( this.logger.warn(
@ -199,7 +200,7 @@ export abstract class BaseCommand extends Command {
private async _initObjectStoreService(options = { isReadOnly: false }) { private async _initObjectStoreService(options = { isReadOnly: false }) {
const objectStoreService = Container.get(ObjectStoreService); const objectStoreService = Container.get(ObjectStoreService);
const host = config.getEnv('externalStorage.s3.host'); const { host, bucket, credentials } = this.globalConfig.externalStorage.s3;
if (host === '') { if (host === '') {
throw new ApplicationError( throw new ApplicationError(
@ -207,11 +208,6 @@ export abstract class BaseCommand extends Command {
); );
} }
const bucket = {
name: config.getEnv('externalStorage.s3.bucket.name'),
region: config.getEnv('externalStorage.s3.bucket.region'),
};
if (bucket.name === '') { if (bucket.name === '') {
throw new ApplicationError( throw new ApplicationError(
'External storage bucket name not configured. Please set `N8N_EXTERNAL_STORAGE_S3_BUCKET_NAME`.', 'External storage bucket name not configured. Please set `N8N_EXTERNAL_STORAGE_S3_BUCKET_NAME`.',
@ -224,11 +220,6 @@ export abstract class BaseCommand extends Command {
); );
} }
const credentials = {
accessKey: config.getEnv('externalStorage.s3.credentials.accessKey'),
accessSecret: config.getEnv('externalStorage.s3.credentials.accessSecret'),
};
if (credentials.accessKey === '') { if (credentials.accessKey === '') {
throw new ApplicationError( throw new ApplicationError(
'External storage access key not configured. Please set `N8N_EXTERNAL_STORAGE_S3_ACCESS_KEY`.', 'External storage access key not configured. Please set `N8N_EXTERNAL_STORAGE_S3_ACCESS_KEY`.',

View file

@ -671,45 +671,6 @@ export const schema = {
}, },
}, },
externalStorage: {
s3: {
host: {
format: String,
default: '',
env: 'N8N_EXTERNAL_STORAGE_S3_HOST',
doc: 'Host of the n8n bucket in S3-compatible external storage, e.g. `s3.us-east-1.amazonaws.com`',
},
bucket: {
name: {
format: String,
default: '',
env: 'N8N_EXTERNAL_STORAGE_S3_BUCKET_NAME',
doc: 'Name of the n8n bucket in S3-compatible external storage',
},
region: {
format: String,
default: '',
env: 'N8N_EXTERNAL_STORAGE_S3_BUCKET_REGION',
doc: 'Region of the n8n bucket in S3-compatible external storage, e.g. `us-east-1`',
},
},
credentials: {
accessKey: {
format: String,
default: '',
env: 'N8N_EXTERNAL_STORAGE_S3_ACCESS_KEY',
doc: 'Access key in S3-compatible external storage',
},
accessSecret: {
format: String,
default: '',
env: 'N8N_EXTERNAL_STORAGE_S3_ACCESS_SECRET',
doc: 'Access secret in S3-compatible external storage',
},
},
},
},
deployment: { deployment: {
type: { type: {
format: String, format: String,