Skip to content

Commit

Permalink
Support grpc.reflection.v1.ServerReflection (#314)
Browse files Browse the repository at this point in the history
Currently, `grpc.reflection.v1.ServerReflection` is shown in the list of
`Service Name`. This PR removes it as well as
`grpc.reflection.v1alpha.ServerReflection`.

Also, change to use `v1` protocol if we can use it.
  • Loading branch information
y-yagi authored Apr 24, 2024
1 parent 0d16d85 commit 11bfac0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GUI. This lets you interactively construct requests to send to a gRPC server.

With this tool you can also browse the schema for gRPC services, which is presented as a
list of available endpoints. This is enabled either by querying a server that supports
[server reflection](https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto),
[server reflection](https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1/reflection.proto),
by reading proto source files, or by loading in compiled "protoset" files (files that contain
encoded file [descriptor protos](https://github.com/google/protobuf/blob/master/src/google/protobuf/descriptor.proto)).
In fact, the way the tool transforms JSON request data into a binary encoded protobuf
Expand Down Expand Up @@ -82,7 +82,7 @@ run `make install`.
If you encounter compile errors, you could have out-dated versions of `grpcui`'s
dependencies. You can update the dependencies by running `make updatedeps`.

### Running without install
### Running without install

```
go run ./cmd/grpcui/grpcui.go -plaintext localhost:9019
Expand Down Expand Up @@ -230,7 +230,7 @@ into text. The sections below document the supported sources and what command-li
are needed to use them.

### Server Reflection
Without any additional command-line flags, `grpcui` will try to use [server reflection](https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1alpha/reflection.proto).
Without any additional command-line flags, `grpcui` will try to use [server reflection](https://github.com/grpc/grpc/blob/master/src/proto/grpc/reflection/v1/reflection.proto).

Examples for how to set up server reflection can be found [here](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md#known-implementations).

Expand Down
5 changes: 2 additions & 3 deletions cmd/grpcui/grpcui.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"

// Register gzip compressor so compressed responses will work
_ "google.golang.org/grpc/encoding/gzip"
Expand Down Expand Up @@ -564,7 +563,7 @@ func main() {
if reflection.val {
md := grpcurl.MetadataFromHeaders(append(addlHeaders, reflHeaders...))
refCtx := metadata.NewOutgoingContext(ctx, md)
refClient = grpcreflect.NewClientV1Alpha(refCtx, reflectpb.NewServerReflectionClient(cc))
refClient = grpcreflect.NewClientAuto(refCtx, cc)
refClient.AllowMissingFileDescriptors()
reflSource := grpcurl.DescriptorSourceFromServer(ctx, refClient)
if fileSource != nil {
Expand Down Expand Up @@ -859,7 +858,7 @@ func getMethods(source grpcurl.DescriptorSource, configs map[string]*svcConfig)

var descs []*desc.MethodDescriptor
for _, svc := range allServices {
if svc == "grpc.reflection.v1alpha.ServerReflection" {
if svc == "grpc.reflection.v1alpha.ServerReflection" || svc == "grpc.reflection.v1.ServerReflection" {
continue
}
d, err := source.FindSymbol(svc)
Expand Down
4 changes: 1 addition & 3 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/jhump/protoreflect/grpcreflect"
"golang.org/x/net/context"
"google.golang.org/grpc"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

Expand All @@ -18,8 +17,7 @@ import (
// reflection. (See "google.golang.org/grpc/reflection" for more on service
// reflection.)
func AllFilesViaReflection(ctx context.Context, cc grpc.ClientConnInterface) ([]*desc.FileDescriptor, error) {
stub := rpb.NewServerReflectionClient(cc)
cli := grpcreflect.NewClientV1Alpha(ctx, stub)
cli := grpcreflect.NewClientAuto(ctx, cc)
source := grpcurl.DescriptorSourceFromServer(ctx, cli)
return grpcurl.GetAllFiles(source)
}
Expand Down
10 changes: 5 additions & 5 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)

// AllMethodsForServices returns a slice that contains the method descriptors
Expand Down Expand Up @@ -47,8 +46,7 @@ func AllMethodsForServer(svr *grpc.Server) ([]*desc.MethodDescriptor, error) {
// This automatically skips the reflection service, since it is assumed this is not
// a desired inclusion.
func AllMethodsViaReflection(ctx context.Context, cc grpc.ClientConnInterface) ([]*desc.MethodDescriptor, error) {
stub := rpb.NewServerReflectionClient(cc)
cli := grpcreflect.NewClientV1Alpha(ctx, stub)
cli := grpcreflect.NewClientAuto(ctx, cc)
svcNames, err := cli.ListServices()
if err != nil {
return nil, err
Expand All @@ -59,7 +57,8 @@ func AllMethodsViaReflection(ctx context.Context, cc grpc.ClientConnInterface) (
if err != nil {
return nil, err
}
if sd.GetFullyQualifiedName() == "grpc.reflection.v1alpha.ServerReflection" {
fullyQualifiedName := sd.GetFullyQualifiedName()
if fullyQualifiedName == "grpc.reflection.v1alpha.ServerReflection" || fullyQualifiedName == "grpc.reflection.v1.ServerReflection" {
continue // skip reflection service
}
descs = append(descs, sd)
Expand All @@ -78,7 +77,8 @@ func AllMethodsViaInProcess(svr reflection.GRPCServer) ([]*desc.MethodDescriptor
}
var descs []*desc.ServiceDescriptor
for _, sd := range sds {
if sd.GetFullyQualifiedName() == "grpc.reflection.v1alpha.ServerReflection" {
fullyQualifiedName := sd.GetFullyQualifiedName()
if fullyQualifiedName == "grpc.reflection.v1alpha.ServerReflection" || fullyQualifiedName == "grpc.reflection.v1.ServerReflection" {
continue // skip reflection service
}
descs = append(descs, sd)
Expand Down
4 changes: 2 additions & 2 deletions testing/cmd/testsvr/testsvr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
reflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
reflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
Expand Down Expand Up @@ -49,7 +49,7 @@ func main() {

svr := grpc.NewServer()
RegisterKitchenSinkServer(svr, &testSvr{})
refSvc := reflection.NewServer(reflection.ServerOptions{
refSvc := reflection.NewServerV1(reflection.ServerOptions{
Services: svr,
DescriptorResolver: sourceinfo.GlobalFiles,
ExtensionResolver: sourceinfo.GlobalFiles,
Expand Down

0 comments on commit 11bfac0

Please sign in to comment.