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
|
package dump
import (
"context"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// Config can be used to skip exporting certain entities
type Config struct {
// If true, consumers and any plugins associated with it
// are not exported.
SkipConsumers bool
// SelectorTags can be used to export entities tagged with only specific
// tags.
SelectorTags []string
}
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
}
// Get queries all the entities using client and returns
// all the entities in KongRawState.
func Get(client *kong.Client, config Config) (*utils.KongRawState, error) {
var state utils.KongRawState
group, ctx := errgroup.WithContext(context.Background())
group.Go(func() error {
services, err := GetAllServices(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "services")
}
state.Services = services
return nil
})
group.Go(func() error {
routes, err := GetAllRoutes(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "routes")
}
state.Routes = routes
return nil
})
group.Go(func() error {
plugins, err := GetAllPlugins(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "plugins")
}
if config.SkipConsumers {
state.Plugins = excludeConsumersPlugins(plugins)
} else {
state.Plugins = plugins
}
return nil
})
group.Go(func() error {
certificates, err := GetAllCertificates(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "certificates")
}
state.Certificates = certificates
return nil
})
group.Go(func() error {
caCerts, err := GetAllCACertificates(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "ca-certificates")
}
state.CACertificates = caCerts
return nil
})
group.Go(func() error {
snis, err := GetAllSNIs(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "snis")
}
state.SNIs = snis
return nil
})
group.Go(func() error {
upstreams, err := GetAllUpstreams(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "upstreams")
}
state.Upstreams = upstreams
targets, err := GetAllTargets(ctx, client, upstreams, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "targets")
}
state.Targets = targets
return nil
})
if !config.SkipConsumers {
group.Go(func() error {
consumers, err := GetAllConsumers(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "consumers")
}
state.Consumers = consumers
return nil
})
group.Go(func() error {
keyAuths, err := GetAllKeyAuths(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "key-auths")
}
state.KeyAuths = keyAuths
return nil
})
group.Go(func() error {
hmacAuths, err := GetAllHMACAuths(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "hmac-auths")
}
state.HMACAuths = hmacAuths
return nil
})
group.Go(func() error {
jwtAuths, err := GetAllJWTAuths(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "jwts")
}
state.JWTAuths = jwtAuths
return nil
})
group.Go(func() error {
basicAuths, err := GetAllBasicAuths(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "basic-auths")
}
state.BasicAuths = basicAuths
return nil
})
group.Go(func() error {
oauth2Creds, err := GetAllOauth2Creds(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "oauth2")
}
state.Oauth2Creds = oauth2Creds
return nil
})
group.Go(func() error {
aclGroups, err := GetAllACLGroups(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "acls")
}
state.ACLGroups = aclGroups
return nil
})
group.Go(func() error {
mtlsAuths, err := GetAllMTLSAuths(ctx, client, config.SelectorTags)
if err != nil {
return errors.Wrap(err, "mtls-auths")
}
state.MTLSAuths = mtlsAuths
return nil
})
}
err := group.Wait()
if err != nil {
return nil, err
}
return &state, 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
}
// 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(nil, 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
}
opt = nextopt
}
return caCertificates, nil
}
// GetAllSNIs queries Kong for all the SNIs using client.
func GetAllSNIs(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.SNI, error) {
var snis []*kong.SNI
opt := newOpt(tags)
for {
s, nextopt, err := client.SNIs.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
snis = append(snis, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return snis, nil
}
// GetAllConsumers queries Kong for all the consumers using client.
// Please use this method with caution if you have a lot of consumers.
func GetAllConsumers(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.Consumer, error) {
var consumers []*kong.Consumer
opt := newOpt(tags)
for {
s, nextopt, err := client.Consumers.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
consumers = append(consumers, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return consumers, nil
}
// GetAllUpstreams queries Kong for all the Upstreams using client.
func GetAllUpstreams(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.Upstream, error) {
var upstreams []*kong.Upstream
opt := newOpt(tags)
for {
s, nextopt, err := client.Upstreams.List(ctx, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
upstreams = append(upstreams, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return upstreams, nil
}
// GetAllTargets queries Kong for all the Targets of upstreams using client.
// Targets are queries per upstream as there exists no endpoint in Kong
// to list all targets of all upstreams.
func GetAllTargets(ctx context.Context, client *kong.Client,
upstreams []*kong.Upstream, tags []string) ([]*kong.Target, error) {
var targets []*kong.Target
opt := newOpt(tags)
for _, upstream := range upstreams {
for {
t, nextopt, err := client.Targets.List(ctx, upstream.ID, opt)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
targets = append(targets, t...)
if nextopt == nil {
break
}
opt = nextopt
}
}
return targets, nil
}
// GetAllKeyAuths queries Kong for all key-auth credentials using client.
func GetAllKeyAuths(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.KeyAuth, error) {
var keyAuths []*kong.KeyAuth
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.KeyAuths.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return keyAuths, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
keyAuths = append(keyAuths, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return keyAuths, nil
}
// GetAllHMACAuths queries Kong for all hmac-auth credentials using client.
func GetAllHMACAuths(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.HMACAuth, error) {
var hmacAuths []*kong.HMACAuth
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.HMACAuths.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return hmacAuths, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
hmacAuths = append(hmacAuths, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return hmacAuths, nil
}
// GetAllJWTAuths queries Kong for all jwt credentials using client.
func GetAllJWTAuths(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.JWTAuth, error) {
var jwtAuths []*kong.JWTAuth
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.JWTAuths.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return jwtAuths, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
jwtAuths = append(jwtAuths, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return jwtAuths, nil
}
// GetAllBasicAuths queries Kong for all basic-auth credentials using client.
func GetAllBasicAuths(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.BasicAuth, error) {
var basicAuths []*kong.BasicAuth
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.BasicAuths.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return basicAuths, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
basicAuths = append(basicAuths, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return basicAuths, nil
}
// GetAllOauth2Creds queries Kong for all oauth2 credentials using client.
func GetAllOauth2Creds(ctx context.Context, client *kong.Client,
tags []string) ([]*kong.Oauth2Credential, error) {
var oauth2Creds []*kong.Oauth2Credential
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.Oauth2Credentials.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return oauth2Creds, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
oauth2Creds = append(oauth2Creds, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return oauth2Creds, nil
}
// GetAllACLGroups queries Kong for all ACL groups using client.
func GetAllACLGroups(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.ACLGroup, error) {
var aclGroups []*kong.ACLGroup
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.ACLs.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return aclGroups, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
aclGroups = append(aclGroups, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return aclGroups, nil
}
// GetAllMTLSAuths queries Kong for all basic-auth credentials using client.
func GetAllMTLSAuths(ctx context.Context,
client *kong.Client, tags []string) ([]*kong.MTLSAuth, error) {
var mtlsAuths []*kong.MTLSAuth
// tags are not supported on credentials
// opt := newOpt(tags)
opt := newOpt(nil)
for {
s, nextopt, err := client.MTLSAuths.List(ctx, opt)
if kong.IsNotFoundErr(err) {
return mtlsAuths, nil
}
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
mtlsAuths = append(mtlsAuths, s...)
if nextopt == nil {
break
}
opt = nextopt
}
return mtlsAuths, nil
}
// excludeConsumersPlugins filter out consumer plugins
func excludeConsumersPlugins(plugins []*kong.Plugin) []*kong.Plugin {
var filtered []*kong.Plugin
for _, p := range plugins {
if p.Consumer != nil && !utils.Empty(p.Consumer.ID) {
continue
}
filtered = append(filtered, p)
}
return filtered
}
|