github.com/polarismesh/polaris@v1.17.8/service/circuitbreaker_rule_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 service
    19  
    20  import (
    21  	"context"
    22  
    23  	apifault "github.com/polarismesh/specification/source/go/api/v1/fault_tolerance"
    24  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    25  
    26  	api "github.com/polarismesh/polaris/common/api/v1"
    27  	"github.com/polarismesh/polaris/common/model"
    28  	"github.com/polarismesh/polaris/common/utils"
    29  )
    30  
    31  func (svr *serverAuthAbility) CreateCircuitBreakerRules(
    32  	ctx context.Context, request []*apifault.CircuitBreakerRule) *apiservice.BatchWriteResponse {
    33  	// TODO not support CircuitBreaker resource auth, so we set op is read
    34  	authCtx := svr.collectCircuitBreakerRuleV2AuthContext(ctx, request, model.Read, "CreateCircuitBreakerRules")
    35  
    36  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    37  	if err != nil {
    38  		return api.NewBatchWriteResponse(convertToErrCode(err))
    39  	}
    40  
    41  	ctx = authCtx.GetRequestContext()
    42  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    43  	return svr.targetServer.CreateCircuitBreakerRules(ctx, request)
    44  }
    45  
    46  func (svr *serverAuthAbility) DeleteCircuitBreakerRules(
    47  	ctx context.Context, request []*apifault.CircuitBreakerRule) *apiservice.BatchWriteResponse {
    48  	authCtx := svr.collectCircuitBreakerRuleV2AuthContext(ctx, request, model.Read, "DeleteCircuitBreakerRules")
    49  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    50  	if err != nil {
    51  		return api.NewBatchWriteResponse(convertToErrCode(err))
    52  	}
    53  
    54  	ctx = authCtx.GetRequestContext()
    55  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    56  	return svr.targetServer.DeleteCircuitBreakerRules(ctx, request)
    57  }
    58  
    59  func (svr *serverAuthAbility) EnableCircuitBreakerRules(
    60  	ctx context.Context, request []*apifault.CircuitBreakerRule) *apiservice.BatchWriteResponse {
    61  	authCtx := svr.collectCircuitBreakerRuleV2AuthContext(ctx, request, model.Read, "EnableCircuitBreakerRules")
    62  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    63  	if err != nil {
    64  		return api.NewBatchWriteResponse(convertToErrCode(err))
    65  	}
    66  
    67  	ctx = authCtx.GetRequestContext()
    68  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    69  	return svr.targetServer.EnableCircuitBreakerRules(ctx, request)
    70  }
    71  
    72  func (svr *serverAuthAbility) UpdateCircuitBreakerRules(
    73  	ctx context.Context, request []*apifault.CircuitBreakerRule) *apiservice.BatchWriteResponse {
    74  	authCtx := svr.collectCircuitBreakerRuleV2AuthContext(ctx, request, model.Read, "UpdateCircuitBreakerRules")
    75  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    76  	if err != nil {
    77  		return api.NewBatchWriteResponse(convertToErrCode(err))
    78  	}
    79  
    80  	ctx = authCtx.GetRequestContext()
    81  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    82  	return svr.targetServer.UpdateCircuitBreakerRules(ctx, request)
    83  }
    84  
    85  func (svr *serverAuthAbility) GetCircuitBreakerRules(
    86  	ctx context.Context, query map[string]string) *apiservice.BatchQueryResponse {
    87  	authCtx := svr.collectCircuitBreakerRuleV2AuthContext(ctx, nil, model.Read, "GetCircuitBreakerRules")
    88  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    89  	if err != nil {
    90  		return api.NewBatchQueryResponse(convertToErrCode(err))
    91  	}
    92  
    93  	ctx = authCtx.GetRequestContext()
    94  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    95  	return svr.targetServer.GetCircuitBreakerRules(ctx, query)
    96  }