diff --git a/pkg/console/service/service_generic_invoke.go b/pkg/console/service/service_generic_invoke.go index 8fa5b8b05..59e3c9e9e 100644 --- a/pkg/console/service/service_generic_invoke.go +++ b/pkg/console/service/service_generic_invoke.go @@ -113,8 +113,38 @@ func InvokeServiceGeneric(ctx consolectx.Context, req model.ServiceGenericInvoke if err != nil { return nil, err } - resolvedMethod := findMethod(metadataList, req.MethodName, req.Signature) + selectedMetadata := make([]*meshresource.ServiceProviderMetadataResource, 0, len(metadataList)) + hessianMetadata := make([]*meshresource.ServiceProviderMetadataResource, 0, len(metadataList)) + unboundIDLMetadata := make([]*meshresource.ServiceProviderMetadataResource, 0) + for _, metadata := range metadataList { + if metadata == nil || metadata.Spec == nil { + continue + } + bound := instanceRes.Spec.AppName != "" && metadata.Spec.ProviderAppName != "" && metadata.Spec.ProviderAppName == instanceRes.Spec.AppName + if bound { + selectedMetadata = append(selectedMetadata, metadata) + } + mode := strings.TrimSpace(metadata.Spec.Parameters[dubboconstant.IDLMode]) + switch { + case mode == "" || strings.EqualFold(mode, dubboconstant.NONIDL): + hessianMetadata = append(hessianMetadata, metadata) + case !bound && strings.EqualFold(mode, dubboconstant.IDL): + unboundIDLMetadata = append(unboundIDLMetadata, metadata) + } + } + idl, err := genericInvokeIDLMode(selectedMetadata) + if err != nil { + return nil, err + } + methodMetadata := hessianMetadata + if idl { + methodMetadata = selectedMetadata + } + resolvedMethod := findMethod(methodMetadata, req.MethodName, req.Signature) if resolvedMethod == nil { + if !idl && findMethod(unboundIDLMetadata, req.MethodName, req.Signature) != nil { + return nil, bizerror.New(bizerror.InvalidArgument, "IDL provider metadata is not bound to the selected instance") + } err := bizerror.New( bizerror.NotFoundError, fmt.Sprintf("method %s not found for service %s", req.MethodName, req.ServiceName), @@ -128,16 +158,21 @@ func InvokeServiceGeneric(ctx consolectx.Context, req model.ServiceGenericInvoke if len(parameterTypes) != len(req.Args) { return nil, bizerror.New(bizerror.InvalidArgument, "resolved method parameter count does not match args length") } - - decodedArgs, err := decodeGenericInvokeArgs(parameterTypes, req.Args) - if err != nil { - return nil, bizerror.New(bizerror.InvalidArgument, err.Error()) + var decodedArgs []any + if !idl { + decodedArgs, err = decodeGenericInvokeArgs(parameterTypes, req.Args) + if err != nil { + return nil, bizerror.New(bizerror.InvalidArgument, err.Error()) + } } rpcInstanceRes, err := findRPCInstanceByInstance(ctx, instanceRes) if err != nil { return nil, err } + if idl && (rpcInstanceRes.Spec.GetIp() != instanceRes.Spec.GetIp() || rpcInstanceRes.Spec.GetAppName() != instanceRes.Spec.GetAppName()) { + return nil, bizerror.New(bizerror.InvalidArgument, "rpc instance does not match selected catalog instance") + } targets, err := buildGenericInvokeTargets(rpcInstanceRes) if err != nil { @@ -159,6 +194,15 @@ func InvokeServiceGeneric(ctx consolectx.Context, req model.ServiceGenericInvoke callCtx = context.WithValue(callCtx, dubboconstant.AttachmentKey, attachmentValues) } + if idl { + startedAt := time.Now() + result, err := invokeIDLService(callCtx, req, targets, selectedMetadata, resolvedMethod) + if err != nil { + return nil, err + } + return &model.ServiceGenericInvokeResp{ElapsedMs: time.Since(startedAt).Milliseconds(), RawResult: result}, nil + } + hessianArgs := make([]hessian.Object, len(decodedArgs)) for i, arg := range decodedArgs { hessianArgs[i] = arg diff --git a/pkg/console/service/service_idl_invoke.go b/pkg/console/service/service_idl_invoke.go new file mode 100644 index 000000000..b3590c0e1 --- /dev/null +++ b/pkg/console/service/service_idl_invoke.go @@ -0,0 +1,385 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package service + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net" + "strconv" + "strings" + + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + "github.com/apache/dubbo-admin/pkg/console/model" + meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/metadata" + reflectionv1 "google.golang.org/grpc/reflection/grpc_reflection_v1" + reflectionalpha "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protodesc" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/reflect/protoregistry" + "google.golang.org/protobuf/types/descriptorpb" + "google.golang.org/protobuf/types/dynamicpb" +) + +// Only service-scoped provider parameters establish the mode; instance tags can +// describe other services exported by the same process. +func genericInvokeIDLMode(selected []*meshresource.ServiceProviderMetadataResource) (bool, error) { + hasIDL, hasNonIDL := false, false + for _, provider := range selected { + if provider == nil || provider.Spec == nil { + continue + } + value := strings.TrimSpace(provider.Spec.Parameters[constant.IDLMode]) + if value == "" { + continue + } + switch { + case strings.EqualFold(value, constant.IDL): + hasIDL = true + case strings.EqualFold(value, constant.NONIDL): + hasNonIDL = true + default: + return false, bizerror.New(bizerror.InvalidArgument, "unknown IDL-mode in provider metadata") + } + } + if hasIDL && hasNonIDL { + return false, bizerror.New(bizerror.InvalidArgument, "conflicting IDL-mode for service providers") + } + return hasIDL, nil +} + +// Known SDK markers require credentials this path cannot configure. Unknown marker +// names are not covered. Reject malformed catalog URL parameters rather than dial. +func idlSecurityRequired(values map[string]string) bool { + if raw, ok := values[constants.URLParamsKey]; ok { + var params map[string]string + if err := json.Unmarshal([]byte(raw), ¶ms); err != nil || params == nil { + return true + } + if idlSecurityMarkers(params) { + return true + } + } + return idlSecurityMarkers(values) +} + +func idlSecurityMarkers(values map[string]string) bool { + for key, value := range values { + key = strings.TrimSpace(key) + value = strings.ToLower(strings.TrimSpace(value)) + switch key { + case constant.SslEnabledKey, constant.ServiceAuthKey: + if value != "" && value != "false" && value != "0" && value != "none" { + return true + } + case constant.TokenKey, constant.AuthenticatorKey, constant.TLSConfigKey, constant.TLSKey, constant.TLSCert, constant.CACert, constant.TLSServerNAME, constant.AccessKeyIDKey, constant.SecretAccessKeyKey: + if value != "" { + return true + } + } + } + return false +} + +func idlCallError(ctx context.Context, operation string, err error) error { + if ctx.Err() != nil { + return bizerror.New(bizerror.InternalError, operation+": "+ctx.Err().Error()) + } + switch status.Code(err) { + case codes.DeadlineExceeded: + return bizerror.New(bizerror.InternalError, operation+": "+context.DeadlineExceeded.Error()) + case codes.Canceled: + return bizerror.New(bizerror.InternalError, operation+": "+context.Canceled.Error()) + } + return bizerror.New(bizerror.InternalError, fmt.Sprintf("%s: %v", operation, err)) +} + +func idlFullName(name string) protoreflect.FullName { + return protoreflect.FullName(strings.TrimPrefix(strings.TrimSpace(name), ".")) +} + +func idlInvokeContext(ctx context.Context, req model.ServiceGenericInvokeReq) (context.Context, error) { + var pairs []string + for key, value := range req.Attachments { + key = strings.ToLower(strings.TrimSpace(key)) + if key == "" { + return nil, bizerror.New(bizerror.InvalidArgument, "attachment key must not be empty") + } + if triple_protocol.IsReservedHeader(key) || key == triple_protocol.TripleServiceGroup || key == triple_protocol.TripleServiceVersion { + return nil, bizerror.New(bizerror.InvalidArgument, fmt.Sprintf("reserved attachment key %q", key)) + } + for _, c := range key { + if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '.' && c != '_' && c != '-' { + return nil, bizerror.New(bizerror.InvalidArgument, fmt.Sprintf("invalid attachment key %q", key)) + } + } + pairs = append(pairs, key, value) + } + if req.Group != "" { + pairs = append(pairs, triple_protocol.TripleServiceGroup, req.Group) + } + if req.Version != "" { + pairs = append(pairs, triple_protocol.TripleServiceVersion, req.Version) + } + return metadata.AppendToOutgoingContext(ctx, pairs...), nil +} + +func invokeIDLService(ctx context.Context, req model.ServiceGenericInvokeReq, targets []*genericInvokeTarget, providers []*meshresource.ServiceProviderMetadataResource, catalogMethod *meshproto.Method) (any, error) { + if len(req.Args) != 1 { + return nil, bizerror.New(bizerror.InvalidArgument, "IDL unary methods require exactly one request message") + } + for _, p := range providers { + if p != nil && p.Spec != nil && idlSecurityRequired(p.Spec.Parameters) { + return nil, bizerror.New(bizerror.InvalidArgument, "secure or authenticated IDL instance is unsupported without configured credentials") + } + } + var target *genericInvokeTarget + for _, t := range targets { + if idlSecurityRequired(t.instance.Spec.GetMetadata()) || idlSecurityRequired(t.instance.Spec.GetTags()) { + return nil, bizerror.New(bizerror.InvalidArgument, "secure or authenticated IDL instance is unsupported without configured credentials") + } + if target == nil && t.protocol == constant.TriProtocol && t.port > 0 && t.port <= 65535 { + target = t + } + } + if target == nil { + return nil, bizerror.New(bizerror.InvalidArgument, "IDL invocation requires a Triple endpoint") + } + invokeCtx, err := idlInvokeContext(ctx, req) + if err != nil { + return nil, err + } + addr := net.JoinHostPort(target.instance.Spec.GetIp(), strconv.FormatInt(target.port, 10)) + cc, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, idlCallError(ctx, "reflection connection failed", err) + } + defer func() { _ = cc.Close() }() + // Reflection is process-global and cannot distinguish divergent descriptors + // for versions of the same service. Routing headers apply only to Invoke. + files, err := idlDescriptors(ctx, cc, req.ServiceName) + if err != nil { + return nil, idlCallError(ctx, "IDL reflection failed", err) + } + desc, err := files.FindDescriptorByName(protoreflect.FullName(req.ServiceName)) + if err != nil { + return nil, bizerror.New(bizerror.NotFoundError, "service not found in reflection descriptor") + } + svc, ok := desc.(protoreflect.ServiceDescriptor) + if !ok { + return nil, bizerror.New(bizerror.InvalidArgument, "reflected symbol is not a service") + } + method := svc.Methods().ByName(protoreflect.Name(req.MethodName)) + if method == nil { + return nil, bizerror.New(bizerror.NotFoundError, "method not found in reflection descriptor") + } + if method.IsStreamingClient() || method.IsStreamingServer() { + return nil, bizerror.New(bizerror.InvalidArgument, "streaming is unsupported; IDL invocation is unary only") + } + for _, provider := range providers { + if provider == nil || provider.Spec == nil || strings.TrimSpace(provider.Spec.GetCanonicalName()) == "" { + continue + } + name := idlFullName(provider.Spec.GetCanonicalName()) + if !name.IsValid() || name != svc.FullName() || name != idlFullName(req.ServiceName) { + return nil, bizerror.New(bizerror.InvalidArgument, fmt.Sprintf("catalog provider canonical name %q conflicts with requested service %q and reflected service %q", provider.Spec.GetCanonicalName(), req.ServiceName, svc.FullName())) + } + } + params := catalogMethod.GetParameterTypes() + if len(params) != 1 || !idlFullName(params[0]).IsValid() || !idlFullName(catalogMethod.GetReturnType()).IsValid() || idlFullName(params[0]) != method.Input().FullName() || idlFullName(catalogMethod.GetReturnType()) != method.Output().FullName() { + return nil, bizerror.New(bizerror.InvalidArgument, fmt.Sprintf("catalog signature %q conflicts with reflected signature %s->%s", methodSig(catalogMethod), method.Input().FullName(), method.Output().FullName())) + } + types := dynamicpb.NewTypes(files) + input := dynamicpb.NewMessage(method.Input()) + // Each args element is the protobuf JSON document, not a Hessian class wrapper. + if err = (protojson.UnmarshalOptions{DiscardUnknown: false, Resolver: types}).Unmarshal(req.Args[0], input); err != nil { + return nil, bizerror.New(bizerror.InvalidArgument, "invalid protobuf request JSON: "+err.Error()) + } + output := dynamicpb.NewMessage(method.Output()) + if err = cc.Invoke(invokeCtx, "/"+string(svc.FullName())+"/"+string(method.Name()), input, output); err != nil { + return nil, idlCallError(ctx, "IDL unary invocation failed", err) + } + encoded, err := (protojson.MarshalOptions{EmitUnpopulated: true, Resolver: types}).Marshal(output) + if err != nil { + return nil, idlCallError(ctx, "IDL response encoding failed", err) + } + var result any + if err = json.Unmarshal(encoded, &result); err != nil { + return nil, idlCallError(ctx, "IDL response encoding failed", err) + } + return result, nil +} + +// A fresh stream per query makes each version probe independent. +func idlReflectionQuery(ctx context.Context, cc *grpc.ClientConn, alpha bool, symbol bool, value string) (*reflectionv1.ServerReflectionResponse, error) { + request := &reflectionv1.ServerReflectionRequest{} + if symbol { + request.MessageRequest = &reflectionv1.ServerReflectionRequest_FileContainingSymbol{FileContainingSymbol: value} + } else { + request.MessageRequest = &reflectionv1.ServerReflectionRequest_FileByFilename{FileByFilename: value} + } + var response *reflectionv1.ServerReflectionResponse + if !alpha { + stream, err := reflectionv1.NewServerReflectionClient(cc).ServerReflectionInfo(ctx) + if err != nil { + return nil, err + } + defer func() { _ = stream.CloseSend() }() + if err = stream.Send(request); err != nil && err != io.EOF { + return nil, err + } + response, err = stream.Recv() + if err != nil { + return nil, err + } + } else { + data, err := proto.Marshal(request) + if err != nil { + return nil, err + } + // Fallback uses the v1alpha wire type; the proto is deprecated but still required. + ar := new(reflectionalpha.ServerReflectionRequest) //nolint:staticcheck + if err = proto.Unmarshal(data, ar); err != nil { + return nil, err + } + stream, err := reflectionalpha.NewServerReflectionClient(cc).ServerReflectionInfo(ctx) + if err != nil { + return nil, err + } + defer func() { _ = stream.CloseSend() }() + if err = stream.Send(ar); err != nil && err != io.EOF { + return nil, err + } + reply, err := stream.Recv() + if err != nil { + return nil, err + } + data, err = proto.Marshal(reply) + if err != nil { + return nil, err + } + response = new(reflectionv1.ServerReflectionResponse) + if err = proto.Unmarshal(data, response); err != nil { + return nil, err + } + } + if e := response.GetErrorResponse(); e != nil { + return nil, status.Error(codes.Code(e.GetErrorCode()), e.GetErrorMessage()) + } + return response, nil +} + +func idlDescriptors(ctx context.Context, cc *grpc.ClientConn, service string) (*protoregistry.Files, error) { + const maxFiles = 256 + const maxBytes = 8 << 20 + const maxRounds = 256 + response, err := idlReflectionQuery(ctx, cc, false, true, service) + alpha := false + if status.Code(err) == codes.Unimplemented { + alpha = true + response, err = idlReflectionQuery(ctx, cc, true, true, service) + } + if err != nil { + if status.Code(err) == codes.Unimplemented { + return nil, fmt.Errorf("reflection unsupported: %w", err) + } + return nil, err + } + files := make(map[string]*descriptorpb.FileDescriptorProto) + fileCount, byteCount := 0, 0 + add := func(response *reflectionv1.ServerReflectionResponse) error { + data := response.GetFileDescriptorResponse() + if data == nil || len(data.FileDescriptorProto) == 0 { + return fmt.Errorf("reflection returned no descriptors") + } + for _, raw := range data.FileDescriptorProto { + fileCount++ + byteCount += len(raw) + if fileCount > maxFiles || byteCount > maxBytes { + return fmt.Errorf("reflection descriptor limit exceeded") + } + file := new(descriptorpb.FileDescriptorProto) + if err := proto.Unmarshal(raw, file); err != nil { + return fmt.Errorf("invalid descriptor: %w", err) + } + if file.GetName() == "" { + return fmt.Errorf("descriptor has no filename") + } + if old, ok := files[file.GetName()]; ok && !proto.Equal(old, file) { + return fmt.Errorf("conflicting descriptor %s", file.GetName()) + } + files[file.GetName()] = file + } + return nil + } + if err = add(response); err != nil { + return nil, err + } + for rounds := 1; ; rounds++ { + missing := "" + for _, file := range files { + for _, dep := range file.Dependency { + if _, ok := files[dep]; !ok { + missing = dep + break + } + } + if missing != "" { + break + } + } + if missing == "" { + break + } + if rounds >= maxRounds { + return nil, fmt.Errorf("reflection fetch round limit exceeded") + } + response, err = idlReflectionQuery(ctx, cc, alpha, false, missing) + if err != nil { + return nil, fmt.Errorf("missing dependency %s: %w", missing, err) + } + if err = add(response); err != nil { + return nil, fmt.Errorf("dependency %s: %w", missing, err) + } + if _, ok := files[missing]; !ok { + return nil, fmt.Errorf("reflection did not return dependency %s", missing) + } + } + set := &descriptorpb.FileDescriptorSet{} + for _, file := range files { + set.File = append(set.File, file) + } + registry, err := protodesc.NewFiles(set) + if err != nil { + return nil, fmt.Errorf("incomplete or invalid descriptors: %w", err) + } + return registry, nil +} diff --git a/pkg/console/service/service_idl_invoke_test.go b/pkg/console/service/service_idl_invoke_test.go new file mode 100644 index 000000000..0e81f43cf --- /dev/null +++ b/pkg/console/service/service_idl_invoke_test.go @@ -0,0 +1,895 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package service + +import ( + "context" + "encoding/json" + "errors" + "net" + "os" + "path/filepath" + "sync" + "sync/atomic" + "testing" + "time" + + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + logcfg "github.com/apache/dubbo-admin/pkg/config/log" + "github.com/apache/dubbo-admin/pkg/console/model" + "github.com/apache/dubbo-admin/pkg/core/logger" + "github.com/apache/dubbo-admin/pkg/core/manager" + meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1" + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" + "github.com/apache/dubbo-admin/pkg/core/store" + memoryst "github.com/apache/dubbo-admin/pkg/store/memory" + "github.com/stretchr/testify/require" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/health" + healthpb "google.golang.org/grpc/health/grpc_health_v1" + "google.golang.org/grpc/metadata" + "google.golang.org/grpc/reflection" + reflectionv1 "google.golang.org/grpc/reflection/grpc_reflection_v1" + reflectionalpha "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protodesc" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/reflect/protoregistry" + "google.golang.org/protobuf/types/descriptorpb" + "google.golang.org/protobuf/types/dynamicpb" + "google.golang.org/protobuf/types/known/anypb" +) + +func TestMain(m *testing.M) { + dir, err := os.MkdirTemp("", "dubbo-admin-service-test-logs-") + if err != nil { + panic(err) + } + // Init replaces the gRPC logger; run it once before any package test starts a server. + cfg := logcfg.DefaultLogConfig() + cfg.OutputPath = filepath.Join(dir, "dubbo-admin.log") + logger.Init(cfg) + code := m.Run() + _ = os.RemoveAll(dir) + os.Exit(code) +} + +func idlCatalog(t *testing.T, port int64, provider *meshresource.ServiceProviderMetadataResource) *testContext { + t.Helper() + stores := map[coremodel.ResourceKind]store.ResourceStore{} + for _, kind := range []coremodel.ResourceKind{meshresource.InstanceKind, meshresource.RPCInstanceKind, meshresource.ServiceProviderMetadataKind} { + s := memoryst.NewMemoryResourceStore(kind) + require.NoError(t, s.Init(nil)) + stores[kind] = s + } + instance := meshresource.NewInstanceResourceWithAttributes("instance", "mesh") + instance.Spec.AppName = "app" + instance.Spec.Ip = "127.0.0.1" + instance.Spec.RpcPort = port + require.NoError(t, stores[meshresource.InstanceKind].Add(instance)) + rpc := meshresource.NewRPCInstanceResourceWithAttributes(meshresource.BuildInstanceResName("app", "127.0.0.1", port), "mesh") + rpc.Spec.AppName = "app" + rpc.Spec.Ip = "127.0.0.1" + rpc.Spec.Endpoints = []*meshproto.Endpoint{{Protocol: "tri", Port: port}} + require.NoError(t, stores[meshresource.RPCInstanceKind].Add(rpc)) + require.NoError(t, stores[meshresource.ServiceProviderMetadataKind].Add(provider)) + return &testContext{rm: manager.NewResourceManager(&testRouter{stores: stores}, nil), stores: stores} +} + +func idlTestCatalog(t *testing.T, port int64) (*testContext, model.ServiceGenericInvokeReq) { + t.Helper() + provider := meshresource.NewServiceProviderMetadataResourceWithAttributes("provider", "mesh") + provider.Spec.ServiceName = "grpc.health.v1.Health" + provider.Spec.ProviderAppName = "app" + provider.Spec.Parameters = map[string]string{"IDL-mode": "IDL"} + provider.Spec.Methods = []*meshproto.Method{{Name: "Check", ParameterTypes: []string{"grpc.health.v1.HealthCheckRequest"}, ReturnType: "grpc.health.v1.HealthCheckResponse"}} + req := model.ServiceGenericInvokeReq{BaseServiceReq: model.BaseServiceReq{Mesh: "mesh", ServiceName: "grpc.health.v1.Health"}, InstanceName: "instance", MethodName: "Check", Signature: "grpc.health.v1.HealthCheckRequest->grpc.health.v1.HealthCheckResponse", Args: []json.RawMessage{json.RawMessage(`{}`)}, TimeoutMs: 3000} + return idlCatalog(t, port, provider), req +} + +func TestIDLUnaryCatalogInvoke(t *testing.T) { + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + server := grpc.NewServer() + healthpb.RegisterHealthServer(server, health.NewServer()) + reflection.Register(server) + go func() { _ = server.Serve(listener) }() + t.Cleanup(func() { server.Stop(); _ = listener.Close() }) + ctx, req := idlTestCatalog(t, int64(listener.Addr().(*net.TCPAddr).Port)) + result, err := InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + require.Equal(t, "SERVING", result.RawResult.(map[string]any)["status"]) +} + +// idlAnyFixture gives each test its own server and descriptor registry. Payload is +// available through reflection only, never through the global message registry. +func idlAnyFixture(t *testing.T, populateResponse bool) (*testContext, model.ServiceGenericInvokeReq, *atomic.Int32) { + t.Helper() + _, err := protoregistry.GlobalTypes.FindMessageByName("fixture.Payload") + require.ErrorIs(t, err, protoregistry.NotFound) + anyFile, err := protoregistry.GlobalFiles.FindFileByPath("google/protobuf/any.proto") + require.NoError(t, err) + optional := descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL + messageType := descriptorpb.FieldDescriptorProto_TYPE_MESSAGE + stringType := descriptorpb.FieldDescriptorProto_TYPE_STRING + root := &descriptorpb.FileDescriptorProto{ + Name: proto.String("any_service.proto"), Package: proto.String("fixture"), Syntax: proto.String("proto3"), + Dependency: []string{"google/protobuf/any.proto"}, + MessageType: []*descriptorpb.DescriptorProto{ + {Name: proto.String("Envelope"), Field: []*descriptorpb.FieldDescriptorProto{{Name: proto.String("content"), Number: proto.Int32(1), Label: &optional, Type: &messageType, TypeName: proto.String(".google.protobuf.Any")}}}, + {Name: proto.String("Payload"), Field: []*descriptorpb.FieldDescriptorProto{{Name: proto.String("value"), Number: proto.Int32(1), Label: &optional, Type: &stringType}}}, + }, + Service: []*descriptorpb.ServiceDescriptorProto{{Name: proto.String("AnyEcho"), Method: []*descriptorpb.MethodDescriptorProto{{Name: proto.String("Echo"), InputType: proto.String(".fixture.Envelope"), OutputType: proto.String(".fixture.Envelope")}}}}, + } + files, err := protodesc.NewFiles(&descriptorpb.FileDescriptorSet{File: []*descriptorpb.FileDescriptorProto{root, protodesc.ToFileDescriptorProto(anyFile)}}) + require.NoError(t, err) + envelopeDesc, err := files.FindDescriptorByName("fixture.Envelope") + require.NoError(t, err) + payloadDesc, err := files.FindDescriptorByName("fixture.Payload") + require.NoError(t, err) + envelope := envelopeDesc.(protoreflect.MessageDescriptor) + payload := dynamicpb.NewMessage(payloadDesc.(protoreflect.MessageDescriptor)) + payload.Set(payload.Descriptor().Fields().ByName("value"), protoreflect.ValueOfString("reflected value")) + packed, err := anypb.New(payload) + require.NoError(t, err) + + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + server := grpc.NewServer() + calls := new(atomic.Int32) + server.RegisterService(&grpc.ServiceDesc{ServiceName: "fixture.AnyEcho", HandlerType: (*idlFixtureService)(nil), Methods: []grpc.MethodDesc{{MethodName: "Echo", Handler: func(_ any, _ context.Context, decode func(any) error, _ grpc.UnaryServerInterceptor) (any, error) { + request := dynamicpb.NewMessage(envelope) + if err := decode(request); err != nil { + return nil, err + } + calls.Add(1) + if populateResponse { + request.Set(envelope.Fields().ByName("content"), protoreflect.ValueOfMessage(packed.ProtoReflect())) + } + return request, nil + }}}}, struct{}{}) + reflectionv1.RegisterServerReflectionServer(server, reflection.NewServerV1(reflection.ServerOptions{Services: server, DescriptorResolver: files})) + go func() { _ = server.Serve(listener) }() + t.Cleanup(func() { server.Stop(); _ = listener.Close() }) + + provider := meshresource.NewServiceProviderMetadataResourceWithAttributes("any-fixture", "mesh") + provider.Spec.ServiceName = "fixture.AnyEcho" + provider.Spec.ProviderAppName = "app" + provider.Spec.Parameters = map[string]string{"IDL-mode": "IDL"} + provider.Spec.Methods = []*meshproto.Method{{Name: "Echo", ParameterTypes: []string{"fixture.Envelope"}, ReturnType: "fixture.Envelope"}} + req := model.ServiceGenericInvokeReq{ + BaseServiceReq: model.BaseServiceReq{Mesh: "mesh", ServiceName: "fixture.AnyEcho"}, + InstanceName: "instance", MethodName: "Echo", Signature: "fixture.Envelope->fixture.Envelope", + Args: []json.RawMessage{json.RawMessage(`{}`)}, TimeoutMs: 3000, + } + return idlCatalog(t, int64(listener.Addr().(*net.TCPAddr).Port), provider), req, calls +} + +func TestIDLRequestAnyResolver(t *testing.T) { + ctx, req, calls := idlAnyFixture(t, false) + req.Args[0] = json.RawMessage(`{"content":{"@type":"type.googleapis.com/fixture.Payload","value":"reflected value"}}`) + result, err := InvokeServiceGeneric(ctx, req) + require.Equal(t, int32(1), calls.Load(), "request must reach the echo handler; invocation error: %v", err) + require.NoError(t, err) + content := result.RawResult.(map[string]any)["content"].(map[string]any) + require.Equal(t, "type.googleapis.com/fixture.Payload", content["@type"]) + require.Equal(t, "reflected value", content["value"]) +} + +func TestIDLResponseAnyResolver(t *testing.T) { + ctx, req, calls := idlAnyFixture(t, true) + result, err := InvokeServiceGeneric(ctx, req) + require.Equal(t, int32(1), calls.Load(), "request must reach the echo handler; invocation error: %v", err) + require.NoError(t, err) + content := result.RawResult.(map[string]any)["content"].(map[string]any) + require.Equal(t, "type.googleapis.com/fixture.Payload", content["@type"]) + require.Equal(t, "reflected value", content["value"]) +} + +// The imported message exercises enum, oneof, int64 and bytes using real protojson rules. +func idlFixtureDescriptors(t *testing.T) (*protoregistry.Files, protoreflect.MessageDescriptor, *descriptorpb.FileDescriptorProto) { + t.Helper() + optional := descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL + str := descriptorpb.FieldDescriptorProto_TYPE_STRING + enum := descriptorpb.FieldDescriptorProto_TYPE_ENUM + i64 := descriptorpb.FieldDescriptorProto_TYPE_INT64 + bytesType := descriptorpb.FieldDescriptorProto_TYPE_BYTES + msg := descriptorpb.FieldDescriptorProto_TYPE_MESSAGE + dep := &descriptorpb.FileDescriptorProto{Name: proto.String("detail.proto"), Package: proto.String("fixture"), Syntax: proto.String("proto3"), MessageType: []*descriptorpb.DescriptorProto{{Name: proto.String("Detail"), EnumType: []*descriptorpb.EnumDescriptorProto{{Name: proto.String("Kind"), Value: []*descriptorpb.EnumValueDescriptorProto{{Name: proto.String("UNKNOWN"), Number: proto.Int32(0)}, {Name: proto.String("READY"), Number: proto.Int32(1)}}}}, OneofDecl: []*descriptorpb.OneofDescriptorProto{{Name: proto.String("choice")}}, Field: []*descriptorpb.FieldDescriptorProto{ + {Name: proto.String("name"), Number: proto.Int32(1), Label: &optional, Type: &str, OneofIndex: proto.Int32(0)}, + {Name: proto.String("kind"), Number: proto.Int32(2), Label: &optional, Type: &enum, TypeName: proto.String(".fixture.Detail.Kind")}, + {Name: proto.String("count"), Number: proto.Int32(3), Label: &optional, Type: &i64}, + {Name: proto.String("payload"), Number: proto.Int32(4), Label: &optional, Type: &bytesType}, + }}}} + root := &descriptorpb.FileDescriptorProto{Name: proto.String("service.proto"), Package: proto.String("fixture"), Syntax: proto.String("proto3"), Dependency: []string{"detail.proto"}, MessageType: []*descriptorpb.DescriptorProto{{Name: proto.String("Request"), Field: []*descriptorpb.FieldDescriptorProto{{Name: proto.String("detail"), Number: proto.Int32(1), Label: &optional, Type: &msg, TypeName: proto.String(".fixture.Detail")}}}}, Service: []*descriptorpb.ServiceDescriptorProto{{Name: proto.String("Echo"), Method: []*descriptorpb.MethodDescriptorProto{ + {Name: proto.String("Call"), InputType: proto.String(".fixture.Request"), OutputType: proto.String(".fixture.Request")}, + {Name: proto.String("Watch"), InputType: proto.String(".fixture.Request"), OutputType: proto.String(".fixture.Request"), ServerStreaming: proto.Bool(true)}, + }}}} + files, err := protodesc.NewFiles(&descriptorpb.FileDescriptorSet{File: []*descriptorpb.FileDescriptorProto{root, dep}}) + require.NoError(t, err) + d, err := files.FindDescriptorByName("fixture.Request") + require.NoError(t, err) + return files, d.(protoreflect.MessageDescriptor), root +} + +type idlFixtureService interface{} + +func idlFixtureServer(t *testing.T, version string) int64 { + t.Helper() + files, message, _ := idlFixtureDescriptors(t) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + server := grpc.NewServer() + server.RegisterService(&grpc.ServiceDesc{ServiceName: "fixture.Echo", HandlerType: (*idlFixtureService)(nil), Methods: []grpc.MethodDesc{{MethodName: "Call", Handler: func(_ any, ctx context.Context, decode func(any) error, _ grpc.UnaryServerInterceptor) (any, error) { + request := dynamicpb.NewMessage(message) + if err := decode(request); err != nil { + return nil, err + } + return request, nil + }}}}, struct{}{}) + options := reflection.ServerOptions{Services: server, DescriptorResolver: files} + switch version { + case "v1": + reflectionv1.RegisterServerReflectionServer(server, reflection.NewServerV1(options)) + case "v1alpha": + reflectionalpha.RegisterServerReflectionServer(server, reflection.NewServer(options)) + } + go func() { _ = server.Serve(listener) }() + t.Cleanup(func() { server.Stop(); _ = listener.Close() }) + return int64(listener.Addr().(*net.TCPAddr).Port) +} + +func idlFixtureCatalog(t *testing.T, port int64) (*testContext, model.ServiceGenericInvokeReq) { + t.Helper() + provider := meshresource.NewServiceProviderMetadataResourceWithAttributes("fixture", "mesh") + provider.Spec.ServiceName = "fixture.Echo" + provider.Spec.ProviderAppName = "app" + provider.Spec.Parameters = map[string]string{"IDL-mode": "IDL"} + for _, name := range []string{"Call", "Watch", "Absent"} { + provider.Spec.Methods = append(provider.Spec.Methods, &meshproto.Method{Name: name, ParameterTypes: []string{"fixture.Request"}, ReturnType: "fixture.Request"}) + } + req := model.ServiceGenericInvokeReq{ + BaseServiceReq: model.BaseServiceReq{Mesh: "mesh", ServiceName: "fixture.Echo"}, + InstanceName: "instance", MethodName: "Call", Signature: "fixture.Request->fixture.Request", + Args: []json.RawMessage{json.RawMessage(`{"detail":{"name":"Ada","kind":"READY","count":"9223372036854775807","payload":"AQI="}}`)}, TimeoutMs: 3000, + } + return idlCatalog(t, port, provider), req +} + +type idlCountingListener struct { + net.Listener + accepted atomic.Int32 + closed atomic.Int32 +} + +type idlCountingConn struct { + net.Conn + once sync.Once + closed *atomic.Int32 +} + +func (l *idlCountingListener) Accept() (net.Conn, error) { + conn, err := l.Listener.Accept() + if err != nil { + return nil, err + } + l.accepted.Add(1) + return &idlCountingConn{Conn: conn, closed: &l.closed}, nil +} + +func (c *idlCountingConn) Close() error { + err := c.Conn.Close() + c.once.Do(func() { c.closed.Add(1) }) + return err +} + +func idlTrackedServer(t *testing.T, handler func(context.Context)) (int64, *idlCountingListener, *atomic.Int32) { + t.Helper() + files, message, _ := idlFixtureDescriptors(t) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + tracked := &idlCountingListener{Listener: listener} + calls := new(atomic.Int32) + server := grpc.NewServer(grpc.StreamInterceptor(func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, next grpc.StreamHandler) error { + md, _ := metadata.FromIncomingContext(stream.Context()) + for _, key := range []string{triple_protocol.TripleServiceGroup, triple_protocol.TripleServiceVersion, "x-e2e-echo"} { + if len(md.Get(key)) != 0 { + return status.Error(codes.InvalidArgument, "business metadata reached reflection") + } + } + return next(srv, stream) + })) + server.RegisterService(&grpc.ServiceDesc{ServiceName: "fixture.Echo", HandlerType: (*idlFixtureService)(nil), Methods: []grpc.MethodDesc{{MethodName: "Call", Handler: func(_ any, ctx context.Context, decode func(any) error, _ grpc.UnaryServerInterceptor) (any, error) { + calls.Add(1) + request := dynamicpb.NewMessage(message) + if err := decode(request); err != nil { + return nil, err + } + if handler != nil { + handler(ctx) + } + return request, nil + }}}}, struct{}{}) + reflectionv1.RegisterServerReflectionServer(server, reflection.NewServerV1(reflection.ServerOptions{Services: server, DescriptorResolver: files})) + go func() { _ = server.Serve(tracked) }() + t.Cleanup(func() { server.Stop(); _ = tracked.Close() }) + return int64(listener.Addr().(*net.TCPAddr).Port), tracked, calls +} + +func TestIDLCatalogReflectionAgreement(t *testing.T) { + tests := []struct { + name, input, output, canonical string + valid bool + }{ + {name: "matching", input: "fixture.Request", output: "fixture.Request", valid: true}, + {name: "leading dots", input: " .fixture.Request ", output: ".fixture.Request", canonical: ".fixture.Echo", valid: true}, + {name: "stale input", input: "fixture.OldRequest", output: "fixture.Request"}, + {name: "stale output", input: "fixture.Request", output: "fixture.OldResponse"}, + {name: "array input", input: "fixture.Request[]", output: "fixture.Request"}, + {name: "generic input", input: "List", output: "fixture.Request"}, + {name: "case mismatch", input: "fixture.request", output: "fixture.Request"}, + {name: "canonical conflict", input: "fixture.Request", output: "fixture.Request", canonical: "fixture.Other"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + port, _, calls := idlTrackedServer(t, nil) + ctx, req := idlFixtureCatalog(t, port) + providers, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + provider := providers[0] + provider.Spec.CanonicalName = tc.canonical + provider.Spec.Methods[0].ParameterTypes = []string{tc.input} + provider.Spec.Methods[0].ReturnType = tc.output + req.Signature = tc.input + "->" + tc.output + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Update(provider)) + _, err = InvokeServiceGeneric(ctx, req) + if tc.valid { + require.NoError(t, err) + require.Equal(t, int32(1), calls.Load()) + } else { + message := "catalog signature" + if tc.canonical != "" { + message = "canonical name" + } + assertIDLBusinessError(t, err, bizerror.InvalidArgument, message) + require.Zero(t, calls.Load()) + } + }) + } +} + +func assertIDLConnectionsClosed(t *testing.T, listener *idlCountingListener, want int32) { + t.Helper() + require.Eventually(t, func() bool { return listener.accepted.Load() == want && listener.closed.Load() == want }, 3*time.Second, 10*time.Millisecond, "accepted=%d closed=%d", listener.accepted.Load(), listener.closed.Load()) +} + +func TestIDLRepeatedSuccessClosesConnections(t *testing.T) { + entered := make(chan metadata.MD, 3) + port, listener, calls := idlTrackedServer(t, func(ctx context.Context) { + md, _ := metadata.FromIncomingContext(ctx) + entered <- md.Copy() + }) + ctx, req := idlFixtureCatalog(t, port) + req.Attachments = map[string]string{" X-E2E-Echo ": "ok"} + for i := int32(1); i <= 3; i++ { + _, err := InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + md := <-entered + require.Equal(t, []string{"ok"}, md.Get("x-e2e-echo")) + require.Empty(t, md.Get(triple_protocol.TripleServiceGroup)) + require.Empty(t, md.Get(triple_protocol.TripleServiceVersion)) + assertIDLConnectionsClosed(t, listener, i) + } + require.Equal(t, int32(3), calls.Load()) +} + +func TestIDLGroupVersionAndAttachments(t *testing.T) { + entered := make(chan metadata.MD, 1) + port, listener, calls := idlTrackedServer(t, func(ctx context.Context) { + md, _ := metadata.FromIncomingContext(ctx) + entered <- md.Copy() + }) + ctx, req := idlFixtureCatalog(t, port) + providers, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + provider := providers[0] + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Delete(provider)) + provider.Spec.Group, provider.Spec.Version = "group-a", "version-b" + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Add(provider)) + req.Group, req.Version = provider.Spec.Group, provider.Spec.Version + req.Attachments = map[string]string{" X-E2E-Echo ": "ok"} + _, err = InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + md := <-entered + require.Equal(t, []string{"ok"}, md.Get("x-e2e-echo")) + require.Equal(t, []string{"group-a"}, md.Get(triple_protocol.TripleServiceGroup)) + require.Equal(t, []string{"version-b"}, md.Get(triple_protocol.TripleServiceVersion)) + require.Equal(t, int32(1), calls.Load()) + assertIDLConnectionsClosed(t, listener, 1) +} + +func TestIDLInvalidJSONDoesNotInvokeBusinessMethod(t *testing.T) { + port, listener, calls := idlTrackedServer(t, nil) + ctx, req := idlFixtureCatalog(t, port) + req.Args = []json.RawMessage{json.RawMessage(`{`)} + _, err := InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "invalid protobuf request JSON") + require.Zero(t, calls.Load()) + assertIDLConnectionsClosed(t, listener, 1) +} + +func TestIDLInFlightCancellationClosesConnection(t *testing.T) { + entered := make(chan struct{}, 1) + port, listener, calls := idlTrackedServer(t, func(ctx context.Context) { + entered <- struct{}{} + <-ctx.Done() + }) + ctx, req := idlFixtureCatalog(t, port) + parent, cancel := context.WithCancel(context.Background()) + defer cancel() + done := make(chan error, 1) + go func() { + _, err := InvokeServiceGeneric(&idlContext{testContext: ctx, parent: parent}, req) + done <- err + }() + select { + case <-entered: + case <-time.After(3 * time.Second): + cancel() + t.Fatal("handler was not entered") + } + cancel() + select { + case err := <-done: + assertIDLBusinessError(t, err, bizerror.InternalError, "context canceled") + case <-time.After(3 * time.Second): + t.Fatal("canceled call did not return") + } + require.Equal(t, int32(1), calls.Load()) + assertIDLConnectionsClosed(t, listener, 1) +} + +func TestIDLRejectsInvalidAttachmentKeys(t *testing.T) { + port, listener, calls := idlTrackedServer(t, nil) + ctx, req := idlFixtureCatalog(t, port) + providers, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + provider := providers[0] + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Delete(provider)) + provider.Spec.Group, provider.Spec.Version = "group-a", "version-b" + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Add(provider)) + req.Group, req.Version = provider.Spec.Group, provider.Spec.Version + + for _, tc := range []struct{ key, value, message string }{ + {"content-type", "secret-value", "content-type"}, + {" :AUTHORITY ", "secret-value", ":authority"}, + {"grpc-status", "secret-value", "grpc-status"}, + {"tri-service-group", "group-a", "tri-service-group"}, + {"tri-service-version", "version-b", "tri-service-version"}, + {" ", "secret-value", "key must not be empty"}, + {"invalid/key", "secret-value", "invalid/key"}, + } { + t.Run(tc.message, func(t *testing.T) { + r := req + r.Attachments = map[string]string{tc.key: tc.value} + _, err := InvokeServiceGeneric(ctx, r) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, tc.message) + require.NotContains(t, err.Error(), tc.value) + require.Zero(t, calls.Load()) + require.Zero(t, listener.accepted.Load()) + }) + } +} + +func TestIDLInvokeContext(t *testing.T) { + req := model.ServiceGenericInvokeReq{ + BaseServiceReq: model.BaseServiceReq{Group: "group-a", Version: "version-b"}, + Attachments: map[string]string{" X-E2E-Echo ": "ok", "payload-bin": "\x00\x01"}, + } + ctx, err := idlInvokeContext(context.Background(), req) + require.NoError(t, err) + md, ok := metadata.FromOutgoingContext(ctx) + require.True(t, ok) + require.Equal(t, []string{"ok"}, md.Get("x-e2e-echo")) + require.Equal(t, []string{"\x00\x01"}, md.Get("payload-bin")) + require.Equal(t, []string{"group-a"}, md.Get(triple_protocol.TripleServiceGroup)) + require.Equal(t, []string{"version-b"}, md.Get(triple_protocol.TripleServiceVersion)) + + for _, key := range []string{"content-type", ":authority", "grpc-status", "tri-service-group", "tri-service-version", " ", "invalid/key"} { + t.Run(key, func(t *testing.T) { + r := req + r.Attachments = map[string]string{key: "secret-value"} + _, err := idlInvokeContext(context.Background(), r) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "attachment key") + require.NotContains(t, err.Error(), "secret-value") + }) + } +} + +func assertIDLBusinessError(t *testing.T, err error, code bizerror.ErrorCode, contains string) { + t.Helper() + require.Error(t, err) + var business bizerror.Error + require.ErrorAs(t, err, &business) + require.Equal(t, code, business.Code()) + require.Contains(t, business.Message(), contains) + envelope := model.NewBizErrorResp(business) + require.Equal(t, string(code), envelope.Code) + require.Nil(t, envelope.Data) +} + +func TestIDLDescriptorsAndUnary(t *testing.T) { + for _, version := range []string{"v1", "v1alpha"} { + t.Run(version, func(t *testing.T) { + ctx, req := idlFixtureCatalog(t, idlFixtureServer(t, version)) + result, err := InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + envelope := model.NewSuccessResp(result) + require.Equal(t, "Success", envelope.Code) + detail := result.RawResult.(map[string]any)["detail"].(map[string]any) + require.Equal(t, "Ada", detail["name"]) + require.Equal(t, "READY", detail["kind"]) + require.Equal(t, "9223372036854775807", detail["count"]) + require.Equal(t, "AQI=", detail["payload"]) + }) + } +} + +func TestIDLInvokeErrors(t *testing.T) { + ctx, req := idlFixtureCatalog(t, idlFixtureServer(t, "v1")) + tests := []struct { + name, method, signature, body string + args []json.RawMessage + code bizerror.ErrorCode + message string + }{ + {name: "invalid JSON", body: `{`, code: bizerror.InvalidArgument, message: "invalid protobuf request JSON"}, + {name: "unknown field wrapper", body: `{"class":"fixture.Request"}`, code: bizerror.InvalidArgument, message: "unknown field"}, + {name: "bare string", body: `"Ada"`, code: bizerror.InvalidArgument, message: "invalid protobuf request JSON"}, + {name: "arity", args: []json.RawMessage{}, code: bizerror.InvalidArgument, message: "parameter count"}, + {name: "unknown catalog method", method: "Unknown", code: bizerror.NotFoundError, message: "method Unknown not found"}, + {name: "unknown descriptor method", method: "Absent", code: bizerror.NotFoundError, message: "method not found in reflection"}, + {name: "streaming", method: "Watch", code: bizerror.InvalidArgument, message: "unary only"}, + {name: "signature mismatch", signature: "bad", code: bizerror.NotFoundError, message: "method Call not found"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + r := req + if tc.method != "" { + r.MethodName = tc.method + } + if tc.signature != "" { + r.Signature = tc.signature + } + if tc.body != "" { + r.Args = []json.RawMessage{json.RawMessage(tc.body)} + } + if tc.args != nil { + r.Args = tc.args + } + _, err := InvokeServiceGeneric(ctx, r) + assertIDLBusinessError(t, err, tc.code, tc.message) + }) + } +} + +func TestIDLReflectionUnsupported(t *testing.T) { + ctx, req := idlFixtureCatalog(t, idlFixtureServer(t, "none")) + _, err := InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "reflection unsupported") +} + +type idlContext struct { + *testContext + parent context.Context +} + +func (c *idlContext) AppContext() context.Context { return c.parent } +func TestIDLContextErrors(t *testing.T) { + ctx, req := idlFixtureCatalog(t, idlFixtureServer(t, "v1")) + req.Attachments = map[string]string{"x-e2e-echo": "ok"} + canceled, cancel := context.WithCancel(context.Background()) + cancel() + _, err := InvokeServiceGeneric(&idlContext{testContext: ctx, parent: canceled}, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "context canceled") + req.TimeoutMs = -1 + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "context deadline exceeded") +} + +func TestIDLRejectsMissingInstance(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + req.InstanceName = "unregistered" + _, err := InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.NotFoundError, "instance unregistered not found") +} + +func TestIDLIgnoresOtherAppProviderMode(t *testing.T) { + port, _, calls := idlTrackedServer(t, nil) + ctx, req := idlFixtureCatalog(t, port) + other := meshresource.NewServiceProviderMetadataResourceWithAttributes("other", "mesh") + other.Spec.ServiceName = req.ServiceName + other.Spec.ProviderAppName = "other-app" + other.Spec.Parameters = map[string]string{"IDL-mode": "non-IDL"} + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Add(other)) + _, err := InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + require.Equal(t, int32(1), calls.Load()) +} + +func TestIDLRejectsConflictingProviderModes(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + other := meshresource.NewServiceProviderMetadataResourceWithAttributes("other", "mesh") + other.Spec.ServiceName = req.ServiceName + other.Spec.ProviderAppName = "app" + other.Spec.Parameters = map[string]string{"IDL-mode": "non-IDL"} + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Add(other)) + _, err := InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "conflicting IDL-mode") +} + +func TestIDLRejectsUnknownProviderMode(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + other := meshresource.NewServiceProviderMetadataResourceWithAttributes("other", "mesh") + other.Spec.ServiceName = req.ServiceName + other.Spec.ProviderAppName = "app" + other.Spec.Parameters = map[string]string{"IDL-mode": "unknown"} + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Add(other)) + _, err := InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "unknown IDL-mode") +} + +func TestIDLRejectsSecureProvider(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + providers, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + providers[0].Spec.Parameters["ssl-enabled"] = "true" + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Update(providers[0])) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "secure or authenticated") +} + +func TestIDLRejectsUnboundProviderMetadata(t *testing.T) { + port, listener, calls := idlTrackedServer(t, nil) + for _, tc := range []struct{ name, app string }{ + {name: "other app", app: "other-app"}, + {name: "missing app"}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, req := idlFixtureCatalog(t, port) + providers, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + providers[0].Spec.ProviderAppName = tc.app + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Update(providers[0])) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "IDL provider metadata is not bound to the selected instance") + require.Zero(t, calls.Load()) + require.Zero(t, listener.accepted.Load()) + }) + } +} + +func TestIDLRejectsMismatchedRPCInstance(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + instance, err := getGenericInvokeInstance(ctx, "mesh", "instance") + require.NoError(t, err) + rpc, err := findRPCInstanceByInstance(ctx, instance) + require.NoError(t, err) + rpc.Spec.AppName = "other-app" + require.NoError(t, ctx.stores[meshresource.RPCInstanceKind].Update(rpc)) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "does not match selected catalog instance") +} + +func TestIDLRejectsInstanceSecurityMarkers(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + instance, err := getGenericInvokeInstance(ctx, "mesh", "instance") + require.NoError(t, err) + rpc, err := findRPCInstanceByInstance(ctx, instance) + require.NoError(t, err) + rpc.Spec.Metadata = map[string]string{constants.URLParamsKey: `{"ssl-enabled":"true"}`} + require.NoError(t, ctx.stores[meshresource.RPCInstanceKind].Update(rpc)) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InvalidArgument, "secure or authenticated") +} + +func TestIDLSecurityKnownMarkers(t *testing.T) { + require.True(t, idlSecurityRequired(map[string]string{constant.CACert: "ca.pem"})) + require.True(t, idlSecurityRequired(map[string]string{constant.AccessKeyIDKey: "key"})) + require.True(t, idlSecurityRequired(map[string]string{constants.URLParamsKey: "{"})) + require.False(t, idlSecurityRequired(map[string]string{constant.SslEnabledKey: "false", "tls-enabled": "true"})) +} + +func TestNonIDLUsesExistingGenericPath(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + p, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + delete(p[0].Spec.Parameters, "IDL-mode") + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Update(p[0])) + previous := invokeGenericServiceRPC + t.Cleanup(func() { invokeGenericServiceRPC = previous }) + called := false + invokeGenericServiceRPC = func(_ context.Context, inv genericInvocation) (any, error) { + called = true + require.Equal(t, "tri://127.0.0.1:1", inv.URL) + return map[string]any{"ok": true}, nil + } + result, err := InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + require.True(t, called) + require.Equal(t, true, result.RawResult.(map[string]any)["ok"]) + invokeGenericServiceRPC = func(_ context.Context, _ genericInvocation) (any, error) { + return nil, errors.New("underlying failure") + } + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "generic invoke failed, please check server logs") +} + +func TestNonIDLMetadataBinding(t *testing.T) { + previous := invokeGenericServiceRPC + t.Cleanup(func() { invokeGenericServiceRPC = previous }) + for _, tc := range []struct { + name, app, mode string + otherProviders bool + mismatchedRPC bool + }{ + {name: "legacy metadata without app"}, + {name: "bound empty mode", app: "app", otherProviders: true}, + {name: "bound non-IDL mode", app: "app", mode: "non-IDL", otherProviders: true, mismatchedRPC: true}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, req := idlFixtureCatalog(t, 1) + providers, err := listProviderMeta(ctx, req.BaseServiceReq) + require.NoError(t, err) + providers[0].Spec.ProviderAppName = tc.app + if tc.mode == "" { + delete(providers[0].Spec.Parameters, "IDL-mode") + } else { + providers[0].Spec.Parameters["IDL-mode"] = tc.mode + } + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Update(providers[0])) + if tc.otherProviders { + for _, mode := range []string{"IDL", "unknown"} { + other := meshresource.NewServiceProviderMetadataResourceWithAttributes(mode, "mesh") + other.Spec.ServiceName = req.ServiceName + other.Spec.ProviderAppName = "other-app" + other.Spec.Parameters = map[string]string{"IDL-mode": mode} + other.Spec.Methods = providers[0].Spec.Methods + require.NoError(t, ctx.stores[meshresource.ServiceProviderMetadataKind].Add(other)) + } + } + if tc.mismatchedRPC { + instance, err := getGenericInvokeInstance(ctx, "mesh", "instance") + require.NoError(t, err) + rpc, err := findRPCInstanceByInstance(ctx, instance) + require.NoError(t, err) + rpc.Spec.AppName = "other-app" + require.NoError(t, ctx.stores[meshresource.RPCInstanceKind].Update(rpc)) + } + called := false + invokeGenericServiceRPC = func(_ context.Context, _ genericInvocation) (any, error) { + called = true + return "ok", nil + } + result, err := InvokeServiceGeneric(ctx, req) + require.NoError(t, err) + require.True(t, called) + require.Equal(t, "ok", result.RawResult) + }) + } +} + +type incompleteReflection struct { + reflectionv1.UnimplementedServerReflectionServer + root []byte +} + +func (s *incompleteReflection) ServerReflectionInfo(stream grpc.BidiStreamingServer[reflectionv1.ServerReflectionRequest, reflectionv1.ServerReflectionResponse]) error { + request, err := stream.Recv() + if err != nil { + return err + } + if request.GetFileContainingSymbol() != "" { + return stream.Send(&reflectionv1.ServerReflectionResponse{MessageResponse: &reflectionv1.ServerReflectionResponse_FileDescriptorResponse{FileDescriptorResponse: &reflectionv1.FileDescriptorResponse{FileDescriptorProto: [][]byte{s.root}}}}) + } + return stream.Send(&reflectionv1.ServerReflectionResponse{MessageResponse: &reflectionv1.ServerReflectionResponse_ErrorResponse{ErrorResponse: &reflectionv1.ErrorResponse{ErrorCode: int32(codes.NotFound), ErrorMessage: "file not found"}}}) +} +func TestIDLMissingDependency(t *testing.T) { + _, _, root := idlFixtureDescriptors(t) + data, err := proto.Marshal(root) + require.NoError(t, err) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + server := grpc.NewServer() + reflectionv1.RegisterServerReflectionServer(server, &incompleteReflection{root: data}) + go func() { _ = server.Serve(listener) }() + t.Cleanup(func() { server.Stop(); _ = listener.Close() }) + ctx, req := idlFixtureCatalog(t, int64(listener.Addr().(*net.TCPAddr).Port)) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "missing dependency detail.proto") +} + +type excessiveReflection struct { + reflectionv1.UnimplementedServerReflectionServer + raw []byte +} + +func (s *excessiveReflection) ServerReflectionInfo(stream grpc.BidiStreamingServer[reflectionv1.ServerReflectionRequest, reflectionv1.ServerReflectionResponse]) error { + if _, err := stream.Recv(); err != nil { + return err + } + files := make([][]byte, 257) + for i := range files { + files[i] = s.raw + } + return stream.Send(&reflectionv1.ServerReflectionResponse{MessageResponse: &reflectionv1.ServerReflectionResponse_FileDescriptorResponse{FileDescriptorResponse: &reflectionv1.FileDescriptorResponse{FileDescriptorProto: files}}}) +} + +func TestIDLDescriptorLimit(t *testing.T) { + _, _, root := idlFixtureDescriptors(t) + raw, err := proto.Marshal(root) + require.NoError(t, err) + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + server := grpc.NewServer() + reflectionv1.RegisterServerReflectionServer(server, &excessiveReflection{raw: raw}) + go func() { _ = server.Serve(listener) }() + t.Cleanup(func() { server.Stop(); _ = listener.Close() }) + ctx, req := idlFixtureCatalog(t, int64(listener.Addr().(*net.TCPAddr).Port)) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "reflection descriptor limit exceeded") +} + +type deniedReflection struct { + reflectionv1.UnimplementedServerReflectionServer +} + +func (*deniedReflection) ServerReflectionInfo(grpc.BidiStreamingServer[reflectionv1.ServerReflectionRequest, reflectionv1.ServerReflectionResponse]) error { + return status.Error(codes.PermissionDenied, "denied") +} + +type countedAlphaReflection struct { + reflectionalpha.UnimplementedServerReflectionServer + calls atomic.Int32 +} + +//nolint:staticcheck // v1alpha types are deprecated but required to assert no fallback. +func (s *countedAlphaReflection) ServerReflectionInfo(grpc.BidiStreamingServer[reflectionalpha.ServerReflectionRequest, reflectionalpha.ServerReflectionResponse]) error { + s.calls.Add(1) + return status.Error(codes.Unimplemented, "unsupported") +} +func TestIDLReflectionDoesNotDowngradePermissionDenied(t *testing.T) { + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + server := grpc.NewServer() + alpha := new(countedAlphaReflection) + reflectionv1.RegisterServerReflectionServer(server, &deniedReflection{}) + reflectionalpha.RegisterServerReflectionServer(server, alpha) + go func() { _ = server.Serve(listener) }() + t.Cleanup(func() { server.Stop(); _ = listener.Close() }) + ctx, req := idlFixtureCatalog(t, int64(listener.Addr().(*net.TCPAddr).Port)) + _, err = InvokeServiceGeneric(ctx, req) + assertIDLBusinessError(t, err, bizerror.InternalError, "PermissionDenied") + require.Equal(t, int32(0), alpha.calls.Load()) +}