Skip to content

Commit

Permalink
Merge branch 'main' into feat/cg-policy-overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K authored Feb 5, 2025
2 parents 6a30943 + 107c160 commit 3e8bf97
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 8 deletions.
1 change: 0 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
const exitCodeDiffDetection = 2

var (
dumpConfig dump.Config
assumeYes bool
noMaskValues bool
)
Expand Down
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/fatih/color"
"github.com/kong/go-apiops/deckformat"
"github.com/kong/go-database-reconciler/pkg/dump"
"github.com/kong/go-database-reconciler/pkg/utils"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand All @@ -21,10 +22,15 @@ const (
defaultKonnectURL = "https://us.api.konghq.com"
)

var supportedCustomEntityTypes = []string{
"degraphql_routes",
}

var (
cfgFile string
rootConfig utils.KongClientConfig
konnectConfig utils.KonnectConfig
dumpConfig dump.Config

disableAnalytics bool
konnectConnectionDesired bool
Expand Down Expand Up @@ -376,6 +382,8 @@ func initConfig() {
// cookie-jar support
rootConfig.CookieJarPath = viper.GetString("kong-cookie-jar-path")

dumpConfig.CustomEntityTypes = supportedCustomEntityTypes

if viper.IsSet("no-color") {
color.NoColor = viper.GetBool("no-color")
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/fatih/color v1.17.0
github.com/google/go-cmp v0.6.0
github.com/kong/go-apiops v0.1.41
github.com/kong/go-database-reconciler v1.19.2
github.com/kong/go-database-reconciler v1.19.3
github.com/kong/go-kong v0.63.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.8.1
Expand Down Expand Up @@ -81,7 +81,7 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/samber/lo v1.49.1 // indirect
github.com/sethvargo/go-password v0.3.1 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/q
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kong/go-apiops v0.1.41 h1:1KXbQqyhO2E4nEnXJNqUDmHZU/LQ1U7Zoi+MAlcM2P0=
github.com/kong/go-apiops v0.1.41/go.mod h1:sATq9Tz+ivzHKZU+tDXkRtEZnf64xroU3lgv3yXqP4I=
github.com/kong/go-database-reconciler v1.19.2 h1:+TA5fs5BJWrX3FpsuekIitCqW66M/rw5ToRoUZTLxdA=
github.com/kong/go-database-reconciler v1.19.2/go.mod h1:fSzg8w4rBaiMqF0H9XqoGXCJsqLIxRBG9f1BhvKU2Lg=
github.com/kong/go-database-reconciler v1.19.3 h1:wcvRIL1lt21ZDCGH0OaGbyTWxKv8tiGpbSCP4hUp6Ew=
github.com/kong/go-database-reconciler v1.19.3/go.mod h1:crxjFGiSSKWtP1AqFZz/IEnlxTX/hFkcBdMdwQbEpmk=
github.com/kong/go-kong v0.63.0 h1:8ECLgkgDqON61qCMq/M0gTwZKYxg55Oy692dRDOOBiU=
github.com/kong/go-kong v0.63.0/go.mod h1:ma9GWnhkxtrXZlLFfED955HjVzmUojYEHet3lm+PDik=
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
Expand Down Expand Up @@ -377,8 +377,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
github.com/samber/lo v1.49.1 h1:4BIFyVfuQSEpluc7Fua+j1NolZHiEHEpaSEKdsH0tew=
github.com/samber/lo v1.49.1/go.mod h1:dO6KHFzUKXgP8LDhU0oI8d2hekjXnGOu0DB8Jecxd6o=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down
69 changes: 69 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,15 @@ var (
}
)

const complexQueryForDegraphqlRoute = `query SearchPosts($filters: PostsFilters) {
posts(filter: $filters) {
id
title
author
}
}
`

// test scope:
// - 1.4.3
func Test_Sync_ServicesRoutes_Till_1_4_3(t *testing.T) {
Expand Down Expand Up @@ -6237,3 +6246,63 @@ func Test_Sync_FilterChainsUnsupported(t *testing.T) {
require.NoError(t, sync(context.Background(), "testdata/sync/033-filter-chains/init.yaml"))
require.Error(t, sync(context.Background(), "testdata/sync/033-filter-chains/create.yaml"))
}

func Test_Sync_DegraphqlRoutes(t *testing.T) {
runWhenEnterpriseOrKonnect(t, ">=3.0.0")
setup(t)

client, err := getTestClient()
require.NoError(t, err)

ctx := context.Background()
dumpConfig := deckDump.Config{CustomEntityTypes: []string{"degraphql_routes"}}

t.Run("create degraphql route", func(t *testing.T) {
require.NoError(t, sync(ctx, "testdata/sync/036-degraphql-routes/kong.yaml"))

newState, err := fetchCurrentState(ctx, client, dumpConfig, t)
require.NoError(t, err)

degraphqlRoutes, err := newState.DegraphqlRoutes.GetAll()
require.NoError(t, err)

assert.Equal(t, 1, len(degraphqlRoutes))

d := degraphqlRoutes[0]
assert.Equal(t, "/foo", *d.URI)
assert.Equal(t, "query{ foo { bar } }", *d.Query)

expectedMethods := kong.StringSlice("GET")
assert.Equal(t, expectedMethods, d.Methods)
})

t.Run("create degraphql route - complex query", func(t *testing.T) {
require.NoError(t, sync(ctx, "testdata/sync/036-degraphql-routes/kong-complex-query.yaml"))

newState, err := fetchCurrentState(ctx, client, dumpConfig, t)
require.NoError(t, err)

degraphqlRoutes, err := newState.DegraphqlRoutes.GetAll()
require.NoError(t, err)

assert.Equal(t, 1, len(degraphqlRoutes))

d := degraphqlRoutes[0]

assert.Equal(t, "/search/posts", *d.URI)
expectedQuery := kong.String(complexQueryForDegraphqlRoute)
assert.Equal(t, expectedQuery, d.Query)

expectedMethods := kong.StringSlice("POST", "GET")
assert.Equal(t, expectedMethods, d.Methods)
})
}

func Test_Sync_CustomEntitiesFake(t *testing.T) {
runWhenEnterpriseOrKonnect(t, ">=3.0.0")
setup(t)
ctx := context.Background()
err := sync(ctx, "testdata/sync/036-degraphql-routes/kong-fake.yaml")
require.Error(t, err)
assert.ErrorContains(t, err, "unknown entity type: fake-entity")
}
22 changes: 21 additions & 1 deletion tests/integration/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/kong/deck/cmd"
deckDump "github.com/kong/go-database-reconciler/pkg/dump"
"github.com/kong/go-database-reconciler/pkg/state"
"github.com/kong/go-database-reconciler/pkg/utils"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -187,7 +188,9 @@ func testKongState(t *testing.T, client *kong.Client, isKonnect bool,

// Get entities from Kong
ctx := context.Background()
dumpConfig := deckDump.Config{}
dumpConfig := deckDump.Config{
CustomEntityTypes: []string{"degraphql_routes"},
}
if expectedState.RBACEndpointPermissions != nil {
dumpConfig.RBACResourcesOnly = true
}
Expand Down Expand Up @@ -233,6 +236,23 @@ func testKongState(t *testing.T, client *kong.Client, isKonnect bool,
}
}

func fetchCurrentState(ctx context.Context, client *kong.Client,
dumpConfig deckDump.Config, t *testing.T,
) (*state.KongState, error) {
t.Helper()

rawState, err := deckDump.Get(ctx, client, dumpConfig)
if err != nil {
return nil, err
}

currentState, err := state.Get(rawState)
if err != nil {
return nil, err
}
return currentState, nil
}

func reset(t *testing.T, opts ...string) {
t.Helper()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
_format_version: "3.0"
plugins:
- config:
graphql_server_path: /graphql
enabled: true
name: degraphql
protocols:
- grpc
- grpcs
- http
- https
services:
- connect_timeout: 60000
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
write_timeout: 60000
custom_entities:
- type: degraphql_routes
fields:
uri: "/search/posts"
methods:
- "POST"
- "GET"
query: |
query SearchPosts($filters: PostsFilters) {
posts(filter: $filters) {
id
title
author
}
}
service:
name: svc1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_format_version: "3.0"
custom_entities:
- type: fake-entity
fields:
foo: bar
27 changes: 27 additions & 0 deletions tests/integration/testdata/sync/036-degraphql-routes/kong.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
_format_version: "3.0"
plugins:
- config:
graphql_server_path: /graphql
enabled: true
name: degraphql
protocols:
- grpc
- grpcs
- http
- https
services:
- connect_timeout: 60000
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
write_timeout: 60000
custom_entities:
- type: degraphql_routes
fields:
uri: "/foo"
query: "query{ foo { bar } }"
service:
name: svc1

0 comments on commit 3e8bf97

Please sign in to comment.