forked from TencentBlueKing/bk-nodeman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: 云梯安全组添加白名单功能 (closed TencentBlueKing#1760)
- Loading branch information
Showing
8 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
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 copy | ||
from dataclasses import _is_dataclass_instance, fields | ||
|
||
|
||
def _asdict_inner(obj, dict_factory): | ||
if _is_dataclass_instance(obj): | ||
result = [] | ||
for f in fields(obj): | ||
value = _asdict_inner(getattr(obj, f.name), dict_factory) | ||
# 过滤掉为空或者None的字段 | ||
if not value: | ||
continue | ||
result.append((f.name, value)) | ||
return dict_factory(result) | ||
elif isinstance(obj, (list, tuple)): | ||
return type(obj)(_asdict_inner(v, dict_factory) for v in obj) | ||
elif isinstance(obj, dict): | ||
return type(obj)((_asdict_inner(k, dict_factory), _asdict_inner(v, dict_factory)) for k, v in obj.items()) | ||
else: | ||
return copy.deepcopy(obj) | ||
|
||
|
||
def asdict(obj, *, dict_factory=dict): | ||
if not _is_dataclass_instance(obj): | ||
raise TypeError("asdict() should be called on dataclass instances") | ||
return _asdict_inner(obj, dict_factory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
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 hashlib | ||
import hmac | ||
import time | ||
import typing | ||
|
||
from django.conf import settings | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from ..base import DataAPI | ||
from ..domains import BKAPP_YUNTI_API_ROOT | ||
|
||
|
||
def yunti_api_before_request(params) -> typing.Dict[str, typing.Any]: | ||
api_ts: int = int(time.time()) | ||
api_key_name: str = settings.BKAPP_REQUEST_YUNTI_APP_KEY | ||
api_key_secret: str = settings.BKAPP_REQUEST_YUNTI_SECRET | ||
api_sign: str = hmac.new( | ||
api_key_secret.encode("utf8"), | ||
(str(api_ts) + api_key_name).encode("utf8"), | ||
hashlib.sha1, | ||
).hexdigest() | ||
|
||
params["api_key"] = api_key_name | ||
params["api_ts"] = api_ts | ||
params["api_sigin"] = api_sign | ||
params["jsonrpc"] = "2.0" | ||
params["id"] = str(api_ts) | ||
return params | ||
|
||
|
||
def yunti_api_after_request(result): | ||
data = result.pop("result") | ||
result["data"] = data | ||
result["result"] = True | ||
return result | ||
|
||
|
||
class _YunTiApi(object): | ||
MODULE = _("云梯") | ||
|
||
def __init__(self): | ||
|
||
self.get_security_group_details = DataAPI( | ||
method="POST", | ||
url=BKAPP_YUNTI_API_ROOT + "/account/manage?api_key={api_key}&api_sign={api_sigin}&api_ts={api_ts}", | ||
module=self.MODULE, | ||
description="查询安全组详情", | ||
before_request=yunti_api_before_request, | ||
after_request=yunti_api_after_request, | ||
) | ||
|
||
self.operate_security_group = DataAPI( | ||
method="POST", | ||
url=BKAPP_YUNTI_API_ROOT + "/apply/api/sg?api_key={api_key}&api_sign={api_sigin}&api_ts={api_ts}", | ||
module=self.MODULE, | ||
description="修改安全组", | ||
before_request=yunti_api_before_request, | ||
after_request=yunti_api_after_request, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters