diff --git a/packages/system-server/src/router/index.ts b/packages/system-server/src/router/index.ts index 33f81983c5..0a0c45e0a5 100644 --- a/packages/system-server/src/router/index.ts +++ b/packages/system-server/src/router/index.ts @@ -1,7 +1,7 @@ /* * @Author: Maslow * @Date: 2021-07-30 10:30:29 - * @LastEditTime: 2021-09-04 00:12:07 + * @LastEditTime: 2021-09-05 23:52:32 * @Description: */ @@ -24,7 +24,7 @@ router.use('/apps', ApplicationRouter) router.use('/apps/:appid/function', checkAppid, FunctionRouter) router.use('/apps/:appid/policy', checkAppid, PolicyRouter) router.use('/apps/:appid/dbm', checkAppid, DbmRouter) -router.use('/apps/:appid/deploy', DeployRouter) +router.use('/apps/:appid/deploy', checkAppid, DeployRouter) router.use('/apps/:appid/file', checkAppid, FileRouter) router.use('/health-check', (_req, res) => { diff --git a/packages/system-server/src/router/policy/create.ts b/packages/system-server/src/router/policy/create.ts index e5828c7edf..02175dddd2 100644 --- a/packages/system-server/src/router/policy/create.ts +++ b/packages/system-server/src/router/policy/create.ts @@ -1,7 +1,7 @@ /* * @Author: Maslow * @Date: 2021-09-03 23:19:36 - * @LastEditTime: 2021-09-03 23:28:01 + * @LastEditTime: 2021-09-06 13:45:24 * @Description: */ @@ -40,8 +40,7 @@ export async function handleCreatePolicy(req: Request, res: Response) { .count() if (total) return res.status(422).send('policy name already exists') - - // build the func data + // build the policy data const policy = { name: body.name, description: body.description, @@ -55,7 +54,7 @@ export async function handleCreatePolicy(req: Request, res: Response) { appid: app.appid } - // add cloud function + // add policy const ret = await db.collection(Constants.cn.policies) .add(policy) diff --git a/packages/system-server/src/router/policy/index.ts b/packages/system-server/src/router/policy/index.ts index 8e5885ceea..bb4b95427b 100644 --- a/packages/system-server/src/router/policy/index.ts +++ b/packages/system-server/src/router/policy/index.ts @@ -1,7 +1,7 @@ /* * @Author: Maslow * @Date: 2021-08-29 11:35:05 - * @LastEditTime: 2021-09-03 23:59:53 + * @LastEditTime: 2021-09-06 13:51:17 * @Description: */ @@ -10,6 +10,7 @@ import { handleCreatePolicy } from "./create" import { handleGetPolicies, handleGetPolicyById } from "./get" import { handlePublishPolicies } from "./publish" import { handleRemovePolicyById } from "./remove" +import { handleUpdatePolicy, handleUpdatePolicyRules } from "./update" export const PolicyRouter = Router() @@ -32,12 +33,12 @@ PolicyRouter.post('/create', handleCreatePolicy) /** * Update the policy's info except the rules */ -PolicyRouter.post('/:policy_id/info') +PolicyRouter.post('/:policy_id/info', handleUpdatePolicy) /** * Update the policy's rules */ -PolicyRouter.post('/:policy_id/rules') +PolicyRouter.post('/:policy_id/rules', handleUpdatePolicyRules) /** * Delete a policy diff --git a/packages/system-server/src/router/policy/update.ts b/packages/system-server/src/router/policy/update.ts new file mode 100644 index 0000000000..c8d769b708 --- /dev/null +++ b/packages/system-server/src/router/policy/update.ts @@ -0,0 +1,111 @@ +/* + * @Author: Maslow + * @Date: 2021-09-03 23:09:23 + * @LastEditTime: 2021-09-06 14:22:53 + * @Description: + */ + + +import { Request, Response } from 'express' +import { ApplicationStruct } from '../../api/application' +import { checkPermission } from '../../api/permission' +import { Constants } from '../../constants' +import { permissions } from '../../constants/permissions' +import { DatabaseAgent } from '../../lib/db-agent' +import { hashFunctionCode } from '../../utils/hash' + +const { POLICY_UPDATE } = permissions + + +/** + * Update a policy + */ +export async function handleUpdatePolicy(req: Request, res: Response) { + const uid = req['auth']?.uid + const db = DatabaseAgent.sys_db + const app: ApplicationStruct = req['parsed-app'] + const policy_id = req.params.policy_id + + // check permission + const code = await checkPermission(uid, POLICY_UPDATE.name, app) + if (code) { + return res.status(code).send() + } + + // get the policy + const { data: policy } = await db.collection(Constants.cn.policies) + .where({ _id: policy_id, appid: app.appid }) + .getOne() + + if (!policy) return res.status(422).send('policy not found') + const body = req.body + + // build the policy data + const data = { + name: body.name ?? policy.name, + description: body.description ?? policy.description, + status: body.status ?? policy.status, + injector: body.injector ?? policy.injector, + updated_at: Date.now(), + } + + // do db query + const ret = await db.collection(Constants.cn.policies) + .where({ appid: app.appid, _id: policy_id }) + .update(data) + + if (ret.error) { + return res.status(400).send(ret.error) + } + + return res.send({ + data: ret + }) +} + + +/** + * Update policy rules + */ +export async function handleUpdatePolicyRules(req: Request, res: Response) { + const uid = req['auth']?.uid + const db = DatabaseAgent.sys_db + const app: ApplicationStruct = req['parsed-app'] + const policy_id = req.params.policy_id + + // check permission + const code = await checkPermission(uid, POLICY_UPDATE.name, app) + if (code) { + return res.status(code).send() + } + + const body = req.body + if (!body.rules) return res.status(422).send('rules cannot be empty') + + // get the policy + const { data: policy } = await db.collection(Constants.cn.policies) + .where({ _id: policy_id, appid: app.appid }) + .getOne() + + if (!policy) return res.status(422).send('policy not found') + + // build the policy data + const data = { + rules: db.command.set(body.rules), + hash: hashFunctionCode(JSON.stringify(body.rules)), + updated_at: Date.now(), + } + + // do db query + const ret = await db.collection(Constants.cn.policies) + .where({ appid: app.appid, _id: policy_id }) + .update(data) + + if (ret.error) { + return res.status(400).send(ret.error) + } + + return res.send({ + data: ret + }) +} \ No newline at end of file