Skip to content

Commit 1f8d984

Browse files
committed
feat: call other platforms using virtual user
--story=122806009
1 parent a9f8c59 commit 1f8d984

File tree

19 files changed

+324
-68
lines changed

19 files changed

+324
-68
lines changed

pkg/tenant/logics/tenant.go

+28
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ package logics
1919

2020
import (
2121
"fmt"
22+
"time"
2223

24+
"configcenter/pkg/tenant"
25+
"configcenter/src/apimachinery"
2326
"configcenter/src/common"
27+
"configcenter/src/common/blog"
28+
"configcenter/src/common/types"
2429
)
2530

2631
// ValidateDisableTenantMode validate disable multi-tenant mode
@@ -35,3 +40,26 @@ func ValidateDisableTenantMode(tenantID string, enableTenantMode bool) (string,
3540

3641
return tenantID, nil
3742
}
43+
44+
// InitTenant init tenant, refresh tenants info while server is starting
45+
func InitTenant(apiMachineryCli apimachinery.ClientSetInterface) error {
46+
coreExist := false
47+
for retry := 0; retry < 10; retry++ {
48+
if _, err := apiMachineryCli.Healthz().HealthCheck(types.CC_MODULE_CORESERVICE); err != nil {
49+
blog.Errorf("connect core server failed: %v", err)
50+
time.Sleep(time.Second * 2)
51+
continue
52+
}
53+
coreExist = true
54+
break
55+
}
56+
if !coreExist {
57+
blog.Errorf("core server not exist")
58+
return fmt.Errorf("core server not exist")
59+
}
60+
err := tenant.Init(&tenant.Options{ApiMachineryCli: apiMachineryCli})
61+
if err != nil {
62+
return err
63+
}
64+
return nil
65+
}

pkg/tenant/tenant.go

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ func GetTenant(tenantID string) (*types.Tenant, bool) {
5151
return nil, false
5252
}
5353

54+
// SetTenantUserName set tenant user name
55+
func SetTenantUserName(tenantID string, userName string) {
56+
lock.Lock()
57+
defer lock.Unlock()
58+
if data, ok := tenantMap[tenantID]; ok {
59+
data.UserName = userName
60+
}
61+
for idx := range allTenants {
62+
if allTenants[idx].TenantID == tenantID {
63+
allTenants[idx].UserName = userName
64+
break
65+
}
66+
}
67+
}
68+
5469
// Options is tenant initialize options
5570
type Options struct {
5671
DB local.DB

pkg/tenant/types/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Tenant struct {
3131
TenantID string `bson:"tenant_id"`
3232
Status Status `bson:"status"`
3333
Database string `bson:"database"`
34+
UserName string `bson:"bk_username"`
3435
}
3536

3637
// Status is the tenant status

src/apiserver/app/server.go

+2-26
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ package app
1515
import (
1616
"context"
1717
"fmt"
18-
"time"
1918

20-
"configcenter/pkg/tenant"
21-
"configcenter/src/apimachinery"
19+
"configcenter/pkg/tenant/logics"
2220
"configcenter/src/apimachinery/util"
2321
"configcenter/src/apiserver/app/options"
2422
"configcenter/src/apiserver/service"
@@ -95,7 +93,7 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
9593
ctnr.Add(item)
9694
}
9795
apiSvr.Core = engine
98-
if err = initTenant(engine.CoreAPI); err != nil {
96+
if err = logics.InitTenant(engine.CoreAPI); err != nil {
9997
return err
10098
}
10199
err = backbone.StartServer(ctx, cancel, engine, ctnr, false)
@@ -109,28 +107,6 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
109107
return nil
110108
}
111109

112-
func initTenant(apiMachineryCli apimachinery.ClientSetInterface) error {
113-
coreExist := false
114-
for retry := 0; retry < 10; retry++ {
115-
if _, err := apiMachineryCli.Healthz().HealthCheck(types.CC_MODULE_CORESERVICE); err != nil {
116-
blog.Errorf("connect core server failed: %v", err)
117-
time.Sleep(time.Second * 2)
118-
continue
119-
}
120-
coreExist = true
121-
break
122-
}
123-
if !coreExist {
124-
blog.Errorf("core server not exist")
125-
return fmt.Errorf("core server not exist")
126-
}
127-
err := tenant.Init(&tenant.Options{ApiMachineryCli: apiMachineryCli})
128-
if err != nil {
129-
return err
130-
}
131-
return nil
132-
}
133-
134110
// APIServer TODO
135111
type APIServer struct {
136112
Core *backbone.Engine

src/common/http/header/util/util.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424

2525
"configcenter/src/common"
26+
cc "configcenter/src/common/backbone/configcenter"
2627
httpheader "configcenter/src/common/http/header"
2728
"configcenter/src/common/util"
2829
)
@@ -56,7 +57,12 @@ func GenCommonHeader(user, tenantID, rid string) http.Header {
5657
}
5758

5859
if tenantID == "" {
59-
tenantID = common.BKDefaultTenantID
60+
enableMultiTenant, _ := cc.Bool("tenant.enableMultiTenantMode")
61+
if enableMultiTenant {
62+
tenantID = common.BKDefaultTenantID
63+
} else {
64+
tenantID = common.BKSingleTenantID
65+
}
6066
}
6167

6268
if rid == "" {

src/scene_server/admin_server/app/server.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"time"
2020

21+
tenantlogics "configcenter/pkg/tenant/logics"
2122
iamcli "configcenter/src/ac/iam"
2223
"configcenter/src/common/auth"
2324
"configcenter/src/common/backbone"
@@ -106,7 +107,11 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
106107
return err
107108
}
108109

109-
if err := service.BackgroundTask(*process.Config); err != nil {
110+
if err = service.BackgroundTask(*process.Config); err != nil {
111+
return err
112+
}
113+
114+
if err = tenantlogics.InitTenant(process.Core.CoreAPI); err != nil {
110115
return err
111116
}
112117
err = backbone.StartServer(ctx, cancel, process.Core, service.WebService(), true)

src/thirdparty/apigw/apigw.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"configcenter/src/thirdparty/apigw/login"
2727
"configcenter/src/thirdparty/apigw/notice"
2828
"configcenter/src/thirdparty/apigw/user"
29-
3029
"github.com/prometheus/client_golang/prometheus"
3130
)
3231

@@ -84,8 +83,13 @@ func NewClientSet(config *apigwutil.ApiGWConfig, metric prometheus.Registerer, n
8483
neededCliMap[neededClient] = struct{}{}
8584
}
8685

86+
cs.user, err = user.NewClient(options)
87+
if err != nil {
88+
return nil, err
89+
}
90+
8791
if _, exists := neededCliMap[Gse]; exists {
88-
cs.gse, err = gse.NewClient(options)
92+
cs.gse, err = gse.NewClient(options, cs.user)
8993
if err != nil {
9094
return nil, err
9195
}
@@ -99,21 +103,14 @@ func NewClientSet(config *apigwutil.ApiGWConfig, metric prometheus.Registerer, n
99103
}
100104

101105
if _, exists := neededCliMap[Notice]; exists {
102-
cs.notice, err = notice.NewClient(options)
106+
cs.notice, err = notice.NewClient(options, cs.user)
103107
if err != nil {
104108
return nil, err
105109
}
106110
}
107111

108112
if _, exists := neededCliMap[Login]; exists {
109-
cs.login, err = login.NewClient(options)
110-
if err != nil {
111-
return nil, err
112-
}
113-
}
114-
115-
if _, exists := neededCliMap[User]; exists {
116-
cs.user, err = user.NewClient(options)
113+
cs.login, err = login.NewClient(options, cs.user)
117114
if err != nil {
118115
return nil, err
119116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)