MSP-8995 migrate admin client from grpc to http and update tests
This commit is contained in:
1 parent
4e924575ce
commit
80d9286498
5 files changed
+76
-49
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/http"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/lb"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
)
|
||||
@@ -30,7 +31,7 @@ type Assembly struct {
|
||||
logger *log.Adapter
|
||||
routes *routes.Routes
|
||||
systemCli *client.Client
|
||||
adminCli *client.Client
|
||||
adminCli *httpclix.ClientBalancer
|
||||
lockerCli *client.Client
|
||||
routerLb *lb.RoundRobin
|
||||
|
||||
@@ -72,10 +73,7 @@ func New(boot *bootstrap.Bootstrap) (*Assembly, error) {
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create system cli")
|
||||
}
|
||||
adminCli, err := client.Default()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create admin cli")
|
||||
}
|
||||
adminCli := httpclix.DefaultWithBalancer(nil)
|
||||
lockerCli, err := client.Default()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create isp-lock-service client")
|
||||
@@ -173,7 +171,7 @@ func (a *Assembly) Closers() []app.Closer {
|
||||
for _, cliCloser := range a.grpcClientByModuleName {
|
||||
closers = append(closers, cliCloser)
|
||||
}
|
||||
closers = append(closers, a.systemCli, a.adminCli, a.lockerCli)
|
||||
closers = append(closers, a.systemCli, a.lockerCli)
|
||||
|
||||
return closers
|
||||
}
|
||||
+3
-2
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/metrics"
|
||||
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
||||
|
||||
@@ -29,7 +30,7 @@ type Locator struct {
|
||||
httpHostManagerByModuleName map[string]*lb.RoundRobin
|
||||
routes *routes.Routes
|
||||
systemCli *client.Client
|
||||
adminCli *client.Client
|
||||
adminCli *httpclix.ClientBalancer
|
||||
lockerCli *client.Client
|
||||
routerLb *lb.RoundRobin
|
||||
usersAuthCache *cache.Cache
|
||||
@@ -41,7 +42,7 @@ func NewLocator(
|
||||
httpHostManagerByModuleName map[string]*lb.RoundRobin,
|
||||
routes *routes.Routes,
|
||||
systemCli *client.Client,
|
||||
adminCli *client.Client,
|
||||
adminCli *httpclix.ClientBalancer,
|
||||
lockerCli *client.Client,
|
||||
routerLb *lb.RoundRobin,
|
||||
usersAuthCache *cache.Cache,
|
||||
|
||||
+16
-9
@@ -7,7 +7,8 @@ import (
|
||||
"isp-gate-service/entity"
|
||||
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,33 +17,39 @@ const (
|
||||
)
|
||||
|
||||
type Admin struct {
|
||||
cli *client.Client
|
||||
cli *httpclix.ClientBalancer
|
||||
}
|
||||
|
||||
func NewAdmin(cli *client.Client) Admin {
|
||||
func NewAdmin(cli *httpclix.ClientBalancer) Admin {
|
||||
return Admin{cli: cli}
|
||||
}
|
||||
|
||||
func (r Admin) Authenticate(ctx context.Context, token string) (*entity.AdminAuthenticateResponse, error) {
|
||||
ctx = http_metrics.ClientEndpointToContext(ctx, adminAuthenticate)
|
||||
|
||||
resp := entity.AdminAuthenticateResponse{}
|
||||
err := r.cli.Invoke(adminAuthenticate).
|
||||
err := r.cli.Post(adminAuthenticate).
|
||||
JsonRequestBody(entity.AuthenticateRequest{Token: token}).
|
||||
JsonResponseBody(&resp).
|
||||
Do(ctx)
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "grpc client invoke %s", adminAuthenticate)
|
||||
return nil, errors.WithMessagef(err, "http client invoke %s", adminAuthenticate)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (r Admin) Authorize(ctx context.Context, adminId int, permission string) (bool, error) {
|
||||
ctx = http_metrics.ClientEndpointToContext(ctx, adminAuthorize)
|
||||
|
||||
resp := entity.AdminAuthorizeResponse{}
|
||||
err := r.cli.Invoke(adminAuthorize).
|
||||
err := r.cli.Post(adminAuthorize).
|
||||
JsonRequestBody(entity.AdminAuthorizeRequest{AdminId: adminId, Permission: permission}).
|
||||
JsonResponseBody(&resp).
|
||||
Do(ctx)
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return false, errors.WithMessagef(err, "grpc client invoke: %s", adminAuthorize)
|
||||
return false, errors.WithMessagef(err, "http client invoke %s", adminAuthorize)
|
||||
}
|
||||
return resp.Authorized, nil
|
||||
}
|
||||
+23
-14
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/txix-open/isp-kit/http/endpoint"
|
||||
"github.com/txix-open/isp-kit/http/endpoint/httplog"
|
||||
"github.com/txix-open/isp-kit/http/httpcli"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/http/router"
|
||||
"github.com/txix-open/isp-kit/test/fake"
|
||||
|
||||
@@ -580,7 +581,7 @@ func (s *HappyPathTestSuite) TestUserAuthorization_SkipAppAuth() { // nolint:fun
|
||||
}
|
||||
|
||||
// nolint:ireturn
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, *client.Client, *client.Client) {
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, *client.Client, *httpclix.ClientBalancer) {
|
||||
config := conf.Remote{
|
||||
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
|
||||
Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true,
|
||||
@@ -608,19 +609,27 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, *
|
||||
return entity.AuthorizeResponse{Authorized: true}
|
||||
})
|
||||
|
||||
adminService, adminCli := grpct.NewMock(test)
|
||||
adminService.Mock("admin/secure/authenticate", func(req entity.AdminAuthorizeRequest) entity.AdminAuthenticateResponse {
|
||||
return entity.AdminAuthenticateResponse{
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AdminId: 1,
|
||||
}
|
||||
}).Mock("admin/secure/authorize", func(req entity.AdminAuthorizeRequest) entity.AdminAuthorizeResponse {
|
||||
if req.Permission == "ok_permission" {
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
}
|
||||
return entity.AdminAuthorizeResponse{Authorized: false}
|
||||
})
|
||||
adminMux := router.New()
|
||||
defaultWrapper := endpoint.DefaultWrapper(test.Logger(), httplog.Noop())
|
||||
adminMux.POST("/admin/secure/authenticate",
|
||||
defaultWrapper.Endpoint(func(req entity.AuthenticateRequest) entity.AdminAuthenticateResponse {
|
||||
return entity.AdminAuthenticateResponse{
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AdminId: 1,
|
||||
}
|
||||
}))
|
||||
adminMux.POST("/admin/secure/authorize",
|
||||
defaultWrapper.Endpoint(func(req entity.AdminAuthorizeRequest) entity.AdminAuthorizeResponse {
|
||||
if req.Permission == "ok_permission" {
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
}
|
||||
return entity.AdminAuthorizeResponse{Authorized: false}
|
||||
}))
|
||||
adminMock := httptest.NewServer(adminMux)
|
||||
adminUrl, err := url.Parse(adminMock.URL)
|
||||
require.New(s.T()).NoError(err)
|
||||
adminCli := httpclix.NewClientBalancer([]string{adminUrl.Host})
|
||||
|
||||
return config, systemCli, adminCli
|
||||
}
|
||||
|
||||
+30
-18
@@ -4,10 +4,11 @@ import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
|
||||
"isp-gate-service/assembly"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/entity"
|
||||
"isp-gate-service/routes"
|
||||
|
||||
@@ -17,6 +18,10 @@ import (
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
endpoint2 "github.com/txix-open/isp-kit/grpc/endpoint"
|
||||
"github.com/txix-open/isp-kit/grpc/isp"
|
||||
"github.com/txix-open/isp-kit/http/endpoint"
|
||||
"github.com/txix-open/isp-kit/http/endpoint/httplog"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/http/router"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
)
|
||||
|
||||
@@ -55,7 +60,7 @@ func main() {
|
||||
Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 5000}},
|
||||
}
|
||||
|
||||
systemService, systemCli, _ := NewMock(logger)
|
||||
systemService, systemCli := NewMock(logger)
|
||||
systemService.Mock("system/secure/authenticate", func() entity.AuthenticateResponse {
|
||||
return entity.AuthenticateResponse{
|
||||
Authenticated: true,
|
||||
@@ -71,16 +76,9 @@ func main() {
|
||||
return entity.AuthorizeResponse{Authorized: true}
|
||||
})
|
||||
|
||||
adminService, _, adminCli := NewMock(logger)
|
||||
adminService.Mock("admin/secure/authenticate", func() domain.AdminAuthenticateResponse {
|
||||
return domain.AdminAuthenticateResponse{
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AdminId: 1,
|
||||
}
|
||||
})
|
||||
adminCli := NewAdminMock(logger)
|
||||
|
||||
targetService, targetCli, _ := NewMock(logger)
|
||||
targetService, targetCli := NewMock(logger)
|
||||
targetService.Mock("endpoint", func(req request) response {
|
||||
return response{Id: req.Id}
|
||||
})
|
||||
@@ -102,19 +100,33 @@ func main() {
|
||||
srv.ListenAndServe()
|
||||
}
|
||||
|
||||
func TestServer(service isp.BackendServiceServer) (*grpc.Server, *client.Client, *client.Client) {
|
||||
func NewAdminMock(logger log.Logger) *httpclix.ClientBalancer {
|
||||
mux := router.New()
|
||||
wrapper := endpoint.DefaultWrapper(logger, httplog.Noop())
|
||||
mux.POST("/admin/secure/authenticate",
|
||||
wrapper.Endpoint(func() entity.AdminAuthenticateResponse {
|
||||
return entity.AdminAuthenticateResponse{
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AdminId: 1,
|
||||
}
|
||||
}))
|
||||
mock := httptest.NewServer(mux)
|
||||
u, _ := url.Parse(mock.URL)
|
||||
return httpclix.NewClientBalancer([]string{u.Host})
|
||||
}
|
||||
|
||||
func TestServer(service isp.BackendServiceServer) (*grpc.Server, *client.Client) {
|
||||
listener, _ := net.Listen("tcp", "127.0.0.1:") // nolint:noctx
|
||||
srv := grpc.NewServer()
|
||||
sysCli, _ := client.Default()
|
||||
adminCli, _ := client.Default()
|
||||
srv.Upgrade(service)
|
||||
go func() {
|
||||
_ = srv.Serve(listener)
|
||||
}()
|
||||
|
||||
sysCli.Upgrade([]string{listener.Addr().String()})
|
||||
adminCli.Upgrade([]string{listener.Addr().String()})
|
||||
return srv, sysCli, adminCli
|
||||
return srv, sysCli
|
||||
}
|
||||
|
||||
type MockServer struct {
|
||||
@@ -123,13 +135,13 @@ type MockServer struct {
|
||||
mockEndpoints map[string]any
|
||||
}
|
||||
|
||||
func NewMock(logger log.Logger) (*MockServer, *client.Client, *client.Client) {
|
||||
srv, syscli, admincli := TestServer(grpc.NewMux())
|
||||
func NewMock(logger log.Logger) (*MockServer, *client.Client) {
|
||||
srv, syscli := TestServer(grpc.NewMux())
|
||||
return &MockServer{
|
||||
srv: srv,
|
||||
logger: logger,
|
||||
mockEndpoints: make(map[string]any),
|
||||
}, syscli, admincli
|
||||
}, syscli
|
||||
}
|
||||
|
||||
func (m *MockServer) Mock(endpoint string, handler any) *MockServer {
|
||||
|
||||
Reference in new issue
Block a user