Skip to content

Commit 269294c

Browse files
authored
feat: add basic-auth with skip-hash in konnect (#342)
* feat: add basic-auth with skip-hash in konnect * fix: updated codegen * fix: kong json schema additions * style: removed stray comment * chore: updated go-kong to v0.69.0 * fix: fixed file read and write for basic-auth skip-hash * style: corrected linting * fix: export IDs when basic-auth present to retain consumer-password mapping
1 parent 9e95d16 commit 269294c

File tree

15 files changed

+120
-45
lines changed

15 files changed

+120
-45
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/hashicorp/go-retryablehttp v0.7.7
2222
github.com/hexops/gotextdiff v1.0.3
2323
github.com/kong/deck v1.51.1
24-
github.com/kong/go-kong v0.68.0
24+
github.com/kong/go-kong v0.69.0
2525
github.com/samber/lo v1.50.0
2626
github.com/shirou/gopsutil/v3 v3.24.5
2727
github.com/ssgelm/cookiejarparser v1.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ github.com/kong/deck v1.51.1 h1:TP/bJtcpahIhTPTUy78kzlEVx6RIXrPJlYAfCYqKYFg=
222222
github.com/kong/deck v1.51.1/go.mod h1:JeLiJp+Ffmuqhe5UaZr08sJVaUUbz7HFb+m28O2FEUM=
223223
github.com/kong/go-apiops v0.1.49 h1:/gjzH31qUUxvmg/lkePrh2b6trI5lrv7jJD2w9I7JPg=
224224
github.com/kong/go-apiops v0.1.49/go.mod h1:yPwbl3P2eQinVGAEA0d3legaYmzPJ+WtJf9fSeGF4b8=
225-
github.com/kong/go-kong v0.68.0 h1:rQrLYRKXD6/xf41GBXj9Ns+woAH9p6a4VvcXNMiPZPI=
226-
github.com/kong/go-kong v0.68.0/go.mod h1:J0vGB3wsZ2i99zly1zTRe3v7rOKpkhQZRwbcTFP76qM=
225+
github.com/kong/go-kong v0.69.0 h1:1LHU3y+i23X+RxxXT/bKml5bsxeUfKTfWFa3RK85cSU=
226+
github.com/kong/go-kong v0.69.0/go.mod h1:J0vGB3wsZ2i99zly1zTRe3v7rOKpkhQZRwbcTFP76qM=
227227
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
228228
github.com/kong/go-slugify v1.0.0/go.mod h1:dbR2h3J2QKXQ1k0aww6cN7o4cIcwlWflr6RKRdcoaiw=
229229
github.com/kong/kubernetes-configuration v1.4.2 h1:/OafLbl2NucvgQV7Xf/uneIgjxmPPUeE92BrssfVAQY=

pkg/dump/dump.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ type Config struct {
7777
// We require it here to signal the Writer about the sanitization, so
7878
// that referential integrity can be handled properly via IDs.
7979
SanitizeContent bool
80+
81+
SkipHashForBasicAuth bool
8082
}
8183

8284
func deduplicate(stringSlice []string) []string {
@@ -268,7 +270,14 @@ func getConsumerConfiguration(ctx context.Context, group *errgroup.Group,
268270
if err != nil {
269271
return fmt.Errorf("basic-auths: %w", err)
270272
}
271-
state.BasicAuths = basicAuths
273+
var options []*kong.BasicAuthOptions
274+
for _, basicAuth := range basicAuths {
275+
option := &kong.BasicAuthOptions{
276+
BasicAuth: *basicAuth,
277+
}
278+
options = append(options, option)
279+
}
280+
state.BasicAuths = options
272281
return nil
273282
})
274283

pkg/file/builder.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type stateBuilder struct {
6565

6666
isConsumerGroupPolicyOverrideSet bool
6767

68+
skipHashForBasicAuth bool
69+
6870
err error
6971
}
7072

@@ -834,10 +836,12 @@ func (b *stateBuilder) consumers() {
834836
return
835837
}
836838

837-
var basicAuths []kong.BasicAuth
839+
var basicAuths []kong.BasicAuthOptions
838840
for _, cred := range c.BasicAuths {
839841
cred.Consumer = utils.GetConsumerReference(c.Consumer)
840-
basicAuths = append(basicAuths, *cred)
842+
basicAuths = append(basicAuths, kong.BasicAuthOptions{
843+
BasicAuth: *cred,
844+
})
841845
}
842846
if err := b.ingestBasicAuths(basicAuths); err != nil {
843847
b.err = err
@@ -948,7 +952,7 @@ func (b *stateBuilder) ingestKeyAuths(creds []kong.KeyAuth) error {
948952
return nil
949953
}
950954

951-
func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuth) error {
955+
func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuthOptions) error {
952956
for _, cred := range creds {
953957
existingCred, err := b.currentState.BasicAuths.Get(*cred.Username)
954958
if utils.Empty(cred.ID) {
@@ -966,6 +970,10 @@ func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuth) error {
966970
if existingCred != nil {
967971
cred.CreatedAt = existingCred.CreatedAt
968972
}
973+
if b.skipHashForBasicAuth {
974+
cred.SkipHash = kong.Bool(true)
975+
}
976+
969977
b.rawState.BasicAuths = append(b.rawState.BasicAuths, &cred)
970978
}
971979
return nil

pkg/file/builder_test.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,14 +1448,16 @@ func Test_stateBuilder_consumers(t *testing.T) {
14481448
},
14491449
},
14501450
},
1451-
BasicAuths: []*kong.BasicAuth{
1451+
BasicAuths: []*kong.BasicAuthOptions{
14521452
{
1453-
ID: kong.String("0cc0d614-4c88-4535-841a-cbe0709b0758"),
1454-
Username: kong.String("basic-username"),
1455-
Password: kong.String("basic-password"),
1456-
Consumer: &kong.Consumer{
1457-
ID: kong.String("5b1484f2-5209-49d9-b43e-92ba09dd9d52"),
1458-
Username: kong.String("foo"),
1453+
BasicAuth: kong.BasicAuth{
1454+
ID: kong.String("0cc0d614-4c88-4535-841a-cbe0709b0758"),
1455+
Username: kong.String("basic-username"),
1456+
Password: kong.String("basic-password"),
1457+
Consumer: &kong.Consumer{
1458+
ID: kong.String("5b1484f2-5209-49d9-b43e-92ba09dd9d52"),
1459+
Username: kong.String("foo"),
1460+
},
14591461
},
14601462
},
14611463
},
@@ -1600,14 +1602,16 @@ func Test_stateBuilder_consumers(t *testing.T) {
16001602
},
16011603
},
16021604
},
1603-
BasicAuths: []*kong.BasicAuth{
1605+
BasicAuths: []*kong.BasicAuthOptions{
16041606
{
1605-
ID: kong.String("92f4c849-960b-43af-aad3-f307051408d3"),
1606-
Username: kong.String("basic-username"),
1607-
Password: kong.String("basic-password"),
1608-
Consumer: &kong.Consumer{
1609-
ID: kong.String("4bfcb11f-c962-4817-83e5-9433cf20b663"),
1610-
Username: kong.String("foo"),
1607+
BasicAuth: kong.BasicAuth{
1608+
ID: kong.String("92f4c849-960b-43af-aad3-f307051408d3"),
1609+
Username: kong.String("basic-username"),
1610+
Password: kong.String("basic-password"),
1611+
Consumer: &kong.Consumer{
1612+
ID: kong.String("4bfcb11f-c962-4817-83e5-9433cf20b663"),
1613+
Username: kong.String("foo"),
1614+
},
16111615
},
16121616
},
16131617
},
@@ -1739,14 +1743,16 @@ func Test_stateBuilder_consumers(t *testing.T) {
17391743
},
17401744
},
17411745
},
1742-
BasicAuths: []*kong.BasicAuth{
1746+
BasicAuths: []*kong.BasicAuthOptions{
17431747
{
1744-
ID: kong.String("92f4c849-960b-43af-aad3-f307051408d3"),
1745-
Username: kong.String("basic-username"),
1746-
Password: kong.String("basic-password"),
1747-
Consumer: &kong.Consumer{
1748-
ID: kong.String("4bfcb11f-c962-4817-83e5-9433cf20b663"),
1749-
Username: kong.String("foo"),
1748+
BasicAuth: kong.BasicAuth{
1749+
ID: kong.String("92f4c849-960b-43af-aad3-f307051408d3"),
1750+
Username: kong.String("basic-username"),
1751+
Password: kong.String("basic-password"),
1752+
Consumer: &kong.Consumer{
1753+
ID: kong.String("4bfcb11f-c962-4817-83e5-9433cf20b663"),
1754+
Username: kong.String("foo"),
1755+
},
17501756
},
17511757
},
17521758
},

pkg/file/kong_json_schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,9 @@
15361536
},
15371537
"consumer_group_policy_overrides": {
15381538
"type": "boolean"
1539+
},
1540+
"skip_hash_for_basic_auth": {
1541+
"type": "boolean"
15391542
}
15401543
},
15411544
"additionalProperties": false,

pkg/file/reader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func Get(ctx context.Context, fileContent *Content, opt RenderConfig, dumpConfig
8383
builder.includeLicenses = dumpConfig.IncludeLicenses
8484
builder.isPartialApply = dumpConfig.IsPartialApply
8585
builder.isConsumerGroupPolicyOverrideSet = dumpConfig.IsConsumerGroupPolicyOverrideSet
86+
builder.skipHashForBasicAuth = dumpConfig.SkipHashForBasicAuth
8687

8788
if len(dumpConfig.SelectorTags) > 0 {
8889
builder.selectTags = dumpConfig.SelectorTags

pkg/file/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ type Info struct {
784784
LookUpSelectorTags *LookUpSelectorTags `json:"default_lookup_tags,omitempty" yaml:"default_lookup_tags,omitempty"` //nolint
785785
Defaults KongDefaults `json:"defaults,omitempty" yaml:"defaults,omitempty"`
786786
ConsumerGroupPolicyOverrides bool `json:"consumer_group_policy_overrides,omitempty" yaml:"consumer_group_policy_overrides,omitempty"` //nolint
787+
SkipHashForBasicAuth bool `json:"skip_hash_for_basic_auth,omitempty" yaml:"skip_hash_for_basic_auth,omitempty"` //nolint
787788
}
788789

789790
// LookUpSelectorTags contains tags to lookup

pkg/file/writer.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sort"
1111
"strings"
1212

13+
"github.com/kong/go-database-reconciler/pkg/cprint"
1314
"github.com/kong/go-database-reconciler/pkg/state"
1415
"github.com/kong/go-database-reconciler/pkg/utils"
1516
"github.com/kong/go-kong/kong"
@@ -45,6 +46,22 @@ func getFormatVersion(kongVersion string) (string, error) {
4546
return formatVersion, nil
4647
}
4748

49+
func exportIDsWithBasicAuth(kongState *state.KongState) bool {
50+
basicAuths, err := kongState.BasicAuths.GetAll()
51+
if err != nil {
52+
return false
53+
}
54+
55+
exportIDs := len(basicAuths) > 0
56+
57+
if exportIDs {
58+
const idsWarning = "Warning: basic-auth credentials detected, IDs will be exported"
59+
cprint.UpdatePrintlnStdErr(idsWarning)
60+
}
61+
62+
return exportIDs
63+
}
64+
4865
// KongStateToFile generates a state object to file.Content.
4966
// It will omit timestamps and IDs while writing.
5067
func KongStateToContent(kongState *state.KongState, config WriteConfig) (*Content, error) {
@@ -80,6 +97,10 @@ func KongStateToContent(kongState *state.KongState, config WriteConfig) (*Conten
8097
}
8198
}
8299

100+
if exportIDsWithBasicAuth(kongState) {
101+
config.WithID = true
102+
}
103+
83104
err = populateServices(kongState, file, config)
84105
if err != nil {
85106
return nil, err

pkg/state/basicauth.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func (k *BasicAuthsCollection) Get(keyOrID string) (*BasicAuth, error) {
3131
if !ok {
3232
panic(unexpectedType)
3333
}
34-
return &BasicAuth{BasicAuth: *basicAuth.DeepCopy()}, nil
34+
return &BasicAuth{
35+
BasicAuth: *basicAuth.DeepCopy(),
36+
SkipHash: basicAuth.SkipHash,
37+
}, nil
3538
}
3639

3740
// GetAllByConsumerID returns all basic-auth credentials
@@ -50,7 +53,10 @@ func (k *BasicAuthsCollection) GetAllByConsumerID(id string) ([]*BasicAuth,
5053
if !ok {
5154
panic(unexpectedType)
5255
}
53-
res = append(res, &BasicAuth{BasicAuth: *r.DeepCopy()})
56+
res = append(res, &BasicAuth{
57+
BasicAuth: *r.DeepCopy(),
58+
SkipHash: r.SkipHash,
59+
})
5460
}
5561
return res, nil
5662
}
@@ -79,7 +85,10 @@ func (k *BasicAuthsCollection) GetAll() ([]*BasicAuth, error) {
7985
if !ok {
8086
panic(unexpectedType)
8187
}
82-
res = append(res, &BasicAuth{BasicAuth: *r.DeepCopy()})
88+
res = append(res, &BasicAuth{
89+
BasicAuth: *r.DeepCopy(),
90+
SkipHash: r.SkipHash,
91+
})
8392
}
8493
return res, nil
8594
}

0 commit comments

Comments
 (0)