fix proxy path & add skip auth
This commit is contained in:
1 parent
38806190e7
commit
244d8fbdc2
13 files changed
+137
-95
No files matched your search
@@ -1,3 +1,6 @@
|
||||
## 2.1.0
|
||||
* fix proxy path
|
||||
* add skip authenticate
|
||||
## 2.0.0
|
||||
* add integration with isp-config-service 2.0
|
||||
## 1.3.0
|
||||
|
||||
Generated
+5
-5
@@ -18,12 +18,12 @@
|
||||
version = "v0.1.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:c1100fc71e23b6a32b2c68a5202a848fd13811d5a10b12edb8019c3667d1cd9a"
|
||||
digest = "1:44f56f927970ceccb0a28eccbea0cde48e56f04619ddbb70198036407f14cd0e"
|
||||
name = "github.com/cenkalti/backoff"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "4b4cebaf850ec58f1bb1fec5bdebdf8501c2bc3f"
|
||||
version = "v3.0.0"
|
||||
revision = "1ff1c09ea627d2be64cea605afa999c582c7cd01"
|
||||
version = "v3.1.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec"
|
||||
@@ -490,7 +490,7 @@
|
||||
"ssh/terminal",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "4f8c1d86b1ba699e7a66cd649947ed270a74e0bb"
|
||||
revision = "ac88ee75c92c889b97e05591e9a39b6480c538b3"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
@@ -506,7 +506,7 @@
|
||||
"trace",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "fc4aabc6c91483155dade6fbb627cc953a723260"
|
||||
revision = "ffdde105785063a81acd95bdf89ea53f6e0aac2d"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
|
||||
@@ -38,14 +38,7 @@ func ReceiveConfiguration(conf conf.Cache) {
|
||||
}
|
||||
}
|
||||
|
||||
func Do(ctx *fasthttp.RequestCtx) (int32, error) {
|
||||
path := ctx.Path()
|
||||
pathStr := getPathWithoutPrefix(path)
|
||||
|
||||
if _, ok := routing.AllMethods[pathStr]; !ok {
|
||||
return 0, createError("not implemented", codes.Unimplemented)
|
||||
}
|
||||
|
||||
func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) {
|
||||
for _, notExpectedHeader := range notExpectedHeaders {
|
||||
ctx.Request.Header.Del(notExpectedHeader)
|
||||
}
|
||||
@@ -86,7 +79,7 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) {
|
||||
verifiableKeys[utils.UserTokenHeader] = string(ctx.Request.Header.Peek(utils.UserTokenHeader))
|
||||
|
||||
permittedToCall := false
|
||||
verifiableKeys, permittedToCall, err = auth.verify.Identity(verifiableKeys, pathStr)
|
||||
verifiableKeys, permittedToCall, err = auth.verify.Identity(verifiableKeys, path)
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return 0, createError("internal Server error", codes.Internal)
|
||||
@@ -101,7 +94,7 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := routing.InnerMethods[pathStr]; ok {
|
||||
if _, ok := routing.InnerMethods[path]; ok {
|
||||
adminToken := ctx.Request.Header.Peek("x-auth-admin") //todo const key
|
||||
if verifyToken.Admin(string(adminToken)) != nil {
|
||||
return 0, createError("forbidden", codes.PermissionDenied)
|
||||
@@ -114,17 +107,3 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) {
|
||||
|
||||
return appId, nil
|
||||
}
|
||||
|
||||
func getPathWithoutPrefix(path []byte) string {
|
||||
firstFound := false
|
||||
for i, value := range path {
|
||||
if value == '/' {
|
||||
if firstFound {
|
||||
return string(path[i+1:])
|
||||
} else {
|
||||
firstFound = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -13,8 +13,26 @@ type (
|
||||
}
|
||||
|
||||
Location struct {
|
||||
SkipAuth bool
|
||||
PathPrefix string `valid:"required~Required"`
|
||||
Protocol string `valid:"required~Required"`
|
||||
TargetModule string `valid:"required~Required"`
|
||||
}
|
||||
)
|
||||
|
||||
func GetRequiredModules(locations []Location) map[string][]Location {
|
||||
requiredModules := make(map[string][]Location)
|
||||
|
||||
for _, location := range locations {
|
||||
if location.TargetModule == "" {
|
||||
continue
|
||||
}
|
||||
if loc, ok := requiredModules[location.TargetModule]; ok {
|
||||
requiredModules[location.TargetModule] = append(loc, location)
|
||||
} else {
|
||||
requiredModules[location.TargetModule] = []Location{location}
|
||||
}
|
||||
}
|
||||
|
||||
return requiredModules
|
||||
}
|
||||
+31
-22
@@ -13,6 +13,7 @@ import (
|
||||
"isp-gate-service/journal"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy"
|
||||
"isp-gate-service/routing"
|
||||
"isp-gate-service/service"
|
||||
"isp-gate-service/service/matcher"
|
||||
"isp-gate-service/utils"
|
||||
@@ -55,36 +56,44 @@ func CompleteRequest(ctx *fasthttp.RequestCtx) {
|
||||
}
|
||||
|
||||
func (handlerHelper) AuthenticateAccountingProxy(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
|
||||
path := string(ctx.Path())
|
||||
|
||||
p := proxy.Find(path)
|
||||
initialPath := string(ctx.Path())
|
||||
p, path := proxy.Find(initialPath)
|
||||
if p == nil {
|
||||
err := errors.Errorf("unknown path %s", path)
|
||||
err := errors.Errorf("unknown path %s", initialPath)
|
||||
utils.WriteError(ctx, "not found", codes.NotFound, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
applicationId, err := authenticate.Do(ctx)
|
||||
if err != nil {
|
||||
message := "unknown error"
|
||||
status := codes.Unknown
|
||||
details := make([]interface{}, 0)
|
||||
switch e := err.(type) {
|
||||
case authenticate.ErrorDescription:
|
||||
message = e.Message()
|
||||
status = e.ConvertToGrpcStatus()
|
||||
details = e.Details()
|
||||
}
|
||||
utils.WriteError(ctx, message, status, details)
|
||||
return domain.Create().SetError(err)
|
||||
if _, ok := routing.AllMethods[path]; !ok {
|
||||
msg := "not implemented"
|
||||
utils.WriteError(ctx, msg, codes.Unimplemented, nil)
|
||||
return domain.Create().SetError(errors.New(msg))
|
||||
}
|
||||
|
||||
if !accounting.Accept(applicationId, path) {
|
||||
err := errors.New("accounting error")
|
||||
utils.WriteError(ctx, "too many requests", codes.ResourceExhausted, nil)
|
||||
return domain.Create().SetError(err)
|
||||
if !p.SkipAuth() {
|
||||
applicationId, err := authenticate.Do(ctx, path)
|
||||
if err != nil {
|
||||
message := "unknown error"
|
||||
status := codes.Unknown
|
||||
details := make([]interface{}, 0)
|
||||
switch e := err.(type) {
|
||||
case authenticate.ErrorDescription:
|
||||
message = e.Message()
|
||||
status = e.ConvertToGrpcStatus()
|
||||
details = e.Details()
|
||||
}
|
||||
utils.WriteError(ctx, message, status, details)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
if !accounting.Accept(applicationId, path) {
|
||||
err := errors.New("accounting error")
|
||||
utils.WriteError(ctx, "too many requests", codes.ResourceExhausted, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
}
|
||||
return p.ProxyRequest(ctx)
|
||||
|
||||
return p.ProxyRequest(ctx, path)
|
||||
}
|
||||
|
||||
func (handlerHelper) SetMetricStatus(statusCode int) string {
|
||||
|
||||
@@ -40,12 +40,18 @@ func main() {
|
||||
RequireRoutes(handleRouteUpdate).
|
||||
RequireModule(journal.RequiredModule())
|
||||
|
||||
for _, location := range cfg.Locations {
|
||||
if p, err := proxy.Init(location); err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidLocalConfig, err)
|
||||
} else if location.TargetModule != "" {
|
||||
bs.RequireModule(location.TargetModule, p.Consumer, false)
|
||||
}
|
||||
requiredModules := conf.GetRequiredModules(cfg.Locations)
|
||||
for targetModule, locations := range requiredModules {
|
||||
bs.RequireModule(targetModule, func(list []structure.AddressConfiguration) bool {
|
||||
for _, location := range locations {
|
||||
if p, err := proxy.Init(location); err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidLocalConfig, err)
|
||||
} else if !p.Consumer(list) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}, false)
|
||||
}
|
||||
|
||||
bs.OnShutdown(onShutdown).
|
||||
|
||||
+22
-7
@@ -22,8 +22,9 @@ const (
|
||||
|
||||
type (
|
||||
Proxy interface {
|
||||
ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse
|
||||
Consumer([]structure.AddressConfiguration) bool
|
||||
SkipAuth() bool
|
||||
Close()
|
||||
}
|
||||
)
|
||||
@@ -34,15 +35,15 @@ func Init(location conf.Location) (Proxy, error) {
|
||||
}
|
||||
switch location.Protocol {
|
||||
case httpProtocol:
|
||||
proxy := http.NewProxy()
|
||||
proxy := http.NewProxy(location.SkipAuth)
|
||||
store[location.PathPrefix] = proxy
|
||||
return proxy, nil
|
||||
case grpcProtocol:
|
||||
proxy := grpc.NewProxy()
|
||||
proxy := grpc.NewProxy(location.SkipAuth)
|
||||
store[location.PathPrefix] = proxy
|
||||
return proxy, nil
|
||||
case healthCheckProtocol:
|
||||
proxy := health_check.NewProxy()
|
||||
proxy := health_check.NewProxy(location.SkipAuth)
|
||||
store[location.PathPrefix] = proxy
|
||||
return proxy, nil
|
||||
default:
|
||||
@@ -50,13 +51,13 @@ func Init(location conf.Location) (Proxy, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func Find(path string) Proxy {
|
||||
func Find(path string) (Proxy, string) {
|
||||
for pathPrefix, proxy := range store {
|
||||
if strings.HasPrefix(path, pathPrefix) {
|
||||
return proxy
|
||||
return proxy, getPathWithoutPrefix(path)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func Close() {
|
||||
@@ -64,3 +65,17 @@ func Close() {
|
||||
p.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func getPathWithoutPrefix(path string) string {
|
||||
firstFound := false
|
||||
for i, value := range path {
|
||||
if value == '/' {
|
||||
if firstFound {
|
||||
return path[i+1:]
|
||||
} else {
|
||||
firstFound = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
+21
-15
@@ -16,23 +16,26 @@ import (
|
||||
)
|
||||
|
||||
type grpcProxy struct {
|
||||
client *backend.RxGrpcClient
|
||||
client *backend.RxGrpcClient
|
||||
skipAuth bool
|
||||
}
|
||||
|
||||
func NewProxy() *grpcProxy {
|
||||
return &grpcProxy{client: backend.NewRxGrpcClient(
|
||||
backend.WithDialOptions(
|
||||
grpc.WithInsecure(), grpc.WithBlock(),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(int(conf.DefaultMaxResponseBodySize))),
|
||||
),
|
||||
backend.WithDialingErrorHandler(func(err error) {
|
||||
log.Errorf(log_code.ErrorClientGrpc, "dialing err: %v", err)
|
||||
}),
|
||||
)}
|
||||
func NewProxy(skiAuth bool) *grpcProxy {
|
||||
return &grpcProxy{
|
||||
client: backend.NewRxGrpcClient(
|
||||
backend.WithDialOptions(
|
||||
grpc.WithInsecure(), grpc.WithBlock(),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(int(conf.DefaultMaxResponseBodySize))),
|
||||
),
|
||||
backend.WithDialingErrorHandler(func(err error) {
|
||||
log.Errorf(log_code.ErrorClientGrpc, "dialing err: %v", err)
|
||||
})),
|
||||
skipAuth: skiAuth,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
|
||||
func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
|
||||
if p.client.InternalGrpcClient == nil {
|
||||
msg := "client undefined"
|
||||
log.Error(log_code.ErrorClientGrpc, msg)
|
||||
@@ -43,14 +46,17 @@ func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
SetError(errors.New(msg))
|
||||
}
|
||||
|
||||
uri := string(ctx.RequestURI())
|
||||
return handlers.Handler.Get(ctx).Complete(ctx, uri, p.client)
|
||||
return handlers.Handler.Get(ctx).Complete(ctx, path, p.client)
|
||||
}
|
||||
|
||||
func (p *grpcProxy) Consumer(addr []structure.AddressConfiguration) bool {
|
||||
return p.client.ReceiveAddressList(addr)
|
||||
}
|
||||
|
||||
func (p *grpcProxy) SkipAuth() bool {
|
||||
return p.skipAuth
|
||||
}
|
||||
|
||||
func (p *grpcProxy) Close() {
|
||||
p.client.Close()
|
||||
}
|
||||
@@ -128,7 +128,7 @@ func openStream(headers *fasthttp.RequestHeader, method string, timeout time.Dur
|
||||
}
|
||||
|
||||
func makeMetadata(r *fasthttp.RequestHeader, method string) (metadata.MD, string) {
|
||||
method = strings.TrimPrefix(method, "/api/")
|
||||
//method = strings.TrimPrefix(method, "/api/")
|
||||
md := metadata.Pairs(utils.ProxyMethodNameHeader, method)
|
||||
r.VisitAll(func(key, v []byte) {
|
||||
lowerHeader := strings.ToLower(string(key))
|
||||
|
||||
@@ -19,13 +19,7 @@ var handleJson handleJsonDesc
|
||||
type handleJsonDesc struct{}
|
||||
|
||||
func (p handleJsonDesc) Complete(c *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) domain.ProxyResponse {
|
||||
//body, err := utils.ReadJsonBody(c)
|
||||
body := c.Request.Body()
|
||||
/*if err != nil {
|
||||
streaming.LogError(log_code.TypeData.JsonContent, method, err)
|
||||
streaming.SendError(err.Error(), codes.InvalidArgument, []interface{}{err.Error()}, c)
|
||||
return
|
||||
}*/
|
||||
|
||||
md, methodName := makeMetadata(&c.Request.Header, method)
|
||||
ctx := metadata.NewOutgoingContext(context.Background(), md)
|
||||
@@ -40,7 +34,6 @@ func (p handleJsonDesc) Complete(c *fasthttp.RequestCtx, method string, client *
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
//structBody := u.ConvertInterfaceToGrpcStruct(body)
|
||||
resp, invokerErr := cli.Request(
|
||||
ctx,
|
||||
&isp.Message{
|
||||
|
||||
@@ -7,20 +7,27 @@ import (
|
||||
)
|
||||
|
||||
type healthCheckProxy struct {
|
||||
skipAuth bool
|
||||
}
|
||||
|
||||
func NewProxy() *healthCheckProxy {
|
||||
return &healthCheckProxy{}
|
||||
func NewProxy(skipAuth bool) *healthCheckProxy {
|
||||
return &healthCheckProxy{skipAuth: skipAuth}
|
||||
}
|
||||
|
||||
func (p *healthCheckProxy) Consumer(addressList []structure.AddressConfiguration) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *healthCheckProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
|
||||
func (p *healthCheckProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
|
||||
ctx.Response.SetBody(ctx.Request.Body())
|
||||
ctx.Request.SetRequestURI(path)
|
||||
return domain.Create()
|
||||
}
|
||||
|
||||
func (p *healthCheckProxy) SkipAuth() bool {
|
||||
return p.skipAuth
|
||||
}
|
||||
|
||||
func (p *healthCheckProxy) Close() {
|
||||
|
||||
}
|
||||
+10
-4
@@ -14,11 +14,12 @@ import (
|
||||
)
|
||||
|
||||
type httpProxy struct {
|
||||
client *fasthttp.HostClient
|
||||
client *fasthttp.HostClient
|
||||
skipAuth bool
|
||||
}
|
||||
|
||||
func NewProxy() *httpProxy {
|
||||
return &httpProxy{client: nil}
|
||||
func NewProxy(skipAuth bool) *httpProxy {
|
||||
return &httpProxy{client: nil, skipAuth: skipAuth}
|
||||
}
|
||||
|
||||
func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool {
|
||||
@@ -33,7 +34,7 @@ func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse {
|
||||
func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
|
||||
if p.client == nil {
|
||||
msg := "client undefined"
|
||||
log.Error(log_code.ErrorClientHttp, msg)
|
||||
@@ -46,6 +47,7 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
|
||||
req := &ctx.Request
|
||||
res := &ctx.Response
|
||||
req.SetRequestURI(path)
|
||||
|
||||
if addr, _, err := net.SplitHostPort(ctx.RemoteAddr().String()); err == nil {
|
||||
req.Header.Add("X-Forwarded-For", addr)
|
||||
@@ -61,6 +63,10 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
SetError(err)
|
||||
}
|
||||
|
||||
func (p *httpProxy) SkipAuth() bool {
|
||||
return p.skipAuth
|
||||
}
|
||||
|
||||
func (p *httpProxy) Close() {
|
||||
p.client = nil
|
||||
}
|
||||
Reference in new issue
Block a user