|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making |
| 3 | + * 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available. |
| 4 | + * Copyright (C) 2017 THL A29 Limited, |
| 5 | + * a Tencent company. All rights reserved. |
| 6 | + * Licensed under the MIT License (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at http://opensource.org/licenses/MIT |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the License is distributed on |
| 11 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 12 | + * either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations under the License. |
| 14 | + * We undertake not to change the open source license (MIT license) applicable |
| 15 | + * to the current version of the project delivered to anyone in the future. |
| 16 | + */ |
| 17 | + |
| 18 | +package user |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "configcenter/src/common/blog" |
| 28 | + httpheader "configcenter/src/common/http/header" |
| 29 | + "configcenter/src/common/http/header/util" |
| 30 | + "configcenter/src/thirdparty/apigw/apigwutil" |
| 31 | + "configcenter/src/thirdparty/apigw/user/types" |
| 32 | +) |
| 33 | + |
| 34 | +// ClientI is the cmdb api gateway client |
| 35 | +type ClientI interface { |
| 36 | + BatchSearchVirtualUser(ctx context.Context, h http.Header, loginNames []string) ([]types.VirtualUserItem, error) |
| 37 | +} |
| 38 | + |
| 39 | +var ( |
| 40 | + virtualUserAuth = make(map[string]string) |
| 41 | + lock sync.RWMutex |
| 42 | +) |
| 43 | + |
| 44 | +// getAuthConfigByTenant get virtual user by tenantID |
| 45 | +func getAuthConfigByTenant(tenantID string) (string, bool) { |
| 46 | + lock.RLock() |
| 47 | + defer lock.RUnlock() |
| 48 | + authConfig, exist := virtualUserAuth[tenantID] |
| 49 | + if !exist { |
| 50 | + return "", false |
| 51 | + } |
| 52 | + return authConfig, true |
| 53 | +} |
| 54 | + |
| 55 | +// setAuthConfig set virtual user by tenantID |
| 56 | +func setAuthConfig(tenantID string, authConfig string) { |
| 57 | + lock.Lock() |
| 58 | + defer lock.Unlock() |
| 59 | + virtualUserAuth[tenantID] = authConfig |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +// SetBKAuthHeader set api gateway authorization header |
| 64 | +func SetBKAuthHeader(ctx context.Context, conf *apigwutil.ApiGWConfig, header http.Header, |
| 65 | + userCli ClientI) (http.Header, error) { |
| 66 | + |
| 67 | + tenantID := httpheader.GetTenantID(header) |
| 68 | + if tenantID == "" { |
| 69 | + tenantID = util.GetDefaultTenant() |
| 70 | + httpheader.SetTenantID(header, tenantID) |
| 71 | + } |
| 72 | + |
| 73 | + if authInfo, exist := getAuthConfigByTenant(tenantID); exist { |
| 74 | + header = httpheader.SetBkAuth(header, authInfo) |
| 75 | + return header, nil |
| 76 | + } |
| 77 | + |
| 78 | + authConf := apigwutil.AuthConfig{ |
| 79 | + AppAuthConfig: apigwutil.AppAuthConfig{ |
| 80 | + AppCode: conf.AppCode, |
| 81 | + AppSecret: conf.AppSecret, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + resp, err := userCli.BatchSearchVirtualUser(ctx, header, []string{"bk_admin"}) |
| 86 | + if err != nil { |
| 87 | + blog.Errorf("search virtual user failed, err: %v", err) |
| 88 | + return header, err |
| 89 | + } |
| 90 | + |
| 91 | + if len(resp) != 1 { |
| 92 | + blog.Errorf("search virtual user failed, resp: %v", resp) |
| 93 | + return header, fmt.Errorf("search virtual user failed, resp: %v", resp) |
| 94 | + } |
| 95 | + |
| 96 | + authConf.UserName = resp[0].VirtualUserName |
| 97 | + |
| 98 | + authInfo, err := json.Marshal(authConf) |
| 99 | + if err != nil { |
| 100 | + blog.Errorf("marshal default api auth config %+v failed, err: %v", authConf, err) |
| 101 | + return header, err |
| 102 | + } |
| 103 | + |
| 104 | + header = httpheader.SetBkAuth(header, string(authInfo)) |
| 105 | + setAuthConfig(tenantID, string(authInfo)) |
| 106 | + return header, nil |
| 107 | +} |
0 commit comments