perf: 密钥备份

This commit is contained in:
xiaojunnuo
2024-10-15 12:59:40 +08:00
parent 28bb4856be
commit 1c6028abcf
12 changed files with 180 additions and 30 deletions
@@ -1,3 +1,5 @@
import { cloneDeep } from 'lodash-es';
export class BaseSettings {
static __key__: string;
static __title__: string;
@@ -29,8 +31,10 @@ export class SysPrivateSettings extends BaseSettings {
httpProxy? = '';
removeSecret() {
delete this.jwtKey;
delete this.encryptSecret;
const clone = cloneDeep(this);
delete clone.jwtKey;
delete clone.encryptSecret;
return clone;
}
}
@@ -83,6 +87,14 @@ export class SysSiteInfo extends BaseSettings {
loginLogo?: string;
}
export class SysSecretBackup extends BaseSettings {
static __title__ = '密钥信息备份';
static __key__ = 'sys.secret';
static __access__ = 'private';
siteId?: string;
encryptSecret?: string;
}
export class SysSiteEnv {
agent?: {
enabled?: boolean;
@@ -3,10 +3,10 @@ import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { SysSettingsEntity } from '../entity/sys-settings.js';
import { CacheManager } from '@midwayjs/cache';
import { BaseSettings, SysPrivateSettings, SysPublicSettings } from './models.js';
import { BaseSettings, SysInstallInfo, SysPrivateSettings, SysPublicSettings, SysSecretBackup } from './models.js';
import * as _ from 'lodash-es';
import { BaseService } from '../../../basic/index.js';
import { setGlobalProxy } from '@certd/basic';
import { logger, setGlobalProxy } from '@certd/basic';
/**
* 设置
@@ -146,4 +146,21 @@ export class SysSettingsService extends BaseService<SysSettingsEntity> {
}
await this.cache.del(`settings.${key}`);
}
async backupSecret() {
const settings = await this.getSettingByKey(SysSecretBackup.__key__);
if (settings == null) {
const backup = new SysSecretBackup();
const privateSettings = await this.getPrivateSettings();
const installInfo = await this.getSetting<SysInstallInfo>(SysInstallInfo);
if (installInfo.siteId == null || privateSettings.encryptSecret == null) {
logger.error('备份密钥失败,siteId或encryptSecret为空');
return;
}
backup.siteId = installInfo.siteId;
backup.encryptSecret = privateSettings.encryptSecret;
await this.saveSetting(backup);
logger.info('备份密钥成功');
}
}
}