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
@@ -2,6 +2,7 @@ import { request } from "/src/api/service";
const apiPrefix = "/pi/pipeline";
const historyApiPrefix = "/pi/history";
const certApiPrefix = "/pi/cert";
export async function GetList(query: any) {
return await request({
@@ -82,3 +83,19 @@ export async function GetFiles(pipelineId: number) {
params: { pipelineId }
});
}
export type CertInfo = {
crt: string;
key: string;
ic: string;
der: string;
pfx: string;
};
export async function GetCert(pipelineId: number): Promise<CertInfo> {
return await request({
url: certApiPrefix + "/get",
method: "post",
params: { id: pipelineId }
});
}
@@ -148,6 +148,69 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp
router.push({ path: "/certd/pipeline/detail", query: { id, editMode: "true" } });
});
}
const viewCert = async (row: any) => {
const cert = await api.GetCert(row.id);
if (!cert) {
notification.error({ message: "还没有产生证书,请先运行流水线" });
return;
}
Modal.success({
title: "查看证书",
maskClosable: true,
okText: "关闭",
content: () => {
return (
<div class={"view-cert"}>
<div class={"cert-txt"}>
<h3>fullchain.pem</h3>
<div>{cert.crt}</div>
</div>
<div class={"cert-txt"}>
<h3>private.pem</h3>
<div>{cert.key}</div>
</div>
<div class={"cert-txt"}>
<h3>.pem</h3>
<div>{cert.ic}</div>
</div>
</div>
);
}
});
};
const downloadCert = async (row: any) => {
const files = await api.GetFiles(row.id);
Modal.success({
title: "文件下载",
maskClosable: true,
okText: "↑↑↑ 点击上面链接下载",
content: () => {
const children = [];
for (const file of files) {
const downloadUrl = `${env.API}/pi/history/download?pipelineId=${row.id}&fileId=${file.id}`;
children.push(
<div>
<div>
<a href={downloadUrl} target={"_blank"}>
{file.filename}
</a>
</div>
<div>
<a-button type={"primary"} onClick={viewCert}>
</a-button>
</div>
</div>
);
}
return <div class={"mt-3"}>{children}</div>;
}
});
};
const userStore = useUserStore();
const settingStore = useSettingStore();
return {
@@ -243,27 +306,7 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp
type: "link",
icon: "ant-design:download-outlined",
async click({ row }) {
const files = await api.GetFiles(row.id);
Modal.success({
title: "文件下载",
maskClosable: true,
okText: "↑↑↑ 点击上面链接下载",
content: () => {
const children = [];
for (const file of files) {
const downloadUrl = `${env.API}/pi/history/download?pipelineId=${row.id}&fileId=${file.id}`;
children.push(
<p>
<a href={downloadUrl} target={"_blank"}>
{file.filename}
</a>
</p>
);
}
return <div class={"mt-3"}>{children}</div>;
}
});
downloadCert(row);
}
},
remove: {
@@ -0,0 +1,21 @@
import { Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { PipelineService } from '../../modules/pipeline/service/pipeline-service.js';
import { BaseController, Constants } from '@certd/lib-server';
import { StorageService } from '../../modules/pipeline/service/storage-service.js';
@Provide()
@Controller('/api/pi/cert')
export class CertController extends BaseController {
@Inject()
pipelineService: PipelineService;
@Inject()
storeService: StorageService;
@Post('/get', { summary: Constants.per.authOnly })
async getCert(@Query('id') id: number) {
const userId = this.getUserId();
await this.pipelineService.checkUserId(id, userId);
const privateVars = this.storeService.getPipelinePrivateVars(id);
return this.ok(privateVars);
}
}
@@ -80,8 +80,8 @@ export class SysSettingsController extends CrudController<SysSettingsService> {
@Post('/getSysSettings', { summary: 'sys:settings:edit' })
async getSysSettings() {
const publicSettings = await this.service.getPublicSettings();
const privateSettings = await this.service.getPrivateSettings();
privateSettings.removeSecret();
let privateSettings = await this.service.getPrivateSettings();
privateSettings = privateSettings.removeSecret();
return this.ok({ public: publicSettings, private: privateSettings });
}
@@ -43,6 +43,8 @@ export class AutoInitSite {
await this.sysSettingsService.saveSetting(privateInfo);
}
await this.sysSettingsService.backupSecret();
await this.sysSettingsService.reloadPrivateSettings();
// 授权许可
@@ -44,6 +44,9 @@ export class StorageService extends BaseService<StorageEntity> {
}
async findPipelineVars(pipelineIds: number[]) {
if (pipelineIds == null || pipelineIds.length === 0) {
throw new Error('pipelineIds 不能为空');
}
return await this.repository.find({
where: {
scope: 'pipeline',
@@ -52,4 +55,17 @@ export class StorageService extends BaseService<StorageEntity> {
},
});
}
async getPipelinePrivateVars(pipelineId: number) {
if (pipelineId == null) {
throw new Error('pipelineId 不能为空');
}
return await this.repository.find({
where: {
scope: 'pipeline',
namespace: pipelineId + '',
key: 'privateVars',
},
});
}
}