github.com/polarismesh/polaris@v1.17.8/config/client_authibility.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 config
    19  
    20  import (
    21  	"context"
    22  
    23  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_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  // GetConfigFileForClient 从缓存中获取配置文件,如果客户端的版本号大于服务端,则服务端重新加载缓存
    31  func (s *serverAuthability) GetConfigFileForClient(ctx context.Context,
    32  	fileInfo *apiconfig.ClientConfigFileInfo) *apiconfig.ConfigClientResponse {
    33  	authCtx := s.collectClientConfigFileAuthContext(ctx,
    34  		[]*apiconfig.ConfigFile{{
    35  			Namespace: fileInfo.Namespace,
    36  			Name:      fileInfo.FileName,
    37  			Group:     fileInfo.Group},
    38  		}, model.Read, "GetConfigFileForClient")
    39  	if _, err := s.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx); err != nil {
    40  		return api.NewConfigClientResponseWithInfo(convertToErrCode(err), err.Error())
    41  	}
    42  
    43  	ctx = authCtx.GetRequestContext()
    44  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    45  	return s.targetServer.GetConfigFileForClient(ctx, fileInfo)
    46  }
    47  
    48  // CreateConfigFileFromClient 调用config_file的方法创建配置文件
    49  func (s *serverAuthability) CreateConfigFileFromClient(ctx context.Context,
    50  	fileInfo *apiconfig.ConfigFile) *apiconfig.ConfigClientResponse {
    51  	authCtx := s.collectClientConfigFileAuthContext(ctx,
    52  		[]*apiconfig.ConfigFile{{
    53  			Namespace: fileInfo.Namespace,
    54  			Name:      fileInfo.Name,
    55  			Group:     fileInfo.Group},
    56  		}, model.Create, "CreateConfigFileFromClient")
    57  	if _, err := s.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx); err != nil {
    58  		return api.NewConfigClientResponseWithInfo(convertToErrCode(err), err.Error())
    59  	}
    60  
    61  	ctx = authCtx.GetRequestContext()
    62  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    63  
    64  	return s.targetServer.CreateConfigFileFromClient(ctx, fileInfo)
    65  }
    66  
    67  // UpdateConfigFileFromClient 调用config_file的方法更新配置文件
    68  func (s *serverAuthability) UpdateConfigFileFromClient(ctx context.Context,
    69  	fileInfo *apiconfig.ConfigFile) *apiconfig.ConfigClientResponse {
    70  	authCtx := s.collectClientConfigFileAuthContext(ctx,
    71  		[]*apiconfig.ConfigFile{fileInfo}, model.Modify, "UpdateConfigFileFromClient")
    72  	if _, err := s.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx); err != nil {
    73  		return api.NewConfigClientResponseWithInfo(convertToErrCode(err), err.Error())
    74  	}
    75  
    76  	ctx = authCtx.GetRequestContext()
    77  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    78  
    79  	return s.targetServer.UpdateConfigFileFromClient(ctx, fileInfo)
    80  }
    81  
    82  // PublishConfigFileFromClient 调用config_file_release的方法发布配置文件
    83  func (s *serverAuthability) PublishConfigFileFromClient(ctx context.Context,
    84  	fileInfo *apiconfig.ConfigFileRelease) *apiconfig.ConfigClientResponse {
    85  	authCtx := s.collectClientConfigFileReleaseAuthContext(ctx,
    86  		[]*apiconfig.ConfigFileRelease{{
    87  			Namespace: fileInfo.Namespace,
    88  			Name:      fileInfo.FileName,
    89  			Group:     fileInfo.Group},
    90  		}, model.Modify, "PublishConfigFileFromClient")
    91  	if _, err := s.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx); err != nil {
    92  		return api.NewConfigClientResponseWithInfo(convertToErrCode(err), err.Error())
    93  	}
    94  
    95  	ctx = authCtx.GetRequestContext()
    96  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    97  
    98  	return s.targetServer.PublishConfigFileFromClient(ctx, fileInfo)
    99  }
   100  
   101  // WatchConfigFiles 监听配置文件变化
   102  func (s *serverAuthability) WatchConfigFiles(ctx context.Context,
   103  	request *apiconfig.ClientWatchConfigFileRequest) (WatchCallback, error) {
   104  	authCtx := s.collectClientWatchConfigFiles(ctx, request, model.Read, "WatchConfigFiles")
   105  	if _, err := s.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx); err != nil {
   106  		return func() *apiconfig.ConfigClientResponse {
   107  			return api.NewConfigClientResponseWithInfo(convertToErrCode(err), err.Error())
   108  		}, nil
   109  	}
   110  
   111  	ctx = authCtx.GetRequestContext()
   112  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
   113  
   114  	return s.targetServer.WatchConfigFiles(ctx, request)
   115  }
   116  
   117  // GetConfigFileNamesWithCache 获取某个配置分组下的配置文件
   118  func (s *serverAuthability) GetConfigFileNamesWithCache(ctx context.Context,
   119  	req *apiconfig.ConfigFileGroupRequest) *apiconfig.ConfigClientListResponse {
   120  
   121  	authCtx := s.collectClientConfigFileReleaseAuthContext(ctx, []*apiconfig.ConfigFileRelease{
   122  		{
   123  			Namespace: req.GetConfigFileGroup().GetNamespace(),
   124  			Group:     req.GetConfigFileGroup().GetName(),
   125  		},
   126  	}, model.Read, "GetConfigFileNamesWithCache")
   127  	if _, err := s.strategyMgn.GetAuthChecker().CheckClientPermission(authCtx); err != nil {
   128  		out := api.NewConfigClientListResponse(convertToErrCode(err))
   129  		return out
   130  	}
   131  
   132  	ctx = authCtx.GetRequestContext()
   133  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
   134  	return s.targetServer.GetConfigFileNamesWithCache(ctx, req)
   135  }