mirror of https://github.com/certd/certd
chore: 禁止普通用户使用不安全插件,比如复制到本机、自定义js脚本等
parent
5aa06f5b07
commit
4fcaab5feb
|
@ -1,7 +1,7 @@
|
||||||
import { ConcurrencyStrategy, NotificationWhen, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../dt/index.js";
|
import { ConcurrencyStrategy, NotificationWhen, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../dt/index.js";
|
||||||
import _ from "lodash-es";
|
import _ from "lodash-es";
|
||||||
import { RunHistory, RunnableCollection } from "./run-history.js";
|
import { RunHistory, RunnableCollection } from "./run-history.js";
|
||||||
import { AbstractTaskPlugin, PluginDefine, pluginRegistry, TaskInstanceContext } from "../plugin/index.js";
|
import { AbstractTaskPlugin, PluginDefine, pluginRegistry, TaskInstanceContext, UserInfo } from "../plugin/index.js";
|
||||||
import { ContextFactory, IContext } from "./context.js";
|
import { ContextFactory, IContext } from "./context.js";
|
||||||
import { IStorage } from "./storage.js";
|
import { IStorage } from "./storage.js";
|
||||||
import { logger } from "../utils/util.log.js";
|
import { logger } from "../utils/util.log.js";
|
||||||
|
@ -16,13 +16,13 @@ import { hashUtils, utils } from "../utils/index.js";
|
||||||
// import { TimeoutPromise } from "../utils/util.promise.js";
|
// import { TimeoutPromise } from "../utils/util.promise.js";
|
||||||
|
|
||||||
export type ExecutorOptions = {
|
export type ExecutorOptions = {
|
||||||
userId: any;
|
|
||||||
pipeline: Pipeline;
|
pipeline: Pipeline;
|
||||||
storage: IStorage;
|
storage: IStorage;
|
||||||
onChanged: (history: RunHistory) => Promise<void>;
|
onChanged: (history: RunHistory) => Promise<void>;
|
||||||
accessService: IAccessService;
|
accessService: IAccessService;
|
||||||
emailService: IEmailService;
|
emailService: IEmailService;
|
||||||
fileRootDir?: string;
|
fileRootDir?: string;
|
||||||
|
user: UserInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class Executor {
|
export class Executor {
|
||||||
|
@ -46,7 +46,7 @@ export class Executor {
|
||||||
this.onChanged = async (history: RunHistory) => {
|
this.onChanged = async (history: RunHistory) => {
|
||||||
await options.onChanged(history);
|
await options.onChanged(history);
|
||||||
};
|
};
|
||||||
this.pipeline.userId = options.userId;
|
this.pipeline.userId = options.user.id;
|
||||||
this.contextFactory = new ContextFactory(options.storage);
|
this.contextFactory = new ContextFactory(options.storage);
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.pipelineContext = this.contextFactory.getContext("pipeline", this.pipeline.id);
|
this.pipelineContext = this.contextFactory.getContext("pipeline", this.pipeline.id);
|
||||||
|
@ -269,7 +269,7 @@ export class Executor {
|
||||||
accessService: this.options.accessService,
|
accessService: this.options.accessService,
|
||||||
emailService: this.options.emailService,
|
emailService: this.options.emailService,
|
||||||
pipelineContext: this.pipelineContext,
|
pipelineContext: this.pipelineContext,
|
||||||
userContext: this.contextFactory.getContext("user", this.options.userId),
|
userContext: this.contextFactory.getContext("user", this.options.user.id),
|
||||||
fileStore: new FileStore({
|
fileStore: new FileStore({
|
||||||
scope: this.pipeline.id,
|
scope: this.pipeline.id,
|
||||||
parent: this.runtime.id,
|
parent: this.runtime.id,
|
||||||
|
@ -277,6 +277,7 @@ export class Executor {
|
||||||
}),
|
}),
|
||||||
signal: this.abort.signal,
|
signal: this.abort.signal,
|
||||||
utils,
|
utils,
|
||||||
|
user: this.options.user,
|
||||||
};
|
};
|
||||||
instance.setCtx(taskCtx);
|
instance.setCtx(taskCtx);
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,10 @@ import { ILogger, logger, utils } from "../utils/index.js";
|
||||||
import { HttpClient } from "../utils/util.request";
|
import { HttpClient } from "../utils/util.request";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import _ from "lodash-es";
|
import _ from "lodash-es";
|
||||||
|
export type UserInfo = {
|
||||||
|
role: "admin" | "user";
|
||||||
|
id: any;
|
||||||
|
};
|
||||||
export enum ContextScope {
|
export enum ContextScope {
|
||||||
global,
|
global,
|
||||||
pipeline,
|
pipeline,
|
||||||
|
@ -81,6 +84,8 @@ export type TaskInstanceContext = {
|
||||||
signal: AbortSignal;
|
signal: AbortSignal;
|
||||||
//工具类
|
//工具类
|
||||||
utils: typeof utils;
|
utils: typeof utils;
|
||||||
|
|
||||||
|
user: UserInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
export abstract class AbstractTaskPlugin implements ITaskPlugin {
|
export abstract class AbstractTaskPlugin implements ITaskPlugin {
|
||||||
|
@ -170,6 +175,10 @@ export abstract class AbstractTaskPlugin implements ITaskPlugin {
|
||||||
}
|
}
|
||||||
throw new Error(`action ${req.action} not found`);
|
throw new Error(`action ${req.action} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isAdmin() {
|
||||||
|
return this.ctx.user.role === "admin";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type OutputVO = {
|
export type OutputVO = {
|
||||||
|
|
|
@ -4,11 +4,19 @@
|
||||||
<div class="title">权限管理</div>
|
<div class="title">权限管理</div>
|
||||||
</template>
|
</template>
|
||||||
<fs-crud ref="crudRef" v-bind="crudBinding">
|
<fs-crud ref="crudRef" v-bind="crudBinding">
|
||||||
<a-button v-permission="'sys:auth:per:add'" style="margin-left: 20px" @click="addHandle({})">
|
<a-button v-permission="'1sys:auth:per:add'" style="margin-left: 20px" @click="addHandle({})">
|
||||||
<fs-icon :icon="ui.icons.add"></fs-icon>
|
<fs-icon :icon="ui.icons.add"></fs-icon>
|
||||||
添加
|
添加
|
||||||
</a-button>
|
</a-button>
|
||||||
<fs-permission-tree class="permission-tree mt-10" :tree="crudBinding.data" :checkable="false" :actions="permission" @add="addHandle" @edit="editHandle" @remove="removeHandle"></fs-permission-tree>
|
<fs-permission-tree
|
||||||
|
class="permission-tree mt-10"
|
||||||
|
:tree="crudBinding.data"
|
||||||
|
:checkable="false"
|
||||||
|
:actions="permission"
|
||||||
|
@add="addHandle"
|
||||||
|
@edit="editHandle"
|
||||||
|
@remove="removeHandle"
|
||||||
|
></fs-permission-tree>
|
||||||
</fs-crud>
|
</fs-crud>
|
||||||
</fs-page>
|
</fs-page>
|
||||||
</template>
|
</template>
|
||||||
|
@ -49,9 +57,9 @@ export default defineComponent({
|
||||||
|
|
||||||
const { hasPermissions } = usePermission();
|
const { hasPermissions } = usePermission();
|
||||||
const permission = ref({
|
const permission = ref({
|
||||||
add: hasPermissions("sys:auth:per:add"),
|
add: hasPermissions("1sys:auth:per:add"),
|
||||||
edit: hasPermissions("sys:auth:per:edit"),
|
edit: hasPermissions("1sys:auth:per:edit"),
|
||||||
remove: hasPermissions("sys:auth:per:remove")
|
remove: hasPermissions("1sys:auth:per:remove")
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -1,12 +1,4 @@
|
||||||
import {
|
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
|
||||||
ALL,
|
|
||||||
Body,
|
|
||||||
Controller,
|
|
||||||
Inject,
|
|
||||||
Post,
|
|
||||||
Provide,
|
|
||||||
Query,
|
|
||||||
} from '@midwayjs/core';
|
|
||||||
import { CrudController } from '../../../basic/crud-controller.js';
|
import { CrudController } from '../../../basic/crud-controller.js';
|
||||||
import { PermissionService } from '../service/permission-service.js';
|
import { PermissionService } from '../service/permission-service.js';
|
||||||
|
|
||||||
|
|
|
@ -16,4 +16,6 @@ export class UserRoleService extends BaseService<UserRoleEntity> {
|
||||||
getRepository() {
|
getRepository() {
|
||||||
return this.repository;
|
return this.repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -204,4 +204,16 @@ export class UserService extends BaseService<UserEntity> {
|
||||||
}
|
}
|
||||||
await super.delete(ids);
|
await super.delete(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async isAdmin(userId: any) {
|
||||||
|
const userRoles = await this.userRoleService.find({
|
||||||
|
where: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const roleIds = userRoles.map(item => item.roleId);
|
||||||
|
if (roleIds.includes(1)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { In, Repository } from 'typeorm';
|
||||||
import { BaseService } from '../../../basic/base-service.js';
|
import { BaseService } from '../../../basic/base-service.js';
|
||||||
import { PipelineEntity } from '../entity/pipeline.js';
|
import { PipelineEntity } from '../entity/pipeline.js';
|
||||||
import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
|
import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
|
||||||
import { Executor, isPlus, Pipeline, ResultType, RunHistory } from '@certd/pipeline';
|
import { Executor, isPlus, Pipeline, ResultType, RunHistory, UserInfo } from '@certd/pipeline';
|
||||||
import { AccessService } from './access-service.js';
|
import { AccessService } from './access-service.js';
|
||||||
import { DbStorage } from './db-storage.js';
|
import { DbStorage } from './db-storage.js';
|
||||||
import { StorageService } from './storage-service.js';
|
import { StorageService } from './storage-service.js';
|
||||||
|
@ -16,9 +16,11 @@ import { HistoryLogService } from './history-log-service.js';
|
||||||
import { logger } from '../../../utils/logger.js';
|
import { logger } from '../../../utils/logger.js';
|
||||||
import { EmailService } from '../../basic/service/email-service.js';
|
import { EmailService } from '../../basic/service/email-service.js';
|
||||||
import { NeedVIPException } from '../../../basic/exception/vip-exception.js';
|
import { NeedVIPException } from '../../../basic/exception/vip-exception.js';
|
||||||
|
import { UserService } from '../../authority/service/user-service.js';
|
||||||
|
|
||||||
const runningTasks: Map<string | number, Executor> = new Map();
|
const runningTasks: Map<string | number, Executor> = new Map();
|
||||||
const freeCount = 10;
|
const freeCount = 10;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 证书申请
|
* 证书申请
|
||||||
*/
|
*/
|
||||||
|
@ -38,6 +40,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||||
@Inject()
|
@Inject()
|
||||||
historyLogService: HistoryLogService;
|
historyLogService: HistoryLogService;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
userService: UserService;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
cron: Cron;
|
cron: Cron;
|
||||||
|
|
||||||
|
@ -331,9 +336,13 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||||
|
|
||||||
const userId = entity.userId;
|
const userId = entity.userId;
|
||||||
const historyId = await this.historyService.start(entity);
|
const historyId = await this.historyService.start(entity);
|
||||||
|
const userIsAdmin = await this.userService.isAdmin(userId);
|
||||||
|
const user: UserInfo = {
|
||||||
|
id: userId,
|
||||||
|
role: userIsAdmin ? 'admin' : 'user',
|
||||||
|
};
|
||||||
const executor = new Executor({
|
const executor = new Executor({
|
||||||
userId,
|
user,
|
||||||
pipeline,
|
pipeline,
|
||||||
onChanged,
|
onChanged,
|
||||||
accessService: this.accessService,
|
accessService: this.accessService,
|
||||||
|
|
|
@ -8,7 +8,7 @@ import path from 'path';
|
||||||
name: 'CopyToLocal',
|
name: 'CopyToLocal',
|
||||||
title: '复制到本机',
|
title: '复制到本机',
|
||||||
icon: 'solar:copy-bold-duotone',
|
icon: 'solar:copy-bold-duotone',
|
||||||
desc: '实际上是复制证书到docker容器内的某个路径,需要做目录映射到宿主机',
|
desc: '【仅管理员使用】实际上是复制证书到docker容器内的某个路径,需要做目录映射到宿主机',
|
||||||
group: pluginGroups.host.key,
|
group: pluginGroups.host.key,
|
||||||
default: {
|
default: {
|
||||||
strategy: {
|
strategy: {
|
||||||
|
@ -114,6 +114,10 @@ export class CopyCertToLocalPlugin extends AbstractTaskPlugin {
|
||||||
fs.copyFileSync(srcFile, destFile);
|
fs.copyFileSync(srcFile, destFile);
|
||||||
}
|
}
|
||||||
async execute(): Promise<void> {
|
async execute(): Promise<void> {
|
||||||
|
if (!this.isAdmin()) {
|
||||||
|
throw new Error('只有管理员才能运行此任务');
|
||||||
|
}
|
||||||
|
|
||||||
let { crtPath, keyPath, icPath, pfxPath, derPath } = this;
|
let { crtPath, keyPath, icPath, pfxPath, derPath } = this;
|
||||||
const certReader = new CertReader(this.cert);
|
const certReader = new CertReader(this.cert);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput
|
||||||
name: 'RestartCertd',
|
name: 'RestartCertd',
|
||||||
title: '重启Certd',
|
title: '重启Certd',
|
||||||
icon: 'mdi:restart',
|
icon: 'mdi:restart',
|
||||||
desc: '延迟一定时间后自动杀死自己,然后通过Docker来自动重启',
|
desc: '【仅管理员】延迟一定时间后自动杀死自己,然后通过Docker来自动重启',
|
||||||
group: pluginGroups.other.key,
|
group: pluginGroups.other.key,
|
||||||
default: {
|
default: {
|
||||||
strategy: {
|
strategy: {
|
||||||
|
@ -25,6 +25,9 @@ export class RestartCertdPlugin extends AbstractTaskPlugin {
|
||||||
delay = 30;
|
delay = 30;
|
||||||
async onInstance() {}
|
async onInstance() {}
|
||||||
async execute(): Promise<void> {
|
async execute(): Promise<void> {
|
||||||
|
if (!this.isAdmin()) {
|
||||||
|
throw new Error('只有管理员才能运行此任务');
|
||||||
|
}
|
||||||
this.logger.info(`Certd 将在 ${this.delay} 秒后关闭`);
|
this.logger.info(`Certd 将在 ${this.delay} 秒后关闭`);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.logger.info('重启 Certd');
|
this.logger.info('重启 Certd');
|
||||||
|
|
|
@ -9,8 +9,8 @@ export type CustomScriptContext = {
|
||||||
@IsTaskPlugin({
|
@IsTaskPlugin({
|
||||||
name: 'CustomScript',
|
name: 'CustomScript',
|
||||||
title: '自定义js脚本',
|
title: '自定义js脚本',
|
||||||
icon:"ri:javascript-line",
|
icon: 'ri:javascript-line',
|
||||||
desc: '测试',
|
desc: '【仅管理员】运行自定义js脚本执行',
|
||||||
group: pluginGroups.other.key,
|
group: pluginGroups.other.key,
|
||||||
default: {
|
default: {
|
||||||
strategy: {
|
strategy: {
|
||||||
|
@ -45,6 +45,9 @@ export class CustomScriptPlugin extends AbstractTaskPlugin {
|
||||||
|
|
||||||
async onInstance() {}
|
async onInstance() {}
|
||||||
async execute(): Promise<void> {
|
async execute(): Promise<void> {
|
||||||
|
if (!this.isAdmin()) {
|
||||||
|
throw new Error('只有管理员才能运行此任务');
|
||||||
|
}
|
||||||
this.logger.info('执行自定义脚本:\n', this.script);
|
this.logger.info('执行自定义脚本:\n', this.script);
|
||||||
const ctx: CustomScriptContext = {
|
const ctx: CustomScriptContext = {
|
||||||
CertReader,
|
CertReader,
|
||||||
|
|
Loading…
Reference in New Issue