github.com/polarismesh/polaris@v1.17.8/admin/maintain_authability.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause 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 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package admin 19 20 import ( 21 "context" 22 23 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 24 25 api "github.com/polarismesh/polaris/common/api/v1" 26 "github.com/polarismesh/polaris/common/model" 27 "github.com/polarismesh/polaris/common/utils" 28 ) 29 30 var _ AdminOperateServer = (*serverAuthAbility)(nil) 31 32 func (svr *serverAuthAbility) GetServerConnections(ctx context.Context, req *ConnReq) (*ConnCountResp, error) { 33 authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "GetServerConnections") 34 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 35 if err != nil { 36 return nil, err 37 } 38 39 ctx = authCtx.GetRequestContext() 40 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 41 42 return svr.targetServer.GetServerConnections(ctx, req) 43 } 44 45 func (svr *serverAuthAbility) GetServerConnStats(ctx context.Context, req *ConnReq) (*ConnStatsResp, error) { 46 authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "GetServerConnStats") 47 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 48 if err != nil { 49 return nil, err 50 } 51 52 ctx = authCtx.GetRequestContext() 53 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 54 55 return svr.targetServer.GetServerConnStats(ctx, req) 56 } 57 58 func (svr *serverAuthAbility) CloseConnections(ctx context.Context, reqs []ConnReq) error { 59 authCtx := svr.collectMaintainAuthContext(ctx, model.Delete, "CloseConnections") 60 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 61 if err != nil { 62 return err 63 } 64 65 ctx = authCtx.GetRequestContext() 66 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 67 68 return svr.targetServer.CloseConnections(ctx, reqs) 69 } 70 71 func (svr *serverAuthAbility) FreeOSMemory(ctx context.Context) error { 72 authCtx := svr.collectMaintainAuthContext(ctx, model.Modify, "FreeOSMemory") 73 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 74 if err != nil { 75 return err 76 } 77 78 ctx = authCtx.GetRequestContext() 79 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 80 81 return svr.targetServer.FreeOSMemory(ctx) 82 } 83 84 func (svr *serverAuthAbility) CleanInstance(ctx context.Context, req *apiservice.Instance) *apiservice.Response { 85 authCtx := svr.collectMaintainAuthContext(ctx, model.Delete, "CleanInstance") 86 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 87 if err != nil { 88 return api.NewResponseWithMsg(convertToErrCode(err), err.Error()) 89 } 90 91 ctx = authCtx.GetRequestContext() 92 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 93 94 return svr.targetServer.CleanInstance(ctx, req) 95 } 96 97 func (svr *serverAuthAbility) BatchCleanInstances(ctx context.Context, batchSize uint32) (uint32, error) { 98 authCtx := svr.collectMaintainAuthContext(ctx, model.Delete, "BatchCleanInstances") 99 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 100 if err != nil { 101 return 0, err 102 } 103 104 return svr.targetServer.BatchCleanInstances(ctx, batchSize) 105 } 106 107 func (svr *serverAuthAbility) GetLastHeartbeat(ctx context.Context, req *apiservice.Instance) *apiservice.Response { 108 authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "GetLastHeartbeat") 109 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 110 if err != nil { 111 return api.NewResponseWithMsg(convertToErrCode(err), err.Error()) 112 } 113 114 ctx = authCtx.GetRequestContext() 115 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 116 117 return svr.targetServer.GetLastHeartbeat(ctx, req) 118 } 119 120 func (svr *serverAuthAbility) GetLogOutputLevel(ctx context.Context) ([]ScopeLevel, error) { 121 authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "GetLogOutputLevel") 122 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 123 if err != nil { 124 return nil, err 125 } 126 127 ctx = authCtx.GetRequestContext() 128 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 129 130 return svr.targetServer.GetLogOutputLevel(ctx) 131 } 132 133 func (svr *serverAuthAbility) SetLogOutputLevel(ctx context.Context, scope string, level string) error { 134 authCtx := svr.collectMaintainAuthContext(ctx, model.Modify, "SetLogOutputLevel") 135 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 136 if err != nil { 137 return err 138 } 139 140 return svr.targetServer.SetLogOutputLevel(ctx, scope, level) 141 } 142 143 func (svr *serverAuthAbility) ListLeaderElections(ctx context.Context) ([]*model.LeaderElection, error) { 144 authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "ListLeaderElections") 145 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 146 if err != nil { 147 return nil, err 148 } 149 150 ctx = authCtx.GetRequestContext() 151 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 152 153 return svr.targetServer.ListLeaderElections(ctx) 154 } 155 156 func (svr *serverAuthAbility) ReleaseLeaderElection(ctx context.Context, electKey string) error { 157 authCtx := svr.collectMaintainAuthContext(ctx, model.Modify, "ReleaseLeaderElection") 158 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 159 if err != nil { 160 return err 161 } 162 163 ctx = authCtx.GetRequestContext() 164 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 165 166 return svr.targetServer.ReleaseLeaderElection(ctx, electKey) 167 } 168 169 func (svr *serverAuthAbility) GetCMDBInfo(ctx context.Context) ([]model.LocationView, error) { 170 authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "GetCMDBInfo") 171 _, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx) 172 if err != nil { 173 return nil, err 174 } 175 176 ctx = authCtx.GetRequestContext() 177 ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx) 178 179 return svr.targetServer.GetCMDBInfo(ctx) 180 }