Skip to content

Commit

Permalink
feat:add plugin register and get func
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Mar 4, 2024
1 parent a8123df commit 83c78f8
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 91 deletions.
124 changes: 51 additions & 73 deletions access_control/auth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,29 @@ package auth

import (
"context"
"errors"
"fmt"
"sync"

apisecurity "github.com/polarismesh/specification/source/go/api/v1/security"
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
)

var (
_checkerSlots = make(map[string]AuthChecker)
)

// Register 注册插件
func RegisterAuthChecker(name string, plugin AuthChecker) {
if _, exist := _checkerSlots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
_checkerSlots[name] = plugin
}

func GetAuthChecker(name string) (AuthChecker, bool) {
plugin, exist := _checkerSlots[name]
return plugin, exist
}

// AuthChecker 权限管理通用接口定义
type AuthChecker interface {
// Initialize 执行初始化动作
Expand All @@ -43,6 +58,23 @@ type AuthChecker interface {
IsOpenClientAuth() bool
}

var (
_userSlots = make(map[string]UserServer)
)

// Register 注册插件
func RegisterUserServer(name string, plugin UserServer) {
if _, exist := _userSlots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
_userSlots[name] = plugin
}

func GetUserServer(name string) (UserServer, bool) {
plugin, exist := _userSlots[name]
return plugin, exist
}

// UserServer 用户数据管理 server
type UserServer interface {
// Initialize 初始化
Expand Down Expand Up @@ -90,6 +122,23 @@ type GroupOperator interface {
ResetGroupToken(ctx context.Context, group *apisecurity.UserGroup) *apiservice.Response
}

var (
_strategySlots = make(map[string]StrategyServer)
)

// Register 注册插件
func RegisterStrategyServer(name string, plugin StrategyServer) {
if _, exist := _strategySlots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
_strategySlots[name] = plugin
}

func GetStrategyServer(name string) (StrategyServer, bool) {
plugin, exist := _strategySlots[name]
return plugin, exist
}

// StrategyServer 策略相关操作
type StrategyServer interface {
// Initialize 初始化
Expand All @@ -116,13 +165,6 @@ type StrategyServer interface {
AfterResourceOperation(afterCtx *AcquireContext) error
}

const (
// DefaultUserMgnPluginName default user server name
DefaultUserMgnPluginName = "defaultUser"
// DefaultStrategyMgnPluginName default strategy server name
DefaultStrategyMgnPluginName = "defaultStrategy"
)

// Config 鉴权能力的相关配置参数
type Config struct {
// Name 原AuthServer名称,已废弃
Expand All @@ -136,21 +178,6 @@ type Config struct {
Strategy *StrategyConfig `yaml:"strategy"`
}

func (c *Config) SetDefault() {
if c.User == nil {
c.User = &UserConfig{
Name: DefaultUserMgnPluginName,
Option: map[string]interface{}{},
}
}
if c.Strategy == nil {
c.Strategy = &StrategyConfig{
Name: DefaultStrategyMgnPluginName,
Option: map[string]interface{}{},
}
}
}

// UserConfig UserOperator的相关配置
type UserConfig struct {
// Name UserOperator的名称
Expand All @@ -166,52 +193,3 @@ type StrategyConfig struct {
// Option StrategyOperator的option
Option map[string]interface{} `yaml:"option"`
}

var (
// userMgnSlots 保存用户管理manager slot
userMgnSlots = map[string]UserServer{}
// strategyMgnSlots 保存策略管理manager slot
strategyMgnSlots = map[string]StrategyServer{}
once sync.Once
userMgn UserServer
strategyMgn StrategyServer
finishInit bool
)

// RegisterUserServer 注册一个新的 UserServer
func RegisterUserServer(s UserServer) error {
name := s.Name()
if _, ok := userMgnSlots[name]; ok {
return fmt.Errorf("UserServer=[%s] exist", name)
}

userMgnSlots[name] = s
return nil
}

// GetUserServer 获取一个 UserServer
func GetUserServer() (UserServer, error) {
if !finishInit {
return nil, errors.New("UserServer has not done Initialize")
}
return userMgn, nil
}

// RegisterStrategyServer 注册一个新的 StrategyServer
func RegisterStrategyServer(s StrategyServer) error {
name := s.Name()
if _, ok := strategyMgnSlots[name]; ok {
return fmt.Errorf("StrategyServer=[%s] exist", name)
}

strategyMgnSlots[name] = s
return nil
}

// GetStrategyServer 获取一个 StrategyServer
func GetStrategyServer() (StrategyServer, error) {
if !finishInit {
return nil, errors.New("StrategyServer has not done Initialize")
}
return strategyMgn, nil
}
21 changes: 21 additions & 0 deletions access_control/ratelimit/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@

package ratelimit

import (
"fmt"
)

var (
slots = make(map[string]Ratelimit)
)

// Register 注册插件
func Register(name string, plugin Ratelimit) {
if _, exist := slots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
slots[name] = plugin
}

func Get(name string) (Ratelimit, bool) {
plugin, exist := slots[name]
return plugin, exist
}

// RatelimitType rate limit type
type RatelimitType int

Expand Down
21 changes: 21 additions & 0 deletions access_control/whitelist/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@

package whitelist

import (
"fmt"
)

var (
slots = make(map[string]Whitelist)
)

// Register 注册插件
func Register(name string, plugin Whitelist) {
if _, exist := slots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
slots[name] = plugin
}

func Get(name string) (Whitelist, bool) {
server, exist := slots[name]
return server, exist
}

// ConfigEntry 单个插件配置
type ConfigEntry struct {
Name string `yaml:"name"`
Expand Down
12 changes: 8 additions & 4 deletions apiserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ type Apiserver interface {
}

var (
Slots = make(map[string]Apiserver)
slots = make(map[string]Apiserver)
)

// Register 注册API服务器
func Register(name string, server Apiserver) error {
if _, exist := Slots[name]; exist {
if _, exist := slots[name]; exist {
return fmt.Errorf("apiserver name:%s exist", name)
}

Slots[name] = server

slots[name] = server
return nil
}

func Get(name string) (Apiserver, bool) {
server, exist := slots[name]
return server, exist
}
19 changes: 19 additions & 0 deletions cmdb/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@
package plugin

import (
"fmt"

apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
)

var (
slots = make(map[string]CMDB)
)

// Register 注册插件
func Register(name string, plugin CMDB) {
if _, exist := slots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
slots[name] = plugin
}

func Get(name string) (CMDB, bool) {
server, exist := slots[name]
return server, exist
}

// Location cmdb信息,对应内存结构体
type Location struct {
Proto *apimodel.Location
Expand Down
36 changes: 36 additions & 0 deletions crypto/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@

package crypto

import "fmt"

var (
slots = make(map[string]Crypto)
)

// Register 注册插件
func RegisterCrypto(name string, plugin Crypto) {
if _, exist := slots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
slots[name] = plugin
}

func GetCrypto(name string) (Crypto, bool) {
server, exist := slots[name]
return server, exist
}

// ConfigEntry 单个插件配置
type ConfigEntry struct {
Name string `yaml:"name"`
Expand All @@ -39,6 +58,23 @@ type Crypto interface {
Decrypt(cryptotext string, key []byte) (string, error)
}

var (
_pwdslots = make(map[string]ParsePassword)
)

// Register 注册插件
func RegisterParsePassword(name string, plugin ParsePassword) {
if _, exist := _pwdslots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
_pwdslots[name] = plugin
}

func GetParsePassword(name string) (ParsePassword, bool) {
server, exist := _pwdslots[name]
return server, exist
}

// ParsePassword Password plug -in
type ParsePassword interface {
// Name .
Expand Down
18 changes: 18 additions & 0 deletions observability/discoverevent/api.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package discoverevent

Check failure on line 1 in observability/discoverevent/api.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.21.5)

file-header: the file doesn't have an appropriate header (revive)

import (
"fmt"
"time"

apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
)

var (
slots = make(map[string]DiscoverChannel)
)

// Register 注册插件
func Register(name string, plugin DiscoverChannel) {
if _, exist := slots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
slots[name] = plugin
}

func Get(name string) (DiscoverChannel, bool) {
server, exist := slots[name]
return server, exist
}

// ConfigEntry 单个插件配置
type ConfigEntry struct {
Name string `yaml:"name"`
Expand Down
17 changes: 17 additions & 0 deletions observability/history/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ import (
"time"
)

var (
slots = make(map[string]History)
)

// Register 注册插件
func Register(name string, plugin History) {
if _, exist := slots[name]; exist {
panic(fmt.Sprintf("existed plugin: name=%v", name))
}
slots[name] = plugin
}

func Get(name string) (History, bool) {
server, exist := slots[name]
return server, exist
}

// ConfigEntry 单个插件配置
type ConfigEntry struct {
Name string `yaml:"name"`
Expand Down
Loading

0 comments on commit 83c78f8

Please sign in to comment.