-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdump.go
More file actions
1656 lines (1478 loc) · 41.2 KB
/
dump.go
File metadata and controls
1656 lines (1478 loc) · 41.2 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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dump
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"github.com/blang/semver/v4"
"github.com/kong/go-database-reconciler/pkg/schema"
"github.com/kong/go-database-reconciler/pkg/utils"
"github.com/kong/go-kong/kong"
"github.com/kong/go-kong/kong/custom"
"golang.org/x/sync/errgroup"
)
const (
// SelectTag represents the select tags
SelectTag = iota
// DefaultLookupTag represents the lookup selector tags
DefaultLookupTag
)
// Config can be used to skip exporting certain entities
type Config struct {
// If true, only RBAC resources are exported.
// SkipConsumers and SelectorTags should be falsy when this is set.
RBACResourcesOnly bool
// If true, consumers would not show associated consumer-groups
SkipConsumersWithConsumerGroups bool
// If true, consumers and any plugins associated with it
// are not exported.
SkipConsumers bool
// If true, CA certificates are not exported.
SkipCACerts bool
// If true, licenses are exported.
IncludeLicenses bool
// CustomEntityTypes lists types of custom entities to list.
CustomEntityTypes []string
SkipCustomEntitiesWithSelectorTags bool
// SelectorTags can be used to export entities tagged with only specific
// tags.
SelectorTags []string
// LookUpSelectorTags* can be used to ensure state lookup for entities using
// these tags. This functionality is essential when using a plugin that references
// consumers or routes associated with tags different from those in the sync command.
LookUpSelectorTagsConsumerGroups []string
LookUpSelectorTagsConsumers []string
LookUpSelectorTagsRoutes []string
LookUpSelectorTagsServices []string
LookUpSelectorTagsPartials []string
// KonnectControlPlane
KonnectControlPlane string
// IsConsumerGroupScopedPluginSupported
IsConsumerGroupScopedPluginSupported bool
// If IsPartialApply is true, we load _all_ existing entities in to
// the intermediate state so that foreign key lookups will work.
// Partial applies to configure a partial state.
IsPartialApply bool
// If IsConsumerGroupPolicyOverrideSet is true, we let users create
// policy-based overrides for RLA plugin
IsConsumerGroupPolicyOverrideSet bool
// This flag is specifically used for the `deck gateway dump` command.
// If true, the content of the entities is sanitized at deck level.
// We require it here to signal the Writer about the sanitization, so
// that referential integrity can be handled properly via IDs.
SanitizeContent bool
SkipHashForBasicAuth bool
// This flag is set to remove default values while dumping entities.
SkipDefaults bool
// SchemaRegistry is an optional shared schema registry. When provided,
// it is reused for schema fetching and caching (e.g. during SkipDefaults
// processing). When nil, a new registry is created internally.
SchemaRegistry *schema.Registry
}
func deduplicate(stringSlice []string) []string {
existing := map[string]struct{}{}
result := []string{}
for _, s := range stringSlice {
if _, exists := existing[s]; !exists {
existing[s] = struct{}{}
result = append(result, s)
}
}
return result
}
func newOpt(tags []string) *kong.ListOpt {
opt := new(kong.ListOpt)
opt.Size = 1000
opt.Tags = kong.StringSlice(deduplicate(tags)...)
opt.MatchAllTags = true
return opt
}
func validateConfig(config Config) error {
if config.RBACResourcesOnly {
if config.SkipConsumers {
return fmt.Errorf("dump: config: SkipConsumer cannot be set when RBACResourcesOnly is set")
}
if len(config.SelectorTags) != 0 {
return fmt.Errorf("dump: config: SelectorTags cannot be set when RBACResourcesOnly is set")
}
}
return nil
}
func isKongVersion34Plus(ctx context.Context, client *kong.Client, config Config) (bool, error) {
if config.KonnectControlPlane != "" {
return false, nil
}
info, err := client.Info.Get(ctx)
if err != nil {
return false, err
}
var cleanKongVersion semver.Version
cleanKongVersion, err = utils.ParseKongVersion(info.Version)
if err != nil {
return false, err
}
if utils.Kong340Version.LTE(cleanKongVersion) {
return true, nil
}
return false, nil
}
func getConsumerGroupsConfiguration(ctx context.Context, group *errgroup.Group,
client *kong.Client, config Config, state *utils.KongRawState,
) {
group.Go(func() error {
var consumerGroups []*kong.ConsumerGroupObject
var err error
isKongVersion34Plus, err := isKongVersion34Plus(ctx, client, config)
if err != nil {
return fmt.Errorf("error retrieving Kong version: %w", err)
}
// Define the function to be used based on
// whether we wish to see policy overrides or not
// GetAllConsumerGroups lists consumers as well as policy-based overrides for consumer-groups
getConsumerGroupsFunc := GetAllConsumerGroups
if !config.IsConsumerGroupPolicyOverrideSet && isKongVersion34Plus {
// This won't dump policy-based overrides for consumer-groups
getConsumerGroupsFunc = GetAllConsumerGroupsDefault
if config.SkipConsumersWithConsumerGroups {
getConsumerGroupsFunc = GetAllConsumerGroupsWithoutConsumersDefault
}
} else if config.SkipConsumersWithConsumerGroups {
getConsumerGroupsFunc = GetAllConsumerGroupsWithoutConsumers
}
// Passing config.SelectorTags here fetches only those consumer-groups (and inclusive consumers,
// where applicable) which are tagged with the same tag as provided in the config.SelectorTags.
// If config.SelectorTags is empty, all consumer-groups are fetched.
consumerGroups, err = getConsumerGroupsFunc(ctx, client, config.SelectorTags, SelectTag)
if err != nil {
if kong.IsNotFoundErr(err) || kong.IsForbiddenErr(err) {
return nil
}
return fmt.Errorf("consumer_groups: %w", err)
}
if config.LookUpSelectorTagsConsumerGroups != nil {
// Passing config.LookUpSelectorTagsConsumerGroups here fetches only those consumer-groups
// which are tagged with the same tag as provided in the config.LookUpSelectorTagsConsumerGroups.
// This doesn't apply to the consumers within the consumer-group; they will be fetched (wherever
// applicable) regardless of their tags.
// LookUpSelectorTagsConsumerGroups are useful when the config is distributed. If the config
// under process refers to a consumer-group which exists on the gateway but is not defined in the
// same config file, the presence of lookup tags will be able to fetch that consumer-group.
globalConsumerGroups, err := getConsumerGroupsFunc(ctx, client, config.LookUpSelectorTagsConsumerGroups,
DefaultLookupTag)
if err != nil {
return fmt.Errorf("error retrieving global consumer groups: %w", err)
}
// if globalConsumers are not present, add them.
for _, globalConsumerGroup := range globalConsumerGroups {
found := false
for _, consumerGroup := range consumerGroups {
if *globalConsumerGroup.ConsumerGroup.ID == *consumerGroup.ConsumerGroup.ID {
found = true
break
}
}
if !found {
consumerGroups = append(consumerGroups, globalConsumerGroup)
}
}
}
state.ConsumerGroups = consumerGroups
return nil
})
}
func getConsumerConfiguration(ctx context.Context, group *errgroup.Group,
client *kong.Client, config Config, state *utils.KongRawState,
) {
group.Go(func() error {
consumers, err := GetAllConsumers(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("consumers: %w", err)
}
if config.LookUpSelectorTagsConsumers != nil {
globalConsumers, err := GetAllConsumers(ctx, client, config.LookUpSelectorTagsConsumers)
if err != nil {
return fmt.Errorf("error retrieving global consumers: %w", err)
}
// if globalConsumers are not present, add them.
for _, globalConsumer := range globalConsumers {
found := false
for _, consumer := range consumers {
if *globalConsumer.ID == *consumer.ID {
found = true
break
}
}
if !found {
consumers = append(consumers, globalConsumer)
}
}
}
state.Consumers = consumers
return nil
})
group.Go(func() error {
keyAuths, err := GetAllKeyAuths(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("key-auths: %w", err)
}
state.KeyAuths = keyAuths
return nil
})
group.Go(func() error {
hmacAuths, err := GetAllHMACAuths(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("hmac-auths: %w", err)
}
state.HMACAuths = hmacAuths
return nil
})
group.Go(func() error {
jwtAuths, err := GetAllJWTAuths(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("jwts: %w", err)
}
state.JWTAuths = jwtAuths
return nil
})
group.Go(func() error {
basicAuths, err := GetAllBasicAuths(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("basic-auths: %w", err)
}
var options []*kong.BasicAuthOptions
for _, basicAuth := range basicAuths {
option := &kong.BasicAuthOptions{
BasicAuth: *basicAuth,
}
options = append(options, option)
}
state.BasicAuths = options
return nil
})
// OAuth2 credentials are not supported in Konnect.
if config.KonnectControlPlane == "" {
group.Go(func() error {
oauth2Creds, err := GetAllOauth2Creds(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("oauth2: %w", err)
}
state.Oauth2Creds = oauth2Creds
return nil
})
}
group.Go(func() error {
aclGroups, err := GetAllACLGroups(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("acls: %w", err)
}
state.ACLGroups = aclGroups
return nil
})
group.Go(func() error {
// XXX Select-tags based filtering is not performed for mTLS-auth credentials
// because of the following problems:
// - We currently do not already tag these credentials, filtering these
// credentials with tags will break any existing user
// - this is not a big issue since only mTLS-auth credentials for tagged
// consumers are exported anyway
// This feature would only benefit a user who uses tagged consumers but
// then managed mtls-auth credentials out-of-band. We expect such users
// to be rare or non-existent.
mtlsAuths, err := GetAllMTLSAuths(ctx, client, nil)
if err != nil {
return fmt.Errorf("mtls-auths: %w", err)
}
state.MTLSAuths = mtlsAuths
return nil
})
}
func getProxyConfiguration(ctx context.Context, group *errgroup.Group,
client *kong.Client, config Config, state *utils.KongRawState,
) {
group.Go(func() error {
services, err := GetAllServices(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("services: %w", err)
}
services, err = excludeKonnectManagedEntities(services)
if err != nil {
return fmt.Errorf("services: %w", err)
}
if config.LookUpSelectorTagsServices != nil {
globalServices, err := GetAllServices(ctx, client, config.LookUpSelectorTagsServices)
if err != nil {
return fmt.Errorf("error retrieving global services: %w", err)
}
// if globalServices are not present, add them.
for _, globalService := range globalServices {
found := false
for _, service := range services {
if *globalService.ID == *service.ID {
found = true
break
}
}
if !found {
services = append(services, globalService)
}
}
}
state.Services = services
return nil
})
group.Go(func() error {
routes, err := GetAllRoutes(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("routes: %w", err)
}
routes, err = excludeKonnectManagedEntities(routes)
if err != nil {
return fmt.Errorf("routes: %w", err)
}
if config.LookUpSelectorTagsRoutes != nil {
globalRoutes, err := GetAllRoutes(ctx, client, config.LookUpSelectorTagsRoutes)
if err != nil {
return fmt.Errorf("error retrieving global routes: %w", err)
}
// if globalRoutes are not present, add them.
for _, globalRoute := range globalRoutes {
found := false
for _, route := range routes {
if *globalRoute.ID == *route.ID {
found = true
break
}
}
if !found {
routes = append(routes, globalRoute)
}
}
}
state.Routes = routes
return nil
})
group.Go(func() error {
plugins, err := GetAllPlugins(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("plugins: %w", err)
}
plugins = excludeKonnectManagedPlugins(plugins)
if config.SkipConsumers {
plugins = excludeConsumersPlugins(plugins)
plugins = excludeConsumerGroupsPlugins(plugins)
}
state.Plugins = plugins
return nil
})
group.Go(func() error {
state.FilterChains = make([]*kong.FilterChain, 0)
filterChains, err := GetAllFilterChains(ctx, client, config.SelectorTags)
if err != nil {
var kongErr *kong.APIError
if errors.As(err, &kongErr) {
// GET /filter-chains returns:
// -> 200 on success
// -> 404 if Kong version < 3.4
// -> 400 if Kong version >= 3.4 but wasm is not enabled
if kongErr.Code() == http.StatusNotFound || kongErr.Code() == http.StatusBadRequest {
return nil
}
}
return fmt.Errorf("filter chains: %w", err)
}
filterChains, err = excludeKonnectManagedEntities(filterChains)
if err != nil {
return fmt.Errorf("filter chains: %w", err)
}
state.FilterChains = filterChains
return nil
})
group.Go(func() error {
certificates, err := GetAllCertificates(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("certificates: %w", err)
}
certificates, err = excludeKonnectManagedEntities(certificates)
if err != nil {
return fmt.Errorf("certificates: %w", err)
}
state.Certificates = certificates
return nil
})
if !config.SkipCACerts {
group.Go(func() error {
caCerts, err := GetAllCACertificates(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("ca-certificates: %w", err)
}
caCerts, err = excludeKonnectManagedEntities(caCerts)
if err != nil {
return fmt.Errorf("ca-certificates: %w", err)
}
state.CACertificates = caCerts
return nil
})
}
group.Go(func() error {
snis, err := GetAllSNIs(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("snis: %w", err)
}
snis, err = excludeKonnectManagedEntities(snis)
if err != nil {
return fmt.Errorf("snis: %w", err)
}
state.SNIs = snis
return nil
})
group.Go(func() error {
upstreams, err := GetAllUpstreams(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("upstreams: %w", err)
}
upstreams, err = excludeKonnectManagedEntities(upstreams)
if err != nil {
return fmt.Errorf("upstreams: %w", err)
}
state.Upstreams = upstreams
if config.KonnectControlPlane == "" {
targets, err := GetAllTargets(ctx, client, upstreams, config.SelectorTags)
if err != nil {
return fmt.Errorf("targets: %w", err)
}
state.Targets = targets
}
return nil
})
if config.KonnectControlPlane != "" {
group.Go(func() error {
targets, err := GetAllTargetsFromKonnect(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("targets: %w", err)
}
targets, err = excludeKonnectManagedEntities(targets)
if err != nil {
return fmt.Errorf("targets: %w", err)
}
state.Targets = targets
return nil
})
}
group.Go(func() error {
vaults, err := GetAllVaults(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("vaults: %w", err)
}
vaults, err = excludeKonnectManagedEntities(vaults)
if err != nil {
return fmt.Errorf("vaults: %w", err)
}
state.Vaults = vaults
return nil
})
group.Go(func() error {
partials, err := GetAllPartials(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("partials: %w", err)
}
partials, err = excludeKonnectManagedEntities(partials)
if err != nil {
return fmt.Errorf("partials: %w", err)
}
if config.LookUpSelectorTagsPartials != nil {
globalPartials, err := GetAllPartials(ctx, client, config.LookUpSelectorTagsPartials)
if err != nil {
return fmt.Errorf("error retrieving global partials: %w", err)
}
// if globalPartials are not present, add them.
for _, globalPartial := range globalPartials {
found := false
for _, partial := range partials {
if *globalPartial.ID == *partial.ID {
found = true
break
}
}
if !found {
partials = append(partials, globalPartial)
}
}
}
state.Partials = partials
return nil
})
group.Go(func() error {
keys, err := GetAllKeys(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("keys: %w", err)
}
keys, err = excludeKonnectManagedEntities(keys)
if err != nil {
return fmt.Errorf("keys: %w", err)
}
state.Keys = keys
return nil
})
group.Go(func() error {
keySets, err := GetAllKeySets(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("key-sets: %w", err)
}
keySets, err = excludeKonnectManagedEntities(keySets)
if err != nil {
return fmt.Errorf("key-sets: %w", err)
}
state.KeySets = keySets
return nil
})
if config.IncludeLicenses {
group.Go(func() error {
licenses, err := GetAllLicenses(ctx, client, config.SelectorTags)
if err != nil {
return fmt.Errorf("licenses: %w", err)
}
licenses, err = excludeKonnectManagedEntities(licenses)
if err != nil {
return fmt.Errorf("licenses: %w", err)
}
state.Licenses = licenses
return nil
})
}
// If SkipCustomEntitiesWithSelectorTags is true and SelectorTags is not empty,
// we want to skip custom entities. This is because custom entities don't support
// tagging and including them in the state results in errors while attempting a
// deck sync or apply.
var skipCustomEntities bool
if config.SkipCustomEntitiesWithSelectorTags && len(config.SelectorTags) > 0 {
skipCustomEntities = true
}
if !skipCustomEntities && len(config.CustomEntityTypes) > 0 {
// Register all entity types first (sequentially) to avoid data race on the registry map.
// The registry is not thread-safe, so we must complete all registrations before
// starting concurrent fetch operations that call Lookup().
for _, entityType := range config.CustomEntityTypes {
if err := tryRegisterEntityType(client, custom.Type(entityType)); err != nil {
group.Go(func() error {
return fmt.Errorf("custom entity %s: %w", entityType, err)
})
continue
}
}
customEntityLock := sync.Mutex{}
for _, entityType := range config.CustomEntityTypes {
t := entityType
group.Go(func() error {
// Fetch all entities with the given type.
entities, err := GetAllCustomEntitiesWithType(ctx, client, t)
if err != nil {
return fmt.Errorf("custom entity %s: %w", t, err)
}
// Add custom entities to rawstate.
customEntityLock.Lock()
state.CustomEntities = append(state.CustomEntities, entities...)
customEntityLock.Unlock()
return nil
})
}
}
}
func tryRegisterEntityType(client *kong.Client, typ custom.Type) error {
if client.Lookup(typ) != nil {
return nil
}
// Determine the CRUD path based on entity type
crudPath := "/" + string(typ)
// Special case: Kong exposes this API at /graphql-rate-limiting-advanced/costs,
// not at /graphql_ratelimiting_cost_decorations (the entity type name).
if typ == "graphql_ratelimiting_cost_decorations" {
crudPath = "/graphql-rate-limiting-advanced/costs"
}
return client.Register(typ, &custom.EntityCRUDDefinition{
Name: typ,
CRUDPath: crudPath,
PrimaryKey: "id",
})
}
func getEnterpriseRBACConfiguration(ctx context.Context, group *errgroup.Group,
client *kong.Client, state *utils.KongRawState,
) {
group.Go(func() error {
roles, err := GetAllRBACRoles(ctx, client)
if err != nil {
return fmt.Errorf("roles: %w", err)
}
state.RBACRoles = roles
return nil
})
group.Go(func() error {
eps, err := GetAllRBACREndpointPermissions(ctx, client)
if err != nil {
return fmt.Errorf("eps: %w", err)
}
state.RBACEndpointPermissions = eps
return nil
})
}
// Get queries all the entities using client and returns
// all the entities in KongRawState.
func Get(ctx context.Context, client *kong.Client, config Config) (*utils.KongRawState, error) {
var state utils.KongRawState
if err := validateConfig(config); err != nil {
return nil, err
}
group, newCtx := errgroup.WithContext(ctx)
// dump only rbac resources
if config.RBACResourcesOnly {
getEnterpriseRBACConfiguration(newCtx, group, client, &state)
} else {
// regular case
getProxyConfiguration(newCtx, group, client, config, &state)
if !config.SkipConsumers {
getConsumerGroupsConfiguration(newCtx, group, client, config, &state)
getConsumerConfiguration(newCtx, group, client, config, &state)
}
}
err := group.Wait()
if err != nil {
return nil, err
}
if config.SkipDefaults {
isKonnect := config.KonnectControlPlane != ""
registry := config.SchemaRegistry
if registry == nil {
registry = schema.NewRegistry(client, isKonnect)
}
group, newCtx := errgroup.WithContext(ctx)
RemoveDefaultsFromState(newCtx, group, &state, registry)
err := group.Wait()
if err != nil {
return nil, err
}
}
return &state, nil
}
// GetAllKeys queries Kong for all the Keys using client.
func GetAllKeys(
ctx context.Context, client *kong.Client, tags []string,
) ([]*kong.Key, error) {
var keys []*kong.Key
opt := newOpt(tags)
for {
s, nextopt, err := client.Keys.List(ctx, opt)
if kong.IsNotFoundErr(err) || kong.IsForbiddenErr(err) {
return keys, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
keys = append(keys, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return keys, nil
}
// GetAllKeySets queries Kong for all the KeySets using client.
func GetAllKeySets(
ctx context.Context, client *kong.Client, tags []string,
) ([]*kong.KeySet, error) {
var sets []*kong.KeySet
opt := newOpt(tags)
for {
s, nextopt, err := client.KeySets.List(ctx, opt)
if kong.IsNotFoundErr(err) || kong.IsForbiddenErr(err) {
return sets, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
sets = append(sets, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return sets, nil
}
// GetAllPartials queries Kong for all the partials using client.
func GetAllPartials(ctx context.Context, client *kong.Client,
tags []string,
) ([]*kong.Partial, error) {
var partials []*kong.Partial
opt := newOpt(tags)
for {
s, nextopt, err := client.Partials.List(ctx, opt)
if kong.IsNotFoundErr(err) || kong.IsForbiddenErr(err) {
return partials, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
partials = append(partials, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return partials, nil
}
// GetAllServices queries Kong for all the services using client.
func GetAllServices(ctx context.Context, client *kong.Client,
tags []string,
) ([]*kong.Service, error) {
var services []*kong.Service
opt := newOpt(tags)
for {
s, nextopt, err := client.Services.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
services = append(services, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return services, nil
}
// GetAllRoutes queries Kong for all the routes using client.
func GetAllRoutes(ctx context.Context, client *kong.Client,
tags []string,
) ([]*kong.Route, error) {
var routes []*kong.Route
opt := newOpt(tags)
for {
s, nextopt, err := client.Routes.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
routes = append(routes, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return routes, nil
}
// GetAllPlugins queries Kong for all the plugins using client.
func GetAllPlugins(ctx context.Context,
client *kong.Client, tags []string,
) ([]*kong.Plugin, error) {
var plugins []*kong.Plugin
opt := newOpt(tags)
for {
s, nextopt, err := client.Plugins.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
plugins = append(plugins, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return plugins, nil
}
// GetAllFilterChains queries Kong for all the filter chains using client.
func GetAllFilterChains(ctx context.Context,
client *kong.Client, tags []string,
) ([]*kong.FilterChain, error) {
var filterChains []*kong.FilterChain
opt := newOpt(tags)
for {
s, nextopt, err := client.FilterChains.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
filterChains = append(filterChains, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return filterChains, nil
}
// GetAllCertificates queries Kong for all the certificates using client.
func GetAllCertificates(ctx context.Context, client *kong.Client,
tags []string,
) ([]*kong.Certificate, error) {
var certificates []*kong.Certificate
opt := newOpt(tags)
for {
s, nextopt, err := client.Certificates.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
for _, cert := range s {
c := cert
c.SNIs = nil
certificates = append(certificates, cert)
}
if nextopt == nil {
break
}
opt = nextopt
}
return certificates, nil
}
// GetAllCACertificates queries Kong for all the CACertificates using client.
func GetAllCACertificates(ctx context.Context,
client *kong.Client,
tags []string,
) ([]*kong.CACertificate, error) {
var caCertificates []*kong.CACertificate
opt := newOpt(tags)
for {
s, nextopt, err := client.CACertificates.List(ctx, opt)
// Compatibility for Kong < 1.3
// This core entitiy was not present in the past
// and the Admin API request will error with 404 Not Found
// If we do get the error, we return back an empty array of
// CACertificates, effectively disabling the entity for versions
// which don't have it.
// A better solution would be to have a version check, and based
// on the version, the entities are loaded and synced.
if err != nil {
if kong.IsNotFoundErr(err) {
return caCertificates, nil
}
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
caCertificates = append(caCertificates, s...)
if nextopt == nil {
break