Skip to content

Commit

Permalink
fix(backend): 增加po环境全流量转发开关 #6630
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud authored and zhangzhw8 committed Sep 3, 2024
1 parent c1d8430 commit f209824
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
7 changes: 4 additions & 3 deletions dbm-ui/backend/bk_web/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,17 @@ def __parser_request_url(self, request):
"""外部转发接口进行路由映射"""
if self.__check_non_proxy_routing(request):
return
self.__verify_request_url(request)
self.__check_specific_request_params(request)
if env.ENABLE_EXTERNAL_PROXY:
self.__verify_request_url(request)
self.__check_specific_request_params(request)
request.path = f"/external/{request.path.lstrip('/')}"
request.path_info = request.path
setattr(request, "_dont_enforce_csrf_checks", True)

@error_handler(return_response=True)
def __call__(self, request):
# 外部路由转发仅提供给外部环境使用
if env.ENABLE_EXTERNAL_PROXY:
if env.ENABLE_EXTERNAL_PROXY or env.ENABLE_OPEN_EXTERNAL_PROXY:
self.__parser_request_url(request)
else:
# 解析来自外部转发的header
Expand Down
4 changes: 4 additions & 0 deletions dbm-ui/backend/bk_web/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def after_response(self, request, response, *args, **kwargs):
if ".css" in request.path:
return HttpResponse(response, headers={"Content-Type": "text/css"})

# 仅转发,提前返回response
if env.ENABLE_OPEN_EXTERNAL_PROXY:
return HttpResponse(response)

# 屏蔽iam申请的内部路由
if "/iam/get_apply_data/" in request.path or "/iam/simple_get_apply_data/" in request.path:
data = response.json()["data"]
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/configuration/views/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def environ(self, request):
"ENABLE_EXTERNAL_PROXY": env.ENABLE_EXTERNAL_PROXY,
}
# 非外部环境,补充额外环境变量
if not env.ENABLE_EXTERNAL_PROXY:
if not env.ENABLE_EXTERNAL_PROXY and not env.ENABLE_OPEN_EXTERNAL_PROXY:
envs.update(
{
"CC_IDLE_MODULE_ID": CcManage(env.DBA_APP_BK_BIZ_ID, "").get_biz_internal_module(
Expand Down
2 changes: 2 additions & 0 deletions dbm-ui/backend/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

# 开启外部路由,供外部环境使用(DBConsole)
ENABLE_EXTERNAL_PROXY = get_type_env(key="ENABLE_EXTERNAL_PROXY", _type=bool, default=False)
# 开启所有路由,不屏蔽。!!这里只用在合作伙伴环境!!
ENABLE_OPEN_EXTERNAL_PROXY = get_type_env(key="ENABLE_OPEN_EXTERNAL_PROXY", _type=bool, default=False)

# ITSM 服务ID
BK_ITSM_PROJECT_KEY = get_type_env(key="BK_ITSM_PROJECT_KEY", _type=str, default="0")
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
urlpatterns.append(path("redoc/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"))

# 外部路由转发仅提供给外部环境使用
if env.ENABLE_EXTERNAL_PROXY:
if env.ENABLE_EXTERNAL_PROXY or env.ENABLE_OPEN_EXTERNAL_PROXY:
methods_map = {method: "external_proxy" for method in views.APIView.http_method_names}
urlpatterns.append(
re_path(
Expand Down

0 comments on commit f209824

Please sign in to comment.