forked from didi/KnowStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDrawer.tsx
More file actions
496 lines (476 loc) · 18 KB
/
EditDrawer.tsx
File metadata and controls
496 lines (476 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
import { Button, Form, Input, Select, Drawer, Space, Divider, Utils, Radio, AutoComplete, Alert } from 'knowdesign';
import message from '@src/components/Message';
import api from '@src/api';
import { useParams } from 'react-router-dom';
import { UsersProps } from '../SecurityUsers';
// 字段对应后端存储值的枚举类型
export enum ACL_OPERATION {
Unknown,
Any,
All,
Read,
Write,
Create,
Delete,
Alter,
Describe,
ClusterAction,
DescribeConfigs,
AlterConfigs,
IdempotentWrite,
}
export enum ACL_PERMISSION_TYPE {
Unknown,
Any,
Deny,
Allow,
}
export enum ACL_PATTERN_TYPE {
Unknown,
Any,
Match,
Literal,
Prefixed,
}
export enum ACL_RESOURCE_TYPE {
Unknown,
Any,
Topic,
Group,
Cluster,
TransactionalId,
DelegationToken,
}
export type RESOURCE_MAP_KEYS = Exclude<keyof typeof ACL_RESOURCE_TYPE, 'Unknown' | 'Any' | 'DelegationToken'>;
// 资源类型和操作映射表
export const RESOURCE_TO_OPERATIONS_MAP: {
[P in RESOURCE_MAP_KEYS]: string[];
} = {
Cluster: ['Alter', 'AlterConfigs', 'ClusterAction', 'Create', 'Describe', 'DescribeConfigs', 'IdempotentWrite'],
Topic: ['Alter', 'AlterConfigs', 'Create', 'Delete', 'Describe', 'DescribeConfigs', 'Read', 'Write'],
Group: ['Delete', 'Describe', 'Read'],
TransactionalId: ['Write', 'Describe'],
};
// ACL 配置类型
const CONFIG_TYPE = [
{
label: '配置生产权限',
value: 'produce',
},
{
label: '配置消费权限',
value: 'consume',
},
{
label: '配置自定义权限',
value: 'custom',
},
];
// eslint-disable-next-line react/display-name
const AddDrawer = forwardRef((_, ref) => {
const { clusterId } = useParams<{
clusterId: string;
}>();
const [form] = Form.useForm();
const [visible, setVisible] = useState<boolean>(false);
const [kafkaUserOptions, setKafkaUserOptions] = useState<{ label: string; value: string }[]>([]);
const [confirmLoading, setConfirmLoading] = useState<boolean>(false);
const callback = useRef(() => {
return;
});
const [topicMetaData, setTopicMetaData] = React.useState([]);
const [groupMetaData, setGroupMetaData] = React.useState([]);
// 获取 Topic 元信息
const getTopicMetaData = (newValue: any) => {
Utils.request(api.getTopicMetaData(+clusterId), {
method: 'GET',
params: { searchKeyword: newValue },
}).then((res: UsersProps[]) => {
const topics = (res || []).map((item: any) => {
return {
label: item.topicName,
value: item.topicName,
};
});
setTopicMetaData(topics);
});
};
// 获取 Group 元信息
const getGroupMetaData = () => {
Utils.request(api.getGroupOverview(+clusterId), {
method: 'GET',
}).then((res: any) => {
const groups = res?.bizData.map((item: any) => {
return {
label: item.name,
value: item.name,
};
});
setGroupMetaData(groups);
});
};
// 获取 kafkaUser 列表
const getKafkaUserList = () => {
Utils.request(api.getKafkaUsers(clusterId), {
method: 'GET',
}).then((res: UsersProps[]) => {
setKafkaUserOptions(res.map(({ name }) => ({ label: name, value: name })));
});
};
// 提交表单
const onSubmit = () => {
form.validateFields().then((formData) => {
const submitData = [];
const { configType, principle, kafkaUser } = formData;
if (configType === 'custom') {
// 1. 自定义权限
// TODO: 需要和后端联调
const { resourceType, resourcePatternType, aclPermissionType, aclOperation, aclClientHost } = formData;
submitData.push({
clusterId,
kafkaUser: principle === 'all' ? '*' : kafkaUser,
resourceType,
resourcePatternType,
resourceName: '*',
aclPermissionType,
aclOperation,
aclClientHost,
});
} else {
// 2. 生产或者消费权限
// 1). 配置生产权限将赋予 User 对应 Topic 的 Create、Write 权限
// 2). 配置消费权限将赋予 User 对应 Topic的 Read 权限和 Group 的 Read 权限
const { topicPatternType, topicPrinciple, topicName } = formData;
submitData.push({
clusterId,
kafkaUser: principle === 'all' ? '*' : kafkaUser,
resourceType: ACL_RESOURCE_TYPE.Topic,
resourcePatternType: topicPatternType,
resourceName: topicPrinciple === 'all' ? '*' : topicName,
aclPermissionType: ACL_PERMISSION_TYPE.Allow,
aclOperation: configType === 'consume' ? ACL_OPERATION.Read : ACL_OPERATION.Create,
aclClientHost: '*',
});
// 消费权限
if (configType === 'consume') {
const { groupPatternType, groupPrinciple, groupName } = formData;
submitData.push({
clusterId,
kafkaUser: principle === 'all' ? '*' : kafkaUser,
resourceType: ACL_RESOURCE_TYPE.Group,
resourcePatternType: groupPatternType,
resourceName: groupPrinciple === 'all' ? '*' : groupName,
aclPermissionType: ACL_PERMISSION_TYPE.Allow,
aclOperation: ACL_OPERATION.Read,
aclClientHost: '*',
});
} else {
submitData.push({
clusterId,
kafkaUser: principle === 'all' ? '*' : kafkaUser,
resourceType: ACL_RESOURCE_TYPE.Topic,
resourcePatternType: topicPatternType,
resourceName: topicPrinciple === 'all' ? '*' : topicName,
aclPermissionType: ACL_PERMISSION_TYPE.Allow,
aclOperation: ACL_OPERATION.Write,
aclClientHost: '*',
});
}
}
setConfirmLoading(true);
Utils.request(api.addACL, {
method: 'POST',
data: submitData,
}).then(
() => {
// 执行回调,刷新列表数据
callback.current();
onClose();
message.success('成功新增 ACL');
},
() => setConfirmLoading(false)
);
});
};
// 展开抽屉
const onOpen = (status: boolean, cbk: () => void) => {
setVisible(status);
callback.current = cbk;
};
// 关闭抽屉
const onClose = () => {
setVisible(false);
setConfirmLoading(false);
form.resetFields();
};
useImperativeHandle(ref, () => ({
onOpen,
}));
useEffect(() => {
getKafkaUserList();
getTopicMetaData('');
getGroupMetaData();
}, []);
return (
<Drawer
className="acls-edit-drawer"
title="新增ACL"
width={480}
visible={visible}
maskClosable={false}
onClose={onClose}
extra={
<Space>
<Button size="small" onClick={onClose}>
取消
</Button>
<Button type="primary" size="small" loading={confirmLoading} onClick={onSubmit}>
确定
</Button>
<Divider type="vertical" />
</Space>
}
>
<Alert
className="drawer-alert-full-screen"
message="新增 ACL 必须在集群已开启 ACL 功能时才会生效"
type="info"
showIcon
style={{ marginBottom: 20 }}
/>
<Form form={form} layout="vertical">
<Form.Item
label="ACL用途"
name="configType"
rules={[{ required: true, message: 'ACL用途不能为空' }]}
initialValue={CONFIG_TYPE[0].value}
>
<Select options={CONFIG_TYPE} />
</Form.Item>
<Form.Item label="Principle" name="principle" rules={[{ required: true, message: 'Principle 不能为空' }]} initialValue="all">
<Radio.Group>
<Radio value="all">ALL</Radio>
<Radio value="special">Special</Radio>
</Radio.Group>
</Form.Item>
<Form.Item dependencies={['principle']} style={{ marginBottom: 0 }}>
{({ getFieldValue }) =>
getFieldValue('principle') === 'special' ? (
<Form.Item name="kafkaUser" rules={[{ required: true, message: 'Kafka User 不能为空' }]}>
<Select placeholder="请选择 Kafka User" options={kafkaUserOptions} />
</Form.Item>
) : null
}
</Form.Item>
<Form.Item dependencies={['configType']} style={{ marginBottom: 0 }}>
{({ getFieldValue }) => {
const SelectFormItems = (props: { type: string }) => {
const { type } = props;
return (
<Form.Item
name={`${type}Name`}
dependencies={[`${type}PatternType`]}
validateTrigger="onBlur"
rules={[
({ getFieldValue }) => ({
validator: (rule: any, value: string) => {
if (!value) {
return Promise.reject(`${type}Name 不能为空`);
}
if (type === 'topic' && getFieldValue(`${type}PatternType`) === ACL_PATTERN_TYPE['Literal']) {
return Utils.request(api.getTopicMetadata(clusterId as any, value)).then((res: any) => {
return res?.exist ? Promise.resolve() : Promise.reject('该 Topic 不存在');
});
}
return Promise.resolve();
},
}),
]}
>
<AutoComplete
filterOption={(value, option) => {
if (option?.value.includes(value)) {
return true;
}
return false;
}}
options={type === 'topic' ? topicMetaData : groupMetaData}
placeholder={`请输入 ${type}Name`}
/>
</Form.Item>
);
};
const PatternTypeFormItems = (props: { type: string }) => {
const { type } = props;
const UpperCaseType = type[0].toUpperCase() + type.slice(1);
return (
<div className="form-item-group">
<Form.Item
label={`${UpperCaseType} Pattern Type`}
name={`${type}PatternType`}
rules={[{ required: true, message: `${UpperCaseType} Pattern Type 不能为空` }]}
initialValue={ACL_PATTERN_TYPE['Literal']}
>
<Radio.Group>
<Radio value={ACL_PATTERN_TYPE['Literal']}>Literal</Radio>
<Radio value={ACL_PATTERN_TYPE['Prefixed']}>Prefixed</Radio>
</Radio.Group>
</Form.Item>
<Form.Item
label={UpperCaseType}
name={`${type}Principle`}
rules={[{ required: true, message: `${UpperCaseType} 不能为空` }]}
initialValue="all"
>
<Radio.Group>
<Radio value="all">ALL</Radio>
<Radio value="special">Special</Radio>
</Radio.Group>
</Form.Item>
<Form.Item dependencies={[`${type}Principle`]} style={{ marginBottom: 0 }}>
{({ getFieldValue }) =>
getFieldValue(`${type}Principle`) === 'special' ? (
<Form.Item
name={`${type}Name`}
dependencies={[`${type}PatternType`]}
validateTrigger="onBlur"
rules={[
({ getFieldValue }) => ({
validator: (rule: any, value: string) => {
if (!value) {
return Promise.reject(`${UpperCaseType}Name 不能为空`);
}
if (type === 'topic' && getFieldValue(`${type}PatternType`) === ACL_PATTERN_TYPE['Literal']) {
return Utils.request(api.getTopicMetadata(clusterId as any, value)).then((res: any) => {
return res?.exist ? Promise.resolve() : Promise.reject('该 Topic 不存在');
});
}
return Promise.resolve();
},
}),
]}
>
<AutoComplete
filterOption={(value, option) => {
if (option?.value.includes(value)) {
return true;
}
return false;
}}
options={type === 'topic' ? topicMetaData : groupMetaData}
placeholder={`请输入 ${type}Name`}
/>
</Form.Item>
) : null
}
</Form.Item>
</div>
);
};
const CustomFormItems = () => {
return (
<>
<Form.Item
label="Permission Type"
name="aclPermissionType"
rules={[{ required: true, message: 'Permission Type 不能为空' }]}
initialValue={ACL_PERMISSION_TYPE['Allow']}
>
<Radio.Group>
<Radio value={ACL_PERMISSION_TYPE['Allow']}>Allow</Radio>
<Radio value={ACL_PERMISSION_TYPE['Deny']}>Deny</Radio>
</Radio.Group>
</Form.Item>
<Form.Item
label="Pattern Type"
name="resourcePatternType"
rules={[{ required: true, message: 'Pattern Type 不能为空' }]}
initialValue={ACL_PATTERN_TYPE['Literal']}
>
<Radio.Group>
<Radio value={ACL_PATTERN_TYPE['Literal']}>Literal</Radio>
<Radio value={ACL_PATTERN_TYPE['Prefixed']}>Prefixed</Radio>
</Radio.Group>
</Form.Item>
<Form.Item
label="Resource Type"
name="resourceType"
rules={[{ required: true, message: 'Resource Type 不能为空' }]}
initialValue={ACL_RESOURCE_TYPE['Cluster']}
>
<Select
placeholder="请选择 Resource Type"
options={Object.keys(RESOURCE_TO_OPERATIONS_MAP).map((type: RESOURCE_MAP_KEYS) => ({
label: type,
value: ACL_RESOURCE_TYPE[type],
}))}
/>
</Form.Item>
<Form.Item dependencies={['resourceType']}>
{({ getFieldValue }) => {
const type = getFieldValue('resourceType');
if (type === ACL_RESOURCE_TYPE['Cluster'] || type === ACL_RESOURCE_TYPE['TransactionalId']) {
//TODO需要和后端获取集群和事务接口联调
return (
<Form.Item
name={`${type === 4 ? 'cluster' : 'transactionalId'}`}
rules={[{ required: true, message: `${type === 4 ? 'Cluster名称' : 'TransactionalId'} 不能为空` }]}
>
<Input placeholder={`请输入${type === 4 ? 'Cluster名称' : 'TransactionalId'}`}></Input>
</Form.Item>
);
} else if (type === ACL_RESOURCE_TYPE['Topic']) {
return <PatternTypeFormItems type="topic" />;
} else if (type === ACL_RESOURCE_TYPE['Group']) {
return <PatternTypeFormItems type="group" />;
}
return null;
}}
</Form.Item>
<Form.Item dependencies={['resourceType']} style={{ marginBottom: 0 }}>
{({ getFieldValue }) => {
form.resetFields(['aclOperation']);
return (
<Form.Item label="Operation" name="aclOperation" rules={[{ required: true, message: 'Operation 不能为空' }]}>
<Select
placeholder="请选择 Resource Type"
options={RESOURCE_TO_OPERATIONS_MAP[ACL_RESOURCE_TYPE[getFieldValue('resourceType')] as RESOURCE_MAP_KEYS].map(
(type) => ({
label: type,
value: ACL_OPERATION[type as keyof typeof ACL_OPERATION],
})
)}
/>
</Form.Item>
);
}}
</Form.Item>
<Form.Item label="Host" name="aclClientHost" initialValue="*">
<Input />
</Form.Item>
</>
);
};
const type = getFieldValue('configType');
if (type === 'produce') {
return <PatternTypeFormItems type="topic" />;
} else if (type === 'consume') {
return (
<>
<PatternTypeFormItems type="topic" />
<PatternTypeFormItems type="group" />
</>
);
} else if (type === 'custom') {
return <CustomFormItems />;
} else {
return null;
}
}}
</Form.Item>
</Form>
</Drawer>
);
});
export default AddDrawer;