Compare commits
49
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6d4324b15 | ||
|
|
798cc6b30f | ||
|
|
6750784d6b | ||
|
|
45ae8cdd89 | ||
|
|
295513e91c | ||
|
|
36699fdc72 | ||
|
|
a269de8e17 | ||
|
|
15cb2cadf2 | ||
|
|
76f88fd593 | ||
|
|
9ff0057014 | ||
|
|
71a1f0a04e | ||
|
|
394bbc82e2 | ||
|
|
7de248e63c | ||
|
|
011a1d7048 | ||
|
|
f3fb9cff75 | ||
|
|
c7bd042239 | ||
|
|
202fa7035e | ||
|
|
602a2a1ac4 | ||
|
|
6a3abf67a2 | ||
|
|
2f88738f78 | ||
|
|
a637d56ea7 | ||
|
|
a841cd082b | ||
|
|
9b18fe7351 | ||
|
|
f3c801e4a4 | ||
|
|
ee13991d87 | ||
|
|
d4846359e4 | ||
|
|
13cf0075f6 | ||
|
|
135e551473 | ||
|
|
689f730a3e | ||
|
|
a30582fa38 | ||
|
|
5b1ce14467 | ||
|
|
264b438cdb | ||
|
|
f699aefa58 | ||
|
|
8d40e76637 | ||
|
|
e2b4becfd6 | ||
|
|
e4ef2d8645 | ||
|
|
bfc28503d5 | ||
|
|
d759f883f4 | ||
|
|
64373fdf3c | ||
|
|
1dd566593e | ||
|
|
560c1f22c4 | ||
|
|
171b85f627 | ||
|
|
734b83b2b8 | ||
|
|
bdeb2c0753 | ||
|
|
97c44fb3bd | ||
|
|
6db18c1495 | ||
|
|
cd985ff5a3 | ||
|
|
2eed857503 | ||
|
|
2b91caece0 |
No files matched your search
@@ -1,84 +0,0 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"isp-gate-service/conf"
|
||||
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
protocol string
|
||||
|
||||
httpClient *httpclix.ClientBalancer
|
||||
grpcClient *client.Client
|
||||
}
|
||||
|
||||
func NewClient(protocol string) (*Client, error) {
|
||||
if protocol == "" {
|
||||
protocol = conf.HttpProtocol
|
||||
}
|
||||
|
||||
switch protocol {
|
||||
case conf.GrpcProtocol:
|
||||
cli, err := client.Default()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "init grpc client")
|
||||
}
|
||||
return &Client{protocol: protocol, grpcClient: cli}, nil
|
||||
case conf.HttpProtocol:
|
||||
return &Client{protocol: protocol, httpClient: httpclix.DefaultWithBalancer(nil)}, nil
|
||||
default:
|
||||
return nil, errors.Errorf("unsupported protocol: %s", protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func (ac *Client) Upgrade(hosts []string) {
|
||||
switch ac.protocol {
|
||||
case conf.HttpProtocol:
|
||||
ac.httpClient.Upgrade(hosts)
|
||||
case conf.GrpcProtocol:
|
||||
ac.grpcClient.Upgrade(hosts)
|
||||
}
|
||||
}
|
||||
|
||||
func (ac *Client) Invoke(ctx context.Context, method string, request any, response any) error {
|
||||
switch ac.protocol {
|
||||
case conf.HttpProtocol:
|
||||
err := ac.httpClient.
|
||||
Post(method).
|
||||
JsonRequestBody(request).
|
||||
JsonResponseBody(response).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "invoke http client")
|
||||
}
|
||||
case conf.GrpcProtocol:
|
||||
err := ac.grpcClient.
|
||||
Invoke(method).
|
||||
JsonRequestBody(request).
|
||||
JsonResponseBody(response).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "invoke grpc client")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ac *Client) Close() error {
|
||||
switch ac.protocol {
|
||||
case conf.GrpcProtocol:
|
||||
err := ac.grpcClient.Close()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "close grpc client")
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,295 +0,0 @@
|
||||
// Package admin_test contains unit tests for the admin protocol multiplexer client.
|
||||
package admin_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"isp-gate-service/admin"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/entity"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
ispgrpc "github.com/txix-open/isp-kit/grpc"
|
||||
"github.com/txix-open/isp-kit/grpc/isp"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
"github.com/txix-open/isp-kit/requestid"
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
httpt "github.com/txix-open/isp-kit/test/httpt"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type AdminClientTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) TestNewClient() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
for _, protocol := range []string{"", conf.HttpProtocol, conf.GrpcProtocol} {
|
||||
cli := s.newAdminClient(test, protocol)
|
||||
require.NotNil(cli)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) TestHttpInvoke() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
receivedToken string
|
||||
receivedReqId string
|
||||
)
|
||||
mock := httpt.NewMock(test)
|
||||
mock.POST("/admin/secure/authenticate", func(r *http.Request, req entity.AuthenticateRequest) entity.AdminAuthenticateResponse {
|
||||
mu.Lock()
|
||||
receivedToken = req.Token
|
||||
receivedReqId = r.Header.Get(requestid.Header)
|
||||
mu.Unlock()
|
||||
|
||||
return entity.AdminAuthenticateResponse{Authenticated: true, AdminId: 42}
|
||||
})
|
||||
|
||||
cli := s.newAdminClient(test, conf.HttpProtocol)
|
||||
cli.Upgrade([]string{s.mustHost(test, mock.BaseURL())})
|
||||
|
||||
resp := entity.AdminAuthenticateResponse{}
|
||||
err := cli.Invoke(test.T().Context(), "admin/secure/authenticate", &entity.AuthenticateRequest{Token: "secret-token"}, &resp)
|
||||
require.NoError(err)
|
||||
|
||||
require.True(resp.Authenticated)
|
||||
require.Equal(42, resp.AdminId)
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
require.Equal("secret-token", receivedToken)
|
||||
require.NotEmpty(receivedReqId)
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) TestHttpInvokeStatusError() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
mock := httpt.NewMock(test)
|
||||
mock.POST("/admin/secure/authenticate", func() (entity.AdminAuthenticateResponse, error) {
|
||||
return entity.AdminAuthenticateResponse{}, errors.New("admin service failure")
|
||||
})
|
||||
|
||||
cli := s.newAdminClient(test, conf.HttpProtocol)
|
||||
cli.Upgrade([]string{s.mustHost(test, mock.BaseURL())})
|
||||
|
||||
resp := entity.AdminAuthenticateResponse{}
|
||||
err := cli.Invoke(test.T().Context(), "admin/secure/authenticate", &entity.AuthenticateRequest{Token: "secret-token"}, &resp)
|
||||
require.Error(err)
|
||||
require.False(resp.Authenticated)
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) TestHttpUpgradeRouting() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
var (
|
||||
hits1 atomic.Int32
|
||||
hits2 atomic.Int32
|
||||
)
|
||||
mock1 := httpt.NewMock(test)
|
||||
mock1.POST("/admin/secure/authorize", func() entity.AdminAuthorizeResponse {
|
||||
hits1.Add(1)
|
||||
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
})
|
||||
mock2 := httpt.NewMock(test)
|
||||
mock2.POST("/admin/secure/authorize", func() entity.AdminAuthorizeResponse {
|
||||
hits2.Add(1)
|
||||
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
})
|
||||
|
||||
cli := s.newAdminClient(test, conf.HttpProtocol)
|
||||
|
||||
invoker := func() {
|
||||
resp := entity.AdminAuthorizeResponse{}
|
||||
err := cli.Invoke(test.T().Context(), "admin/secure/authorize", entity.AdminAuthorizeRequest{AdminId: 1, Permission: "read"}, &resp)
|
||||
require.NoError(err)
|
||||
require.True(resp.Authorized)
|
||||
}
|
||||
|
||||
cli.Upgrade([]string{s.mustHost(test, mock1.BaseURL())})
|
||||
invoker()
|
||||
require.EqualValues(1, hits1.Load())
|
||||
require.EqualValues(0, hits2.Load())
|
||||
|
||||
cli.Upgrade([]string{s.mustHost(test, mock2.BaseURL())})
|
||||
invoker()
|
||||
require.EqualValues(1, hits1.Load())
|
||||
require.EqualValues(1, hits2.Load())
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) TestHttpUpgradeBalancing() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
var (
|
||||
hits1 atomic.Int32
|
||||
hits2 atomic.Int32
|
||||
)
|
||||
mock1 := httpt.NewMock(test)
|
||||
mock1.POST("/admin/secure/authorize", func() entity.AdminAuthorizeResponse {
|
||||
hits1.Add(1)
|
||||
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
})
|
||||
mock2 := httpt.NewMock(test)
|
||||
mock2.POST("/admin/secure/authorize", func() entity.AdminAuthorizeResponse {
|
||||
hits2.Add(1)
|
||||
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
})
|
||||
|
||||
cli := s.newAdminClient(test, conf.HttpProtocol)
|
||||
cli.Upgrade([]string{s.mustHost(test, mock1.BaseURL()), s.mustHost(test, mock2.BaseURL())})
|
||||
|
||||
invoker := func() {
|
||||
resp := entity.AdminAuthorizeResponse{}
|
||||
err := cli.Invoke(test.T().Context(), "admin/secure/authorize", entity.AdminAuthorizeRequest{AdminId: 1, Permission: "read"}, &resp)
|
||||
require.NoError(err)
|
||||
require.True(resp.Authorized)
|
||||
}
|
||||
|
||||
invoker()
|
||||
invoker()
|
||||
require.EqualValues(1, hits1.Load())
|
||||
require.EqualValues(1, hits2.Load())
|
||||
}
|
||||
|
||||
// TestGrpcInvoke uses a custom backend because grpct.NewMock does not expose
|
||||
// the mock address, which is required to upgrade the client.
|
||||
func (s *AdminClientTestSuite) TestGrpcInvoke() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
stub := &backendStub{}
|
||||
cli := s.newAdminClient(test, conf.GrpcProtocol)
|
||||
cli.Upgrade([]string{s.startGrpcBackend(test, stub)})
|
||||
|
||||
resp := entity.AdminAuthenticateResponse{}
|
||||
err := cli.Invoke(test.T().Context(), "admin/secure/authenticate", &entity.AuthenticateRequest{Token: "secret-token"}, &resp)
|
||||
require.NoError(err)
|
||||
|
||||
require.True(resp.Authenticated)
|
||||
require.Equal(7, resp.AdminId)
|
||||
|
||||
methods, tokens := stub.calls()
|
||||
require.Equal([]string{"admin/secure/authenticate"}, methods)
|
||||
require.Equal([]string{"secret-token"}, tokens)
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) TestGrpcUpgradeRouting() {
|
||||
test, require := test.New(s.T())
|
||||
|
||||
first := &backendStub{}
|
||||
second := &backendStub{}
|
||||
cli := s.newAdminClient(test, conf.GrpcProtocol)
|
||||
cli.Upgrade([]string{s.startGrpcBackend(test, first)})
|
||||
|
||||
resp := entity.AdminAuthenticateResponse{}
|
||||
err := cli.Invoke(test.T().Context(), "admin/secure/authenticate", &entity.AuthenticateRequest{Token: "first"}, &resp)
|
||||
require.NoError(err)
|
||||
require.True(resp.Authenticated)
|
||||
|
||||
cli.Upgrade([]string{s.startGrpcBackend(test, second)})
|
||||
|
||||
resp = entity.AdminAuthenticateResponse{}
|
||||
err = cli.Invoke(test.T().Context(), "admin/secure/authenticate", &entity.AuthenticateRequest{Token: "second"}, &resp)
|
||||
require.NoError(err)
|
||||
require.True(resp.Authenticated)
|
||||
|
||||
_, tokens := first.calls()
|
||||
require.Equal([]string{"first"}, tokens)
|
||||
_, tokens = second.calls()
|
||||
require.Equal([]string{"second"}, tokens)
|
||||
}
|
||||
|
||||
// backendStub is a minimal isp.BackendService implementation that records
|
||||
// received requests and answers with a fixed authentication response.
|
||||
type backendStub struct {
|
||||
isp.UnimplementedBackendServiceServer
|
||||
|
||||
locker sync.Mutex
|
||||
methods []string
|
||||
tokens []string
|
||||
}
|
||||
|
||||
func (s *backendStub) Request(ctx context.Context, in *isp.Message) (*isp.Message, error) {
|
||||
md, _ := metadata.FromIncomingContext(ctx)
|
||||
method, _ := ispgrpc.StringFromMd(ispgrpc.ProxyMethodNameHeader, md)
|
||||
|
||||
var req entity.AuthenticateRequest
|
||||
_ = json.Unmarshal(in.GetBytesBody(), &req)
|
||||
|
||||
s.locker.Lock()
|
||||
s.methods = append(s.methods, method)
|
||||
s.tokens = append(s.tokens, req.Token)
|
||||
s.locker.Unlock()
|
||||
|
||||
data, err := json.Marshal(entity.AdminAuthenticateResponse{Authenticated: true, AdminId: 7})
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "marshal admin authenticate response")
|
||||
}
|
||||
return &isp.Message{Body: &isp.Message_BytesBody{BytesBody: data}}, nil
|
||||
}
|
||||
|
||||
func (s *backendStub) calls() ([]string, []string) {
|
||||
s.locker.Lock()
|
||||
defer s.locker.Unlock()
|
||||
|
||||
return slices.Clone(s.methods), slices.Clone(s.tokens)
|
||||
}
|
||||
|
||||
// nolint:ireturn
|
||||
func (s *AdminClientTestSuite) newAdminClient(test *test.Test, protocol string) *admin.Client {
|
||||
test.T().Helper()
|
||||
require := test.Assert()
|
||||
|
||||
cli, err := admin.NewClient(protocol)
|
||||
require.NoError(err)
|
||||
require.NotNil(cli)
|
||||
s.T().Cleanup(func() { require.NoError(cli.Close()) })
|
||||
|
||||
return cli
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) startGrpcBackend(test *test.Test, stub *backendStub) string {
|
||||
test.T().Helper()
|
||||
require := test.Assert()
|
||||
|
||||
var lc net.ListenConfig
|
||||
listener, err := lc.Listen(test.T().Context(), "tcp", "127.0.0.1:")
|
||||
require.NoError(err)
|
||||
|
||||
srv := ispgrpc.NewServer()
|
||||
srv.Upgrade(stub)
|
||||
s.T().Cleanup(srv.Shutdown)
|
||||
go func() {
|
||||
_ = srv.Serve(listener)
|
||||
}()
|
||||
|
||||
return listener.Addr().String()
|
||||
}
|
||||
|
||||
func (s *AdminClientTestSuite) mustHost(test *test.Test, rawUrl string) string {
|
||||
test.T().Helper()
|
||||
|
||||
u, err := url.Parse(rawUrl)
|
||||
test.Assert().NoError(err)
|
||||
|
||||
return u.Host
|
||||
}
|
||||
|
||||
func TestAdminClientTestSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, new(AdminClientTestSuite))
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package assembly
|
||||
|
||||
import (
|
||||
"context"
|
||||
"isp-gate-service/admin"
|
||||
"time"
|
||||
|
||||
"isp-gate-service/cache"
|
||||
@@ -15,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"
|
||||
)
|
||||
@@ -31,7 +31,7 @@ type Assembly struct {
|
||||
logger *log.Adapter
|
||||
routes *routes.Routes
|
||||
systemCli *client.Client
|
||||
adminCli *admin.Client
|
||||
adminCli *httpclix.ClientBalancer
|
||||
lockerCli *client.Client
|
||||
routerLb *lb.RoundRobin
|
||||
|
||||
@@ -73,16 +73,12 @@ func New(boot *bootstrap.Bootstrap) (*Assembly, error) {
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create system cli")
|
||||
}
|
||||
adminCli := httpclix.DefaultWithBalancer(nil)
|
||||
lockerCli, err := client.Default()
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create isp-lock-service client")
|
||||
}
|
||||
|
||||
adminCli, err := admin.NewClient(localConfig.AdminServiceProtocol)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "create admin service client")
|
||||
}
|
||||
|
||||
return &Assembly{
|
||||
boot: boot,
|
||||
server: server,
|
||||
@@ -175,7 +171,7 @@ func (a *Assembly) Closers() []app.Closer {
|
||||
for _, cliCloser := range a.grpcClientByModuleName {
|
||||
closers = append(closers, cliCloser)
|
||||
}
|
||||
closers = append(closers, a.systemCli, a.lockerCli, a.adminCli)
|
||||
closers = append(closers, a.systemCli, a.lockerCli)
|
||||
|
||||
return closers
|
||||
}
|
||||
+3
-3
@@ -1,11 +1,11 @@
|
||||
package assembly
|
||||
|
||||
import (
|
||||
"isp-gate-service/admin"
|
||||
"net/http"
|
||||
"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"
|
||||
|
||||
@@ -30,7 +30,7 @@ type Locator struct {
|
||||
httpHostManagerByModuleName map[string]*lb.RoundRobin
|
||||
routes *routes.Routes
|
||||
systemCli *client.Client
|
||||
adminCli *admin.Client
|
||||
adminCli *httpclix.ClientBalancer
|
||||
lockerCli *client.Client
|
||||
routerLb *lb.RoundRobin
|
||||
usersAuthCache *cache.Cache
|
||||
@@ -42,7 +42,7 @@ func NewLocator(
|
||||
httpHostManagerByModuleName map[string]*lb.RoundRobin,
|
||||
routes *routes.Routes,
|
||||
systemCli *client.Client,
|
||||
adminCli *admin.Client,
|
||||
adminCli *httpclix.ClientBalancer,
|
||||
lockerCli *client.Client,
|
||||
routerLb *lb.RoundRobin,
|
||||
usersAuthCache *cache.Cache,
|
||||
|
||||
@@ -18,7 +18,6 @@ logfile:
|
||||
compress: true
|
||||
|
||||
adminServiceName: msp-admin-service
|
||||
adminServiceProtocol: grpc
|
||||
|
||||
locations:
|
||||
- pathPrefix: /api
|
||||
|
||||
+2
-3
@@ -7,9 +7,8 @@ const (
|
||||
)
|
||||
|
||||
type Local struct {
|
||||
Locations []Location
|
||||
AdminServiceName string
|
||||
AdminServiceProtocol string `validate:"oneof=http grpc"`
|
||||
Locations []Location
|
||||
AdminServiceName string
|
||||
}
|
||||
|
||||
type Location struct {
|
||||
|
||||
+15
-20
@@ -7,6 +7,7 @@ import (
|
||||
"isp-gate-service/entity"
|
||||
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
||||
)
|
||||
|
||||
@@ -15,15 +16,11 @@ const (
|
||||
adminAuthorize = "admin/secure/authorize"
|
||||
)
|
||||
|
||||
type Invoker interface {
|
||||
Invoke(ctx context.Context, method string, request any, response any) error
|
||||
}
|
||||
|
||||
type Admin struct {
|
||||
cli Invoker
|
||||
cli *httpclix.ClientBalancer
|
||||
}
|
||||
|
||||
func NewAdmin(cli Invoker) Admin {
|
||||
func NewAdmin(cli *httpclix.ClientBalancer) Admin {
|
||||
return Admin{cli: cli}
|
||||
}
|
||||
|
||||
@@ -31,14 +28,13 @@ func (r Admin) Authenticate(ctx context.Context, token string) (*entity.AdminAut
|
||||
ctx = http_metrics.ClientEndpointToContext(ctx, adminAuthenticate)
|
||||
|
||||
resp := entity.AdminAuthenticateResponse{}
|
||||
err := r.cli.Invoke(
|
||||
ctx,
|
||||
adminAuthenticate,
|
||||
&entity.AuthenticateRequest{Token: token},
|
||||
&resp,
|
||||
)
|
||||
err := r.cli.Post(adminAuthenticate).
|
||||
JsonRequestBody(entity.AuthenticateRequest{Token: token}).
|
||||
JsonResponseBody(&resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "client invoke %s", adminAuthenticate)
|
||||
return nil, errors.WithMessagef(err, "http client invoke %s", adminAuthenticate)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
@@ -47,14 +43,13 @@ func (r Admin) Authorize(ctx context.Context, adminId int, permission string) (b
|
||||
ctx = http_metrics.ClientEndpointToContext(ctx, adminAuthorize)
|
||||
|
||||
resp := entity.AdminAuthorizeResponse{}
|
||||
err := r.cli.Invoke(
|
||||
ctx,
|
||||
adminAuthorize,
|
||||
entity.AdminAuthorizeRequest{AdminId: adminId, Permission: permission},
|
||||
&resp,
|
||||
)
|
||||
err := r.cli.Post(adminAuthorize).
|
||||
JsonRequestBody(entity.AdminAuthorizeRequest{AdminId: adminId, Permission: permission}).
|
||||
JsonResponseBody(&resp).
|
||||
StatusCodeToError().
|
||||
DoWithoutResponse(ctx)
|
||||
if err != nil {
|
||||
return false, errors.WithMessagef(err, "client invoke %s", adminAuthorize)
|
||||
return false, errors.WithMessagef(err, "http client invoke %s", adminAuthorize)
|
||||
}
|
||||
return resp.Authorized, nil
|
||||
}
|
||||
@@ -4,7 +4,6 @@ package tests
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"isp-gate-service/admin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -25,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"
|
||||
|
||||
@@ -572,7 +572,7 @@ func (s *HappyPathTestSuite) TestUserAuthorization_SkipAppAuth() { // nolint:fun
|
||||
}
|
||||
|
||||
// nolint:ireturn
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, *client.Client, *admin.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,
|
||||
@@ -617,9 +617,7 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, *
|
||||
adminMock := httptest.NewServer(adminMux)
|
||||
adminUrl, err := url.Parse(adminMock.URL)
|
||||
require.New(s.T()).NoError(err)
|
||||
adminCli, err := admin.NewClient(conf.HttpProtocol)
|
||||
require.New(s.T()).NoError(err)
|
||||
adminCli.Upgrade([]string{adminUrl.Host})
|
||||
adminCli := httpclix.NewClientBalancer([]string{adminUrl.Host})
|
||||
|
||||
return config, systemCli, adminCli
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"isp-gate-service/admin"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -21,6 +20,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
@@ -97,7 +97,7 @@ func main() {
|
||||
srv.ListenAndServe()
|
||||
}
|
||||
|
||||
func NewAdminMock(logger log.Logger) *admin.Client {
|
||||
func NewAdminMock(logger log.Logger) *httpclix.ClientBalancer {
|
||||
mux := router.New()
|
||||
wrapper := endpoint.DefaultWrapper(logger, httplog.Noop())
|
||||
mux.POST("/admin/secure/authenticate",
|
||||
@@ -110,10 +110,7 @@ func NewAdminMock(logger log.Logger) *admin.Client {
|
||||
}))
|
||||
mock := httptest.NewServer(mux)
|
||||
u, _ := url.Parse(mock.URL)
|
||||
|
||||
adminCli, _ := admin.NewClient("http")
|
||||
adminCli.Upgrade([]string{u.Host})
|
||||
return adminCli
|
||||
return httpclix.NewClientBalancer([]string{u.Host})
|
||||
}
|
||||
|
||||
func TestServer(service isp.BackendServiceServer) (*grpc.Server, *client.Client) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"isp-gate-service/admin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -13,11 +12,11 @@ import (
|
||||
"isp-gate-service/entity"
|
||||
"isp-gate-service/routes"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/http/httpcli"
|
||||
"github.com/txix-open/isp-kit/http/httpclix"
|
||||
"github.com/txix-open/isp-kit/lb"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
@@ -81,7 +80,7 @@ func (s *SkipBodyLoggingTestSuite) Test_KnownEndpoint_BodySkip_StrippedPrefix()
|
||||
}
|
||||
|
||||
// nolint:ireturn
|
||||
func (s *SkipBodyLoggingTestSuite) authDependencies(test *test.Test) (conf.Remote, *client.Client, *admin.Client) {
|
||||
func (s *SkipBodyLoggingTestSuite) authDependencies(test *test.Test) (conf.Remote, *client.Client, *httpclix.ClientBalancer) {
|
||||
config := conf.Remote{
|
||||
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
|
||||
Logging: conf.Logging{
|
||||
@@ -117,9 +116,7 @@ func (s *SkipBodyLoggingTestSuite) authDependencies(test *test.Test) (conf.Remot
|
||||
}).Mock(http.MethodPost, "/admin/secure/authorize", func(req entity.AdminAuthorizeRequest) entity.AdminAuthorizeResponse {
|
||||
return entity.AdminAuthorizeResponse{Authorized: true}
|
||||
})
|
||||
adminCli, err := admin.NewClient(conf.HttpProtocol)
|
||||
require.New(s.T()).NoError(err)
|
||||
adminCli.Upgrade([]string{adminService.BaseURL()})
|
||||
adminCli := httpclix.NewClientBalancer([]string{adminService.BaseURL()})
|
||||
|
||||
return config, systemCli, adminCli
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user