-
Notifications
You must be signed in to change notification settings - Fork 658
/
protobuf.go
512 lines (484 loc) · 16.7 KB
/
protobuf.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
* Copyright 2022 Confluent Inc.
*
* 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 protobuf
import (
"encoding/binary"
"fmt"
"io"
"log"
"strings"
"sync"
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry"
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/cache"
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/confluent"
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/confluent/types"
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/serde"
protoV1 "github.com/golang/protobuf/proto"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/desc/protoprint"
"google.golang.org/genproto/googleapis/type/calendarperiod"
"google.golang.org/genproto/googleapis/type/color"
"google.golang.org/genproto/googleapis/type/date"
"google.golang.org/genproto/googleapis/type/datetime"
"google.golang.org/genproto/googleapis/type/dayofweek"
"google.golang.org/genproto/googleapis/type/expr"
"google.golang.org/genproto/googleapis/type/fraction"
"google.golang.org/genproto/googleapis/type/latlng"
"google.golang.org/genproto/googleapis/type/money"
"google.golang.org/genproto/googleapis/type/month"
"google.golang.org/genproto/googleapis/type/postaladdress"
"google.golang.org/genproto/googleapis/type/quaternion"
"google.golang.org/genproto/googleapis/type/timeofday"
"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/known/anypb"
"google.golang.org/protobuf/types/known/apipb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/sourcecontextpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/typepb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// Serializer represents a Protobuf serializer
type Serializer struct {
serde.BaseSerializer
}
// Deserializer represents a Protobuf deserializer
type Deserializer struct {
serde.BaseDeserializer
ProtoRegistry *protoregistry.Types
schemaToDescCache cache.Cache
schemaToDescCacheLock sync.RWMutex
}
var _ serde.Serializer = new(Serializer)
var _ serde.Deserializer = new(Deserializer)
var builtInDeps = make(map[string]string)
func init() {
builtins := map[string]protoreflect.FileDescriptor{
"confluent/meta.proto": confluent.File_schemaregistry_confluent_meta_proto,
"confluent/type/decimal.proto": types.File_schemaregistry_confluent_type_decimal_proto,
"google/type/calendar_period.proto": calendarperiod.File_google_type_calendar_period_proto,
"google/type/color.proto": color.File_google_type_color_proto,
"google/type/date.proto": date.File_google_type_date_proto,
"google/type/datetime.proto": datetime.File_google_type_datetime_proto,
"google/type/dayofweek.proto": dayofweek.File_google_type_dayofweek_proto,
"google/type/expr.proto": expr.File_google_type_expr_proto,
"google/type/fraction.proto": fraction.File_google_type_fraction_proto,
"google/type/latlng.proto": latlng.File_google_type_latlng_proto,
"google/type/money.proto": money.File_google_type_money_proto,
"google/type/month.proto": month.File_google_type_month_proto,
"google/type/postal_address.proto": postaladdress.File_google_type_postal_address_proto,
"google/type/quaternion.proto": quaternion.File_google_type_quaternion_proto,
"google/type/timeofday.proto": timeofday.File_google_type_timeofday_proto,
"google/protobuf/any.proto": anypb.File_google_protobuf_any_proto,
"google/protobuf/api.proto": apipb.File_google_protobuf_api_proto,
"google/protobuf/descriptor.proto": descriptorpb.File_google_protobuf_descriptor_proto,
"google/protobuf/duration.proto": durationpb.File_google_protobuf_duration_proto,
"google/protobuf/empty.proto": emptypb.File_google_protobuf_empty_proto,
"google/protobuf/field_mask.proto": fieldmaskpb.File_google_protobuf_field_mask_proto,
"google/protobuf/source_context.proto": sourcecontextpb.File_google_protobuf_source_context_proto,
"google/protobuf/struct.proto": structpb.File_google_protobuf_struct_proto,
"google/protobuf/timestamp.proto": timestamppb.File_google_protobuf_timestamp_proto,
"google/protobuf/type.proto": typepb.File_google_protobuf_type_proto,
"google/protobuf/wrappers.proto": wrapperspb.File_google_protobuf_wrappers_proto,
}
var fds []*descriptorpb.FileDescriptorProto
for _, value := range builtins {
fd := protodesc.ToFileDescriptorProto(value)
fds = append(fds, fd)
}
fdMap, err := desc.CreateFileDescriptors(fds)
if err != nil {
log.Fatalf("Could not create fds")
}
printer := protoprint.Printer{OmitComments: protoprint.CommentsAll}
for key, value := range fdMap {
var writer strings.Builder
err = printer.PrintProtoFile(value, &writer)
if err != nil {
log.Fatalf("Could not print %s", key)
}
builtInDeps[key] = writer.String()
}
}
// NewSerializer creates a Protobuf serializer for Protobuf-generated objects
func NewSerializer(client schemaregistry.Client, serdeType serde.Type, conf *SerializerConfig) (*Serializer, error) {
s := &Serializer{}
err := s.ConfigureSerializer(client, serdeType, &conf.SerializerConfig)
if err != nil {
return nil, err
}
return s, nil
}
// ConfigureDeserializer configures the Protobuf deserializer
func (s *Deserializer) ConfigureDeserializer(client schemaregistry.Client, serdeType serde.Type, conf *serde.DeserializerConfig) error {
if client == nil {
return fmt.Errorf("schema registry client missing")
}
s.Client = client
s.Conf = conf
s.SerdeType = serdeType
s.SubjectNameStrategy = serde.TopicNameStrategy
s.MessageFactory = s.protoMessageFactory
s.ProtoRegistry = new(protoregistry.Types)
return nil
}
// Serialize implements serialization of Protobuf data
func (s *Serializer) Serialize(topic string, msg interface{}) ([]byte, error) {
if msg == nil {
return nil, nil
}
var protoMsg proto.Message
switch t := msg.(type) {
case proto.Message:
protoMsg = t
default:
return nil, fmt.Errorf("serialization target must be a protobuf message. Got '%v'", t)
}
autoRegister := s.Conf.AutoRegisterSchemas
normalize := s.Conf.NormalizeSchemas
fileDesc, deps, err := s.toProtobufSchema(protoMsg)
if err != nil {
return nil, err
}
metadata, err := s.resolveDependencies(fileDesc, deps, "", autoRegister, normalize)
if err != nil {
return nil, err
}
info := schemaregistry.SchemaInfo{
Schema: metadata.Schema,
SchemaType: metadata.SchemaType,
References: metadata.References,
}
id, err := s.GetID(topic, protoMsg, &info)
if err != nil {
return nil, err
}
msgIndexBytes := toMessageIndexBytes(protoMsg.ProtoReflect().Descriptor())
msgBytes, err := proto.Marshal(protoMsg)
if err != nil {
return nil, err
}
payload, err := s.WriteBytes(id, append(msgIndexBytes, msgBytes...))
if err != nil {
return nil, err
}
return payload, nil
}
func (s *Serializer) toProtobufSchema(msg proto.Message) (*desc.FileDescriptor, map[string]string, error) {
messageDesc, err := desc.LoadMessageDescriptorForMessage(protoV1.MessageV1(msg))
if err != nil {
return nil, nil, err
}
fileDesc := messageDesc.GetFile()
deps := make(map[string]string)
err = s.toDependencies(fileDesc, deps)
if err != nil {
return nil, nil, err
}
return fileDesc, deps, nil
}
func (s *Serializer) toDependencies(fileDesc *desc.FileDescriptor, deps map[string]string) error {
printer := protoprint.Printer{OmitComments: protoprint.CommentsAll}
var writer strings.Builder
err := printer.PrintProtoFile(fileDesc, &writer)
if err != nil {
return err
}
deps[fileDesc.GetName()] = writer.String()
for _, d := range fileDesc.GetDependencies() {
if ignoreFile(d.GetName()) {
continue
}
err = s.toDependencies(d, deps)
if err != nil {
return err
}
}
for _, d := range fileDesc.GetPublicDependencies() {
if ignoreFile(d.GetName()) {
continue
}
err = s.toDependencies(d, deps)
if err != nil {
return err
}
}
return nil
}
func (s *Serializer) resolveDependencies(fileDesc *desc.FileDescriptor, deps map[string]string, subject string, autoRegister bool, normalize bool) (schemaregistry.SchemaMetadata, error) {
refs := make([]schemaregistry.Reference, 0, len(fileDesc.GetDependencies())+len(fileDesc.GetPublicDependencies()))
for _, d := range fileDesc.GetDependencies() {
if ignoreFile(d.GetName()) {
continue
}
ref, err := s.resolveDependencies(d, deps, d.GetName(), autoRegister, normalize)
if err != nil {
return schemaregistry.SchemaMetadata{}, err
}
refs = append(refs, schemaregistry.Reference{d.GetName(), ref.Subject, ref.Version})
}
for _, d := range fileDesc.GetPublicDependencies() {
if ignoreFile(d.GetName()) {
continue
}
ref, err := s.resolveDependencies(d, deps, d.GetName(), autoRegister, normalize)
if err != nil {
return schemaregistry.SchemaMetadata{}, err
}
refs = append(refs, schemaregistry.Reference{d.GetName(), ref.Subject, ref.Version})
}
info := schemaregistry.SchemaInfo{
Schema: deps[fileDesc.GetName()],
SchemaType: "PROTOBUF",
References: refs,
}
var id = -1
var err error
var version = 0
if subject != "" {
if autoRegister {
id, err = s.Client.Register(subject, info, normalize)
if err != nil {
return schemaregistry.SchemaMetadata{}, err
}
} else {
id, err = s.Client.GetID(subject, info, normalize)
if err != nil {
return schemaregistry.SchemaMetadata{}, err
}
}
version, err = s.Client.GetVersion(subject, info, normalize)
if err != nil {
return schemaregistry.SchemaMetadata{}, err
}
}
metadata := schemaregistry.SchemaMetadata{
SchemaInfo: info,
ID: id,
Subject: subject,
Version: version,
}
return metadata, nil
}
func toMessageIndexBytes(descriptor protoreflect.Descriptor) []byte {
if descriptor.Index() == 0 {
switch descriptor.Parent().(type) {
case protoreflect.FileDescriptor:
// This is an optimization for the first message in the schema
return []byte{0}
}
}
msgIndexes := toMessageIndexes(descriptor, 0)
buf := make([]byte, (1+len(msgIndexes))*binary.MaxVarintLen64)
length := binary.PutVarint(buf, int64(len(msgIndexes)))
for _, element := range msgIndexes {
length += binary.PutVarint(buf[length:], int64(element))
}
return buf[0:length]
}
// Adapted from ideasculptor, see https://github.com/riferrei/srclient/issues/17
func toMessageIndexes(descriptor protoreflect.Descriptor, count int) []int {
index := descriptor.Index()
switch v := descriptor.Parent().(type) {
case protoreflect.FileDescriptor:
// parent is FileDescriptor, we reached the top of the stack, so we are
// done. Allocate an array large enough to hold count+1 entries and
// populate first value with index
msgIndexes := make([]int, count+1)
msgIndexes[0] = index
return msgIndexes[0:1]
default:
// parent is another MessageDescriptor. We were nested so get that
// descriptor's indexes and append the index of this one
msgIndexes := toMessageIndexes(v, count+1)
return append(msgIndexes, index)
}
}
func ignoreFile(name string) bool {
return strings.HasPrefix(name, "confluent/") ||
strings.HasPrefix(name, "google/protobuf/") ||
strings.HasPrefix(name, "google/type/")
}
// NewDeserializer creates a Protobuf deserializer for Protobuf-generated objects
func NewDeserializer(client schemaregistry.Client, serdeType serde.Type, conf *DeserializerConfig) (*Deserializer, error) {
cache, err := cache.NewLRUCache(1000)
if err != nil {
return nil, err
}
s := &Deserializer{
schemaToDescCache: cache,
}
err = s.ConfigureDeserializer(client, serdeType, &conf.DeserializerConfig)
if err != nil {
return nil, err
}
return s, nil
}
// Deserialize implements deserialization of Protobuf data
func (s *Deserializer) Deserialize(topic string, payload []byte) (interface{}, error) {
if payload == nil {
return nil, nil
}
info, err := s.GetSchema(topic, payload)
if err != nil {
return nil, err
}
fd, err := s.toFileDesc(info)
if err != nil {
return nil, err
}
bytesRead, msgIndexes, err := readMessageIndexes(payload[5:])
if err != nil {
return nil, err
}
messageDesc, err := toMessageDesc(fd, msgIndexes)
if err != nil {
return nil, err
}
subject, err := s.SubjectNameStrategy(topic, s.SerdeType, info)
if err != nil {
return nil, err
}
msg, err := s.MessageFactory(subject, messageDesc.GetFullyQualifiedName())
if err != nil {
return nil, err
}
var protoMsg proto.Message
switch t := msg.(type) {
case proto.Message:
protoMsg = t
default:
return nil, fmt.Errorf("deserialization target must be a protobuf message. Got '%v'", t)
}
err = proto.Unmarshal(payload[5+bytesRead:], protoMsg)
return protoMsg, err
}
// DeserializeInto implements deserialization of Protobuf data to the given object
func (s *Deserializer) DeserializeInto(topic string, payload []byte, msg interface{}) error {
if payload == nil {
return nil
}
var protoMsg proto.Message
switch t := msg.(type) {
case proto.Message:
protoMsg = t
default:
return fmt.Errorf("deserialization target must be a protobuf message. Got '%v'", t)
}
bytesRead, _, err := readMessageIndexes(payload[5:])
if err != nil {
return err
}
return proto.Unmarshal(payload[5+bytesRead:], protoMsg)
}
func (s *Deserializer) toFileDesc(info schemaregistry.SchemaInfo) (*desc.FileDescriptor, error) {
s.schemaToDescCacheLock.RLock()
value, ok := s.schemaToDescCache.Get(info.Schema)
s.schemaToDescCacheLock.RUnlock()
if ok {
return value.(*desc.FileDescriptor), nil
}
deps := make(map[string]string)
err := serde.ResolveReferences(s.Client, info, deps)
if err != nil {
return nil, err
}
parser := protoparse.Parser{
Accessor: func(filename string) (io.ReadCloser, error) {
var schema string
if filename == "." {
schema = info.Schema
} else {
schema = deps[filename]
}
if schema == "" {
schema = builtInDeps[filename]
}
return io.NopCloser(strings.NewReader(schema)), nil
},
}
fileDescriptors, err := parser.ParseFiles(".")
if err != nil {
return nil, err
}
if len(fileDescriptors) != 1 {
return nil, fmt.Errorf("could not resolve schema")
}
fd := fileDescriptors[0]
s.schemaToDescCacheLock.Lock()
s.schemaToDescCache.Put(info.Schema, fd)
s.schemaToDescCacheLock.Unlock()
return fd, nil
}
func readMessageIndexes(payload []byte) (int, []int, error) {
arrayLen, bytesRead := binary.Varint(payload)
if bytesRead <= 0 {
return bytesRead, nil, fmt.Errorf("unable to read message indexes")
}
if arrayLen < 0 {
return bytesRead, nil, fmt.Errorf("parsed invalid message index count")
}
if arrayLen == 0 {
// Handle the optimization for the first message in the schema
return bytesRead, []int{0}, nil
}
msgIndexes := make([]int, arrayLen)
for i := 0; i < int(arrayLen); i++ {
idx, read := binary.Varint(payload[bytesRead:])
if read <= 0 {
return bytesRead, nil, fmt.Errorf("unable to read message indexes")
}
bytesRead += read
msgIndexes[i] = int(idx)
}
return bytesRead, msgIndexes, nil
}
func toMessageDesc(descriptor desc.Descriptor, msgIndexes []int) (*desc.MessageDescriptor, error) {
index := msgIndexes[0]
switch v := descriptor.(type) {
case *desc.FileDescriptor:
if len(msgIndexes) == 1 {
return v.GetMessageTypes()[index], nil
}
return toMessageDesc(v.GetMessageTypes()[index], msgIndexes[1:])
case *desc.MessageDescriptor:
if len(msgIndexes) == 1 {
return v.GetNestedMessageTypes()[index], nil
}
return toMessageDesc(v.GetNestedMessageTypes()[index], msgIndexes[1:])
default:
return nil, fmt.Errorf("unexpected type")
}
}
func (s *Deserializer) protoMessageFactory(subject string, name string) (interface{}, error) {
mt, err := s.ProtoRegistry.FindMessageByName(protoreflect.FullName(name))
if mt == nil {
err = fmt.Errorf("unable to find MessageType %s", name)
}
if err != nil {
return nil, err
}
msg := mt.New()
return msg.Interface(), nil
}