github.com/polarismesh/polaris@v1.17.8/store/boltdb/default.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 boltdb 19 20 import ( 21 "time" 22 23 apisecurity "github.com/polarismesh/specification/source/go/api/v1/security" 24 bolt "go.etcd.io/bbolt" 25 "go.uber.org/zap" 26 27 "github.com/polarismesh/polaris/common/model" 28 "github.com/polarismesh/polaris/common/utils" 29 "github.com/polarismesh/polaris/store" 30 ) 31 32 const ( 33 34 // SystemNamespace system namespace 35 SystemNamespace = "Polaris" 36 // STORENAME database storage name 37 STORENAME = "boltdbStore" 38 // DefaultConnMaxLifetime default maximum connection lifetime 39 DefaultConnMaxLifetime = 60 * 30 // 默认是30分钟 40 ) 41 42 const ( 43 svcSpecificQueryKeyService = "service" 44 svcSpecificQueryKeyNamespace = "serviceNamespace" 45 exactName = "exactName" 46 excludeId = "excludeId" 47 ) 48 49 const ( 50 CommonFieldValid = "Valid" 51 CommonFieldEnableTime = "EnableTime" 52 CommonFieldModifyTime = "ModifyTime" 53 CommonFieldRevision = "Revision" 54 CommonFieldID = "ID" 55 CommonFieldName = "Name" 56 CommonFieldNamespace = "Namespace" 57 CommonFieldDescription = "Description" 58 CommonFieldEnable = "Enable" 59 ) 60 61 type boltStore struct { 62 *namespaceStore 63 *clientStore 64 65 // 服务注册发现、治理 66 *serviceStore 67 *instanceStore 68 *l5Store 69 *routingStore 70 *rateLimitStore 71 *circuitBreakerStore 72 *faultDetectStore 73 74 // 工具 75 *toolStore 76 77 // 鉴权模块相关 78 *userStore 79 *groupStore 80 *strategyStore 81 82 // 配置中心stores 83 *configFileGroupStore 84 *configFileStore 85 *configFileReleaseStore 86 *configFileReleaseHistoryStore 87 *configFileTemplateStore 88 89 // v2 存储 90 *routingStoreV2 91 92 // adminStore store 93 *adminStore 94 95 handler BoltHandler 96 start bool 97 } 98 99 // Name store name 100 func (m *boltStore) Name() string { 101 return STORENAME 102 } 103 104 // Initialize init store 105 func (m *boltStore) Initialize(c *store.Config) error { 106 if m.start { 107 return nil 108 } 109 boltConfig := &BoltConfig{} 110 boltConfig.Parse(c.Option) 111 handler, err := NewBoltHandler(boltConfig) 112 if err != nil { 113 return err 114 } 115 m.handler = handler 116 if err = m.newStore(); err != nil { 117 _ = handler.Close() 118 return err 119 } 120 121 if err = m.initAuthStoreData(); err != nil { 122 _ = handler.Close() 123 return err 124 } 125 126 if err = m.initNamingStoreData(); err != nil { 127 _ = handler.Close() 128 return err 129 } 130 m.start = true 131 return nil 132 } 133 134 const ( 135 namespacePolaris = "Polaris" 136 ownerToInit = "polaris" 137 ) 138 139 var ( 140 namespacesToInit = []string{"default", namespacePolaris} 141 servicesToInit = map[string]string{ 142 "polaris.checker": "fbca9bfa04ae4ead86e1ecf5811e32a9", 143 } 144 145 mainUser = &model.User{ 146 ID: "65e4789a6d5b49669adf1e9e8387549c", 147 Name: "polaris", 148 Password: "$2a$10$3izWuZtE5SBdAtSZci.gs.iZ2pAn9I8hEqYrC6gwJp1dyjqQnrrum", 149 Owner: "", 150 Source: "Polaris", 151 Mobile: "", 152 Email: "", 153 Type: 20, 154 Token: "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=", 155 TokenEnable: true, 156 Valid: true, 157 Comment: "default polaris admin account", 158 CreateTime: time.Now(), 159 ModifyTime: time.Now(), 160 } 161 162 mainDefaultStrategy = &model.StrategyDetail{ 163 ID: "fbca9bfa04ae4ead86e1ecf5811e32a9", 164 Name: "(用户) polaris的默认策略", 165 Action: "READ_WRITE", 166 Comment: "default admin", 167 Principals: []model.Principal{ 168 { 169 StrategyID: "fbca9bfa04ae4ead86e1ecf5811e32a9", 170 PrincipalID: "65e4789a6d5b49669adf1e9e8387549c", 171 PrincipalRole: model.PrincipalUser, 172 }, 173 }, 174 Default: true, 175 Owner: "65e4789a6d5b49669adf1e9e8387549c", 176 Resources: []model.StrategyResource{ 177 { 178 StrategyID: "fbca9bfa04ae4ead86e1ecf5811e32a9", 179 ResType: int32(apisecurity.ResourceType_Namespaces), 180 ResID: "*", 181 }, 182 { 183 StrategyID: "fbca9bfa04ae4ead86e1ecf5811e32a9", 184 ResType: int32(apisecurity.ResourceType_Services), 185 ResID: "*", 186 }, 187 { 188 StrategyID: "fbca9bfa04ae4ead86e1ecf5811e32a9", 189 ResType: int32(apisecurity.ResourceType_ConfigGroups), 190 ResID: "*", 191 }, 192 }, 193 Valid: true, 194 Revision: "fbca9bfa04ae4ead86e1ecf5811e32a9", 195 CreateTime: time.Now(), 196 ModifyTime: time.Now(), 197 } 198 ) 199 200 func (m *boltStore) initNamingStoreData() error { 201 for _, namespace := range namespacesToInit { 202 curTime := time.Now() 203 err := m.AddNamespace(&model.Namespace{ 204 Name: namespace, 205 Token: utils.NewUUID(), 206 Owner: ownerToInit, 207 Valid: true, 208 CreateTime: curTime, 209 ModifyTime: curTime, 210 }) 211 if err != nil { 212 return err 213 } 214 } 215 for svc, id := range servicesToInit { 216 curTime := time.Now() 217 err := m.AddService(&model.Service{ 218 ID: id, 219 Name: svc, 220 Namespace: namespacePolaris, 221 Token: utils.NewUUID(), 222 Owner: ownerToInit, 223 Revision: utils.NewUUID(), 224 Valid: true, 225 CreateTime: curTime, 226 ModifyTime: curTime, 227 }) 228 if err != nil { 229 return err 230 } 231 } 232 return nil 233 } 234 235 func (m *boltStore) initAuthStoreData() error { 236 return m.handler.Execute(true, func(tx *bolt.Tx) error { 237 user, err := m.getUser(tx, mainUser.ID) 238 if err != nil { 239 return err 240 } 241 242 if user == nil { 243 user = mainUser 244 // 添加主账户主体信息 245 if err := saveValue(tx, tblUser, user.ID, converToUserStore(user)); err != nil { 246 authLog.Error("[Store][User] save user fail", zap.Error(err), zap.String("name", user.Name)) 247 return err 248 } 249 } 250 251 rule, err := m.getStrategyDetail(tx, mainDefaultStrategy.ID) 252 if err != nil { 253 return err 254 } 255 256 if rule == nil { 257 strategy := mainDefaultStrategy 258 // 添加主账户的默认鉴权策略信息 259 if err := saveValue(tx, tblStrategy, strategy.ID, convertForStrategyStore(strategy)); err != nil { 260 authLog.Error("[Store][Strategy] save auth_strategy", zap.Error(err), 261 zap.String("name", strategy.Name), zap.String("owner", strategy.Owner)) 262 return err 263 } 264 } 265 return nil 266 }) 267 } 268 269 func (m *boltStore) newStore() error { 270 var err error 271 272 m.l5Store = &l5Store{handler: m.handler} 273 if err = m.l5Store.InitL5Data(); err != nil { 274 return err 275 } 276 m.namespaceStore = &namespaceStore{handler: m.handler} 277 if err = m.namespaceStore.InitData(); err != nil { 278 return err 279 } 280 m.clientStore = &clientStore{handler: m.handler} 281 282 m.newDiscoverModuleStore() 283 m.newAuthModuleStore() 284 m.newConfigModuleStore() 285 m.newMaintainModuleStore() 286 return nil 287 } 288 289 func (m *boltStore) newDiscoverModuleStore() { 290 m.serviceStore = &serviceStore{handler: m.handler} 291 m.instanceStore = &instanceStore{handler: m.handler} 292 m.routingStore = &routingStore{handler: m.handler} 293 m.rateLimitStore = &rateLimitStore{handler: m.handler} 294 m.circuitBreakerStore = &circuitBreakerStore{handler: m.handler} 295 m.faultDetectStore = &faultDetectStore{handler: m.handler} 296 m.routingStoreV2 = &routingStoreV2{handler: m.handler} 297 } 298 299 func (m *boltStore) newAuthModuleStore() { 300 m.userStore = &userStore{handler: m.handler} 301 m.strategyStore = &strategyStore{handler: m.handler} 302 m.groupStore = &groupStore{handler: m.handler} 303 } 304 305 func (m *boltStore) newConfigModuleStore() { 306 m.configFileStore = newConfigFileStore(m.handler) 307 m.configFileGroupStore = newConfigFileGroupStore(m.handler) 308 m.configFileReleaseHistoryStore = newConfigFileReleaseHistoryStore(m.handler) 309 m.configFileReleaseStore = newConfigFileReleaseStore(m.handler) 310 m.configFileTemplateStore = newConfigFileTemplateStore(m.handler) 311 } 312 313 func (m *boltStore) newMaintainModuleStore() { 314 m.adminStore = &adminStore{handler: m.handler, leMap: make(map[string]bool)} 315 } 316 317 // Destroy store 318 func (m *boltStore) Destroy() error { 319 m.start = false 320 if m.handler != nil { 321 return m.handler.Close() 322 } 323 return nil 324 } 325 326 // CreateTransaction create store transaction 327 func (m *boltStore) CreateTransaction() (store.Transaction, error) { 328 return &transaction{handler: m.handler}, nil 329 } 330 331 // StartTx starting transactions 332 func (m *boltStore) StartTx() (store.Tx, error) { 333 return m.handler.StartTx() 334 } 335 336 func (m *boltStore) StartReadTx() (store.Tx, error) { 337 return m.handler.StartTx() 338 } 339 340 func init() { 341 s := &boltStore{} 342 _ = store.RegisterStore(s) 343 }