Skip to content

Commit

Permalink
refactor:鉴权能力优化调整
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Sep 12, 2024
1 parent 8a995b3 commit e82149b
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ linters-settings:
govet:
# Report about shadowed variables.
# Default: false
check-shadowing: true
shadow: false
# Settings per analyzer.
settings:
# Analyzer name, run `go tool vet help` to see all analyzers.
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Start(configFilePath string) {
fmt.Printf("[ERROR] config yaml marshal fail\n")
return
}
fmt.Printf(string(c))
_, _ = fmt.Println(string(c))

// 初始化日志打印
err = log.Configure(cfg.Bootstrap.Logger)
Expand Down
2 changes: 1 addition & 1 deletion import-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# 格式化 go.mod
go mod tidy -compat=1.17

docker run -t --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.55.2 golangci-lint run -v --timeout 30m
docker run -t --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.61.0 golangci-lint run -v --timeout 30m

# 处理 go imports 的格式化
rm -rf style_tool
Expand Down
2 changes: 1 addition & 1 deletion plugin/statis/logger/statis.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *StatisWorker) ReportConfigMetrics(metric ...metrics.ConfigMetrics) {

// ReportDiscoverCall report discover service times
func (s *StatisWorker) ReportDiscoverCall(metric metrics.ClientDiscoverMetric) {
discoverlog.Infof(metric.String())
discoverlog.Info(metric.String())
}

func (a *StatisWorker) metricsHandle(mt metrics.CallMetricType, start time.Time,
Expand Down
10 changes: 4 additions & 6 deletions store/boltdb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package boltdb

import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/golang/protobuf/ptypes/wrappers"
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/wrapperspb"

"github.com/polarismesh/polaris/common/model"
Expand Down Expand Up @@ -603,8 +603,7 @@ func (i *instanceStore) SetInstanceHealthStatus(instanceID string, flag int, rev
return err
}
if len(instances) == 0 {
msg := fmt.Sprintf("cant not find instance in kv, %s", instanceID)
log.Errorf(msg)
log.Errorf("cant not find instance in kv, %s", instanceID)
return nil
}

Expand Down Expand Up @@ -667,8 +666,7 @@ func (i *instanceStore) BatchSetInstanceIsolate(ids []interface{}, isolate int,
return err
}
if len(instances) == 0 {
msg := fmt.Sprintf("cant not find instance in kv, %v", ids)
log.Errorf(msg)
log.Errorf("cant not find instance in kv, %v", ids)
return nil
}

Expand All @@ -684,7 +682,7 @@ func (i *instanceStore) BatchSetInstanceIsolate(ids []interface{}, isolate int,
instance.Mtime = &wrappers.StringValue{Value: commontime.Time2String(curr)}
err = i.handler.UpdateValue(tblNameInstance, id, properties)
if err != nil {
log.Errorf("[Store][boltdb] update instance in set instance isolate error, %v", err)
log.Error("[Store][boltdb] update instance in set instance isolate error", zap.Error(err))
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions store/mysql/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (m *adminStore) BatchCleanDeletedInstances(timeout time.Duration, batchSize
}

cleanCheckStr := fmt.Sprintf("delete from health_check where id in (%s)", inSql)
if _, err := tx.Exec(cleanCheckStr, waitDelIds...); err != nil {
if _, err = tx.Exec(cleanCheckStr, waitDelIds...); err != nil {
log.Errorf("[Store][database] batch clean soft deleted instances(%d), err: %s", batchSize, err.Error())
return store.Error(err)
}
Expand All @@ -487,7 +487,7 @@ func (m *adminStore) BatchCleanDeletedInstances(timeout time.Duration, batchSize
return store.Error(err)
}

if err := tx.Commit(); err != nil {
if err = tx.Commit(); err != nil {
log.Errorf("[Store][database] batch clean soft deleted instances(%d) commit tx err: %s",
batchSize, err.Error())
return err
Expand Down
2 changes: 1 addition & 1 deletion store/mysql/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (cf *configFileStore) QueryConfigFiles(filter map[string]string, offset, li
err = cf.slave.processWithTransaction("batch-load-file-tags", func(tx *BaseTx) error {
for i := range files {
item := files[i]
if err := cf.loadFileTags(tx, item); err != nil {
if err = cf.loadFileTags(tx, item); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion store/mysql/config_file_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (cfr *configFileReleaseStore) CreateConfigFileReleaseTx(tx store.Tx, data *
}

clean := "DELETE FROM config_file_release WHERE namespace = ? AND `group` = ? AND file_name = ? AND name = ? AND flag = 1"
if _, err := dbTx.Exec(clean, data.Namespace, data.Group, data.FileName, data.Name); err != nil {
if _, err = dbTx.Exec(clean, data.Namespace, data.Group, data.FileName, data.Name); err != nil {
return store.Error(err)
}

Expand Down
6 changes: 3 additions & 3 deletions store/mysql/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ func (u *groupStore) updateGroup(group *authcommon.ModifyUserGroup) error {

// 更新用户-用户组关联数据
if len(group.AddUserIds) != 0 {
if err := u.addGroupRelation(tx, group.ID, group.AddUserIds); err != nil {
if err = u.addGroupRelation(tx, group.ID, group.AddUserIds); err != nil {
log.Errorf("[Store][Group] add usergroup relation err: %s", err.Error())
return err
}
}

if len(group.RemoveUserIds) != 0 {
if err := u.removeGroupRelation(tx, group.ID, group.RemoveUserIds); err != nil {
if err = u.removeGroupRelation(tx, group.ID, group.RemoveUserIds); err != nil {
log.Errorf("[Store][Group] remove usergroup relation err: %s", err.Error())
return err
}
Expand All @@ -153,7 +153,7 @@ func (u *groupStore) updateGroup(group *authcommon.ModifyUserGroup) error {
return err
}

if err := tx.Commit(); err != nil {
if err = tx.Commit(); err != nil {
log.Errorf("[Store][Group] update usergroup tx commit err: %s", err.Error())
return err
}
Expand Down
4 changes: 2 additions & 2 deletions store/mysql/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ SELECT id, name, rule, description, revision, flag, UNIX_TIMESTAMP(ctime), UNIX_
for k, v := range filter {
switch k {
case "name":
if v, ok := utils.ParseWildName(v); ok {
if pv, ok := utils.ParseWildName(v); ok {
conditions = append(conditions, "name = ?")
args = append(args, v)
args = append(args, pv)
} else {
conditions = append(conditions, "name LIKE ?")
args = append(args, "%"+v+"%")
Expand Down
18 changes: 9 additions & 9 deletions store/mysql/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,30 @@ func (s *strategyStore) updateStrategy(strategy *authcommon.ModifyStrategyDetail
defer func() { _ = tx.Rollback() }()

// 调整 principal 信息
if err := s.addPolicyPrincipals(tx, strategy.ID, strategy.AddPrincipals); err != nil {
if err = s.addPolicyPrincipals(tx, strategy.ID, strategy.AddPrincipals); err != nil {
log.Errorf("[Store][Strategy] add strategy principal err: %s", err.Error())
return err
}
if err := s.deletePolicyPrincipals(tx, strategy.ID, strategy.RemovePrincipals); err != nil {
if err = s.deletePolicyPrincipals(tx, strategy.ID, strategy.RemovePrincipals); err != nil {
log.Errorf("[Store][Strategy] remove strategy principal err: %s", err.Error())
return err
}

// 调整鉴权资源信息
if err := s.addPolicyResources(tx, strategy.ID, strategy.AddResources); err != nil {
if err = s.addPolicyResources(tx, strategy.ID, strategy.AddResources); err != nil {
log.Errorf("[Store][Strategy] add strategy resource err: %s", err.Error())
return err
}
if err := s.deletePolicyResources(tx, strategy.ID, strategy.RemoveResources); err != nil {
if err = s.deletePolicyResources(tx, strategy.ID, strategy.RemoveResources); err != nil {
log.Errorf("[Store][Strategy] remove strategy resource err: %s", err.Error())
return err
}

if err := s.savePolicyFunctions(tx, strategy.ID, strategy.CalleeMethods); err != nil {
if err = s.savePolicyFunctions(tx, strategy.ID, strategy.CalleeMethods); err != nil {
log.Error("[Store][Strategy] save auth_strategy functions", zap.Error(err))
return err
}
if err := s.savePolicyConditions(tx, strategy.ID, strategy.Conditions); err != nil {
if err = s.savePolicyConditions(tx, strategy.ID, strategy.Conditions); err != nil {
log.Error("[Store][Strategy] save auth_strategy conditions", zap.Error(err))
return err
}
Expand All @@ -163,7 +163,7 @@ func (s *strategyStore) updateStrategy(strategy *authcommon.ModifyStrategyDetail
return err
}

if err := tx.Commit(); err != nil {
if err = tx.Commit(); err != nil {
log.Errorf("[Store][Strategy] update auth_strategy tx commit err: %s", err.Error())
return err
}
Expand Down Expand Up @@ -420,7 +420,7 @@ func (s *strategyStore) RemoveStrategyResources(resources []authcommon.StrategyR
saveResSql = "DELETE FROM auth_strategy_resource WHERE res_id = ? AND res_type = ?"
args = append(args, resource.ResID, resource.ResType)
}
if _, err := tx.Exec(saveResSql, args...); err != nil {
if _, err = tx.Exec(saveResSql, args...); err != nil {
return err
}
// 主要是为了能够触发 StrategyCache 的刷新逻辑
Expand All @@ -430,7 +430,7 @@ func (s *strategyStore) RemoveStrategyResources(resources []authcommon.StrategyR
}
}

if err := tx.Commit(); err != nil {
if err = tx.Commit(); err != nil {
log.Errorf("[Store][Strategy] add auth_strategy tx commit err: %s", err.Error())
return err
}
Expand Down
15 changes: 15 additions & 0 deletions test/data/bolt-data.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Tencent is pleased to support the open source community by making Polaris available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# 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.

users:
- name: polaris
token: nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=
Expand Down

0 comments on commit e82149b

Please sign in to comment.