Skip to content

Commit

Permalink
Merge pull request polarismesh#351 from chuntaojun/feat_issue_343
Browse files Browse the repository at this point in the history
[ISSUE polarismesh#343] 控制台访问与SDK访问鉴权分离
  • Loading branch information
andrewshan authored Apr 27, 2022
2 parents bebf8e4 + 725470a commit 95a63aa
Show file tree
Hide file tree
Showing 45 changed files with 888 additions and 284 deletions.
8 changes: 4 additions & 4 deletions apiserver/eurekaserver/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (h *EurekaServer) registerInstances(ctx context.Context, appId string, inst
// 1. 先转换数据结构
totalInstance := convertEurekaInstance(instance, h.namespace, appId)
// 3. 注册实例
resp := h.namingServer.CreateInstances(ctx, []*api.Instance{totalInstance})
resp := h.namingServer.RegisterInstance(ctx, totalInstance)
// 4. 注册成功,则返回
if resp.GetCode().GetValue() == api.ExecuteSuccess || resp.GetCode().GetValue() == api.ExistedResource {
return api.ExecuteSuccess
Expand All @@ -190,14 +190,14 @@ func (h *EurekaServer) registerInstances(ctx context.Context, appId string, inst
return svcCreateCode
}
// 6. 再重试注册实例列表
resp = h.namingServer.CreateInstances(ctx, []*api.Instance{totalInstance})
resp = h.namingServer.RegisterInstance(ctx, totalInstance)
return resp.GetCode().GetValue()
}
return resp.GetCode().GetValue()
}

func (h *EurekaServer) deregisterInstance(ctx context.Context, appId string, instanceId string) uint32 {
resp := h.namingServer.DeleteInstances(ctx, []*api.Instance{{Id: &wrappers.StringValue{Value: instanceId}}})
resp := h.namingServer.DeregisterInstance(ctx, &api.Instance{Id: &wrappers.StringValue{Value: instanceId}})
return resp.GetCode().GetValue()
}

Expand All @@ -207,7 +207,7 @@ func (h *EurekaServer) update(ctx context.Context, appId string, instanceId stri
isolated = true
}
resp := h.namingServer.UpdateInstances(ctx,
[]*api.Instance{&api.Instance{Id: &wrappers.StringValue{Value: instanceId}, Isolate: &wrappers.BoolValue{Value: isolated}}})
[]*api.Instance{{Id: &wrappers.StringValue{Value: instanceId}, Isolate: &wrappers.BoolValue{Value: isolated}}})
return resp.GetCode().GetValue()
}

Expand Down
14 changes: 6 additions & 8 deletions apiserver/grpcserver/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,30 @@ func (g *GRPCServer) ReportClient(ctx context.Context, in *api.Client) (*api.Res
func (g *GRPCServer) RegisterInstance(ctx context.Context, in *api.Instance) (*api.Response, error) {
// 需要记录操作来源,提高效率,只针对特殊接口添加operator
rCtx := ConvertContext(ctx)
operator := ParseGrpcOperator(ctx)
rCtx = context.WithValue(rCtx, utils.StringContext("operator"), operator)
rCtx = context.WithValue(rCtx, utils.StringContext("operator"), ParseGrpcOperator(ctx))

// 客户端请求中带了 token 的,优先已请求中的为准
if in.GetServiceToken().GetValue() != "" {
rCtx = context.WithValue(rCtx, utils.ContextAuthTokenKey, in.GetServiceToken().GetValue())
}

out := g.namingServer.CreateInstances(rCtx, []*api.Instance{in})
return out.Responses[0], nil
out := g.namingServer.RegisterInstance(rCtx, in)
return out, nil
}

// DeregisterInstance 反注册服务实例
func (g *GRPCServer) DeregisterInstance(ctx context.Context, in *api.Instance) (*api.Response, error) {
// 需要记录操作来源,提高效率,只针对特殊接口添加operator
rCtx := ConvertContext(ctx)
operator := ParseGrpcOperator(ctx)
rCtx = context.WithValue(rCtx, utils.StringContext("operator"), operator)
rCtx = context.WithValue(rCtx, utils.StringContext("operator"), ParseGrpcOperator(ctx))

// 客户端请求中带了 token 的,优先已请求中的为准
if in.GetServiceToken().GetValue() != "" {
rCtx = context.WithValue(rCtx, utils.ContextAuthTokenKey, in.GetServiceToken().GetValue())
}

out := g.namingServer.DeleteInstances(rCtx, []*api.Instance{in})
return out.Responses[0], nil
out := g.namingServer.DeregisterInstance(rCtx, in)
return out, nil
}

// Discover 统一发现接口
Expand Down
3 changes: 1 addition & 2 deletions apiserver/httpserver/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func (h *HTTPServer) GetAuthServer(ws *restful.WebService) error {
func (h *HTTPServer) AuthStatus(req *restful.Request, rsp *restful.Response) {
handler := &Handler{req, rsp}

isOpen := h.authServer.GetAuthChecker().IsOpenAuth()

isOpen := h.authServer.GetAuthChecker().IsOpenConsoleAuth()
resp := api.NewResponse(api.ExecuteSuccess)
resp.OptionSwitch = &api.OptionSwitch{
Options: map[string]string{
Expand Down
4 changes: 2 additions & 2 deletions apiserver/httpserver/naming_client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (h *HTTPServer) RegisterInstance(req *restful.Request, rsp *restful.Respons
return
}

handler.WriteHeaderAndProto(h.namingServer.CreateInstances(ctx, []*api.Instance{instance}))
handler.WriteHeaderAndProto(h.namingServer.RegisterInstance(ctx, instance))
}

// DeregisterInstance 反注册服务实例
Expand All @@ -113,7 +113,7 @@ func (h *HTTPServer) DeregisterInstance(req *restful.Request, rsp *restful.Respo
return
}

handler.WriteHeaderAndProto(h.namingServer.DeleteInstances(ctx, []*api.Instance{instance}))
handler.WriteHeaderAndProto(h.namingServer.DeregisterInstance(ctx, instance))
}

// Discover 统一发现接口
Expand Down
12 changes: 6 additions & 6 deletions apiserver/httpserver/naming_console_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (h *HTTPServer) CreateNamespaces(req *restful.Request, rsp *restful.Respons
return
}

handler.WriteHeaderAndProto(h.namingServer.CreateNamespaces(ctx, namespaces))
handler.WriteHeaderAndProto(h.namespaceServer.CreateNamespaces(ctx, namespaces))
}

// DeleteNamespaces 删除命名空间
Expand All @@ -198,7 +198,7 @@ func (h *HTTPServer) DeleteNamespaces(req *restful.Request, rsp *restful.Respons
return
}

ret := h.namingServer.DeleteNamespaces(ctx, namespaces)
ret := h.namespaceServer.DeleteNamespaces(ctx, namespaces)
if code := api.CalcCode(ret); code != http.StatusOK {
handler.WriteHeaderAndProto(ret)
return
Expand All @@ -222,7 +222,7 @@ func (h *HTTPServer) UpdateNamespaces(req *restful.Request, rsp *restful.Respons
return
}

ret := h.namingServer.UpdateNamespaces(ctx, namespaces)
ret := h.namespaceServer.UpdateNamespaces(ctx, namespaces)
if code := api.CalcCode(ret); code != http.StatusOK {
handler.WriteHeaderAndProto(ret)
return
Expand All @@ -235,7 +235,7 @@ func (h *HTTPServer) UpdateNamespaces(req *restful.Request, rsp *restful.Respons
func (h *HTTPServer) GetNamespaces(req *restful.Request, rsp *restful.Response) {
handler := &Handler{req, rsp}

ret := h.namingServer.GetNamespaces(handler.ParseHeaderContext(), req.Request.URL.Query())
ret := h.namespaceServer.GetNamespaces(handler.ParseHeaderContext(), req.Request.URL.Query())
handler.WriteHeaderAndProto(ret)
}

Expand All @@ -252,7 +252,7 @@ func (h *HTTPServer) GetNamespaceToken(req *restful.Request, rsp *restful.Respon
Token: utils.NewStringValue(queryParams["token"]),
}

ret := h.namingServer.GetNamespaceToken(ctx, namespace)
ret := h.namespaceServer.GetNamespaceToken(ctx, namespace)
handler.WriteHeaderAndProto(ret)
}

Expand All @@ -267,7 +267,7 @@ func (h *HTTPServer) UpdateNamespaceToken(req *restful.Request, rsp *restful.Res
return
}

ret := h.namingServer.UpdateNamespaceToken(ctx, &namespace)
ret := h.namespaceServer.UpdateNamespaceToken(ctx, &namespace)
handler.WriteHeaderAndProto(ret)
}

Expand Down
11 changes: 11 additions & 0 deletions apiserver/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/polarismesh/polaris-server/common/connlimit"
"github.com/polarismesh/polaris-server/common/utils"
"github.com/polarismesh/polaris-server/config"
"github.com/polarismesh/polaris-server/namespace"
"github.com/polarismesh/polaris-server/plugin"
"github.com/polarismesh/polaris-server/plugin/statis/local"
"github.com/polarismesh/polaris-server/service"
Expand All @@ -59,6 +60,7 @@ type HTTPServer struct {
freeMemMu *sync.Mutex

server *http.Server
namespaceServer namespace.NamespaceOperateServer
namingServer service.DiscoverServer
configServer *config.Server
healthCheckServer *healthcheck.Server
Expand Down Expand Up @@ -126,6 +128,15 @@ func (h *HTTPServer) Run(errCh chan error) {
}()

var err error

// 引入命名空间模块
h.namespaceServer, err = namespace.GetServer()
if err != nil {
log.Errorf("%v", err)
errCh <- err
return
}

// 引入功能模块和插件
h.namingServer, err = service.GetServer()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions apiserver/xdsserverv3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
api "github.com/polarismesh/polaris-server/common/api/v1"
"github.com/polarismesh/polaris-server/common/connlimit"
"github.com/polarismesh/polaris-server/common/model"
"github.com/polarismesh/polaris-server/namespace"
"github.com/polarismesh/polaris-server/service"
)

Expand Down Expand Up @@ -673,12 +674,12 @@ func (x *XDSServer) getRegistryInfoWithCache(ctx context.Context, registryInfo m

func (x *XDSServer) initRegistryInfo() error {

namingServer, err := service.GetOriginServer()
namespaceServer, err := namespace.GetOriginServer()
if err != nil {
return err
}

resp := namingServer.GetNamespaces(context.Background(), make(map[string][]string))
resp := namespaceServer.GetNamespaces(context.Background(), make(map[string][]string))
if resp.Code.Value != api.ExecuteSuccess {
return fmt.Errorf("error to init registry info %s", resp.Code)
}
Expand Down
16 changes: 8 additions & 8 deletions auth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ type AuthServer interface {

// AuthChecker 权限管理通用接口定义
type AuthChecker interface {

// Initialize 执行初始化动作
Initialize(options *Config, cacheMgn *cache.NamingCache) error

// VerifyToken 验证令牌
VerifyCredential(preCtx *model.AcquireContext) error

// CheckPermission 执行检查动作判断是否有权限,并且将 RequestContext 进行插入一些新的数据
CheckPermission(preCtx *model.AcquireContext) (bool, error)

// IsOpenAuth 返回是否开启了操作鉴权,可以用于前端查询
IsOpenAuth() bool
// CheckClientPermission 执行检查客户端动作判断是否有权限,并且对 RequestContext 注入操作者数据
CheckClientPermission(preCtx *model.AcquireContext) (bool, error)
// CheckConsolePermission 执行检查控制台动作判断是否有权限,并且对 RequestContext 注入操作者数据
CheckConsolePermission(preCtx *model.AcquireContext) (bool, error)
// IsOpenConsoleAuth 返回是否开启了操作鉴权,可以用于前端查询
IsOpenConsoleAuth() bool
// IsOpenClientAuth
IsOpenClientAuth() bool
}

// UserOperator 用户数据管理 server
Expand Down
Loading

0 comments on commit 95a63aa

Please sign in to comment.