Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add referer-restriction plugin form #1727

Merged
merged 16 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-undef */

context('Create and delete Consumer with referer-restriction form ', () => {
beforeEach(() => {
cy.login();

cy.fixture('selector.json').as('domSelector');
cy.fixture('data.json').as('data');
});

const selector = {
whitelist: "#whitelist_0",
bypass_missing: "#bypass_missing",
}

const data = {
whitelist: 'yy.com',
}

it('creates consumer with referer-restriction form', function () {
cy.visit('/');
cy.contains('Consumer').click();
cy.get(this.domSelector.empty).should('be.visible');
cy.contains('Create').click();
// basic information
cy.get(this.domSelector.username).type(this.data.consumerName);
cy.get(this.domSelector.description).type(this.data.description);
cy.contains('Next').click();

// config auth plugin
cy.contains(this.domSelector.pluginCard, 'key-auth').within(() => {
cy.contains('Enable').click({
force: true,
});
});
cy.focused(this.domSelector.drawer).should('exist');
cy.get(this.domSelector.disabledSwitcher).click();
// edit codemirror
cy.get(this.domSelector.codeMirror)
.first()
.then((editor) => {
editor[0].CodeMirror.setValue(
JSON.stringify({
key: 'test',
}),
);
cy.contains('button', 'Submit').click();
});

cy.contains(this.domSelector.pluginCard, 'referer-restriction').within(() => {
cy.contains('Enable').click({
force: true,
});
});

cy.focused(this.domSelector.drawer).should('exist');

// config referer-restriction form
cy.get(selector.whitelist).type(data.whitelist);
cy.get(selector.bypass_missing).click();
cy.get(this.domSelector.drawer).within(() => {
cy.contains('Submit').click({
force: true,
});
});

cy.get(this.domSelector.drawer).should('not.exist');

cy.contains('button', 'Next').click();
cy.contains('button', 'Submit').click();
cy.get(this.domSelector.notification).should('contain', this.data.createConsumerSuccess);
});

it('delete the consumer', function () {
cy.visit('/consumer/list');
cy.contains(this.data.consumerName).should('be.visible').siblings().contains('Delete').click();
cy.contains('button', 'Confirm').click();
cy.get(this.domSelector.notification).should('contain', this.data.deleteConsumerSuccess);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-undef */

context('Create and delete route with referer-restriction form', () => {
const selector = {
whitlist: '#whitelist_0',
alert:'.ant-form-item-explain-error [role=alert]'
}

beforeEach(() => {
cy.login();

cy.fixture('selector.json').as('domSelector');
cy.fixture('data.json').as('data');
});

it('should create route with referer-restriction form', function () {
cy.visit('/');
cy.contains('Route').click();
cy.get(this.domSelector.empty).should('be.visible');
cy.contains('Create').click();
cy.contains('Next').click().click();
cy.get(this.domSelector.name).type('routeName');
cy.get(this.domSelector.description).type('desc');
cy.contains('Next').click();

cy.get(this.domSelector.nodes_0_host).type('127.0.0.1');
cy.contains('Next').click();

// config referer-restriction plugin
cy.contains('referer-restriction').parents(this.domSelector.pluginCardBordered).within(() => {
cy.get('button').click({
force: true
});
});

cy.get(this.domSelector.drawer).should('be.visible').within(() => {
cy.get(this.domSelector.disabledSwitcher).click();
cy.get(this.domSelector.checkedSwitcher).should('exist');
});

// config referer-restriction form without whitelist
cy.get(selector.whitlist).click();
cy.get(selector.alert).contains('Please Enter whitelist');
cy.get(this.domSelector.drawer).within(() => {
cy.contains('Submit').click({
force: true,
});
});
cy.get(this.domSelector.notification).should('contain', 'Invalid plugin data');
cy.get(this.domSelector.notificationCloseIcon).click();

// config referer-restriction form with whitelist
cy.get(selector.whitlist).type('127.0.0.1');
cy.get(selector.alert).should('not.exist');
cy.get(this.domSelector.disabledSwitcher).click();
cy.get(this.domSelector.drawer).within(() => {
cy.contains('Submit').click({
force: true,
});
});
cy.get(this.domSelector.drawer).should('not.exist');

cy.contains('button', 'Next').click();
cy.contains('button', 'Submit').click();
cy.contains(this.data.submitSuccess);

// back to route list page
cy.contains('Goto List').click();
cy.url().should('contains', 'routes/list');
});

it('should delete the route', function () {
cy.visit('/routes/list');
const {
domSelector,
data
} = this;

cy.get(domSelector.name).clear().type('routeName');
cy.contains('Search').click();
cy.contains('routeName').siblings().contains('More').click();
cy.contains('Delete').click();
cy.get(domSelector.deleteAlert).should('be.visible').within(() => {
cy.contains('OK').click();
});
cy.get(domSelector.notification).should('contain', data.deleteRouteSuccess);
cy.get(domSelector.notificationCloseIcon).click();
});
});
5 changes: 4 additions & 1 deletion web/src/components/Plugin/UI/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import LimitReq from './limit-req';
import ApiBreaker from './api-breaker';
import ProxyMirror from './proxy-mirror';
import LimitConn from './limit-conn';
import RefererRestriction from './referer-restriction'

type Props = {
name: string,
form: FormInstance,
renderForm: boolean
}

export const PLUGIN_UI_LIST = ['api-breaker', 'basic-auth', 'limit-req', 'limit-conn', 'proxy-mirror'];
export const PLUGIN_UI_LIST = ['api-breaker', 'basic-auth', 'limit-req', 'limit-conn', 'proxy-mirror', 'referer-restriction'];

export const PluginForm: React.FC<Props> = ({ name, renderForm, form }) => {

Expand All @@ -50,6 +51,8 @@ export const PluginForm: React.FC<Props> = ({ name, renderForm, form }) => {
return <ProxyMirror form={form} />
case 'limit-conn':
return <LimitConn form={form} />
case 'referer-restriction':
return <RefererRestriction form={form} />
default:
return null;
}
Expand Down
126 changes: 126 additions & 0 deletions web/src/components/Plugin/UI/referer-restriction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import type { FormInstance } from 'antd/es/form';
import { Form, Input, Button, Switch, Row, Col } from 'antd';
import { useIntl } from 'umi';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';

type Props = {
form: FormInstance;
};

const FORM_ITEM_LAYOUT = {
labelCol: {
span: 5,
},
wrapperCol: {
span: 18
},
};

const FORM_ITEM_WITHOUT_LABEL = {
wrapperCol: {
span: 10, offset: 5
},
};

const removeBtnStyle = {
marginLeft: 20,
display: 'flex',
alignItems: 'center',
};

const RefererRestriction: React.FC<Props> = ({ form }) => {
const { formatMessage } = useIntl()
return (
<Form
form={form}
{...FORM_ITEM_LAYOUT}
initialValues={{ whitelist: [''] }}
>
<Form.List name="whitelist">
{(fields, { add, remove }) => {
return (
<div>
<Form.Item
extra={formatMessage({ id: 'component.pluginForm.referer-restriction.whitelist.tooltip' })}
label='whitelist'
tooltip={formatMessage({ id: 'component.pluginForm.referer-restriction.whitelist.tooltip' })}
required
style={{ marginBottom: 0 }}
>
{fields.map((field, index) => (
<Row style={{ marginBottom: 10 }} gutter={16} key={index}>
<Col span={10}>
<Form.Item
{...field}
validateTrigger={['onChange', 'onBlur', 'onClick']}
noStyle
required
rules={[{
message: "",
pattern: new RegExp(/^\*?[0-9a-zA-Z-._]+$/, 'g')
}, {
required: true,
message: `${formatMessage({ id: 'component.global.pleaseEnter' })} whitelist`
}]}
>
<Input />
</Form.Item>
</Col>
<Col style={{ ...removeBtnStyle, marginLeft: -10 }}>
{fields.length > 1 ? (
<MinusCircleOutlined
className="dynamic-delete-button"
onClick={() => {
remove(field.name);
}}
/>
) : null}
</Col>
</Row>
))}
</Form.Item>
<Form.Item {...FORM_ITEM_WITHOUT_LABEL}>
<Button
type="dashed"
onClick={() => {
add();
}}
>
<PlusOutlined /> {formatMessage({ id: 'component.global.add' })}
</Button>
</Form.Item>
</div>
);
}}
</Form.List>
<Form.Item
extra={formatMessage({ id: 'component.pluginForm.referer-restriction.bypass_missing.tooltip' })}
label="bypass_missing"
name="bypass_missing"
tooltip={formatMessage({ id: 'component.pluginForm.referer-restriction.bypass_missing.tooltip' })}
valuePropName="checked"
>
<Switch />
</Form.Item>
</Form >
);
}

export default RefererRestriction;
5 changes: 5 additions & 0 deletions web/src/components/Plugin/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default {
'component.plugin.pluginTemplate.tip1': '1. When a route already have plugins field configured, the plugins in the plugin template will be merged into it.',
'component.plugin.pluginTemplate.tip2': '2. The same plugin in the plugin template will override one in the plugins',

// referer-restriction
'component.pluginForm.referer-restriction.whitelist.tooltip': 'List of hostname to whitelist. The hostname can be started with * as a wildcard.',
'component.pluginForm.referer-restriction.bypass_missing.tooltip': 'Whether to bypass the check when the Referer header is missing or malformed.',

// api-breaker
'component.pluginForm.api-breaker.break_response_code.tooltip': 'Return error code when unhealthy.',
'component.pluginForm.api-breaker.max_breaker_sec.tooltip': 'Maximum breaker time(seconds).',
Expand All @@ -41,6 +45,7 @@ export default {
'component.pluginForm.limit-conn.default_conn_delay.tooltip': 'the latency seconds of request when concurrent requests exceeding conn but below (conn + burst).',
'component.pluginForm.limit-conn.key.tooltip': 'to limit the concurrency level.For example, one can use the host name (or server zone) as the key so that we limit concurrency per host name. Otherwise, we can also use the client address as the key so that we can avoid a single client from flooding our service with too many parallel connections or requests.Now accept those as key: "remote_addr"(client\'s IP), "server_addr"(server\'s IP), "X-Forwarded-For/X-Real-IP" in request header, "consumer_name"(consumer\'s username).',
'component.pluginForm.limit-conn.rejected_code.tooltip': 'returned when the request exceeds conn + burst will be rejected.',

// limit-req
'component.pluginForm.limit-req.rate.tooltip': 'The specified request rate (number per second) threshold. Requests exceeding this rate (and below burst) will get delayed to conform to the rate.',
'component.pluginForm.limit-req.burst.tooltip': 'The number of excessive requests per second allowed to be delayed. Requests exceeding this hard limit will get rejected immediately.',
Expand Down
4 changes: 4 additions & 0 deletions web/src/components/Plugin/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default {
'component.plugin.pluginTemplate.tip1': '1. 若路由已配置插件,则插件模板数据将与已配置的插件数据合并。',
'component.plugin.pluginTemplate.tip2': '2. 插件模板相同的插件会覆盖掉原有的插件。',

// referer-restriction
'component.pluginForm.referer-restriction.whitelist.tooltip': '域名列表。域名开头可以用\'*\'作为通配符。',
'component.pluginForm.referer-restriction.bypass_missing.tooltip': '当 Referer 不存在或格式有误时,是否绕过检查。',

// api-breaker
'component.pluginForm.api-breaker.break_response_code.tooltip': '不健康返回错误码。',
'component.pluginForm.api-breaker.max_breaker_sec.tooltip': '最大熔断持续时间。',
Expand Down