Merge pull request #355 from jameslcj/hotfix_337

validate config syntax #337
This commit is contained in:
zhichen 2018-12-06 15:20:00 +08:00 committed by GitHub
commit 9425a53124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 236 additions and 125 deletions

View File

@ -21,7 +21,7 @@ module.exports = Object.assign({}, base, {
path: path.resolve(__dirname, '../dist'),
},
devServer: {
port: 8000,
port: process.env.PORT || 8000,
proxy: [{
context: ['/'],
changeOrigin: true,

View File

@ -16,7 +16,7 @@ import $ from 'jquery';
import { getParams, request, aliwareIntl } from '../../../globalLib';
import DiffEditorDialog from '../../../components/DiffEditorDialog';
import SuccessDialog from '../../../components/SuccessDialog';
import './index.scss';
import validateContent from 'utils/validateContent';
import {
Balloon,
Button,
@ -32,6 +32,8 @@ import {
Message,
} from '@alifd/next';
import './index.scss';
const TabPane = Tab.Item;
const FormItem = Form.Item;
const { Group: RadioGroup } = Radio;
@ -309,13 +311,7 @@ class ConfigEditor extends React.Component {
return;
}
let content = '';
const self = this;
// if (this.commoneditor) {
// content = this.commoneditor.doc.getValue();
// //content = content.replace("↵", "\n\r");
// } else {
// content = this.codeValue;
// }
let { configType } = this.state;
if (this.monacoEditor) {
content = this.monacoEditor.getValue();
@ -329,76 +325,89 @@ class ConfigEditor extends React.Component {
});
return;
}
this.codeValue = content;
this.tenant = getParams('namespace') || '';
this.serverId = getParams('serverId') || 'center';
const payload = {
dataId: this.field.getValue('dataId'),
appName: this.inApp ? this.edasAppId : this.field.getValue('appName'),
group: this.field.getValue('group'),
desc: this.field.getValue('desc'),
config_tags: this.state.config_tags.join(),
type: this.state.configType,
content,
tenant: this.tenant,
};
const url = 'v1/cs/configs';
request({
type: 'post',
contentType: 'application/x-www-form-urlencoded',
url,
data: payload,
success(res) {
const _payload = {};
_payload.maintitle = aliwareIntl.get('com.alibaba.nacos.page.configeditor.toedittitle');
_payload.title = (
<div>{aliwareIntl.get('com.alibaba.nacos.page.configeditor.toedit')}</div>
);
_payload.content = '';
_payload.dataId = payload.dataId;
_payload.group = payload.group;
if (res != null) {
_payload.isok = true;
const activeKey = self.state.activeKey.split('-')[0];
if (activeKey === 'normal' && self.hasips === true) {
// 如果是在normal面板选择了beta发布
const sufex = new Date().getTime();
self.setState({
tag: [
{
title: aliwareIntl.get('com.alibaba.nacos.page.configeditor.official'),
key: `normal-${sufex}`,
},
{ title: 'BETA', key: `beta-${sufex}` },
],
hasbeta: true,
activeKey: `beta-${sufex}`,
});
payload.betaIps = payload.betaIps || payload.ips;
self.valueMap.beta = payload; // 赋值beta
self.changeTab(`beta-${sufex}`);
}
if (activeKey === 'normal' && self.hasips === false) {
// 如果是在normal面板选择了发布
self.valueMap.normal = payload; // 赋值正式
}
if (activeKey === 'beta' && self.hasips === true) {
// 如果是在beta面板继续beta发布
self.valueMap.beta = payload; // 赋值beta
}
} else {
_payload.isok = false;
_payload.message = res.message;
}
self.refs.success.openDialog(_payload);
},
error() {},
});
if (validateContent.validate({ content, type: configType })) {
this._publishConfig(content);
} else {
Dialog.confirm({
content: '配置信息可能有语法错误, 确定提交吗?',
language: aliwareIntl.currentLanguageCode || 'zh-cn',
onOk: () => {
this._publishConfig(content);
},
});
}
});
}
_publishConfig = content => {
const self = this;
this.codeValue = content;
this.tenant = getParams('namespace') || '';
this.serverId = getParams('serverId') || 'center';
const payload = {
dataId: this.field.getValue('dataId'),
appName: this.inApp ? this.edasAppId : this.field.getValue('appName'),
group: this.field.getValue('group'),
desc: this.field.getValue('desc'),
config_tags: this.state.config_tags.join(),
type: this.state.configType,
content,
tenant: this.tenant,
};
const url = 'v1/cs/configs';
request({
type: 'post',
contentType: 'application/x-www-form-urlencoded',
url,
data: payload,
success(res) {
const _payload = {};
_payload.maintitle = aliwareIntl.get('com.alibaba.nacos.page.configeditor.toedittitle');
_payload.title = <div>{aliwareIntl.get('com.alibaba.nacos.page.configeditor.toedit')}</div>;
_payload.content = '';
_payload.dataId = payload.dataId;
_payload.group = payload.group;
if (res != null) {
_payload.isok = true;
const activeKey = self.state.activeKey.split('-')[0];
if (activeKey === 'normal' && self.hasips === true) {
// 如果是在normal面板选择了beta发布
const sufex = new Date().getTime();
self.setState({
tag: [
{
title: aliwareIntl.get('com.alibaba.nacos.page.configeditor.official'),
key: `normal-${sufex}`,
},
{ title: 'BETA', key: `beta-${sufex}` },
],
hasbeta: true,
activeKey: `beta-${sufex}`,
});
payload.betaIps = payload.betaIps || payload.ips;
self.valueMap.beta = payload; // 赋值beta
self.changeTab(`beta-${sufex}`);
}
if (activeKey === 'normal' && self.hasips === false) {
// 如果是在normal面板选择了发布
self.valueMap.normal = payload; // 赋值正式
}
if (activeKey === 'beta' && self.hasips === true) {
// 如果是在beta面板继续beta发布
self.valueMap.beta = payload; // 赋值beta
}
} else {
_payload.isok = false;
_payload.message = res.message;
}
self.refs.success.openDialog(_payload);
},
error() {},
});
};
validateChart(rule, value, callback) {
const chartReg = /[@#\$%\^&\*]+/g;

View File

@ -29,6 +29,7 @@ import {
Radio,
ConfigProvider,
} from '@alifd/next';
import validateContent from 'utils/validateContent';
import './index.scss';
@ -222,6 +223,7 @@ class NewConfig extends React.Component {
if (errors) {
return;
}
let { configType } = this.state;
let content = '';
const self = this;
if (this.monacoEditor) {
@ -230,60 +232,83 @@ class NewConfig extends React.Component {
content = this.codeValue;
}
if (!content) {
Message.error({
content: aliwareIntl.get('nacos.page.ConfigEditor.submit_failed'),
align: 'cc cc',
});
return;
}
this.tenant = getParams('namespace') || '';
const payload = {
dataId: self.state.addonBefore + this.field.getValue('dataId'),
group: this.field.getValue('group'),
content,
desc: this.field.getValue('desc'),
config_tags: this.state.config_tags.join(),
type: this.state.configType,
appName: this.inApp ? this.edasAppId : this.field.getValue('appName'),
tenant: this.tenant,
};
this.serverId = getParams('serverId') || 'center';
const url = 'v1/cs/configs';
request({
type: 'post',
contentType: 'application/x-www-form-urlencoded',
url,
data: payload,
beforeSend: () => {
this.openLoading();
},
success(res) {
const _payload = {};
_payload.maintitle = locale.newListingMain;
_payload.title = locale.newListing;
_payload.content = '';
_payload.dataId = payload.dataId;
_payload.group = payload.group;
if (res === true) {
self.group = payload.group;
self.dataId = payload.dataId;
setParams({ group: payload.group, dataId: payload.dataId }); // 设置参数
_payload.isok = true;
} else {
_payload.isok = false;
_payload.message = res.message;
}
self.refs.success.openDialog(_payload);
},
complete() {
self.closeLoading();
},
error(res) {
Dialog.alert({
content: locale.publishFailed,
});
self.closeLoading();
},
});
if (validateContent.validate({ content, type: configType })) {
this._publishConfig(content);
} else {
Dialog.confirm({
content: '配置信息可能有语法错误, 确定提交吗?',
language: aliwareIntl.currentLanguageCode || 'zh-cn',
onOk: () => {
this._publishConfig(content);
},
});
}
});
}
_publishConfig = content => {
const self = this;
let { addonBefore, config_tags, configType } = this.state;
this.tenant = getParams('namespace') || '';
const payload = {
dataId: addonBefore + this.field.getValue('dataId'),
group: this.field.getValue('group'),
content,
desc: this.field.getValue('desc'),
config_tags: config_tags.join(),
type: configType,
appName: this.inApp ? this.edasAppId : this.field.getValue('appName'),
tenant: this.tenant,
};
this.serverId = getParams('serverId') || 'center';
const url = 'v1/cs/configs';
request({
type: 'post',
contentType: 'application/x-www-form-urlencoded',
url,
data: payload,
beforeSend: () => {
this.openLoading();
},
success(res) {
const _payload = {};
_payload.maintitle = aliwareIntl.get('com.alibaba.nacos.page.newconfig.new_listing_main');
_payload.title = aliwareIntl.get('com.alibaba.nacos.page.newconfig.new_listing');
_payload.content = '';
_payload.dataId = payload.dataId;
_payload.group = payload.group;
if (res === true) {
self.group = payload.group;
self.dataId = payload.dataId;
setParams({ group: payload.group, dataId: payload.dataId }); // 设置参数
_payload.isok = true;
} else {
_payload.isok = false;
_payload.message = res.message;
}
self.refs.success.openDialog(_payload);
},
complete() {
self.closeLoading();
},
error(res) {
Dialog.alert({
language: aliwareIntl.currentLanguageCode || 'zh-cn',
content: aliwareIntl.get('com.alibaba.nacos.page.newconfig.publish_failed'),
});
self.closeLoading();
},
});
};
changeEnv(values) {
this.targetEnvs = values;
this.setState({

View File

@ -0,0 +1,77 @@
export default {
/**
* 检测json是否合法
*/
validateJson(str) {
try {
return !!JSON.parse(str);
} catch (e) {
return false;
}
},
/**
* 检测xml和html是否合法
*/
validateXml(str) {
try {
if (typeof DOMParser !== 'undefined') {
let parserObj =
new window.DOMParser()
.parseFromString(str, 'application/xml')
.getElementsByTagName('parsererror') || {};
return parserObj.length === 0;
} else if (typeof window.ActiveXObject !== 'undefined') {
let xml = new window.ActiveXObject('Microsoft.XMLDOM');
xml.async = 'false';
xml.loadXML(str);
return xml;
}
} catch (e) {
return false;
}
},
/**
* 检测yaml是否合法
*/
// validateYaml(str) {
// try {
// console.log('yaml: ', yaml, yaml.safeLoadAll(str));
// return !!yaml.safeLoadAll(str);
// } catch (e) {
// console.log('e: ', e);
// return false;
// }
// },
/**
* 检测属性是否正确
*/
validateProperties(str = '') {
let reg = /^[A-Za-z\d-_]+=.+$/;
return str
.replace('\n\r', '\n')
.split('\n')
.every(_str => reg.test(_str));
},
/**
* 根据类型验证类型
*/
validate({ content, type }) {
let validateObj = {
json: this.validateJson,
xml: this.validateXml,
'text/html': this.validateXml,
html: this.validateXml,
properties: this.validateProperties,
};
if (!validateObj[type]) {
return true;
}
return validateObj[type](content);
},
};