Skip to content

Commit

Permalink
inject uuid generator
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo authored and rainest committed May 9, 2024
1 parent d8a7afb commit 2ee6705
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 37 deletions.
4 changes: 2 additions & 2 deletions internal/dataplane/kong_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ func (c *KongClient) sendToClient(
// If the client is Konnect and the feature flag is turned on,
// we should sanitize the configuration before sending it out.
if client.IsKonnect() && config.SanitizeKonnectConfigDumps {
s = s.SanitizedCopy()
s = s.SanitizedCopy(util.DefaultUUIDGenerator{})
}
deckGenParams := deckgen.GenerateDeckContentParams{
SelectorTags: config.FilterTags,
Expand Down Expand Up @@ -641,7 +641,7 @@ func prepareSendDiagnosticFn(
if diagnosticConfig.DumpsIncludeSensitive {
redactedConfig := deckgen.ToDeckContent(ctx,
logger,
targetState.SanitizedCopy(),
targetState.SanitizedCopy(util.DefaultUUIDGenerator{}),
deckGenParams,
)
config = redactedConfig
Expand Down
5 changes: 3 additions & 2 deletions internal/dataplane/kongstate/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/kong/go-kong/kong"

"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1"
)

Expand All @@ -27,13 +28,13 @@ type Consumer struct {
}

// SanitizedCopy returns a shallow copy with sensitive values redacted best-effort.
func (c *Consumer) SanitizedCopy() *Consumer {
func (c *Consumer) SanitizedCopy(uuidGenerator util.UUIDGenerator) *Consumer {
return &Consumer{
Consumer: c.Consumer,
Plugins: c.Plugins,
KeyAuths: func() (res []*KeyAuth) {
for _, v := range c.KeyAuths {
res = append(res, v.SanitizedCopy())
res = append(res, v.SanitizedCopy(uuidGenerator))
}
return
}(),
Expand Down
11 changes: 3 additions & 8 deletions internal/dataplane/kongstate/consumer_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package kongstate

import (
"math/rand"
"testing"

"github.com/google/uuid"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"

kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1"
"github.com/kong/kubernetes-ingress-controller/v3/test/mocks"
)

func int64Ptr(i int64) *int64 {
return &i
}

func TestConsumer_SanitizedCopy(t *testing.T) {
// this needs a static random seed because some auths generate random values
uuid.SetRand(rand.New(rand.NewSource(1))) //nolint:gosec
for _, tt := range []struct {
name string
in Consumer
Expand Down Expand Up @@ -54,7 +51,7 @@ func TestConsumer_SanitizedCopy(t *testing.T) {
Tags: []*string{kong.String("5.1"), kong.String("5.2")},
},
Plugins: []kong.Plugin{{ID: kong.String("1")}},
KeyAuths: []*KeyAuth{{kong.KeyAuth{ID: kong.String("1"), Key: randRedactedString()}}},
KeyAuths: []*KeyAuth{{kong.KeyAuth{ID: kong.String("1"), Key: kong.String("{vault://52fdfc07-2182-454f-963f-5f0f9a621d72}")}}},
HMACAuths: []*HMACAuth{{kong.HMACAuth{ID: kong.String("1"), Secret: redactedString}}},
JWTAuths: []*JWTAuth{{kong.JWTAuth{ID: kong.String("1"), Secret: redactedString}}},
BasicAuths: []*BasicAuth{{kong.BasicAuth{ID: kong.String("1"), Password: redactedString}}},
Expand All @@ -67,10 +64,8 @@ func TestConsumer_SanitizedCopy(t *testing.T) {
},
},
} {
// this needs a static random seed because some auths generate random values
uuid.SetRand(rand.New(rand.NewSource(1))) //nolint:gosec
t.Run(tt.name, func(t *testing.T) {
got := *tt.in.SanitizedCopy()
got := *tt.in.SanitizedCopy(mocks.StaticUUIDGenerator{UUID: "52fdfc07-2182-454f-963f-5f0f9a621d72"})
assert.Equal(t, tt.want, got)
})
}
Expand Down
11 changes: 6 additions & 5 deletions internal/dataplane/kongstate/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package kongstate
import (
"fmt"

"github.com/google/uuid"
"github.com/kong/go-kong/kong"
"github.com/mitchellh/mapstructure"

"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
)

// redactedString is used to redact sensitive values in the KongState.
Expand All @@ -15,8 +16,8 @@ var redactedString = kong.String("{vault://redacted-value}")

// randRedactedString is used to redact sensitive values in the KongState when the value must be random to avoid
// collisions.
func randRedactedString() *string {
s := fmt.Sprintf("{vault://%s}", uuid.NewString())
func randRedactedString(uuidGenerator util.UUIDGenerator) *string {
s := fmt.Sprintf("{vault://%s}", uuidGenerator.NewString())
return &s
}

Expand Down Expand Up @@ -160,13 +161,13 @@ func NewMTLSAuth(config interface{}) (*MTLSAuth, error) {
}

// SanitizedCopy returns a shallow copy with sensitive values redacted best-effort.
func (c *KeyAuth) SanitizedCopy() *KeyAuth {
func (c *KeyAuth) SanitizedCopy(uuidGenerator util.UUIDGenerator) *KeyAuth {
return &KeyAuth{
kong.KeyAuth{
// Consumer field omitted
CreatedAt: c.CreatedAt,
ID: c.ID,
Key: randRedactedString(),
Key: randRedactedString(uuidGenerator),
Tags: c.Tags,
},
}
Expand Down
12 changes: 4 additions & 8 deletions internal/dataplane/kongstate/credentials_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package kongstate

import (
"math/rand"
"testing"

"github.com/google/uuid"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"

"github.com/kong/kubernetes-ingress-controller/v3/test/mocks"
)

func TestKeyAuth_SanitizedCopy(t *testing.T) {
// this needs a static random seed because some auths generate random values
uuid.SetRand(rand.New(rand.NewSource(1))) //nolint:gosec
for _, tt := range []struct {
name string
in KeyAuth
Expand All @@ -29,15 +27,13 @@ func TestKeyAuth_SanitizedCopy(t *testing.T) {
want: KeyAuth{kong.KeyAuth{
CreatedAt: kong.Int(1),
ID: kong.String("2"),
Key: randRedactedString(),
Key: kong.String("{vault://52fdfc07-2182-454f-963f-5f0f9a621d72}"),
Tags: []*string{kong.String("4.1"), kong.String("4.2")},
}},
},
} {
// this needs a static random seed because some auths generate random values
uuid.SetRand(rand.New(rand.NewSource(1))) //nolint:gosec
t.Run(tt.name, func(t *testing.T) {
got := *tt.in.SanitizedCopy()
got := *tt.in.SanitizedCopy(mocks.StaticUUIDGenerator{UUID: "52fdfc07-2182-454f-963f-5f0f9a621d72"})
assert.Equal(t, tt.want, got)
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/dataplane/kongstate/kongstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type KongState struct {
}

// SanitizedCopy returns a shallow copy with sensitive values redacted best-effort.
func (ks *KongState) SanitizedCopy() *KongState {
func (ks *KongState) SanitizedCopy(uuidGenerator util.UUIDGenerator) *KongState {
return &KongState{
Services: ks.Services,
Upstreams: ks.Upstreams,
Expand All @@ -52,7 +52,7 @@ func (ks *KongState) SanitizedCopy() *KongState {
}),
Consumers: func() (res []Consumer) {
for _, v := range ks.Consumers {
res = append(res, *v.SanitizedCopy())
res = append(res, *v.SanitizedCopy(uuidGenerator))
}
return
}(),
Expand Down
11 changes: 3 additions & 8 deletions internal/dataplane/kongstate/kongstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kongstate

import (
"fmt"
"math/rand"
"reflect"
"strconv"
"strings"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/go-logr/logr"
"github.com/go-logr/logr/testr"
"github.com/go-logr/zapr"
"github.com/google/uuid"
"github.com/kong/go-kong/kong"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand All @@ -33,6 +31,7 @@ import (
kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1"
kongv1alpha1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1alpha1"
kongv1beta1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1beta1"
"github.com/kong/kubernetes-ingress-controller/v3/test/mocks"
)

var kongConsumerTypeMeta = metav1.TypeMeta{
Expand All @@ -47,8 +46,6 @@ var serviceTypeMeta = metav1.TypeMeta{

func TestKongState_SanitizedCopy(t *testing.T) {
testedFields := sets.New[string]()
// this needs a static random seed because some auths generate random values
uuid.SetRand(rand.New(rand.NewSource(1))) //nolint:gosec
for _, tt := range []struct {
name string
in KongState
Expand Down Expand Up @@ -90,7 +87,7 @@ func TestKongState_SanitizedCopy(t *testing.T) {
Plugin: kong.Plugin{ID: kong.String("1"), Config: kong.Configuration{"secret": *redactedString}},
}},
Consumers: []Consumer{{
KeyAuths: []*KeyAuth{{kong.KeyAuth{ID: kong.String("1"), Key: randRedactedString()}}},
KeyAuths: []*KeyAuth{{kong.KeyAuth{ID: kong.String("1"), Key: kong.String("{vault://52fdfc07-2182-454f-963f-5f0f9a621d72}")}}},
}},
Licenses: []License{{kong.License{ID: kong.String("1"), Payload: redactedString}}},
ConsumerGroups: []ConsumerGroup{{
Expand All @@ -106,11 +103,9 @@ func TestKongState_SanitizedCopy(t *testing.T) {
},
},
} {
// this needs a static random seed because some auths generate random values
uuid.SetRand(rand.New(rand.NewSource(1))) //nolint:gosec
t.Run(tt.name, func(t *testing.T) {
testedFields.Insert(extractNotEmptyFieldNames(tt.in)...)
got := *tt.in.SanitizedCopy()
got := *tt.in.SanitizedCopy(mocks.StaticUUIDGenerator{UUID: "52fdfc07-2182-454f-963f-5f0f9a621d72"})
assert.Equal(t, tt.want, got)
})
}
Expand Down
2 changes: 0 additions & 2 deletions internal/dataplane/kongstate/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package kongstate

import (
"math/rand"
"testing"

"github.com/google/uuid"
"github.com/kong/go-kong/kong"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
15 changes: 15 additions & 0 deletions internal/util/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package util

import "github.com/google/uuid"

// UUIDGenerator is an interface to generate UUIDs.
type UUIDGenerator interface {
NewString() string
}

// DefaultUUIDGenerator is the default implementation of UUIDGenerator.
type DefaultUUIDGenerator struct{}

func (DefaultUUIDGenerator) NewString() string {
return uuid.NewString()
}
13 changes: 13 additions & 0 deletions test/mocks/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mocks

import "github.com/kong/kubernetes-ingress-controller/v3/internal/util"

var _ = util.UUIDGenerator(&StaticUUIDGenerator{})

type StaticUUIDGenerator struct {
UUID string
}

func (s StaticUUIDGenerator) NewString() string {
return s.UUID
}

0 comments on commit 2ee6705

Please sign in to comment.