github.com/polarismesh/polaris@v1.17.8/auth/auth.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 auth 19 20 import ( 21 "context" 22 "errors" 23 "fmt" 24 "log" 25 "sync" 26 27 "github.com/polarismesh/polaris/cache" 28 "github.com/polarismesh/polaris/store" 29 ) 30 31 const ( 32 // DefaultUserMgnPluginName default user server name 33 DefaultUserMgnPluginName = "defaultUser" 34 // DefaultStrategyMgnPluginName default strategy server name 35 DefaultStrategyMgnPluginName = "defaultStrategy" 36 ) 37 38 // Config 鉴权能力的相关配置参数 39 type Config struct { 40 // Name 原AuthServer名称,已废弃 41 Name string 42 // Option 原AuthServer的option,已废弃 43 // Deprecated 44 Option map[string]interface{} 45 // User UserOperator的相关配置 46 User *UserConfig `yaml:"user"` 47 // Strategy StrategyOperator的相关配置 48 Strategy *StrategyConfig `yaml:"strategy"` 49 } 50 51 func (c *Config) SetDefault() { 52 if c.User == nil { 53 c.User = &UserConfig{ 54 Name: DefaultUserMgnPluginName, 55 Option: map[string]interface{}{}, 56 } 57 } 58 if c.Strategy == nil { 59 c.Strategy = &StrategyConfig{ 60 Name: DefaultStrategyMgnPluginName, 61 Option: map[string]interface{}{}, 62 } 63 } 64 } 65 66 // UserConfig UserOperator的相关配置 67 type UserConfig struct { 68 // Name UserOperator的名称 69 Name string `yaml:"name"` 70 // Option UserOperator的option 71 Option map[string]interface{} `yaml:"option"` 72 } 73 74 // StrategyConfig StrategyOperator的相关配置 75 type StrategyConfig struct { 76 // Name StrategyOperator的名称 77 Name string `yaml:"name"` 78 // Option StrategyOperator的option 79 Option map[string]interface{} `yaml:"option"` 80 } 81 82 var ( 83 // userMgnSlots 保存用户管理manager slot 84 userMgnSlots = map[string]UserServer{} 85 // strategyMgnSlots 保存策略管理manager slot 86 strategyMgnSlots = map[string]StrategyServer{} 87 once sync.Once 88 userMgn UserServer 89 strategyMgn StrategyServer 90 finishInit bool 91 ) 92 93 // RegisterUserServer 注册一个新的 UserServer 94 func RegisterUserServer(s UserServer) error { 95 name := s.Name() 96 if _, ok := userMgnSlots[name]; ok { 97 return fmt.Errorf("UserServer=[%s] exist", name) 98 } 99 100 userMgnSlots[name] = s 101 return nil 102 } 103 104 // GetUserServer 获取一个 UserServer 105 func GetUserServer() (UserServer, error) { 106 if !finishInit { 107 return nil, errors.New("UserServer has not done Initialize") 108 } 109 return userMgn, nil 110 } 111 112 // RegisterStrategyServer 注册一个新的 StrategyServer 113 func RegisterStrategyServer(s StrategyServer) error { 114 name := s.Name() 115 if _, ok := strategyMgnSlots[name]; ok { 116 return fmt.Errorf("StrategyServer=[%s] exist", name) 117 } 118 119 strategyMgnSlots[name] = s 120 return nil 121 } 122 123 // GetStrategyServer 获取一个 StrategyServer 124 func GetStrategyServer() (StrategyServer, error) { 125 if !finishInit { 126 return nil, errors.New("StrategyServer has not done Initialize") 127 } 128 return strategyMgn, nil 129 } 130 131 // Initialize 初始化 132 func Initialize(ctx context.Context, authOpt *Config, storage store.Store, cacheMgn *cache.CacheManager) error { 133 var err error 134 once.Do(func() { 135 userMgn, strategyMgn, err = initialize(ctx, authOpt, storage, cacheMgn) 136 }) 137 138 if err != nil { 139 return err 140 } 141 return nil 142 } 143 144 // initialize 包裹了初始化函数,在 Initialize 的时候会在自动调用,全局初始化一次 145 func initialize(_ context.Context, authOpt *Config, storage store.Store, 146 cacheMgn *cache.CacheManager) (UserServer, StrategyServer, error) { 147 authOpt.SetDefault() 148 name := authOpt.User.Name 149 if name == "" { 150 return nil, nil, errors.New("UserServer Name is empty") 151 } 152 153 namedUserMgn, ok := userMgnSlots[name] 154 if !ok { 155 return nil, nil, fmt.Errorf("no such UserServer plugin. name(%s)", name) 156 } 157 if err := namedUserMgn.Initialize(authOpt, storage, cacheMgn); err != nil { 158 log.Printf("UserServer do initialize err: %s", err.Error()) 159 return nil, nil, err 160 } 161 162 name = authOpt.Strategy.Name 163 if name == "" { 164 return nil, nil, errors.New("StrategyServer Name is empty") 165 } 166 167 namedStrategyMgn, ok := strategyMgnSlots[name] 168 if !ok { 169 return nil, nil, fmt.Errorf("no such StrategyServer plugin. name(%s)", name) 170 } 171 if err := namedStrategyMgn.Initialize(authOpt, storage, cacheMgn); err != nil { 172 log.Printf("StrategyServer do initialize err: %s", err.Error()) 173 return nil, nil, err 174 } 175 finishInit = true 176 return namedUserMgn, namedStrategyMgn, nil 177 }