Skip to content

Commit

Permalink
feat: 支持websocket #2494
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuwan committed Nov 19, 2024
1 parent ae04b5b commit aec4be1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions src/gateway/auth.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
3 changes: 3 additions & 0 deletions src/gateway/lua/auth/auth_web.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions src/gateway/lua/auth/auth_websocket.lua
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 5 additions & 4 deletions src/gateway/vhosts/bkrepo.websocket.conf
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -43,4 +44,4 @@ location ~ /websocket/(ws/app.*) {
proxy_set_header Connection "Upgrade";
proxy_http_version 1.1;
proxy_pass http://$target/$path?$args;
}
}

0 comments on commit aec4be1

Please sign in to comment.