mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 03:02:19 +08:00
chore: move AesEncryptor to the core (#6132)
This commit is contained in:
parent
97333d0c06
commit
fda336ed31
@ -8,10 +8,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import Application from './application';
|
||||||
|
|
||||||
class AesEncryptor {
|
export class AesEncryptor {
|
||||||
private key: Buffer;
|
private key: Buffer;
|
||||||
|
|
||||||
constructor(key: Buffer) {
|
constructor(key: Buffer) {
|
||||||
@ -71,6 +72,29 @@ class AesEncryptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async getKeyPath(appName: string) {
|
||||||
|
const appKeyPath = path.resolve(process.cwd(), 'storage', 'apps', appName, 'aes_key.dat');
|
||||||
|
const appKeyExists = await fs.exists(appKeyPath);
|
||||||
|
if (appKeyExists) {
|
||||||
|
return appKeyPath;
|
||||||
|
}
|
||||||
|
const envKeyPath = path.resolve(process.cwd(), 'storage', 'environment-variables', appName, 'aes_key.dat');
|
||||||
|
const envKeyExists = await fs.exists(envKeyPath);
|
||||||
|
if (envKeyExists) {
|
||||||
|
return envKeyPath;
|
||||||
|
}
|
||||||
|
return appKeyPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async create(app: Application) {
|
||||||
|
let key: any = process.env.APP_AES_SECRET_KEY;
|
||||||
|
if (!key) {
|
||||||
|
const keyPath = await this.getKeyPath(app.name);
|
||||||
|
key = await AesEncryptor.getOrGenerateKey(keyPath);
|
||||||
|
}
|
||||||
|
return new AesEncryptor(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AesEncryptor;
|
export default AesEncryptor;
|
@ -73,10 +73,11 @@ import { createPubSubManager, PubSubManager, PubSubManagerOptions } from './pub-
|
|||||||
import { SyncMessageManager } from './sync-message-manager';
|
import { SyncMessageManager } from './sync-message-manager';
|
||||||
|
|
||||||
import packageJson from '../package.json';
|
import packageJson from '../package.json';
|
||||||
import { ServiceContainer } from './service-container';
|
|
||||||
import { availableActions } from './acl/available-action';
|
import { availableActions } from './acl/available-action';
|
||||||
|
import AesEncryptor from './aes-encryptor';
|
||||||
import { AuditManager } from './audit-manager';
|
import { AuditManager } from './audit-manager';
|
||||||
import { Environment } from './environment';
|
import { Environment } from './environment';
|
||||||
|
import { ServiceContainer } from './service-container';
|
||||||
|
|
||||||
export type PluginType = string | typeof Plugin;
|
export type PluginType = string | typeof Plugin;
|
||||||
export type PluginConfiguration = PluginType | [PluginType, any];
|
export type PluginConfiguration = PluginType | [PluginType, any];
|
||||||
@ -437,6 +438,12 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
|||||||
return this._dataSourceManager;
|
return this._dataSourceManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected _aesEncryptor: AesEncryptor;
|
||||||
|
|
||||||
|
get aesEncryptor() {
|
||||||
|
return this._aesEncryptor;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@ -623,6 +630,8 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._aesEncryptor = await AesEncryptor.create(this);
|
||||||
|
|
||||||
if (this.cacheManager) {
|
if (this.cacheManager) {
|
||||||
await this.cacheManager.close();
|
await this.cacheManager.close();
|
||||||
}
|
}
|
||||||
|
@ -7,15 +7,16 @@
|
|||||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export * from './aes-encryptor';
|
||||||
export * from './app-supervisor';
|
export * from './app-supervisor';
|
||||||
export * from './application';
|
export * from './application';
|
||||||
export { Application as default } from './application';
|
export { Application as default } from './application';
|
||||||
|
export * from './audit-manager';
|
||||||
export * from './gateway';
|
export * from './gateway';
|
||||||
export * as middlewares from './middlewares';
|
export * as middlewares from './middlewares';
|
||||||
export * from './migration';
|
export * from './migration';
|
||||||
export * from './plugin';
|
export * from './plugin';
|
||||||
export * from './plugin-manager';
|
export * from './plugin-manager';
|
||||||
export * from './audit-manager';
|
|
||||||
export * from './pub-sub-manager';
|
export * from './pub-sub-manager';
|
||||||
export const OFFICIAL_PLUGIN_PREFIX = '@nocobase/plugin-';
|
export const OFFICIAL_PLUGIN_PREFIX = '@nocobase/plugin-';
|
||||||
|
|
||||||
|
@ -8,12 +8,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Plugin } from '@nocobase/server';
|
import { Plugin } from '@nocobase/server';
|
||||||
import path from 'path';
|
|
||||||
import AesEncryptor from './AesEncryptor';
|
|
||||||
|
|
||||||
export class PluginEnvironmentVariablesServer extends Plugin {
|
export class PluginEnvironmentVariablesServer extends Plugin {
|
||||||
aesEncryptor: AesEncryptor;
|
|
||||||
updated = false;
|
updated = false;
|
||||||
|
|
||||||
|
get aesEncryptor() {
|
||||||
|
return this.app.aesEncryptor;
|
||||||
|
}
|
||||||
|
|
||||||
async handleSyncMessage(message) {
|
async handleSyncMessage(message) {
|
||||||
const { type, name, value } = message;
|
const { type, name, value } = message;
|
||||||
if (type === 'updated') {
|
if (type === 'updated') {
|
||||||
@ -27,22 +29,11 @@ export class PluginEnvironmentVariablesServer extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
this.createAesEncryptor();
|
|
||||||
this.registerACL();
|
this.registerACL();
|
||||||
this.onEnvironmentSaved();
|
this.onEnvironmentSaved();
|
||||||
await this.loadVariables();
|
await this.loadVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAesEncryptor() {
|
|
||||||
let key: any = process.env.ENV_VARS_AES_SECRET_KEY;
|
|
||||||
if (!key) {
|
|
||||||
key = await AesEncryptor.getOrGenerateKey(
|
|
||||||
path.resolve(process.cwd(), 'storage', this.name, this.app.name, 'aes_key.dat'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
this.aesEncryptor = new AesEncryptor(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
registerACL() {
|
registerACL() {
|
||||||
this.app.acl.allow('environmentVariables', 'list', 'loggedIn');
|
this.app.acl.allow('environmentVariables', 'list', 'loggedIn');
|
||||||
this.app.acl.registerSnippet({
|
this.app.acl.registerSnippet({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user