Skip to content

Commit 6a8b69e

Browse files
committed
feat: call other platforms using virtual user
--story=122806009
1 parent 2ca6b3f commit 6a8b69e

File tree

21 files changed

+370
-122
lines changed

21 files changed

+370
-122
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+
}

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

+12-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
)
@@ -46,6 +47,16 @@ func CCHeader(header http.Header) http.Header {
4647
return newHeader
4748
}
4849

50+
// GetDefaultTenant get default tenant
51+
func GetDefaultTenant() string {
52+
enableMultiTenant, _ := cc.Bool("tenant.enableMultiTenantMode")
53+
if enableMultiTenant {
54+
return common.BKDefaultTenantID
55+
}
56+
57+
return common.BKSingleTenantID
58+
}
59+
4960
// GenCommonHeader generate common cmdb http header, use default value if parameter is not set
5061
func GenCommonHeader(user, tenantID, rid string) http.Header {
5162
header := make(http.Header)
@@ -56,7 +67,7 @@ func GenCommonHeader(user, tenantID, rid string) http.Header {
5667
}
5768

5869
if tenantID == "" {
59-
tenantID = common.BKDefaultTenantID
70+
tenantID = GetDefaultTenant()
6071
}
6172

6273
if rid == "" {

src/common/metadata/audit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ var auditEnDict = []resourceTypeInfo{
13571357
},
13581358
{
13591359
ID: TenantTemplateRes,
1360-
Name: "tenant template",
1360+
Name: "Tenant Template",
13611361
Operations: []actionTypeInfo{
13621362
actionInfoEnMap[AuditCreate],
13631363
actionInfoEnMap[AuditUpdate],

src/scene_server/admin_server/app/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
106106
return err
107107
}
108108

109-
if err := service.BackgroundTask(*process.Config); err != nil {
109+
if err = service.BackgroundTask(*process.Config); err != nil {
110110
return err
111111
}
112112
err = backbone.StartServer(ctx, cancel, process.Core, service.WebService(), true)

src/scene_server/admin_server/service/tenant.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import (
3737
"configcenter/src/storage/dal/mongo/local"
3838
daltypes "configcenter/src/storage/dal/types"
3939
"configcenter/src/storage/driver/mongodb"
40-
"configcenter/src/thirdparty/apigw/user"
41-
4240
"github.com/emicklei/go-restful/v3"
4341
)
4442

@@ -74,12 +72,12 @@ func (s *Service) addTenant(req *restful.Request, resp *restful.Response) {
7472
resp.WriteError(http.StatusInternalServerError, result)
7573
}
7674

77-
tenantMap := make(map[string]user.Status)
75+
tenantMap := make(map[string]types.Status)
7876
for _, tenant := range tenants {
7977
tenantMap[tenant.ID] = tenant.Status
8078
}
8179

82-
if status, ok := tenantMap[kit.TenantID]; !ok || status != user.EnabledStatus {
80+
if status, ok := tenantMap[kit.TenantID]; !ok || status != types.EnabledStatus {
8381
blog.Errorf("tenant %s invalid, rid: %s", kit.TenantID, kit.Rid)
8482
result := &metadata.RespError{
8583
Msg: defErr.Errorf(common.CCErrCommAddTenantErr,

src/thirdparty/apigw/apigw.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ func NewClientSet(config *apigwutil.ApiGWConfig, metric prometheus.Registerer, n
8484
neededCliMap[neededClient] = struct{}{}
8585
}
8686

87+
cs.user, err = user.NewClient(options)
88+
if err != nil {
89+
return nil, err
90+
}
91+
8792
if _, exists := neededCliMap[Gse]; exists {
88-
cs.gse, err = gse.NewClient(options)
93+
cs.gse, err = gse.NewClient(options, cs.user)
8994
if err != nil {
9095
return nil, err
9196
}
@@ -99,21 +104,14 @@ func NewClientSet(config *apigwutil.ApiGWConfig, metric prometheus.Registerer, n
99104
}
100105

101106
if _, exists := neededCliMap[Notice]; exists {
102-
cs.notice, err = notice.NewClient(options)
107+
cs.notice, err = notice.NewClient(options, cs.user)
103108
if err != nil {
104109
return nil, err
105110
}
106111
}
107112

108113
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)
114+
cs.login, err = login.NewClient(options, cs.user)
117115
if err != nil {
118116
return nil, err
119117
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)