Skip to content

Commit 51949c1

Browse files
committed
feat(backend): 增加资源池相关权限管控 #7747
1 parent cbc3331 commit 51949c1

File tree

6 files changed

+88
-29
lines changed

6 files changed

+88
-29
lines changed

dbm-ui/backend/db_dirty/views.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ class DBDirtyMachineViewSet(viewsets.SystemViewSet):
3737
pagination_class = AuditedLimitOffsetPagination
3838
filter_class = None
3939

40-
action_permission_map = {("query_operation_list",): []}
41-
default_permission_class = [ResourceActionPermission([ActionEnum.DIRTY_POLL_MANAGE])]
40+
action_permission_map = {
41+
(
42+
"list_machine_events",
43+
"get_host_current_events",
44+
"query_machine_pool",
45+
): [ResourceActionPermission([ActionEnum.RESOURCE_MANAGE])],
46+
("transfer_hosts_to_pool",): [ResourceActionPermission([ActionEnum.RESOURCE_POLL_MANAGE])],
47+
}
48+
default_permission_class = [ResourceActionPermission([ActionEnum.RESOURCE_POLL_MANAGE])]
4249

4350
@common_swagger_auto_schema(
4451
operation_summary=_("将主机转移至待回收/故障池模块"),

dbm-ui/backend/db_services/dbresource/serializers.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from rest_framework import serializers
1515

1616
from backend import env
17+
from backend.components.hcm.client import HCMApi
1718
from backend.configuration.constants import DBType
1819
from backend.constants import INT_MAX
1920
from backend.db_dirty.constants import MachineEventType
@@ -51,14 +52,24 @@ class ResourceHostSerializer(serializers.Serializer):
5152
return_resource = serializers.BooleanField(help_text=_("是否为退回资源"), required=False)
5253

5354
def validate(self, attrs):
54-
host_ids = [host["host_id"] for host in attrs["hosts"]]
55+
host_id__ip_map = {host["host_id"]: host["ip"] for host in attrs["hosts"]}
56+
host_ids = list(host_id__ip_map.keys())
5557

5658
# 如果主机存在元数据,则拒绝导入
5759
exist_hosts = list(Machine.objects.filter(bk_host_id__in=host_ids).values_list("ip", flat=True))
5860
if exist_hosts:
5961
raise serializers.ValidationError(_("导入主机{}存在元数据,请检查后重新导入").format(exist_hosts))
6062

61-
# TODO:如果主机存在裁撤单 / uwork单,则不允许导入
63+
# 存在uwork或者是待裁撤主机,则不允许导入
64+
check_work = HCMApi.check_host_has_uwork(host_ids)
65+
if check_work:
66+
ips = [host_id__ip_map[host_id] for host_id in check_work.keys()]
67+
raise serializers.ValidationError(_("导入主机{}存在uwork单据,请处理后重新导入").format(ips))
68+
69+
check_dissolved = HCMApi.check_host_is_dissolved(host_ids)
70+
if check_dissolved:
71+
ips = [host_id__ip_map[host_id] for host_id in check_dissolved]
72+
raise serializers.ValidationError(_("导入主机包含裁撤主机:{},无法进行导入").format(ips))
6273

6374
return attrs
6475

dbm-ui/backend/db_services/dbresource/views/resource.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,19 @@ class DBResourceViewSet(viewsets.SystemViewSet):
8888
"resource_confirm",
8989
"resource_delete",
9090
"resource_update",
91+
"append_labels",
9192
): [ResourceActionPermission([ActionEnum.RESOURCE_POLL_MANAGE])],
9293
(
9394
"spec_resource_count",
95+
"spec_cost_estimate",
9496
"get_mountpoints",
9597
"get_disktypes",
9698
"get_subzones",
9799
"get_device_class",
100+
"list_dba_hosts",
101+
"query_dba_hosts",
102+
"resource_import_urls",
98103
): [],
99-
("query_operation_list",): [ResourceActionPermission([ActionEnum.RESOURCE_OPERATION_VIEW])],
100104
}
101105
default_permission_class = [ResourceActionPermission([ActionEnum.RESOURCE_MANAGE])]
102106
filter_class = None

dbm-ui/backend/db_services/tag/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from backend.db_services.tag import serializers
2222
from backend.db_services.tag.filters import TagListFilter
2323
from backend.db_services.tag.handlers import TagHandler
24+
from backend.iam_app.handlers.drf_perm.tag import TagPermission
2425

2526
SWAGGER_TAG = _("标签")
2627

@@ -46,9 +47,8 @@ class TagViewSet(AuditedModelViewSet):
4647
filter_class = TagListFilter
4748
ordering_fields = ["create_at", "creator"]
4849

49-
action_permission_map = {("related_resources",): []}
50-
# TODO:需要约定标签的权限
51-
default_permission_class = []
50+
action_permission_map = {("related_resources", "list", "verify_duplicated"): []}
51+
default_permission_class = [TagPermission()]
5252

5353
@common_swagger_auto_schema(
5454
operation_summary=_("查询标签关联资源"), request_body=serializers.QueryRelatedResourceSerializer(), tags=[SWAGGER_TAG]

dbm-ui/backend/iam_app/dataclass/actions.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ class ActionEnum:
10581058

10591059
REDIS_ACCESS_ENTRY_VIEW = ActionMeta(
10601060
id="redis_access_entry_view",
1061-
name=_("Redis 获取访问方式"),
1061+
name=_("Redis 集群访问"),
10621062
name_en="redis_access_entry_view",
10631063
type="view",
10641064
related_actions=[DB_MANAGE.id],
@@ -1174,7 +1174,7 @@ class ActionEnum:
11741174

11751175
ES_ACCESS_ENTRY_VIEW = ActionMeta(
11761176
id="es_access_entry_view",
1177-
name=_("ES 获取访问方式"),
1177+
name=_("ES 集群访问"),
11781178
name_en="es_access_entry_view",
11791179
type="view",
11801180
related_actions=[DB_MANAGE.id],
@@ -1229,7 +1229,7 @@ class ActionEnum:
12291229

12301230
DORIS_ACCESS_ENTRY_VIEW = ActionMeta(
12311231
id="doris_access_entry_view",
1232-
name=_("Doris 获取访问方式"),
1232+
name=_("Doris 集群访问"),
12331233
name_en="doris_access_entry_view",
12341234
type="view",
12351235
related_actions=[DB_MANAGE.id],
@@ -1265,7 +1265,7 @@ class ActionEnum:
12651265

12661266
KAFKA_ACCESS_ENTRY_VIEW = ActionMeta(
12671267
id="kafka_access_entry_view",
1268-
name=_("Kafka 获取访问方式"),
1268+
name=_("Kafka 集群访问"),
12691269
name_en="kafka_access_entry_view",
12701270
type="view",
12711271
related_actions=[DB_MANAGE.id],
@@ -1317,7 +1317,7 @@ class ActionEnum:
13171317

13181318
HDFS_ACCESS_ENTRY_VIEW = ActionMeta(
13191319
id="hdfs_access_entry_view",
1320-
name=_("HDFS 获取访问方式"),
1320+
name=_("HDFS 集群访问"),
13211321
name_en="hdfs_access_entry_view",
13221322
type="view",
13231323
related_actions=[DB_MANAGE.id],
@@ -1361,7 +1361,7 @@ class ActionEnum:
13611361

13621362
PULSAR_ACCESS_ENTRY_VIEW = ActionMeta(
13631363
id="pulsar_access_entry_view",
1364-
name=_("Pulsar 获取访问方式"),
1364+
name=_("Pulsar 集群访问"),
13651365
name_en="pulsar_access_entry_view",
13661366
type="view",
13671367
related_actions=[DB_MANAGE.id],
@@ -1406,7 +1406,7 @@ class ActionEnum:
14061406

14071407
RIAK_ACCESS_ENTRY_VIEW = ActionMeta(
14081408
id="riak_access_entry_view",
1409-
name=_("Riak 获取访问方式"),
1409+
name=_("Riak 集群访问"),
14101410
name_en="riak_access_entry_view",
14111411
type="view",
14121412
related_actions=[DB_MANAGE.id],
@@ -1652,7 +1652,7 @@ class ActionEnum:
16521652

16531653
RESOURCE_POLL_MANAGE = ActionMeta(
16541654
id="resource_pool_manage",
1655-
name=_("资源池管理"),
1655+
name=_("资源管理"),
16561656
name_en="resource_pool_manage",
16571657
type="manage",
16581658
related_actions=[RESOURCE_MANAGE.id],
@@ -1662,27 +1662,27 @@ class ActionEnum:
16621662
hidden=True,
16631663
)
16641664

1665-
RESOURCE_OPERATION_VIEW = ActionMeta(
1666-
id="resource_operation_view",
1667-
name=_("资源池操作记录查看"),
1668-
name_en="resource_operation_view",
1669-
type="view",
1665+
GLOBAL_RESOURCE_TAG_MANAGE = ActionMeta(
1666+
id="global_resource_tag_manage",
1667+
name=_("全局资源标签管理"),
1668+
name_en="global_resource_tag_manage",
1669+
type="manage",
16701670
related_actions=[RESOURCE_MANAGE.id],
16711671
related_resource_types=[],
16721672
group=_("资源管理"),
1673-
subgroup=_("资源池"),
1673+
subgroup=_("标签"),
16741674
hidden=True,
16751675
)
16761676

1677-
DIRTY_POLL_MANAGE = ActionMeta(
1678-
id="dirty_pool_manage",
1679-
name=_("污点池管理"),
1680-
name_en="dirty_pool_manage",
1677+
RESOURCE_TAG_MANAGE = ActionMeta(
1678+
id="resource_tag_manage",
1679+
name=_("资源标签管理"),
1680+
name_en="resource_tag_manage",
16811681
type="manage",
1682-
related_actions=[RESOURCE_MANAGE.id],
1683-
related_resource_types=[],
1682+
related_actions=[DB_MANAGE.id],
1683+
related_resource_types=[ResourceEnum.BUSINESS],
16841684
group=_("资源管理"),
1685-
subgroup=_("污点池"),
1685+
subgroup=_("标签"),
16861686
hidden=True,
16871687
)
16881688

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
4+
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
5+
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at https://opensource.org/licenses/MIT
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
specific language governing permissions and limitations under the License.
10+
"""
11+
import logging
12+
13+
from backend.iam_app.dataclass.actions import ActionEnum
14+
from backend.iam_app.dataclass.resources import ResourceEnum
15+
from backend.iam_app.handlers.drf_perm.base import ResourceActionPermission, get_request_key_id
16+
17+
logger = logging.getLogger("root")
18+
19+
20+
class TagPermission(ResourceActionPermission):
21+
"""
22+
标签管理相关权限
23+
"""
24+
25+
def __init__(self, actions=None, resource_meta=None, instance_ids_getter=None):
26+
super().__init__(actions=actions, resource_meta=resource_meta, instance_ids_getter=self.instance_ids_getter)
27+
28+
def instance_ids_getter(self, request, view):
29+
# Todo 后续要考虑集群标签权限
30+
bk_biz_id = get_request_key_id(request, "bk_biz_id")
31+
if bk_biz_id:
32+
self.actions = [ActionEnum.RESOURCE_TAG_MANAGE]
33+
self.resource_meta = ResourceEnum.BUSINESS
34+
return [bk_biz_id]
35+
else:
36+
self.actions = [ActionEnum.GLOBAL_RESOURCE_TAG_MANAGE]
37+
return []

0 commit comments

Comments
 (0)