Skip to content

使用虚拟用户调用其他平台 #8394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: feature-tenant
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions pkg/tenant/logics/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,33 @@ package logics

import (
"fmt"
"time"

"configcenter/src/common"
"configcenter/pkg/tenant"
"configcenter/src/apimachinery"
"configcenter/src/common/blog"
"configcenter/src/common/types"
)

// ValidateDisableTenantMode validate disable multi-tenant mode
func ValidateDisableTenantMode(tenantID string, enableTenantMode bool) (string, error) {
if !enableTenantMode {
if tenantID == "" || tenantID == common.BKSingleTenantID {
return common.BKSingleTenantID, nil
// InitTenant init tenant, refresh tenants info while server is starting
func InitTenant(apiMachineryCli apimachinery.ClientSetInterface) error {
coreExist := false
for retry := 0; retry < 10; retry++ {
if _, err := apiMachineryCli.Healthz().HealthCheck(types.CC_MODULE_CORESERVICE); err != nil {
blog.Errorf("connect core server failed: %v", err)
time.Sleep(time.Second * 2)
continue
}

return "", fmt.Errorf("tenant mode is disable, but tenant id %s is set", tenantID)
coreExist = true
break
}

return tenantID, nil
if !coreExist {
blog.Errorf("core server not exist")
return fmt.Errorf("core server not exist")
}
err := tenant.Init(&tenant.Options{ApiMachineryCli: apiMachineryCli})
if err != nil {
return err
}
return nil
}
48 changes: 48 additions & 0 deletions pkg/tenant/tools/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 THL A29 Limited,
* a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package tools

import (
"fmt"

"configcenter/src/common"
cc "configcenter/src/common/backbone/configcenter"
)

// GetDefaultTenant get default tenant
func GetDefaultTenant() string {
enableMultiTenant, _ := cc.Bool("tenant.enableMultiTenantMode")
if enableMultiTenant {
return common.BKDefaultTenantID
}

return common.BKSingleTenantID
}

// ValidateDisableTenantMode validate disable multi-tenant mode
func ValidateDisableTenantMode(tenantID string, enableTenantMode bool) (string, error) {
if !enableTenantMode {
if tenantID == "" || tenantID == common.BKSingleTenantID {
return common.BKSingleTenantID, nil
}

return "", fmt.Errorf("tenant mode is disable, but tenant id %s is set", tenantID)
}

return tenantID, nil
}
28 changes: 2 additions & 26 deletions src/apiserver/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ package app
import (
"context"
"fmt"
"time"

"configcenter/pkg/tenant"
"configcenter/src/apimachinery"
"configcenter/pkg/tenant/logics"
"configcenter/src/apimachinery/util"
"configcenter/src/apiserver/app/options"
"configcenter/src/apiserver/service"
Expand Down Expand Up @@ -95,7 +93,7 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
ctnr.Add(item)
}
apiSvr.Core = engine
if err = initTenant(engine.CoreAPI); err != nil {
if err = logics.InitTenant(engine.CoreAPI); err != nil {
return err
}
err = backbone.StartServer(ctx, cancel, engine, ctnr, false)
Expand All @@ -109,28 +107,6 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
return nil
}

func initTenant(apiMachineryCli apimachinery.ClientSetInterface) error {
coreExist := false
for retry := 0; retry < 10; retry++ {
if _, err := apiMachineryCli.Healthz().HealthCheck(types.CC_MODULE_CORESERVICE); err != nil {
blog.Errorf("connect core server failed: %v", err)
time.Sleep(time.Second * 2)
continue
}
coreExist = true
break
}
if !coreExist {
blog.Errorf("core server not exist")
return fmt.Errorf("core server not exist")
}
err := tenant.Init(&tenant.Options{ApiMachineryCli: apiMachineryCli})
if err != nil {
return err
}
return nil
}

// APIServer TODO
type APIServer struct {
Core *backbone.Engine
Expand Down
4 changes: 2 additions & 2 deletions src/apiserver/service/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"

"configcenter/pkg/tenant"
"configcenter/pkg/tenant/logics"
"configcenter/pkg/tenant/tools"
"configcenter/pkg/tenant/types"
tenantset "configcenter/pkg/types/tenant-set"
"configcenter/src/ac/meta"
Expand Down Expand Up @@ -503,7 +503,7 @@ func (s *service) JwtFilter() func(req *restful.Request, resp *restful.Response,
func (s *service) TenantVerify() func(req *restful.Request, resp *restful.Response, fchain *restful.FilterChain) {
return func(req *restful.Request, resp *restful.Response, fchain *restful.FilterChain) {

tenantID, err := logics.ValidateDisableTenantMode(httpheader.GetTenantID(req.Request.Header),
tenantID, err := tools.ValidateDisableTenantMode(httpheader.GetTenantID(req.Request.Header),
s.config.EnableMultiTenantMode)
if err != nil {
blog.Errorf("get tenant with mode failed, err: %v, rid: %s", err, httpheader.GetRid(req.Request.Header))
Expand Down
3 changes: 2 additions & 1 deletion src/common/http/header/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"net/http"

"configcenter/pkg/tenant/tools"
"configcenter/src/common"
httpheader "configcenter/src/common/http/header"
"configcenter/src/common/util"
Expand Down Expand Up @@ -56,7 +57,7 @@ func GenCommonHeader(user, tenantID, rid string) http.Header {
}

if tenantID == "" {
tenantID = common.BKDefaultTenantID
tenantID = tools.GetDefaultTenant()
}

if rid == "" {
Expand Down
2 changes: 1 addition & 1 deletion src/common/metadata/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ var auditEnDict = []resourceTypeInfo{
},
{
ID: TenantTemplateRes,
Name: "tenant template",
Name: "Tenant Template",
Operations: []actionTypeInfo{
actionInfoEnMap[AuditCreate],
actionInfoEnMap[AuditUpdate],
Expand Down
2 changes: 1 addition & 1 deletion src/scene_server/admin_server/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Run(ctx context.Context, cancel context.CancelFunc, op *options.ServerOptio
return err
}

if err := service.BackgroundTask(*process.Config); err != nil {
if err = service.BackgroundTask(*process.Config); err != nil {
return err
}
err = backbone.StartServer(ctx, cancel, process.Core, service.WebService(), true)
Expand Down
4 changes: 2 additions & 2 deletions src/scene_server/admin_server/service/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

"configcenter/pkg/tenant"
tenantlogics "configcenter/pkg/tenant/logics"
"configcenter/pkg/tenant/tools"
"configcenter/src/common"
"configcenter/src/common/blog"
httpheader "configcenter/src/common/http/header"
Expand All @@ -52,7 +52,7 @@ func (s *Service) migrateDatabase(req *restful.Request, resp *restful.Response)
defErr := s.CCErr.CreateDefaultCCErrorIf(httpheader.GetLanguage(rHeader))

// get tenant id
tenantID, err := tenantlogics.ValidateDisableTenantMode(req.Request.Header.Get(httpheader.TenantHeader),
tenantID, err := tools.ValidateDisableTenantMode(req.Request.Header.Get(httpheader.TenantHeader),
s.Config.EnableMultiTenantMode)
if err != nil {
result := &metadata.RespError{
Expand Down
6 changes: 2 additions & 4 deletions src/scene_server/admin_server/service/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import (
"configcenter/src/storage/dal/mongo/local"
daltypes "configcenter/src/storage/dal/types"
"configcenter/src/storage/driver/mongodb"
"configcenter/src/thirdparty/apigw/user"

"github.com/emicklei/go-restful/v3"
)

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

tenantMap := make(map[string]user.Status)
tenantMap := make(map[string]types.Status)
for _, tenant := range tenants {
tenantMap[tenant.ID] = tenant.Status
}

if status, ok := tenantMap[kit.TenantID]; !ok || status != user.EnabledStatus {
if status, ok := tenantMap[kit.TenantID]; !ok || status != types.EnabledStatus {
blog.Errorf("tenant %s invalid, rid: %s", kit.TenantID, kit.Rid)
result := &metadata.RespError{
Msg: defErr.Errorf(common.CCErrCommAddTenantErr,
Expand Down
4 changes: 3 additions & 1 deletion src/scene_server/event_server/sync/hostidentifier/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"
"time"

"configcenter/pkg/tenant/tools"
"configcenter/src/common"
"configcenter/src/common/blog"
headerutil "configcenter/src/common/http/header/util"
Expand Down Expand Up @@ -49,7 +50,8 @@ func sleepForFail(failCount int) {

func newHeaderWithRid() (http.Header, string) {
rid := util.GenerateRID()
header := headerutil.GenCommonHeader(common.CCSystemOperatorUserName, common.BKDefaultTenantID, rid)
tenantID := tools.GetDefaultTenant()
header := headerutil.GenCommonHeader(common.CCSystemOperatorUserName, tenantID, rid)
return header, rid
}

Expand Down
18 changes: 8 additions & 10 deletions src/thirdparty/apigw/apigw.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ func NewClientSet(config *apigwutil.ApiGWConfig, metric prometheus.Registerer, n
neededCliMap[neededClient] = struct{}{}
}

cs.user, err = user.NewClient(options)
if err != nil {
return nil, err
}

if _, exists := neededCliMap[Gse]; exists {
cs.gse, err = gse.NewClient(options)
cs.gse, err = gse.NewClient(options, cs.user)
if err != nil {
return nil, err
}
Expand All @@ -99,21 +104,14 @@ func NewClientSet(config *apigwutil.ApiGWConfig, metric prometheus.Registerer, n
}

if _, exists := neededCliMap[Notice]; exists {
cs.notice, err = notice.NewClient(options)
cs.notice, err = notice.NewClient(options, cs.user)
if err != nil {
return nil, err
}
}

if _, exists := neededCliMap[Login]; exists {
cs.login, err = login.NewClient(options)
if err != nil {
return nil, err
}
}

if _, exists := neededCliMap[User]; exists {
cs.user, err = user.NewClient(options)
cs.login, err = login.NewClient(options, cs.user)
if err != nil {
return nil, err
}
Expand Down
Loading