From aec4be16fb846ddf3c2b0228907f79a948a5eff6 Mon Sep 17 00:00:00 2001 From: yaoxuwan Date: Fri, 15 Nov 2024 15:43:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81websocket=20#2494?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/WebsocketConfiguration.kt | 5 +- .../websocket/constant/WebsocketKeys.kt | 1 + src/gateway/auth.conf | 6 ++ src/gateway/lua/auth/auth_web.lua | 3 + src/gateway/lua/auth/auth_websocket.lua | 55 +++++++++++++++++++ src/gateway/vhosts/bkrepo.websocket.conf | 9 +-- 6 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/gateway/lua/auth/auth_websocket.lua diff --git a/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/config/WebsocketConfiguration.kt b/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/config/WebsocketConfiguration.kt index 4832a58839..aced41f2e9 100644 --- a/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/config/WebsocketConfiguration.kt +++ b/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/config/WebsocketConfiguration.kt @@ -30,6 +30,7 @@ package com.tencent.bkrepo.websocket.config import com.tencent.bkrepo.common.security.http.jwt.JwtAuthProperties import com.tencent.bkrepo.common.security.manager.AuthenticationManager import com.tencent.bkrepo.websocket.constant.APP_ENDPOINT +import com.tencent.bkrepo.websocket.constant.DESKTOP_ENDPOINT import com.tencent.bkrepo.websocket.constant.USER_ENDPOINT import com.tencent.bkrepo.websocket.dispatch.push.TransferPush import com.tencent.bkrepo.websocket.handler.SessionWebSocketHandlerDecoratorFactory @@ -68,9 +69,9 @@ class WebsocketConfiguration( } override fun registerStompEndpoints(registry: StompEndpointRegistry) { - registry.addEndpoint(USER_ENDPOINT, APP_ENDPOINT) + registry.addEndpoint(USER_ENDPOINT, APP_ENDPOINT, DESKTOP_ENDPOINT) .setAllowedOriginPatterns("*") - registry.addEndpoint(USER_ENDPOINT, APP_ENDPOINT) + registry.addEndpoint(USER_ENDPOINT, APP_ENDPOINT, DESKTOP_ENDPOINT) .setAllowedOriginPatterns("*") .withSockJS() } diff --git a/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/constant/WebsocketKeys.kt b/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/constant/WebsocketKeys.kt index 42d405be9b..bf0769d39c 100644 --- a/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/constant/WebsocketKeys.kt +++ b/src/backend/websocket/biz-websocket/src/main/kotlin/com/tencent/bkrepo/websocket/constant/WebsocketKeys.kt @@ -29,5 +29,6 @@ package com.tencent.bkrepo.websocket.constant const val USER_ENDPOINT = "/ws/user" const val APP_ENDPOINT = "/ws/app" +const val DESKTOP_ENDPOINT = "/ws/desktop" const val SESSION_ID = "sessionId" diff --git a/src/gateway/auth.conf b/src/gateway/auth.conf index ff05036afc..7e66552fe0 100644 --- a/src/gateway/auth.conf +++ b/src/gateway/auth.conf @@ -5,3 +5,9 @@ internal; content_by_lua_file 'conf/lua/auth/auth_web.lua'; } + + # websocket验证身份 + location = /auth/websocket { + internal; + content_by_lua_file 'conf/lua/auth/auth_websocket.lua'; + } \ No newline at end of file diff --git a/src/gateway/lua/auth/auth_web.lua b/src/gateway/lua/auth/auth_web.lua index 3471d42bea..4740e9aac9 100644 --- a/src/gateway/lua/auth/auth_web.lua +++ b/src/gateway/lua/auth/auth_web.lua @@ -52,6 +52,9 @@ elseif config.auth_mode == "ticket" then local bk_ticket = cookieUtil:get_cookie("bk_ticket") if bk_ticket == nil then bk_ticket = ngx.var.http_x_devops_bk_ticket + if bk_ticket == nil then + bk_ticket = urlUtil:parseUrl(ngx.var.request_uri)["x-devops-bk-ticket"] + end if bk_ticket == nil then ngx.exit(401) return diff --git a/src/gateway/lua/auth/auth_websocket.lua b/src/gateway/lua/auth/auth_websocket.lua new file mode 100644 index 0000000000..b614836667 --- /dev/null +++ b/src/gateway/lua/auth/auth_websocket.lua @@ -0,0 +1,55 @@ +--[[ +Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + +Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + +BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + +A copy of the MIT License is included in this file. + + +Terms of the MIT License: +--------------------------------------------------- +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] + +--- 获取Url请求参数中bk_token 和 bk_ticket + +local token, username + +local bk_ticket = urlUtil:parseUrl(ngx.var.request_uri)["x-devops-bk-ticket"] +local bk_token = urlUtil:parseUrl(ngx.var.request_uri)["x-devops-bk-token"] +local platform_token = ngx.var.http_authorization + +if platform_token ~= nil and string.find(string.lower(platform_token), "^platform") then + ngx.header["x-bkrepo-authorization"] = platform_token + ngx.header["x-bkrepo-uid"] = ngx.var.http_x_bkrepo_uid + ngx.exit(200) + return +end + +if bk_ticket == nil and bk_token == nil then + ngx.exit(401) + return +end + +if bk_ticket ~= nil then + username = oauthUtil:verify_ticket(bk_ticket, "ticket") + token = bk_ticket +end + +if bk_token ~= nil then + username = oauthUtil:verify_tai_token(bk_token) + token = bk_token +end + +--- 设置用户信息 +ngx.header["authorization"] = config.bkrepo.authorization +ngx.header["x-bkrepo-uid"] = username +ngx.header["x-bkrepo-bk-token"] = token +ngx.header["x-bkrepo-access-token"] = token +ngx.exit(200) diff --git a/src/gateway/vhosts/bkrepo.websocket.conf b/src/gateway/vhosts/bkrepo.websocket.conf index 62886b8b5c..66f2672166 100644 --- a/src/gateway/vhosts/bkrepo.websocket.conf +++ b/src/gateway/vhosts/bkrepo.websocket.conf @@ -1,10 +1,11 @@ -location ~ /websocket/(ws/user.*) { +location ~ /websocket/(ws/user/.*|ws/desktop/.*) { header_filter_by_lua_file 'conf/lua/cors_filter.lua'; - auth_request /auth/web; + auth_request /auth/websocket; # 设置auth的变量 auth_request_set $uid $sent_http_x_bkrepo_uid; auth_request_set $accessToken $sent_http_x_bkrepo_access_token; auth_request_set $bk_token $sent_http_x_bkrepo_bk_token; + auth_request_set $authorization $sent_http_x_bkrepo_authorization; set $service "websocket"; set $path $1; @@ -15,7 +16,7 @@ location ~ /websocket/(ws/user.*) { proxy_set_header X-DEVOPS-BK-TOKEN $bk_token; proxy_set_header X-DEVOPS-BK-TICKET $bk_token; proxy_set_header X-DEVOPS-ACCESS-TOKEN $accessToken; - proxy_set_header authorization "$bkrepo_authorization"; + proxy_set_header authorization $authorization; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; @@ -43,4 +44,4 @@ location ~ /websocket/(ws/app.*) { proxy_set_header Connection "Upgrade"; proxy_http_version 1.1; proxy_pass http://$target/$path?$args; -} \ No newline at end of file +}