|
| 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 | + |
| 26 | + "configcenter/pkg/tenant" |
| 27 | + "configcenter/src/common/blog" |
| 28 | + httpheader "configcenter/src/common/http/header" |
| 29 | + "configcenter/src/thirdparty/apigw/apigwutil" |
| 30 | + "configcenter/src/thirdparty/apigw/user" |
| 31 | +) |
| 32 | + |
| 33 | +// ClientI is the cmdb api gateway client |
| 34 | +type ClientI interface { |
| 35 | + BatchSearchVirtualUser(ctx context.Context, h http.Header, loginNames []string) ([]user.VirtualUserItem, error) |
| 36 | +} |
| 37 | + |
| 38 | +// SetBKAuthHeader set api gateway authorization header |
| 39 | +func SetBKAuthHeader(ctx context.Context, conf *apigwutil.ApiGWConfig, header http.Header, |
| 40 | + userCli ClientI) (http.Header, error) { |
| 41 | + |
| 42 | + tenantInfo, exist := tenant.GetTenant(httpheader.GetTenantID(header)) |
| 43 | + if !exist { |
| 44 | + blog.Errorf("tenant not exist") |
| 45 | + return header, fmt.Errorf("tenant not exist") |
| 46 | + } |
| 47 | + |
| 48 | + authConf := apigwutil.AuthConfig{ |
| 49 | + AppAuthConfig: apigwutil.AppAuthConfig{ |
| 50 | + AppCode: conf.AppCode, |
| 51 | + AppSecret: conf.AppSecret, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + if len(tenantInfo.UserName) > 0 { |
| 56 | + authConf.UserName = tenantInfo.UserName |
| 57 | + } else { |
| 58 | + resp, err := userCli.BatchSearchVirtualUser(ctx, header, []string{"bk_admin"}) |
| 59 | + if err != nil { |
| 60 | + blog.Errorf("search virtual user failed, err: %v", err) |
| 61 | + return header, err |
| 62 | + } |
| 63 | + |
| 64 | + if len(resp) != 1 { |
| 65 | + blog.Errorf("search virtual user failed, resp: %v", resp) |
| 66 | + return header, fmt.Errorf("search virtual user failed, resp: %v", resp) |
| 67 | + } |
| 68 | + |
| 69 | + authConf.UserName = resp[0].VirtualUserName |
| 70 | + tenant.SetTenantUserName(httpheader.GetTenantID(header), resp[0].VirtualUserName) |
| 71 | + } |
| 72 | + |
| 73 | + authInfo, err := json.Marshal(authConf) |
| 74 | + if err != nil { |
| 75 | + blog.Errorf("marshal default api auth config %+v failed, err: %v", authConf, err) |
| 76 | + return header, err |
| 77 | + } |
| 78 | + |
| 79 | + return httpheader.SetBkAuth(header, string(authInfo)), nil |
| 80 | +} |
0 commit comments