Katherine 5d5f455b3c
feat: supports configuring dynamic environment variables and secrets (#5966)
* feat: environments plugin

* feat: improve code

* fix: improve code

* feat: improve code

* refactor: package description

* feat: bulk import

* fix: remove

* refactor: file manager support environment variables

* refactor: file manager support environment variables

* refactor: map manager support environment variables

* refactor: support environment variables

* refactor: support environment variables

* refactor: support delete environment variables

* fix: bug

* refactor: workflow support environment variables

* refactor: email  environment variables

* refactor: support bulk import

* refactor: support bulk import

* refactor: support bulk import

* refactor: support bulk import

* refactor: code improve

* feat: env

* chore: update

* feat: environment

* fix: bug

* fix: acl snippet

* fix: acl snippets

* chore: map manager

* refactor: support line break

* refactor: support password

* chore: environment variables

* fix: bug

* fix: bug

* chore: enviroment variables

* chore: system settings

* fix: improve code

* feat: verification

* feat: map

* feat: file-manager

* feat: notification

* fix: bug

* feat: workflow

* fix: improve code

* fix: bug

* feat: data-source

* feat: auth

* fix: error

* fix: bug

* refactor: description

* refactor: locale

* refactor: locale

* refactor: locale

* refactor: code improve

* refactor: locale

* refactor: locale

* style: style improve

* fix: error

* fix: bug

* fix: bug

* refactor: environment

* fix: ellipsis

* refactor: password

* fix: bug

* fix: bug

* fix: bug

* fix: bug

* fix: bug

* chore: test

* fix: cache

* fix: mysql dialect options

* refactor: email config form

* fix: bug

* fix: bug

* fix: authenticator.dataValues parse

* fix: include undefined

* fix: json

* fix: json parse

* chore: enviromentProvider

* fix: acl

* fix: rowKey

* fix: update ProviderOptions.tsx

* feat: get app instance

* fix: bug

* fix: text

* fix: build error

* fix: error

* chore: migration rules options

* chore: migration rules

* refactor: code improve

* feat: env v2

* chore: environment varibales

* chore: environment serve

* fix: getVariables

* feat: improve code

* fix: bug

* chore: collection options for migration

* chore: tree collection options

* chore: migration rules

* chore: migration rules

* chore: env api

* chore: env api

* fix: optionsKeysNotAllowedInEnv

* fix: required true

* fix: improve code

* fix: app refresh

* fix: remove db.import

* fix: type error

* fix: map

* refactor: locale improve

* refactor: tx-cos

* fix: undefined

* refactor: code improve

* chore: use bookworm

* fix: npm add user

* fix: npm login

* fix: npm adduser

* fix: npm adduser

* fix: expect

* fix: expect

* fix: environmentVariables

* refactor: support bulk delete & filter

* refactor: locale improve

* feat: filter

* refactor: useGlobalVariable

* fix: scope

* fix: bug

* fix: optionsKeysNotAllowedInEnv

* fix: test error

* fix: test

* fix: test

* feat: improve code

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
Co-authored-by: Chareice <chareice@live.com>
2025-01-08 09:32:49 +08:00

209 lines
5.8 KiB
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { Context, Next } from '@nocobase/actions';
import { parse } from '@nocobase/utils';
import { appendArrayColumn } from '@nocobase/evaluators';
import Application from '@nocobase/server';
import axios from 'axios';
import CustomRequestPlugin from '../plugin';
const getHeaders = (headers: Record<string, any>) => {
return Object.keys(headers).reduce((hds, key) => {
if (key.toLocaleLowerCase().startsWith('x-')) {
hds[key] = headers[key];
}
return hds;
}, {});
};
const arrayToObject = (arr: { name: string; value: string }[]) => {
return arr.reduce((acc, cur) => {
acc[cur.name] = cur.value;
return acc;
}, {});
};
const omitNullAndUndefined = (obj: any) => {
return Object.keys(obj).reduce((acc, cur) => {
if (obj[cur] !== null && typeof obj[cur] !== 'undefined') {
acc[cur] = obj[cur];
}
return acc;
}, {});
};
const CurrentUserVariableRegExp = /{{\s*(currentUser[^}]+)\s*}}/g;
const getCurrentUserAppends = (str: string, user) => {
const matched = str.matchAll(CurrentUserVariableRegExp);
return Array.from(matched)
.map((item) => {
const keys = item?.[1].split('.') || [];
const appendKey = keys[1];
if (keys.length > 2 && !Reflect.has(user || {}, appendKey)) {
return appendKey;
}
})
.filter(Boolean);
};
export const getParsedValue = (value, variables) => {
const template = parse(value);
template.parameters.forEach(({ key }) => {
appendArrayColumn(variables, key);
});
return template(variables);
};
export async function send(this: CustomRequestPlugin, ctx: Context, next: Next) {
const resourceName = ctx.action.resourceName;
const { filterByTk, values = {} } = ctx.action.params;
const {
currentRecord = {
id: 0,
appends: [],
data: {},
},
$nForm,
} = values;
// root role has all permissions
if (ctx.state.currentRole !== 'root') {
const crRepo = ctx.db.getRepository('customRequestsRoles');
const hasRoles = await crRepo.find({
filter: {
customRequestKey: filterByTk,
},
});
if (hasRoles.length) {
if (!hasRoles.find((item) => item.roleName === ctx.state.currentRole)) {
return ctx.throw(403, 'custom request no permission');
}
}
}
const repo = ctx.db.getRepository(resourceName);
const requestConfig = await repo.findOne({
filter: {
key: filterByTk,
},
});
if (!requestConfig) {
ctx.throw(404, 'request config not found');
}
ctx.withoutDataWrapping = true;
const {
dataSourceKey,
collectionName,
url,
headers = [],
params = [],
data = {},
...options
} = requestConfig.options || {};
if (!url) {
return ctx.throw(400, ctx.t('Please configure the request settings first', { ns: 'action-custom-request' }));
}
let currentRecordValues = {};
if (collectionName && typeof currentRecord.id !== 'undefined') {
const app = ctx.app as Application;
const dataSource = app.dataSourceManager.get(dataSourceKey || currentRecord.dataSourceKey || 'main');
const recordRepo = dataSource.collectionManager.getRepository(collectionName);
currentRecordValues =
(
await recordRepo.findOne({
filterByTk: currentRecord.id,
appends: currentRecord.appends,
})
)?.toJSON() || {};
}
let currentUser = ctx.auth.user;
const userAppends = getCurrentUserAppends(
JSON.stringify(url) + JSON.stringify(headers) + JSON.stringify(params) + JSON.stringify(data),
ctx.auth.user,
);
if (userAppends.length) {
currentUser =
(
await ctx.db.getRepository('users').findOne({
filterByTk: ctx.auth.user.id,
appends: userAppends,
})
)?.toJSON() || {};
}
const variables = {
currentRecord: {
...currentRecordValues,
...currentRecord.data,
},
currentUser,
currentTime: new Date().toISOString(),
$nToken: ctx.getBearerToken(),
$nForm,
$env: ctx.app.environment.getVariables(),
};
const axiosRequestConfig = {
baseURL: ctx.origin,
...options,
url: getParsedValue(url, variables),
headers: {
Authorization: 'Bearer ' + ctx.getBearerToken(),
...getHeaders(ctx.headers),
...omitNullAndUndefined(getParsedValue(arrayToObject(headers), variables)),
},
params: getParsedValue(arrayToObject(params), variables),
data: getParsedValue(data, variables),
};
const requestUrl = axios.getUri(axiosRequestConfig);
this.logger.info(`custom-request:send:${filterByTk} request url ${requestUrl}`);
this.logger.info(
`custom-request:send:${filterByTk} request config ${JSON.stringify({
...axiosRequestConfig,
headers: {
...axiosRequestConfig.headers,
Authorization: null,
},
})}`,
);
try {
const res = await axios(axiosRequestConfig);
this.logger.info(`custom-request:send:${filterByTk} success`);
ctx.body = res.data;
if (res.headers['content-disposition']) {
ctx.set('Content-Disposition', res.headers['content-disposition']);
}
} catch (err) {
if (axios.isAxiosError(err)) {
ctx.status = err.response?.status || 500;
ctx.body = err.response?.data || { message: err.message };
this.logger.error(
`custom-request:send:${filterByTk} error. status: ${ctx.status}, body: ${
typeof ctx.body === 'string' ? ctx.body : JSON.stringify(ctx.body)
}`,
);
} else {
this.logger.error(`custom-request:send:${filterByTk} error. status: ${ctx.status}, message: ${err.message}`);
ctx.throw(500, err?.message);
}
}
return next();
}