-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathhandle_schema_registry.go
More file actions
848 lines (750 loc) · 25.6 KB
/
handle_schema_registry.go
File metadata and controls
848 lines (750 loc) · 25.6 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// Copyright 2023 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0
package api
import (
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/cloudhut/common/rest"
"github.com/twmb/franz-go/pkg/sr"
"github.com/redpanda-data/console/backend/pkg/console"
)
// createSchemaRequest defines the expected JSON body to create a schema.
type createSchemaRequest struct {
sr.Schema
Params struct {
Normalize bool `json:"normalize"`
} `json:"params"`
}
func (api *API) handleSchemaRegistryNotConfigured() http.HandlerFunc {
type response struct {
IsConfigured bool `json:"isConfigured"`
}
return func(w http.ResponseWriter, r *http.Request) {
rest.SendResponse(w, r, api.Logger, http.StatusOK, &response{IsConfigured: false})
}
}
func (api *API) handleGetSchemaRegistryMode() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
res, err := api.ConsoleSvc.GetSchemaRegistryMode(r.Context(), "")
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve schema registry mode from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleGetSchemaRegistrySchemaTypes() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
res, err := api.ConsoleSvc.GetSchemaRegistrySchemaTypes(r.Context())
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve schema registry types from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleGetSchemaUsagesByID() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse and validate version input
schemaIDStr := rest.GetURLParam(r, "id")
schemaID, err := strconv.Atoi(schemaIDStr)
if err != nil {
descriptiveErr := fmt.Errorf("schema id %q is not valid. Must be a positive integer", schemaIDStr)
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: descriptiveErr,
Status: http.StatusBadRequest,
Message: descriptiveErr.Error(),
IsSilent: false,
})
return
}
subject := rest.GetQueryParam(r, "subject")
res, err := api.ConsoleSvc.GetSchemaUsagesByID(r.Context(), schemaID, subject)
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve schema usages for schema id: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleGetSchemaRegistryConfig() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
res, err := api.ConsoleSvc.GetSchemaRegistryConfig(r.Context(), "")
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve schema registry types from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handlePutSchemaRegistryConfig() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
type request struct {
Compatibility sr.CompatibilityLevel `json:"compatibility"`
}
return func(w http.ResponseWriter, r *http.Request) {
req := request{}
restErr := rest.Decode(w, r, &req)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
res, err := api.ConsoleSvc.PutSchemaRegistryConfig(r.Context(), "", sr.SetCompatibility{Level: req.Compatibility})
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to set global compatibility level: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handlePutSchemaRegistrySubjectConfig() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
type request struct {
Compatibility sr.CompatibilityLevel `json:"compatibility"`
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request parameters
subjectName := getSubjectFromRequestPath(r)
req := request{}
restErr := rest.Decode(w, r, &req)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
// 2. Set subject compatibility level
res, err := api.ConsoleSvc.PutSchemaRegistryConfig(r.Context(), subjectName, sr.SetCompatibility{Level: req.Compatibility})
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to set subject's compatibility level: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleDeleteSchemaRegistrySubjectConfig() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request parameters
subjectName := getSubjectFromRequestPath(r)
// 2. Set subject compatibility level
err := api.ConsoleSvc.DeleteSchemaRegistrySubjectConfig(r.Context(), subjectName)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to delete subject's compatibility level: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, nil)
}
}
func (api *API) handleGetSchemaRegistrySubjectConfig() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
subjectName := getSubjectFromRequestPath(r)
res, err := api.ConsoleSvc.GetSchemaRegistryConfig(r.Context(), subjectName)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve subject's compatibility level: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleGetSchemaRegistrySubjectMode() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
subjectName := getSubjectFromRequestPath(r)
res, err := api.ConsoleSvc.GetSchemaRegistryMode(r.Context(), subjectName)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve subject's mode: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handlePutSchemaRegistryMode() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
type request struct {
Mode sr.Mode `json:"mode"`
}
return func(w http.ResponseWriter, r *http.Request) {
req := request{}
restErr := rest.Decode(w, r, &req)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
res, err := api.ConsoleSvc.PutSchemaRegistryMode(r.Context(), req.Mode, "")
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to set global mode: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handlePutSchemaRegistrySubjectMode() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
type request struct {
Mode sr.Mode `json:"mode"`
}
return func(w http.ResponseWriter, r *http.Request) {
subjectName := getSubjectFromRequestPath(r)
req := request{}
restErr := rest.Decode(w, r, &req)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
res, err := api.ConsoleSvc.PutSchemaRegistryMode(r.Context(), req.Mode, subjectName)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to set subject's mode: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleDeleteSchemaRegistrySubjectMode() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
subjectName := getSubjectFromRequestPath(r)
err := api.ConsoleSvc.DeleteSchemaRegistrySubjectMode(r.Context(), subjectName)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to delete subject's mode: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, nil)
}
}
func (api *API) handleGetSchemaRegistryContexts() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
contexts, err := api.ConsoleSvc.GetSchemaRegistryContexts(r.Context())
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve contexts from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, contexts)
}
}
func (api *API) handleGetSchemaSubjects() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
subjectPrefix := rest.GetQueryParam(r, "subjectPrefix")
res, err := api.ConsoleSvc.GetSchemaRegistrySubjects(r.Context(), subjectPrefix)
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve subjects from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleGetSchemaSubjectDetails() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request params
subjectName := getSubjectFromRequestPath(r)
// 2. Parse and validate version input
version := rest.GetURLParam(r, "version")
switch version {
case console.SchemaVersionsAll, console.SchemaVersionsLatest:
default:
// Must be number or it's invalid input
_, err := strconv.Atoi(version)
if err != nil {
descriptiveErr := fmt.Errorf("version %q is not valid. Must be %q, %q or a positive integer", version, console.SchemaVersionsLatest, console.SchemaVersionsAll)
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: descriptiveErr,
Status: http.StatusBadRequest,
Message: descriptiveErr.Error(),
IsSilent: false,
})
return
}
}
// 3. Get all subjects' details
res, err := api.ConsoleSvc.GetSchemaRegistrySubjectDetails(r.Context(), subjectName, version)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve subjects from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleGetSchemaReferencedBy() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request params
subjectName := getSubjectFromRequestPath(r)
// 2. Parse and validate version input
versionStr := rest.GetURLParam(r, "version")
version, restErr := parseVersionStr(versionStr)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
// 3. Get all subjects' details
res, err := api.ConsoleSvc.GetSchemaRegistrySchemaReferencedBy(r.Context(), subjectName, version)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusBadGateway,
Message: fmt.Sprintf("Failed to retrieve schema references from the schema registry: %v", err.Error()),
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleDeleteSubject() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request parameters
subjectName := getSubjectFromRequestPath(r)
deletePermanentlyStr := rest.GetQueryParam(r, "permanent")
if deletePermanentlyStr == "" {
deletePermanentlyStr = "false"
}
deletePermanently, err := strconv.ParseBool(deletePermanentlyStr)
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: fmt.Errorf("failed to parse deletePermanently query param %q: %w", deletePermanentlyStr, err),
Status: http.StatusBadRequest,
Message: fmt.Sprintf("Failed to parse 'permanent' query param with value %q: %v", deletePermanentlyStr, err.Error()),
IsSilent: false,
})
return
}
// 2. Send delete request
res, err := api.ConsoleSvc.DeleteSchemaRegistrySubject(r.Context(), subjectName, deletePermanently)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusServiceUnavailable,
Message: fmt.Sprintf("Failed to delete schema registry subject: %v", err.Error()),
InternalLogs: []slog.Attr{slog.String("subject_name", subjectName)},
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func parseVersionStr(versionStr string) (int, *rest.Error) {
if versionStr == console.SchemaVersionsLatest {
return -1, nil
}
// Must be number or it's invalid input
version, err := strconv.Atoi(versionStr)
if err != nil {
descriptiveErr := fmt.Errorf("version %q is not valid. Must be %q or a positive integer", versionStr, console.SchemaVersionsLatest)
return 0, &rest.Error{
Err: descriptiveErr,
Status: http.StatusBadRequest,
Message: descriptiveErr.Error(),
IsSilent: false,
}
}
return version, nil
}
func (api *API) handleDeleteSubjectVersion() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request parameters
subjectName := getSubjectFromRequestPath(r)
versionStr := rest.GetURLParam(r, "version")
version, restErr := parseVersionStr(versionStr)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
deletePermanentlyStr := rest.GetQueryParam(r, "permanent")
if deletePermanentlyStr == "" {
deletePermanentlyStr = "false"
}
deletePermanently, err := strconv.ParseBool(deletePermanentlyStr)
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: fmt.Errorf("failed to parse deletePermanently query param %q: %w", deletePermanentlyStr, err),
Status: http.StatusBadRequest,
Message: fmt.Sprintf("Failed to parse 'permanent' query param with value %q: %v", deletePermanentlyStr, err.Error()),
IsSilent: false,
})
return
}
// 2. Send delete request
res, err := api.ConsoleSvc.DeleteSchemaRegistrySubjectVersion(r.Context(), subjectName, version, deletePermanently)
if err != nil {
var schemaError *sr.ResponseError
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40401 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested subject does not exist",
IsSilent: false,
})
return
}
if errors.As(err, &schemaError) && schemaError.ErrorCode == 40402 {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusNotFound,
Message: "Requested version does not exist on the given subject",
IsSilent: false,
})
return
}
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusServiceUnavailable,
Message: fmt.Sprintf("Failed to delete schema registry subject version: %v", err.Error()),
InternalLogs: []slog.Attr{
slog.String("subject_name", subjectName),
slog.Int("version", version),
},
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleCreateSchema() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request parameters
subjectName := getSubjectFromRequestPath(r)
var payload createSchemaRequest
restErr := rest.Decode(w, r, &payload)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
if payload.Schema.Schema == "" {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: errors.New("payload validation failed for creating schema"),
Status: http.StatusBadRequest,
Message: "You must set the schema field when creating a new schema",
InternalLogs: []slog.Attr{slog.String("subject_name", subjectName)},
IsSilent: false,
})
return
}
// 2. Send create request
res, err := api.ConsoleSvc.CreateSchemaRegistrySchema(r.Context(), subjectName, payload.Schema, payload.Params)
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: err,
Status: http.StatusServiceUnavailable,
Message: fmt.Sprintf("Failed to create schema: %v", err.Error()),
InternalLogs: []slog.Attr{slog.String("subject_name", subjectName)},
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func (api *API) handleValidateSchema() http.HandlerFunc {
if !api.Cfg.SchemaRegistry.Enabled {
return api.handleSchemaRegistryNotConfigured()
}
return func(w http.ResponseWriter, r *http.Request) {
// 1. Parse request parameters
subjectName := getSubjectFromRequestPath(r)
versionStr := rest.GetURLParam(r, "version")
version, restErr := parseVersionStr(versionStr)
if restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
var payload sr.Schema
if restErr := rest.Decode(w, r, &payload); restErr != nil {
rest.SendRESTError(w, r, api.Logger, restErr)
return
}
if payload.Schema == "" {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: errors.New("payload validation failed for validating schema"),
Status: http.StatusBadRequest,
Message: "You must set the schema field when validating the schema",
InternalLogs: []slog.Attr{slog.String("subject_name", subjectName)},
IsSilent: false,
})
return
}
// 2. Send validate request
res, err := api.ConsoleSvc.ValidateSchemaRegistrySchema(r.Context(), subjectName, version, sr.Schema{
Schema: payload.Schema,
Type: payload.Type,
References: payload.References,
})
if err != nil {
rest.SendRESTError(w, r, api.Logger, &rest.Error{
Err: fmt.Errorf("failed validating schema: %w", err),
Status: http.StatusBadRequest,
Message: fmt.Sprintf("Failed validating schema: %v", err.Error()),
InternalLogs: []slog.Attr{slog.String("subject_name", subjectName)},
IsSilent: false,
})
return
}
rest.SendResponse(w, r, api.Logger, http.StatusOK, res)
}
}
func getSubjectFromRequestPath(r *http.Request) string {
// Subject extraction is a little tricky.
// Subjects can have characters such as "/" and "%"".
// Subjects are passed to the router as part of path parameter, URL escaped.
// However, there seems to be _some_ unescaping within Go's HTTP request itself.
// If a subject is "%2F", the subject path parameter would be "%252F".
// With an HTTP request path like "/api/schema-registry/subjects/%252F/versions/latest"
// Go's HTTP request.URL.Path is "/api/schemas/schema-registry/%2F/versions/latest",
// which is already unescaped.
// While Go HTTP request.RequestURI is "/api/schemas/schema-registry/%252F/versions/latest"
// Which is the raw version that the client sent.
// That means that if we use rest.GetURLParam(),
// it first calls chi.URLParam() to get the path param, which returns "%2F"
// Which then url.PathUnescape() within rest.GetURLParam() to finally return "/".
// which is "double escaped" and _not_ what we want.
// So that means we have to manually extract and unescape the subject part ourselves
// from the raw path in request.RequestURI.
// See url.setPath() and parse() functions in url.go.
requestURI := r.RequestURI
// remove the api route prefix
requestURI = strings.Replace(requestURI, r.URL.Scheme+"://", "", 1)
requestURI = strings.Replace(requestURI, r.Host, "", 1)
requestURI = strings.Replace(requestURI, "/api/schema-registry/subjects/", "", 1)
requestURI = strings.Replace(requestURI, "/api/schema-registry/config/", "", 1)
requestURI = strings.Replace(requestURI, "/api/schema-registry/mode/", "", 1)
if r.URL.RawQuery != "" {
requestURI = strings.TrimSuffix(requestURI, "?"+r.URL.RawQuery)
}
// find the versions suffix of the path
subjectPart := requestURI
lastIndex := strings.Index(requestURI, "/")
if lastIndex > 0 { // satisfy linter
// and extract the subject at the start of the string
subjectPart = requestURI[:lastIndex]
}
// finally unescape
subject, err := url.PathUnescape(subjectPart)
if err != nil {
subject = subjectPart
}
return subject
}