forked from connectrpc/grpcreflect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpcreflect.go
476 lines (431 loc) · 16.2 KB
/
grpcreflect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2022-2024 The Connect Authors
//
// Licensed 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 grpcreflect enables any net/http server, including those built with
// Connect, to handle gRPC's server reflection API. This lets ad-hoc debugging
// tools call your Protobuf services and print the responses without a copy of
// the schema.
//
// The exposed reflection API is wire compatible with Google's gRPC
// implementations, so it works with grpcurl, grpcui, BloomRPC, and many other
// tools.
//
// The core Connect package is connectrpc.com/connect. Documentation is
// available at https://connectrpc.com.
package grpcreflect
import (
"context"
_ "embed" // required for go:embed directive
"errors"
"fmt"
"io"
"net/http"
"sort"
"connectrpc.com/connect"
reflectionv1 "connectrpc.com/grpcreflect/internal/gen/go/connectext/grpc/reflection/v1"
"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"
)
const (
// ReflectV1ServiceName is the fully-qualified name of the v1 version of the reflection service.
ReflectV1ServiceName = "grpc.reflection.v1.ServerReflection"
// ReflectV1AlphaServiceName is the fully-qualified name of the v1alpha version of the reflection service.
ReflectV1AlphaServiceName = "grpc.reflection.v1alpha.ServerReflection"
serviceURLPathV1 = "/" + ReflectV1ServiceName + "/"
serviceURLPathV1Alpha = "/" + ReflectV1AlphaServiceName + "/"
methodName = "ServerReflectionInfo"
)
//nolint:gochecknoglobals
var (
//go:embed services.bin
embeddedDescriptors []byte
globalFiles = resolverHackForConnectext(embeddedDescriptors)
)
// NewHandlerV1 constructs an implementation of v1 of the gRPC server reflection
// API. It returns an HTTP handler and the path on which to mount it.
//
// Note that because the reflection API requires bidirectional streaming, the
// returned handler doesn't support HTTP/1.1. If your server must also support
// older tools that use the v1alpha server reflection API, see NewHandlerV1Alpha.
func NewHandlerV1(reflector *Reflector, options ...connect.HandlerOption) (string, http.Handler) {
return newHandler(reflector, serviceURLPathV1, options)
}
// NewHandlerV1Alpha constructs an implementation of v1alpha of the gRPC server
// reflection API, which is useful to support tools that haven't updated to the
// v1 API. It returns an HTTP handler and the path on which to mount it.
//
// Like NewHandlerV1, the returned handler doesn't support HTTP/1.1.
func NewHandlerV1Alpha(reflector *Reflector, options ...connect.HandlerOption) (string, http.Handler) {
// v1 is binary-compatible with v1alpha, so we only need to change paths.
return newHandler(reflector, serviceURLPathV1Alpha, options)
}
// Reflector implements the underlying logic for gRPC's protobuf server
// reflection. They're configurable, so they can support straightforward
// process-local reflection or more complex proxying.
//
// Keep in mind that by default, Reflectors expose every protobuf type and
// extension compiled into your binary. Think twice before including the
// default Reflector in a public API.
//
// For more information, see
// https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md,
// https://github.com/grpc/grpc/blob/master/doc/server-reflection.md, and
// https://github.com/fullstorydev/grpcurl.
type Reflector struct {
namer Namer
extensionResolver ExtensionResolver
descriptorResolver protodesc.Resolver
}
// NewReflector constructs a highly configurable Reflector: it can serve a
// dynamic list of services, proxy reflection requests to other backends, or
// use an alternate source of extension information.
//
// To build a simpler Reflector that supports a static list of services using
// information from the package-global Protobuf registry, use
// NewStaticReflector.
func NewReflector(namer Namer, options ...Option) *Reflector {
reflector := &Reflector{
namer: namer,
extensionResolver: protoregistry.GlobalTypes,
descriptorResolver: globalFiles,
}
for _, option := range options {
option.apply(reflector)
}
return reflector
}
// NewStaticReflector constructs a simple Reflector that supports a static list
// of services using information from the package-global Protobuf registry. For
// a more configurable Reflector, use NewReflector.
//
// The supplied strings should be fully-qualified Protobuf service names (for
// example, "acme.user.v1.UserService"). Generated Connect service files
// have this declared as a constant.
func NewStaticReflector(services ...string) *Reflector {
namer := &staticNames{names: services}
return NewReflector(namer)
}
// serverReflectionInfo implements the gRPC server reflection API.
func (r *Reflector) serverReflectionInfo(
_ context.Context,
stream *connect.BidiStream[
reflectionv1.ServerReflectionRequest,
reflectionv1.ServerReflectionResponse,
],
) error {
fileDescriptorsSent := &fileDescriptorNameSet{}
for {
request, err := stream.Receive()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
// The server reflection API sends file descriptors as uncompressed
// Protobuf-serialized bytes.
response := &reflectionv1.ServerReflectionResponse{
ValidHost: request.Host,
OriginalRequest: request,
}
switch messageRequest := request.MessageRequest.(type) {
case *reflectionv1.ServerReflectionRequest_FileByFilename:
data, err := r.getFileByFilename(messageRequest.FileByFilename, fileDescriptorsSent)
if err != nil {
response.MessageResponse = newNotFoundResponse(err)
} else {
response.MessageResponse = &reflectionv1.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &reflectionv1.FileDescriptorResponse{FileDescriptorProto: data},
}
}
case *reflectionv1.ServerReflectionRequest_FileContainingSymbol:
data, err := r.getFileContainingSymbol(
messageRequest.FileContainingSymbol,
fileDescriptorsSent,
)
if err != nil {
response.MessageResponse = newNotFoundResponse(err)
} else {
response.MessageResponse = &reflectionv1.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &reflectionv1.FileDescriptorResponse{FileDescriptorProto: data},
}
}
case *reflectionv1.ServerReflectionRequest_FileContainingExtension:
msgFQN := messageRequest.FileContainingExtension.ContainingType
extNumber := messageRequest.FileContainingExtension.ExtensionNumber
data, err := r.getFileContainingExtension(msgFQN, extNumber, fileDescriptorsSent)
if err != nil {
response.MessageResponse = newNotFoundResponse(err)
} else {
response.MessageResponse = &reflectionv1.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &reflectionv1.FileDescriptorResponse{FileDescriptorProto: data},
}
}
case *reflectionv1.ServerReflectionRequest_AllExtensionNumbersOfType:
nums, err := r.getAllExtensionNumbersOfType(messageRequest.AllExtensionNumbersOfType)
if err != nil {
response.MessageResponse = newNotFoundResponse(err)
} else {
response.MessageResponse = &reflectionv1.ServerReflectionResponse_AllExtensionNumbersResponse{
AllExtensionNumbersResponse: &reflectionv1.ExtensionNumberResponse{
BaseTypeName: messageRequest.AllExtensionNumbersOfType,
ExtensionNumber: nums,
},
}
}
case *reflectionv1.ServerReflectionRequest_ListServices:
services := r.namer.Names()
serviceResponses := make([]*reflectionv1.ServiceResponse, len(services))
for i, name := range services {
serviceResponses[i] = &reflectionv1.ServiceResponse{Name: name}
}
response.MessageResponse = &reflectionv1.ServerReflectionResponse_ListServicesResponse{
ListServicesResponse: &reflectionv1.ListServiceResponse{Service: serviceResponses},
}
default:
return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf(
"invalid MessageRequest: %v",
request.MessageRequest,
))
}
if err := stream.Send(response); err != nil {
return err
}
}
}
func (r *Reflector) getFileByFilename(fname string, sent *fileDescriptorNameSet) ([][]byte, error) {
fd, err := r.descriptorResolver.FindFileByPath(fname)
if err != nil {
return nil, err
}
return fileDescriptorWithDependencies(fd, sent)
}
func (r *Reflector) getFileContainingSymbol(fqn string, sent *fileDescriptorNameSet) ([][]byte, error) {
desc, err := r.descriptorResolver.FindDescriptorByName(protoreflect.FullName(fqn))
if err != nil {
return nil, err
}
fd := desc.ParentFile()
if fd == nil {
return nil, fmt.Errorf("no file for symbol %s", fqn)
}
return fileDescriptorWithDependencies(fd, sent)
}
func (r *Reflector) getFileContainingExtension(
msgFQN string,
extNumber int32,
sent *fileDescriptorNameSet,
) ([][]byte, error) {
extension, err := r.extensionResolver.FindExtensionByNumber(
protoreflect.FullName(msgFQN),
protoreflect.FieldNumber(extNumber),
)
if err != nil {
return nil, err
}
fd := extension.TypeDescriptor().ParentFile()
if fd == nil {
return nil, fmt.Errorf("no file for extension %d of message %s", extNumber, msgFQN)
}
return fileDescriptorWithDependencies(fd, sent)
}
func (r *Reflector) getAllExtensionNumbersOfType(fqn string) ([]int32, error) {
nums := []int32{}
name := protoreflect.FullName(fqn)
r.extensionResolver.RangeExtensionsByMessage(name, func(ext protoreflect.ExtensionType) bool {
num := int32(ext.TypeDescriptor().Number())
nums = append(nums, num)
return true
})
if len(nums) == 0 {
if _, err := r.descriptorResolver.FindDescriptorByName(name); err != nil {
return nil, err
}
}
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
return nums, nil
}
// A Namer lists the fully-qualified Protobuf service names available for
// reflection (for example, "acme.user.v1.UserService"). Namers must be safe to
// call concurrently.
type Namer interface {
Names() []string
}
// NamerFunc is an adapter to allow the use of an ordinary function as a Namer.
// Example:
//
// reflector := grpcreflect.NewReflector(grpcreflect.NamerFunc(
// func() []string { return s.names },
// ))
type NamerFunc func() []string
// Names returns the service names, implements the Namer interface.
func (f NamerFunc) Names() []string {
return f()
}
// An Option configures a Reflector.
type Option interface {
apply(*Reflector)
}
// WithExtensionResolver sets the resolver used to find Protobuf extensions. By
// default, Reflectors use protoregistry.GlobalTypes.
func WithExtensionResolver(resolver ExtensionResolver) Option {
return &extensionResolverOption{resolver: resolver}
}
// WithDescriptorResolver sets the resolver used to find Protobuf type
// information (typically called a "descriptor"). By default, Reflectors use
// protoregistry.GlobalFiles.
func WithDescriptorResolver(resolver protodesc.Resolver) Option {
return &descriptorResolverOption{resolver: resolver}
}
// An ExtensionResolver lets server reflection implementations query details
// about the registered Protobuf extensions. protoregistry.GlobalTypes
// implements ExtensionResolver.
//
// ExtensionResolvers must be safe to call concurrently.
type ExtensionResolver interface {
protoregistry.ExtensionTypeResolver
RangeExtensionsByMessage(protoreflect.FullName, func(protoreflect.ExtensionType) bool)
}
type fileDescriptorNameSet struct {
names map[string]struct{}
}
func (s *fileDescriptorNameSet) Insert(fd protoreflect.FileDescriptor) {
if s.names == nil {
s.names = make(map[string]struct{}, 1)
}
s.names[fd.Path()] = struct{}{}
}
func (s *fileDescriptorNameSet) Contains(fd protoreflect.FileDescriptor) bool {
_, ok := s.names[fd.Path()]
return ok
}
func fileDescriptorWithDependencies(rootFile protoreflect.FileDescriptor, sent *fileDescriptorNameSet) ([][]byte, error) {
if rootFile.IsPlaceholder() {
// A placeholder is used when a dependency is missing. If a placeholder is all we have
// then we don't actually have anything.
return nil, protoregistry.NotFound
}
results := make([][]byte, 0, 1)
queue := []protoreflect.FileDescriptor{rootFile}
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
if curr.IsPlaceholder() {
continue // don't bother serializing placeholders
}
if len(results) == 0 || !sent.Contains(curr) { // always send root fd
// Mark as sent immediately. If we hit an error marshaling below, there's
// no point trying again later.
sent.Insert(curr)
encoded, err := proto.Marshal(protodesc.ToFileDescriptorProto(curr))
if err != nil {
return nil, err
}
results = append(results, encoded)
}
imports := curr.Imports()
for i := 0; i < imports.Len(); i++ {
queue = append(queue, imports.Get(i).FileDescriptor)
}
}
return results, nil
}
func newNotFoundResponse(err error) *reflectionv1.ServerReflectionResponse_ErrorResponse {
return &reflectionv1.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &reflectionv1.ErrorResponse{
ErrorCode: int32(connect.CodeNotFound),
ErrorMessage: err.Error(),
},
}
}
func newHandler(reflector *Reflector, servicePath string, options []connect.HandlerOption) (string, http.Handler) {
return servicePath, connect.NewBidiStreamHandler(
servicePath+methodName,
reflector.serverReflectionInfo,
options...,
)
}
type extensionResolverOption struct {
resolver ExtensionResolver
}
func (o *extensionResolverOption) apply(reflector *Reflector) {
reflector.extensionResolver = o.resolver
}
type descriptorResolverOption struct {
resolver protodesc.Resolver
}
func (o *descriptorResolverOption) apply(reflector *Reflector) {
reflector.descriptorResolver = o.resolver
}
type staticNames struct {
names []string
}
func (n *staticNames) Names() []string {
return n.names
}
// resolverHackForConnectext returns a resolver that can successfully resolve the descriptors
// for the gRPC health and reflection services. We need a work-around since this repo (and the
// connect-grpchealth-go repo) use "hacked" services that have a "connectext." package prefix.
// We don't use the "authoritative" packages for these descriptors because they depend on the
// gRPC runtime (ew!). We add a special prefix to the packages to avoid an init-time panic from
// duplicate registrations, in the event that the calling application _also_ imports the gRPC
// versions.
//
// This works by serving embedded descriptors (from "services.bin") for items not found in
// protoregistry.GlobalFiles. The only thing in the embedded descriptors are for the health
// and reflection services.
func resolverHackForConnectext(data []byte) protodesc.Resolver {
var backupResolver protodesc.Resolver
var fileSet descriptorpb.FileDescriptorSet
if err := proto.Unmarshal(data, &fileSet); err != nil {
backupResolver = &errResolver{err}
} else if files, err := protodesc.NewFiles(&fileSet); err != nil {
backupResolver = &errResolver{err}
} else {
backupResolver = files
}
return &combinedResolver{
first: protoregistry.GlobalFiles,
second: backupResolver,
}
}
type combinedResolver struct {
first, second protodesc.Resolver
}
func (r *combinedResolver) FindFileByPath(s string) (protoreflect.FileDescriptor, error) {
file, err := r.first.FindFileByPath(s)
if errors.Is(err, protoregistry.NotFound) {
file, err = r.second.FindFileByPath(s)
}
return file, err
}
func (r *combinedResolver) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
desc, err := r.first.FindDescriptorByName(name)
if errors.Is(err, protoregistry.NotFound) {
desc, err = r.second.FindDescriptorByName(name)
}
return desc, err
}
type errResolver struct {
err error
}
func (r *errResolver) FindFileByPath(_ string) (protoreflect.FileDescriptor, error) {
return nil, r.err
}
func (r *errResolver) FindDescriptorByName(_ protoreflect.FullName) (protoreflect.Descriptor, error) {
return nil, r.err
}