Skip to content

Commit bb5d676

Browse files
authored
feat: added skip-hash for basic auth for Konnect (#1765)
* feat: added skip-hash for basic auth for Konnect * tests: added integration tests * feat: added skip-hash flag for apply command * chore: updated gdr * fix: ids export test fix * chore: updated gdr version
1 parent c9e1aa8 commit bb5d676

File tree

13 files changed

+380
-6
lines changed

13 files changed

+380
-6
lines changed

cmd/common.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
177177
cmd = "diff"
178178
}
179179

180+
dumpConfig.SkipHashForBasicAuth = determineSkipHashForBasicAuth(*targetContent, dumpConfig)
181+
180182
var kongClient *kong.Client
181183
mode := getMode(targetContent)
182184
if mode == modeKonnect {
@@ -210,6 +212,10 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
210212
dumpConfig.KonnectControlPlane = konnectControlPlane
211213
}
212214

215+
if dumpConfig.SkipHashForBasicAuth && mode != modeKonnect {
216+
return errors.New("skip-hash-for-basic-auth functionality can be used with Konnect only")
217+
}
218+
213219
rootClient, err := reconcilerUtils.GetKongClient(rootConfig)
214220
if err != nil {
215221
return err
@@ -513,6 +519,18 @@ func determinePolicyOverride(targetContent file.Content, config dump.Config) boo
513519
return false
514520
}
515521

522+
func determineSkipHashForBasicAuth(targetContent file.Content, config dump.Config) bool {
523+
if config.SkipHashForBasicAuth {
524+
return true
525+
}
526+
527+
if targetContent.Info != nil && targetContent.Info.SkipHashForBasicAuth {
528+
return targetContent.Info.SkipHashForBasicAuth
529+
}
530+
531+
return false
532+
}
533+
516534
func determineLookUpSelectorTagsConsumerGroups(targetContent file.Content) ([]string, error) {
517535
if targetContent.Info != nil &&
518536
targetContent.Info.LookUpSelectorTags != nil &&

cmd/gateway_apply.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func newApplyCmd() *cobra.Command {
4747
0, "artificial delay (in seconds) that is injected between insert operations \n"+
4848
"for related entities (usually for Cassandra deployments).\n"+
4949
"See `db_update_propagation` in kong.conf.")
50+
applyCmd.Flags().BoolVar(&dumpConfig.SkipHashForBasicAuth, "skip-hash-for-basic-auth",
51+
false, "do not sync hash for basic auth credentials.\n"+
52+
"This flag is only valid with Konnect.")
5053
applyCmd.Flags().BoolVar(&syncJSONOutput, "json-output",
5154
false, "generate command execution report in a JSON format")
5255
addSilenceEventsFlag(applyCmd.Flags())

cmd/gateway_sync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ to get Kong's state in sync with the input state.`,
106106
"thus gaining some performance with large configs.\n"+
107107
"Usage of this flag without apt select-tags and default-lookup-tags can be problematic.\n"+
108108
"This flag is not valid with Konnect.")
109+
syncCmd.Flags().BoolVar(&dumpConfig.SkipHashForBasicAuth, "skip-hash-for-basic-auth",
110+
false, "do not sync hash for basic auth credentials.\n"+
111+
"This flag is only valid with Konnect.")
109112
syncCmd.Flags().BoolVar(&syncCmdAssumeYes, "yes",
110113
false, "assume `yes` to prompts and run non-interactively.")
111114
syncCmd.Flags().BoolVar(&syncJSONOutput, "json-output",

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ require (
1414
github.com/fatih/color v1.18.0
1515
github.com/google/go-cmp v0.7.0
1616
github.com/kong/go-apiops v0.2.0
17-
github.com/kong/go-database-reconciler v1.27.1
18-
github.com/kong/go-kong v0.68.0
17+
github.com/kong/go-database-reconciler v1.28.0
18+
github.com/kong/go-kong v0.69.0
1919
github.com/mitchellh/go-homedir v1.1.0
2020
github.com/spf13/cobra v1.9.1
2121
github.com/spf13/pflag v1.0.6

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/q
244244
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
245245
github.com/kong/go-apiops v0.2.0 h1:QzHmEvl12vr3lxayTuY40dFuQpPECHTvopsILpwuLdE=
246246
github.com/kong/go-apiops v0.2.0/go.mod h1:yPwbl3P2eQinVGAEA0d3legaYmzPJ+WtJf9fSeGF4b8=
247-
github.com/kong/go-database-reconciler v1.27.1 h1:a5EyqQsY5BF2p964J2PQW9BMIMvTz30A2FInIAf1TcA=
248-
github.com/kong/go-database-reconciler v1.27.1/go.mod h1:6EnCqJqkYWwf9UjkiIKXCv29kapPFUBQ2+FVjR9ZslE=
249-
github.com/kong/go-kong v0.68.0 h1:rQrLYRKXD6/xf41GBXj9Ns+woAH9p6a4VvcXNMiPZPI=
250-
github.com/kong/go-kong v0.68.0/go.mod h1:J0vGB3wsZ2i99zly1zTRe3v7rOKpkhQZRwbcTFP76qM=
247+
github.com/kong/go-database-reconciler v1.28.0 h1:gUJDlPvo0P+/BVBGRZZxmscZC0IXvohUb23HAjTvIlQ=
248+
github.com/kong/go-database-reconciler v1.28.0/go.mod h1:DnqxRK/TH8HugJca1cw2n1NCApaNgpzEZhXUzITU0Ro=
249+
github.com/kong/go-kong v0.69.0 h1:1LHU3y+i23X+RxxXT/bKml5bsxeUfKTfWFa3RK85cSU=
250+
github.com/kong/go-kong v0.69.0/go.mod h1:J0vGB3wsZ2i99zly1zTRe3v7rOKpkhQZRwbcTFP76qM=
251251
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
252252
github.com/kong/go-slugify v1.0.0/go.mod h1:dbR2h3J2QKXQ1k0aww6cN7o4cIcwlWflr6RKRdcoaiw=
253253
github.com/kong/kubernetes-configuration v1.4.2 h1:/OafLbl2NucvgQV7Xf/uneIgjxmPPUeE92BrssfVAQY=

tests/integration/apply_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"context"
77
"testing"
88

9+
"github.com/google/go-cmp/cmp"
10+
"github.com/google/go-cmp/cmp/cmpopts"
911
"github.com/kong/go-database-reconciler/pkg/utils"
1012
"github.com/kong/go-kong/kong"
1113
"github.com/stretchr/testify/assert"
@@ -377,3 +379,141 @@ func Test_Apply_NestedEntities_Konnect(t *testing.T) {
377379
})
378380
}
379381
}
382+
383+
// test scope:
384+
//
385+
// - konnect
386+
func Test_Apply_BasicAuth_SkipHash_Konnect(t *testing.T) {
387+
setDefaultKonnectControlPlane(t)
388+
runWhenKonnect(t)
389+
setup(t)
390+
391+
client, err := getTestClient()
392+
require.NoError(t, err)
393+
ctx := context.Background()
394+
395+
tests := []struct {
396+
name string
397+
kongFile string
398+
updateFile string
399+
cmdArgs []string
400+
expectedState utils.KongRawState
401+
}{
402+
{
403+
name: "skip basic auth hash - via flag",
404+
kongFile: "testdata/sync/047-basic-auth-skip-hash/kong.yaml",
405+
cmdArgs: []string{"--skip-hash-for-basic-auth"},
406+
expectedState: utils.KongRawState{
407+
Consumers: []*kong.Consumer{
408+
{
409+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
410+
Username: kong.String("foo"),
411+
},
412+
},
413+
BasicAuths: []*kong.BasicAuthOptions{
414+
{
415+
BasicAuth: kong.BasicAuth{
416+
Username: kong.String("user1"),
417+
Password: kong.String("76789880fe1b54caf0875cf1f1fbcfd89202468d"),
418+
Consumer: &kong.Consumer{
419+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
420+
},
421+
},
422+
},
423+
},
424+
},
425+
},
426+
{
427+
name: "skip basic auth hash - via info in config",
428+
kongFile: "testdata/sync/047-basic-auth-skip-hash/kong-with-info.yaml",
429+
expectedState: utils.KongRawState{
430+
Consumers: []*kong.Consumer{
431+
{
432+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
433+
Username: kong.String("foo"),
434+
},
435+
},
436+
BasicAuths: []*kong.BasicAuthOptions{
437+
{
438+
BasicAuth: kong.BasicAuth{
439+
Username: kong.String("user1"),
440+
Password: kong.String("76789880fe1b54caf0875cf1f1fbcfd89202468d"),
441+
Consumer: &kong.Consumer{
442+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
443+
},
444+
},
445+
},
446+
},
447+
},
448+
},
449+
{
450+
name: "update basic auth with skip hash flag",
451+
kongFile: "testdata/sync/047-basic-auth-skip-hash/kong.yaml",
452+
updateFile: "testdata/sync/047-basic-auth-skip-hash/kong-update.yaml",
453+
cmdArgs: []string{"--skip-hash-for-basic-auth"},
454+
expectedState: utils.KongRawState{
455+
Consumers: []*kong.Consumer{
456+
{
457+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
458+
Username: kong.String("foo"),
459+
},
460+
},
461+
BasicAuths: []*kong.BasicAuthOptions{
462+
{
463+
BasicAuth: kong.BasicAuth{
464+
Username: kong.String("user1"),
465+
Password: kong.String("76789880fe1b54caf0875cf1f1fbcfd89202468d"),
466+
Consumer: &kong.Consumer{
467+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
468+
},
469+
Tags: kong.StringSlice("tag1", "tag2"),
470+
},
471+
},
472+
},
473+
},
474+
},
475+
{
476+
name: "update basic auth with skip hash in info config",
477+
kongFile: "testdata/sync/047-basic-auth-skip-hash/kong-with-info.yaml",
478+
updateFile: "testdata/sync/047-basic-auth-skip-hash/kong-with-info-update.yaml",
479+
cmdArgs: []string{"--skip-hash-for-basic-auth"},
480+
expectedState: utils.KongRawState{
481+
Consumers: []*kong.Consumer{
482+
{
483+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
484+
Username: kong.String("foo"),
485+
},
486+
},
487+
BasicAuths: []*kong.BasicAuthOptions{
488+
{
489+
BasicAuth: kong.BasicAuth{
490+
Username: kong.String("user1"),
491+
Password: kong.String("76789880fe1b54caf0875cf1f1fbcfd89202468d"),
492+
Consumer: &kong.Consumer{
493+
ID: kong.String("9efa87d1-0f29-4b8b-bf71-b947ddccf100"),
494+
},
495+
Tags: kong.StringSlice("tag1", "tag2"),
496+
},
497+
},
498+
},
499+
},
500+
},
501+
}
502+
503+
for _, tc := range tests {
504+
t.Run(tc.name, func(t *testing.T) {
505+
reset(t)
506+
507+
require.NoError(t, apply(ctx, tc.kongFile, tc.cmdArgs...))
508+
509+
if tc.updateFile != "" {
510+
require.NoError(t, apply(ctx, tc.updateFile, tc.cmdArgs...))
511+
}
512+
513+
ignoreFields := []cmp.Option{
514+
cmpopts.IgnoreFields(kong.BasicAuthOptions{}, "ID", "CreatedAt"),
515+
}
516+
testKongState(t, client, true, false, tc.expectedState, ignoreFields)
517+
})
518+
}
519+
}

tests/integration/dump_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,22 @@ func Test_Dump_Sanitize_Special_Entities(t *testing.T) {
866866
})
867867
}
868868
}
869+
870+
// test scope:
871+
//
872+
// - konnect
873+
func Test_Dump_BasicAuth_SkipHash(t *testing.T) {
874+
setDefaultKonnectControlPlane(t)
875+
runWhenKonnect(t)
876+
setup(t)
877+
878+
ctx := context.Background()
879+
require.NoError(t, sync(ctx, "testdata/sync/047-basic-auth-skip-hash/kong.yaml", "--skip-hash-for-basic-auth"))
880+
881+
output, err := dump("-o", "-")
882+
require.NoError(t, err)
883+
884+
expected, err := readFile("testdata/sync/047-basic-auth-skip-hash/expected-dump.yaml")
885+
require.NoError(t, err)
886+
assert.Equal(t, expected, output)
887+
}

0 commit comments

Comments
 (0)