MSP-1777 [v] after review#2
This commit is contained in:
1 parent
dfbc05cd7d
commit
1fa746c3e9
5 files changed
+23
-71
No files matched your search
+4
-22
@@ -2,7 +2,6 @@ package assembly
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
mux2 "github.com/gorilla/mux"
|
||||
@@ -65,11 +64,6 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
|
||||
throttlingRepo := repository.NewThrottling(redisCli)
|
||||
throttlingService := service.NewThrottling(throttlingRepo, config.Throttling)
|
||||
|
||||
skipRegexps, err := makeSkipRegExps(config.Logging.Skip)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "compile regexp")
|
||||
}
|
||||
|
||||
mux := mux2.NewRouter()
|
||||
for _, location := range locations {
|
||||
var proxyFunc middleware.Handler
|
||||
@@ -93,7 +87,8 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
|
||||
handler := middleware.Chain(
|
||||
proxyFunc,
|
||||
middleware.RequestId(),
|
||||
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, skipRegexps),
|
||||
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog,
|
||||
config.Logging.SkipBodyLoggingEndpointPrefixes),
|
||||
middleware.ErrorHandler(l.logger),
|
||||
middleware.Authenticate(authentication),
|
||||
middleware.AdminAuthenticate(adminService),
|
||||
@@ -106,7 +101,8 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
|
||||
handler = middleware.Chain(
|
||||
proxyFunc,
|
||||
middleware.RequestId(),
|
||||
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, skipRegexps),
|
||||
middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog,
|
||||
config.Logging.SkipBodyLoggingEndpointPrefixes),
|
||||
middleware.ErrorHandler(l.logger),
|
||||
)
|
||||
}
|
||||
@@ -122,17 +118,3 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli
|
||||
|
||||
return mux, nil
|
||||
}
|
||||
|
||||
func makeSkipRegExps(skip []string) ([]*regexp.Regexp, error) {
|
||||
ret := make([]*regexp.Regexp, 0)
|
||||
|
||||
for _, s := range skip {
|
||||
re, err := regexp.Compile(s)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "re=[%s]", s)
|
||||
}
|
||||
ret = append(ret, re)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
"logLevel": "debug",
|
||||
"requestLogEnable": true,
|
||||
"bodyLogEnable": false,
|
||||
"skip": []
|
||||
"skipBodyLoggingEndpointPrefixes": []
|
||||
},
|
||||
"caching": {
|
||||
"authenticationDataInSec": 60,
|
||||
|
||||
+4
-4
@@ -31,10 +31,10 @@ type Http struct {
|
||||
}
|
||||
|
||||
type Logging struct {
|
||||
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования,логирование запросов осуществляется на уровне debug"`
|
||||
RequestLogEnable bool `schema:"Включить логирование запросов"`
|
||||
BodyLogEnable bool `schema:"Включить логирование тел запросов и ответов,должно быть включено логирование запросов"`
|
||||
Skip []string `schema:"регулярные выражения для отключения логирования"`
|
||||
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования,логирование запросов осуществляется на уровне debug"`
|
||||
RequestLogEnable bool `schema:"Включить логирование запросов"`
|
||||
BodyLogEnable bool `schema:"Включить логирование тел запросов и ответов,должно быть включено логирование запросов"`
|
||||
SkipBodyLoggingEndpointPrefixes []string `schema:"регулярные выражения для отключения логирования"`
|
||||
}
|
||||
|
||||
type Caching struct {
|
||||
|
||||
+12
-7
@@ -6,7 +6,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/integration-system/isp-kit/http/endpoint/buffer"
|
||||
"github.com/integration-system/isp-kit/log"
|
||||
@@ -43,7 +43,7 @@ func (w *writerWrapper) WriteHeader(statusCode int) {
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool, skip []*regexp.Regexp) Middleware {
|
||||
func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool, skip []string) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
if !enableRequestLogging {
|
||||
@@ -52,11 +52,6 @@ func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool
|
||||
|
||||
r := ctx.Request()
|
||||
|
||||
for _, re := range skip {
|
||||
if re.MatchString(r.URL.Path) {
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
}
|
||||
var scSrc scSource
|
||||
var buf *buffer.Buffer
|
||||
if enableBodyLogging {
|
||||
@@ -96,6 +91,16 @@ func Logger(logger log.Logger, enableRequestLogging bool, enableBodyLogging bool
|
||||
log.Int("application_id", authData.ApplicationId),
|
||||
log.Int("admin_id", ctx.AdminId()),
|
||||
}
|
||||
|
||||
if enableBodyLogging {
|
||||
for _, sskip := range skip {
|
||||
if strings.HasPrefix(ctx.Endpoint(), sskip) {
|
||||
enableBodyLogging = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if enableBodyLogging {
|
||||
fields = append(fields,
|
||||
log.ByteString("request", buf.RequestBody()),
|
||||
|
||||
@@ -2,14 +2,11 @@ package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
etpcli "github.com/integration-system/isp-etp-go/v2/client"
|
||||
@@ -144,14 +141,6 @@ func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
func (s *HappyPathTestSuite) TestWsProxy() { // nolint: funlen
|
||||
test, require := test.New(s.T())
|
||||
config, redisCli, systemCli, adminCli := s.commonDependencies(test)
|
||||
fName := "/tmp/TestWsProxy" + strconv.FormatInt(time.Now().Unix(), 10) + ".log"
|
||||
logger, _ := log.New(log.WithLevel(log.DebugLevel), log.WithFileRotation(log.Rotation{
|
||||
File: fName,
|
||||
MaxSizeMb: 1,
|
||||
MaxDays: 1,
|
||||
MaxBackups: 1,
|
||||
Compress: false,
|
||||
}))
|
||||
|
||||
requestId := requestid.Next()
|
||||
wsServer := etp.NewServer(context.Background(), etp.ServerConfig{
|
||||
@@ -177,7 +166,7 @@ func (s *HappyPathTestSuite) TestWsProxy() { // nolint: funlen
|
||||
require.NoError(err)
|
||||
rr := lb.NewRoundRobin([]string{targetUrl.Host})
|
||||
targetClients := map[string]*lb.RoundRobin{"target": rr}
|
||||
locator := assembly.NewLocator(logger, nil, targetClients, routes.NewRoutes(), systemCli, adminCli)
|
||||
locator := assembly.NewLocator(test.Logger(), nil, targetClients, routes.NewRoutes(), systemCli, adminCli)
|
||||
locations := []conf.Location{{
|
||||
SkipAuth: false,
|
||||
PathPrefix: "/ws",
|
||||
@@ -208,30 +197,6 @@ func (s *HappyPathTestSuite) TestWsProxy() { // nolint: funlen
|
||||
require.EqualValues("world", string(resp))
|
||||
err = cli.Close()
|
||||
require.NoError(err)
|
||||
|
||||
requestUrl, err = url.Parse(srv.URL)
|
||||
require.NoError(err)
|
||||
requestUrl.Path = "ws/sevrice"
|
||||
requestUrl.RawQuery = url.Values{
|
||||
"x-application-token": []string{"token"},
|
||||
"x-auth-admin": []string{"mock-token"},
|
||||
}.Encode()
|
||||
err = cli.Dial(context.Background(), requestUrl.String())
|
||||
require.NoError(err)
|
||||
resp, err = cli.EmitWithAck(context.Background(), "hello", []byte("data"))
|
||||
require.NoError(err)
|
||||
require.EqualValues("world", string(resp))
|
||||
err = cli.Close()
|
||||
require.NoError(err)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
f, err := os.Open(fName)
|
||||
require.NoError(err)
|
||||
logBody, err := io.ReadAll(f)
|
||||
require.NoError(err)
|
||||
require.Contains(string(logBody), "ws/sevrice")
|
||||
require.NotContains(string(logBody), "ws/service")
|
||||
}
|
||||
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, redis.UniversalClient, *client.Client, *client.Client) {
|
||||
@@ -256,7 +221,7 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, r
|
||||
Redis: &conf.Redis{Address: redisCli.address},
|
||||
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
|
||||
Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true,
|
||||
Skip: []string{"endpoint", "ws.service"}},
|
||||
SkipBodyLoggingEndpointPrefixes: []string{"endpoint"}},
|
||||
Caching: conf.Caching{AuthorizationDataInSec: 1, AuthenticationDataInSec: 1},
|
||||
DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100}},
|
||||
Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 100}},
|
||||
|
||||
Reference in new issue
Block a user