Skip to content

Commit

Permalink
Merge branch 'main' into fix-konnect-integration-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K authored Dec 12, 2024
2 parents 5d8fa34 + 9c2f736 commit 0f08a01
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 94 deletions.
4 changes: 3 additions & 1 deletion cmd/file_kong2tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ into Terraform resources.`,
kong2TfCmd.Flags().StringVarP(&cmdKong2TfOutputFilename, "output-file", "o", "-",
"Output file to write. Use - to write to stdout.")
kong2TfCmd.Flags().StringVarP(&cmdKong2TfGenerateImportsForControlPlaneID,
"generate-imports-for-control-plane-id", "g", "", "Generate terraform import statements for the control plane ID.")
"generate-imports-for-control-plane-id", "g", "",
"Generate terraform import statements for the control plane ID."+
"Typically used after `deck gateway dump --with-id` to obtain the IDs of all entities.")
kong2TfCmd.Flags().BoolVar(&cmdKong2TfIgnoreCredentialChanges, "ignore-credential-changes", false,
"Enable flag to add a 'lifecycle' block to each consumer credential, "+
"that ignores any changes from local to remote state.")
Expand Down
47 changes: 36 additions & 11 deletions cmd/gateway_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"sort"

"github.com/blang/semver/v4"
"github.com/kong/deck/utils"
Expand All @@ -17,12 +18,13 @@ import (
)

var (
validateCmdKongStateFile []string
validateCmdRBACResourcesOnly bool
validateOnline bool
validateWorkspace string
validateParallelism int
validateKonnectCompatibility bool
validateCmdKongStateFile []string
validateCmdOnlineEntitiesFilter []string
validateCmdRBACResourcesOnly bool
validateOnline bool
validateKonnectCompatibility bool
validateWorkspace string
validateParallelism int
)

func executeValidate(cmd *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -210,6 +212,26 @@ this command unless --online flag is used.
if len(validateCmdKongStateFile) == 0 {
validateCmdKongStateFile = []string{"-"}
}

// Iterate over the input values and validate them against the keys in entityMap
for _, value := range validateCmdOnlineEntitiesFilter {
// Check if the value is valid by comparing it with keys in EntityMap
if _, exists := validate.EntityMap[value]; !exists {
// Generate an error message with the list of valid keys
listOfKeys := make([]string, 0, len(validate.EntityMap))
for key := range validate.EntityMap {
listOfKeys = append(listOfKeys, key)
}
// Sort the keys alphabetically
sort.Strings(listOfKeys)

return fmt.Errorf(
"invalid value '%s' for --online-entities-list; it should be a valid Kong entity (case-sensitive). "+
"Valid entities: %v",
value, listOfKeys,
)
}
}
return preRunSilenceEventsFlag()
}

Expand Down Expand Up @@ -237,6 +259,8 @@ this command unless --online flag is used.

validateCmd.Flags().BoolVar(&validateCmdRBACResourcesOnly, "rbac-resources-only",
false, "indicate that the state file(s) contains RBAC resources only (Kong Enterprise only).")
validateCmd.Flags().StringSliceVarP(&validateCmdOnlineEntitiesFilter, "online-entities-list",
"", []string{}, "indicate the list of entities that should be validated online validation.")
if deprecated {
validateCmd.Flags().StringSliceVarP(&validateCmdKongStateFile,
"state", "s", []string{"kong.yaml"}, "file(s) containing Kong's configuration.\n"+
Expand Down Expand Up @@ -279,11 +303,12 @@ func validateWithKong(
return []error{fmt.Errorf("parsing Kong version: %w", err)}
}
opts := validate.ValidatorOpts{
Ctx: ctx,
State: ks,
Client: kongClient,
Parallelism: validateParallelism,
RBACResourcesOnly: validateCmdRBACResourcesOnly,
Ctx: ctx,
State: ks,
Client: kongClient,
Parallelism: validateParallelism,
RBACResourcesOnly: validateCmdRBACResourcesOnly,
OnlineEntitiesFilter: validateCmdOnlineEntitiesFilter,
}
validator := validate.NewValidator(opts)
return validator.Validate(parsedFormatVersion)
Expand Down
11 changes: 5 additions & 6 deletions tests/integration/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,11 @@ func Test_Diff_NoDiffUnorderedArray(t *testing.T) {
stateFile: "testdata/diff/004-no-diff-plugin/kong.yaml",
runWhen: ">=3.5.0 <3.8.1",
},
// Uncomment post solving: https://konghq.atlassian.net/browse/FTI-6303
// {
// name: "no diffs with unordered arrays >=3.8.1",
// stateFile: "testdata/diff/004-no-diff-plugin/kong.yaml",
// runWhen: ">=3.8.1",
// },
{
name: "no diffs with unordered arrays >=3.8.1",
stateFile: "testdata/diff/004-no-diff-plugin/kong.yaml",
runWhen: ">=3.8.1",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion tests/integration/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ func Test_Dump_SkipConsumers(t *testing.T) {
stateFile: "testdata/dump/002-skip-consumers/kong34.yaml",
expectedFile: "testdata/dump/002-skip-consumers/expected-no-skip-381.yaml",
skipConsumers: false,
runWhen: func(t *testing.T) { runWhen(t, "enterprise", ">=3.8.1") },
runWhen: func(t *testing.T) { runWhen(t, "enterprise", ">=3.8.1 <3.9.0") },
},
{
name: "3.9.0 dump with no skip-consumers",
stateFile: "testdata/dump/002-skip-consumers/kong34.yaml",
expectedFile: "testdata/dump/002-skip-consumers/expected-no-skip-39.yaml",
skipConsumers: false,
runWhen: func(t *testing.T) { runWhen(t, "enterprise", ">=3.9.0") },
},
}
for _, tc := range tests {
Expand Down
238 changes: 237 additions & 1 deletion tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,189 @@ var (
Protocols: []*string{kong.String("http"), kong.String("https")},
},
}

consumerGroupScopedPlugins390x = []*kong.Plugin{
{
Name: kong.String("rate-limiting-advanced"),
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("77e6691d-67c0-446a-9401-27be2b141aae"),
},
Config: kong.Configuration{
"compound_identifier": nil,
"consumer_groups": nil,
"dictionary_name": string("kong_rate_limiting_counters"),
"disable_penalty": bool(false),
"enforce_consumer_groups": bool(false),
"error_code": float64(429),
"error_message": string("API rate limit exceeded"),
"header_name": nil,
"hide_client_headers": bool(false),
"identifier": string("consumer"),
"limit": []any{float64(10)},
"lock_dictionary_name": string("kong_locks"),
"namespace": string("gold"),
"path": nil,
"redis": map[string]any{
"cluster_addresses": nil,
"cluster_max_redirections": float64(5),
"cluster_nodes": nil,
"connect_timeout": float64(2000),
"connection_is_proxied": bool(false),
"database": float64(0),
"host": string("127.0.0.1"),
"keepalive_backlog": nil,
"keepalive_pool_size": float64(256),
"password": nil,
"port": float64(6379),
"read_timeout": float64(2000),
"redis_proxy_type": nil,
"send_timeout": float64(2000),
"sentinel_addresses": nil,
"sentinel_master": nil,
"sentinel_nodes": nil,
"sentinel_password": nil,
"sentinel_role": nil,
"sentinel_username": nil,
"server_name": nil,
"ssl": false,
"ssl_verify": false,
"timeout": float64(2000),
"username": nil,
},
"retry_after_jitter_max": float64(1),
"strategy": string("local"),
"sync_rate": float64(-1),
"window_size": []any{float64(60)},
"window_type": string("sliding"),
},
Enabled: kong.Bool(true),
Protocols: []*string{kong.String("grpc"), kong.String("grpcs"), kong.String("http"), kong.String("https")},
},
{
Name: kong.String("rate-limiting-advanced"),
ConsumerGroup: &kong.ConsumerGroup{
ID: kong.String("5bcbd3a7-030b-4310-bd1d-2721ff85d236"),
},
Config: kong.Configuration{
"compound_identifier": nil,
"consumer_groups": nil,
"dictionary_name": string("kong_rate_limiting_counters"),
"disable_penalty": bool(false),
"enforce_consumer_groups": bool(false),
"error_code": float64(429),
"error_message": string("API rate limit exceeded"),
"header_name": nil,
"hide_client_headers": bool(false),
"identifier": string("consumer"),
"limit": []any{float64(7)},
"lock_dictionary_name": string("kong_locks"),
"namespace": string("silver"),
"path": nil,
"redis": map[string]any{
"cluster_addresses": nil,
"cluster_max_redirections": float64(5),
"cluster_nodes": nil,
"connect_timeout": float64(2000),
"connection_is_proxied": bool(false),
"database": float64(0),
"host": string("127.0.0.1"),
"keepalive_backlog": nil,
"keepalive_pool_size": float64(256),
"password": nil,
"port": float64(6379),
"read_timeout": float64(2000),
"redis_proxy_type": nil,
"send_timeout": float64(2000),
"sentinel_addresses": nil,
"sentinel_master": nil,
"sentinel_nodes": nil,
"sentinel_password": nil,
"sentinel_role": nil,
"sentinel_username": nil,
"server_name": nil,
"ssl": false,
"ssl_verify": false,
"timeout": float64(2000),
"username": nil,
},
"retry_after_jitter_max": float64(1),
"strategy": string("local"),
"sync_rate": float64(-1),
"window_size": []any{float64(60)},
"window_type": string("sliding"),
},
Enabled: kong.Bool(true),
Protocols: []*string{kong.String("grpc"), kong.String("grpcs"), kong.String("http"), kong.String("https")},
},
{
Name: kong.String("rate-limiting-advanced"),
Config: kong.Configuration{
"compound_identifier": nil,
"consumer_groups": nil,
"dictionary_name": string("kong_rate_limiting_counters"),
"disable_penalty": bool(false),
"enforce_consumer_groups": bool(false),
"error_code": float64(429),
"error_message": string("API rate limit exceeded"),
"header_name": nil,
"hide_client_headers": bool(false),
"identifier": string("consumer"),
"limit": []any{float64(5)},
"lock_dictionary_name": string("kong_locks"),
"namespace": string("silver"),
"path": nil,
"redis": map[string]any{
"cluster_addresses": nil,
"cluster_max_redirections": float64(5),
"cluster_nodes": nil,
"connect_timeout": float64(2000),
"connection_is_proxied": bool(false),
"database": float64(0),
"host": string("127.0.0.1"),
"keepalive_backlog": nil,
"keepalive_pool_size": float64(256),
"password": nil,
"port": float64(6379),
"read_timeout": float64(2000),
"redis_proxy_type": nil,
"send_timeout": float64(2000),
"sentinel_addresses": nil,
"sentinel_master": nil,
"sentinel_nodes": nil,
"sentinel_password": nil,
"sentinel_role": nil,
"sentinel_username": nil,
"server_name": nil,
"ssl": false,
"ssl_verify": false,
"timeout": float64(2000),
"username": nil,
},
"retry_after_jitter_max": float64(1),
"strategy": string("local"),
"sync_rate": float64(-1),
"window_size": []any{float64(60)},
"window_type": string("sliding"),
},
Enabled: kong.Bool(true),
Protocols: []*string{kong.String("grpc"), kong.String("grpcs"), kong.String("http"), kong.String("https")},
},
{
Name: kong.String("key-auth"),
Config: kong.Configuration{
"anonymous": nil,
"hide_credentials": false,
"key_in_body": false,
"key_in_header": true,
"key_in_query": true,
"key_names": []interface{}{"apikey"},
"realm": nil, // This is present on 3.7.x+
"run_on_preflight": true,
},
Enabled: kong.Bool(true),
Protocols: []*string{kong.String("http"), kong.String("https")},
},
}
)

// test scope:
Expand Down Expand Up @@ -5045,7 +5228,7 @@ func Test_Sync_ConsumerGroupsScopedPlugins_After360(t *testing.T) {
},
{
name: "creates consumer groups scoped plugins",
runWhen: ">=3.8.1",
runWhen: ">=3.8.1 <3.9.0",
kongFile: "testdata/sync/025-consumer-groups-scoped-plugins/kong3x.yaml",
expectedState: utils.KongRawState{
Consumers: consumerGroupsConsumers,
Expand Down Expand Up @@ -5096,6 +5279,59 @@ func Test_Sync_ConsumerGroupsScopedPlugins_After360(t *testing.T) {
},
},
},
{
name: "creates consumer groups scoped plugins",
runWhen: ">=3.9.0",
kongFile: "testdata/sync/025-consumer-groups-scoped-plugins/kong3x.yaml",
expectedState: utils.KongRawState{
Consumers: consumerGroupsConsumers,
ConsumerGroups: []*kong.ConsumerGroupObject{
{
ConsumerGroup: &kong.ConsumerGroup{
Name: kong.String("silver"),
},
Consumers: []*kong.Consumer{
{
Username: kong.String("bar"),
},
},
},
{
ConsumerGroup: &kong.ConsumerGroup{
Name: kong.String("gold"),
},
Consumers: []*kong.Consumer{
{
Username: kong.String("foo"),
},
},
},
},
Plugins: consumerGroupScopedPlugins390x,
Services: svc1_207,
Routes: route1_20x,
KeyAuths: []*kong.KeyAuth{
{
Consumer: &kong.Consumer{
ID: kong.String("87095815-5395-454e-8c18-a11c9bc0ef04"),
},
Key: kong.String("i-am-special"),
},
{
Consumer: &kong.Consumer{
ID: kong.String("5a5b9369-baeb-4faa-a902-c40ccdc2928e"),
},
Key: kong.String("i-am-not-so-special"),
},
{
Consumer: &kong.Consumer{
ID: kong.String("e894ea9e-ad08-4acf-a960-5a23aa7701c7"),
},
Key: kong.String("i-am-just-average"),
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name+"/"+tc.runWhen, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 0f08a01

Please sign in to comment.