From 6ee42d0b37751737d235b908e72e4dc033bb5d51 Mon Sep 17 00:00:00 2001 From: Pavol Loffay Date: Wed, 14 Jul 2021 16:01:59 +0200 Subject: [PATCH] Fix base path in grpc gateway for api_v3 (#3139) --- cmd/all-in-one/all_in_one_test.go | 19 +++++++++++++++++++ cmd/query/app/apiv3/grpc_gateway.go | 6 +++++- cmd/query/app/apiv3/grpc_gateway_test.go | 13 +++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cmd/all-in-one/all_in_one_test.go b/cmd/all-in-one/all_in_one_test.go index c3d823fa135..dd859a71d87 100644 --- a/cmd/all-in-one/all_in_one_test.go +++ b/cmd/all-in-one/all_in_one_test.go @@ -25,10 +25,12 @@ import ( "testing" "time" + "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ui "github.com/jaegertracing/jaeger/model/json" + "github.com/jaegertracing/jaeger/proto-gen/api_v3" "github.com/jaegertracing/jaeger/thrift-gen/sampling" ) @@ -44,6 +46,8 @@ const ( getServicesURL = queryURL + "/api/services" getTraceURL = queryURL + "/api/traces?service=jaeger-query&tag=jaeger-debug-id:debug" getSamplingStrategyURL = agentURL + "/sampling?service=whatever" + + getServicesAPIV3URL = queryURL + "/v3/services" ) var ( @@ -60,6 +64,7 @@ func TestAllInOne(t *testing.T) { createTrace(t) getAPITrace(t) getSamplingStrategy(t) + getServicesAPIV3(t) } func createTrace(t *testing.T) { @@ -131,3 +136,17 @@ func healthCheck() error { } return fmt.Errorf("query service is not ready") } + +func getServicesAPIV3(t *testing.T) { + req, err := http.NewRequest("GET", getServicesAPIV3URL, nil) + require.NoError(t, err) + resp, err := httpClient.Do(req) + require.NoError(t, err) + body, _ := ioutil.ReadAll(resp.Body) + + var servicesResponse api_v3.GetServicesResponse + jsonpb := runtime.JSONPb{} + err = jsonpb.Unmarshal(body, &servicesResponse) + require.NoError(t, err) + assert.Equal(t, []string{"jaeger-query"}, servicesResponse.GetServices()) +} diff --git a/cmd/query/app/apiv3/grpc_gateway.go b/cmd/query/app/apiv3/grpc_gateway.go index 7b9f7c4a568..77629d2ec9f 100644 --- a/cmd/query/app/apiv3/grpc_gateway.go +++ b/cmd/query/app/apiv3/grpc_gateway.go @@ -34,7 +34,11 @@ func RegisterGRPCGateway(ctx context.Context, logger *zap.Logger, r *mux.Router, grpcGatewayMux := runtime.NewServeMux( runtime.WithMarshalerOption(runtime.MIMEWildcard, jsonpb), ) - r.PathPrefix("/v3/").Handler(http.StripPrefix(basePath, grpcGatewayMux)) + var handler http.Handler = grpcGatewayMux + if basePath != "/" { + handler = http.StripPrefix(basePath, grpcGatewayMux) + } + r.PathPrefix("/v3/").Handler(handler) var dialOpts []grpc.DialOption if grpcTLS.Enabled { diff --git a/cmd/query/app/apiv3/grpc_gateway_test.go b/cmd/query/app/apiv3/grpc_gateway_test.go index d03d68d006a..28ef40ecaa5 100644 --- a/cmd/query/app/apiv3/grpc_gateway_test.go +++ b/cmd/query/app/apiv3/grpc_gateway_test.go @@ -44,7 +44,7 @@ import ( var testCertKeyLocation = "../../../../pkg/config/tlscfg/testdata/" -func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Options) { +func testGRPCGateway(t *testing.T, basePath string, serverTLS tlscfg.Options, clientTLS tlscfg.Options) { defer serverTLS.Close() defer clientTLS.Close() @@ -83,9 +83,10 @@ func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Op defer grpcServer.Stop() router := &mux.Router{} + router = router.PathPrefix(basePath).Subrouter() ctx, cancel := context.WithCancel(context.Background()) defer cancel() - err := RegisterGRPCGateway(ctx, zap.NewNop(), router, "", lis.Addr().String(), clientTLS) + err := RegisterGRPCGateway(ctx, zap.NewNop(), router, basePath, lis.Addr().String(), clientTLS) require.NoError(t, err) httpLis, err := net.Listen("tcp", ":0") @@ -98,7 +99,7 @@ func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Op require.Equal(t, http.ErrServerClosed, err) }() defer httpServer.Shutdown(context.Background()) - req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost%s/v3/traces/123", strings.Replace(httpLis.Addr().String(), "[::]", "", 1)), nil) + req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost%s%s/v3/traces/123", strings.Replace(httpLis.Addr().String(), "[::]", "", 1), basePath), nil) req.Header.Set("Content-Type", "application/json") response, err := http.DefaultClient.Do(req) buf := bytes.Buffer{} @@ -117,10 +118,10 @@ func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Op } func TestGRPCGateway(t *testing.T) { - testGRPCGateway(t, tlscfg.Options{}, tlscfg.Options{}) + testGRPCGateway(t, "/", tlscfg.Options{}, tlscfg.Options{}) } -func TestGRPCGateway_TLS(t *testing.T) { +func TestGRPCGateway_TLS_with_base_path(t *testing.T) { serverTLS := tlscfg.Options{ Enabled: true, CAPath: testCertKeyLocation + "/example-CA-cert.pem", @@ -134,7 +135,7 @@ func TestGRPCGateway_TLS(t *testing.T) { KeyPath: testCertKeyLocation + "/example-client-key.pem", ServerName: "example.com", } - testGRPCGateway(t, serverTLS, clientTLS) + testGRPCGateway(t, "/jaeger", serverTLS, clientTLS) } // For more details why this is needed see https://github.com/grpc-ecosystem/grpc-gateway/issues/2189