Skip to content

Commit

Permalink
fix: fix a panic when receiving a broken config from Gateway (#5003) (#…
Browse files Browse the repository at this point in the history
…5120)

(cherry picked from commit 477e932)

Co-authored-by: Patryk Małek <[email protected]>
  • Loading branch information
czeslavo and pmalek authored Nov 8, 2023
1 parent e0bfd68 commit d1b0b0d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions internal/dataplane/configfetcher/config_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configfetcher
import (
"context"
"errors"
"fmt"

"github.com/kong/deck/dump"
"github.com/kong/deck/utils"
Expand Down Expand Up @@ -70,6 +71,10 @@ func (cf *DefaultKongLastGoodConfigFetcher) TryFetchingValidConfigFromGateways(
if err != nil {
errs = errors.Join(errs, err)
}
if rs == nil {
errs = errors.Join(errs, fmt.Errorf("failed to fetch configuration from %q, got nil kong raw state", client.BaseRootURL()))
continue
}
status, err := cf.getKongStatus(ctx, client.AdminAPIClient())
if err != nil {
errs = errors.Join(errs, err)
Expand Down
3 changes: 3 additions & 0 deletions internal/dataplane/configfetcher/kongrawstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
// KongRawStateToKongState converts a Deck kongRawState to a KIC KongState.
func KongRawStateToKongState(rawstate *utils.KongRawState) *kongstate.KongState {
kongState := &kongstate.KongState{}
if rawstate == nil {
return kongState
}

routes := make(map[string][]*kong.Route)
for _, r := range rawstate.Routes {
Expand Down
21 changes: 16 additions & 5 deletions internal/dataplane/configfetcher/kongrawstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestKongRawStateToKongState(t *testing.T) {

for _, tt := range []struct {
name string
kongRawState utils.KongRawState
kongRawState *utils.KongRawState
expectedKongState *kongstate.KongState
}{
{
name: "sanitizes all services, routes, and upstreams and create a KongState out of a KongRawState",
kongRawState: utils.KongRawState{
kongRawState: &utils.KongRawState{
Services: []*kong.Service{
{
Name: kong.String("service"),
Expand Down Expand Up @@ -267,15 +267,26 @@ func TestKongRawStateToKongState(t *testing.T) {
},
},
},
{
name: "doesn't panic when KongRawState is nil",
kongRawState: nil,
},
} {
t.Run(tt.name, func(t *testing.T) {
tt := tt

// Collect all fields that are tested in this test case.
testedKongRawStateFields.Insert(extractNotEmptyFieldNames(tt.kongRawState)...)
if tt.kongRawState != nil {
testedKongRawStateFields.Insert(extractNotEmptyFieldNames(*tt.kongRawState)...)
}

state := configfetcher.KongRawStateToKongState(&tt.kongRawState)
require.Equal(t, tt.expectedKongState, state)
var state *kongstate.KongState
require.NotPanics(t, func() {
state = configfetcher.KongRawStateToKongState(tt.kongRawState)
})
if tt.kongRawState != nil {
require.Equal(t, tt.expectedKongState, state)
}
})
}

Expand Down

0 comments on commit d1b0b0d

Please sign in to comment.