Skip to content

Commit 0a70ccd

Browse files
adevjoeevanxinli
authored andcommitted
fix(user-manager): 使用 BKLogin API 获取用户信息,支持 display_name 与 bk_token (merge request !2731)
Squash merge branch 'fix/user-tenant' into 'bk-bcs-multitenant' ## 变更说明 - 新增 bklogin.go:使用 BKLogin API(bk-tokens/userinfo)替代 BKUser API 获取用户信息 - GetBKUserInfo 改为通过 bk_token 参数获取用户信息,替代原先的 username 方式 - Userinfo 模型新增 DisplayName 字段 - GetCurrentUserInfo 从 bk_token cookie 获取 token,调用 BKLogin API 获取 language、timezone、display_name - TenantID 改为从 utils.GetTenantIDFromAttribute 获取 - 无 bk_token 时使用默认值:Language=zh-CN,TimeZone=Asia/Shanghai - 从 bkuser.go 移除原 GetBKUserInfo 实现 ## 变更类型 - [ ] 新功能 (feat) - [x] Bug 修复 (fix) - [x] 重构 (refactor) - [ ] 性能优化 (perf) - [ ] 文档 (docs) - [ ] 测试 (test) - [ ] 其他 (chore) ## 测试说明 待补充 ## 关联 Issue 无
1 parent 4a7a4a0 commit 0a70ccd

File tree

4 files changed

+115
-61
lines changed

4 files changed

+115
-61
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Blueking Container Service available.
3+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
* http://opensource.org/licenses/MIT
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9+
* either express or implied. See the License for the specific language governing permissions and
10+
* limitations under the License.
11+
*/
12+
13+
package component
14+
15+
import (
16+
"context"
17+
"encoding/json"
18+
"fmt"
19+
"net/http"
20+
"os"
21+
22+
"github.com/pkg/errors"
23+
24+
apputils "github.com/Tencent/bk-bcs/bcs-services/bcs-user-manager/app/utils"
25+
)
26+
27+
// BKLoginAPIServer bk login api server
28+
var BKLoginAPIServer = os.Getenv("BKLOGIN_API_SERVER")
29+
30+
// GetBKTokenUserInfoResp get bk token user info response
31+
type GetBKTokenUserInfoResp struct {
32+
Data BKUserInfo `json:"data"`
33+
}
34+
35+
// GetBKTokenUserInfoErrResp get bk token user info error response
36+
type GetBKTokenUserInfoErrResp struct {
37+
Error BKTokenUserInfoErr `json:"error"`
38+
}
39+
40+
// BKTokenUserInfoErr bk token user info error
41+
type BKTokenUserInfoErr struct {
42+
Code string `json:"code"`
43+
Message string `json:"message"`
44+
}
45+
46+
// BKUserInfo bk user info
47+
type BKUserInfo struct {
48+
BKUsername string `json:"bk_username"`
49+
TenantID string `json:"tenant_id"`
50+
DisplayName string `json:"display_name"`
51+
Language string `json:"language"`
52+
TimeZone string `json:"time_zone"`
53+
}
54+
55+
// GetBKUserInfo get bk user info by bk token
56+
func GetBKUserInfo(ctx context.Context, tenantID, bkToken string) (*BKUserInfo, error) {
57+
if bkToken == "" {
58+
return nil, nil
59+
}
60+
61+
url := fmt.Sprintf("%s/%s", BKLoginAPIServer, "login/api/v3/open/bk-tokens/userinfo/")
62+
authInfo, err := GetBKAPIAuthorization("")
63+
if err != nil {
64+
return nil, err
65+
}
66+
resp, err := GetClient().R().
67+
SetContext(ctx).
68+
SetHeader(apputils.HeaderTenantID, tenantID).
69+
SetHeader("X-Bkapi-Authorization", authInfo).
70+
SetQueryParam("bk_token", bkToken).
71+
Get(url)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
if resp.StatusCode() != http.StatusOK {
77+
errResp := &GetBKTokenUserInfoErrResp{}
78+
if err = json.Unmarshal(resp.Body(), errResp); err == nil &&
79+
(errResp.Error.Code != "" || errResp.Error.Message != "") {
80+
return nil, errors.Errorf("get bk token userinfo failed, code: %s, message: %s",
81+
errResp.Error.Code, errResp.Error.Message)
82+
}
83+
return nil, errors.Errorf("http code %d != 200, body: %s", resp.StatusCode(), resp.Body())
84+
}
85+
86+
result := &GetBKTokenUserInfoResp{}
87+
err = json.Unmarshal(resp.Body(), result)
88+
if err != nil {
89+
return nil, errors.Errorf("unmarshal resp body error, %s", err.Error())
90+
}
91+
92+
return &result.Data, nil
93+
}

bcs-services/bcs-user-manager/app/pkg/component/bkuser.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,51 +70,3 @@ func ListTenant(ctx context.Context) ([]BkTenant, error) {
7070

7171
return result.Data, nil
7272
}
73-
74-
// GetBKUserInfoResp get bk user info response
75-
type GetBKUserInfoResp struct {
76-
Data BKUserInfo `json:"data"`
77-
}
78-
79-
// BKUserInfo bk user info
80-
type BKUserInfo struct {
81-
BKUsername string `json:"bk_username"`
82-
TenantID string `json:"tenant_id"`
83-
LoginName string `json:"login_name"`
84-
DisplayName string `json:"display_name"`
85-
Language string `json:"language"`
86-
TimeZone string `json:"time_zone"`
87-
}
88-
89-
// GetBKUserInfo get bk user info
90-
func GetBKUserInfo(ctx context.Context, tenantID, username string) (*BKUserInfo, error) {
91-
url := fmt.Sprintf("%s/%s/%s/", BKUserAPIServer, "api/v3/open/tenant/users", username)
92-
93-
authInfo, err := GetBKAPIAuthorization("")
94-
if err != nil {
95-
return nil, err
96-
}
97-
resp, err := GetClient().R().
98-
SetContext(ctx).
99-
SetHeader(apputils.HeaderTenantID, tenantID).
100-
SetHeader("X-Bkapi-Authorization", authInfo).
101-
Get(url)
102-
103-
if err != nil {
104-
105-
return nil, err
106-
}
107-
108-
if !resp.IsSuccess() {
109-
return nil, errors.Errorf("http code %d != 200, body: %s", resp.StatusCode(), resp.Body())
110-
}
111-
112-
result := &GetBKUserInfoResp{}
113-
err = json.Unmarshal(resp.Body(), result)
114-
if err != nil {
115-
return nil, errors.Errorf("unmarshal resp body error, %s", err.Error())
116-
}
117-
118-
return &result.Data, nil
119-
120-
}

bcs-services/bcs-user-manager/app/user-manager/models/user.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ func (t *BcsUser) IsAdmin() bool {
5959

6060
// Userinfo response user info
6161
type Userinfo struct {
62-
UserName string `json:"username"`
63-
AvatarUrl string `json:"avatar_url"`
64-
TenantID string `json:"tenant_id"`
65-
Language string `json:"language"`
66-
TimeZone string `json:"time_zone"`
62+
UserName string `json:"username"`
63+
AvatarUrl string `json:"avatar_url"`
64+
TenantID string `json:"tenant_id"`
65+
Language string `json:"language"`
66+
TimeZone string `json:"time_zone"`
67+
DisplayName string `json:"display_name"`
6768
}
6869

6970
// BcsClientUser client user table 平台账号

bcs-services/bcs-user-manager/app/user-manager/v1http/user/user.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,27 @@ func GetCurrentUserInfo(request *restful.Request, response *restful.Response) {
312312
userInfo := &models.Userinfo{
313313
UserName: "",
314314
AvatarUrl: "",
315+
Language: "zh-CN",
316+
TimeZone: "Asia/Shanghai",
315317
}
316318
if user, ok := request.Attribute(constant.CurrentUserAttr).(*models.BcsUser); ok {
317319
userInfo.UserName = user.Name
318-
userInfo.TenantID = user.TenantID
320+
userInfo.TenantID = utils.GetTenantIDFromAttribute(request)
319321
}
320322

321-
bkUserInfo, err := component.GetBKUserInfo(request.Request.Context(), userInfo.TenantID, userInfo.UserName)
322-
if err != nil {
323-
blog.Warnf("failed to get bk user info: %s", err.Error())
324-
}
325-
if bkUserInfo != nil {
326-
userInfo.Language = bkUserInfo.Language
327-
userInfo.TimeZone = bkUserInfo.TimeZone
323+
bkTokenCookie, err := request.Request.Cookie("bk_token")
324+
if err != nil || bkTokenCookie == nil || bkTokenCookie.Value == "" {
325+
blog.Infof("GetCurrentUserInfo request has no bk_token cookie")
326+
} else {
327+
bkUserInfo, getUserErr := component.GetBKUserInfo(request.Request.Context(), userInfo.TenantID, bkTokenCookie.Value)
328+
if getUserErr != nil {
329+
blog.Warnf("failed to get bk user info by bk_token: %s", getUserErr.Error())
330+
}
331+
if bkUserInfo != nil {
332+
userInfo.Language = bkUserInfo.Language
333+
userInfo.TimeZone = bkUserInfo.TimeZone
334+
userInfo.DisplayName = bkUserInfo.DisplayName
335+
}
328336
}
329337

330338
data := utils.CreateResponseData(nil, "success", *userInfo)

0 commit comments

Comments
 (0)