-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: error with decoding config document with wrong apiVersion
Fixes #8270 The base bug was that the registry will return `nil` `ConfigDocument` if the version is not registered for a kind, which would result into weird config decoding errors. Add more unit-tests, while at it, also add more fuzzing samples. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
8 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
pkg/machinery/config/configloader/testdata/fuzz/FuzzConfigLoader/dfd1adfe630cffc2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("apiVersion: v1alpha1\nkind: SideroLinkConfig") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: v1alpha1 | ||
kind: ExtensionServicesConfig | ||
config: | ||
- name: foo | ||
configFiles: | ||
- content: hello | ||
mountPath: /etc/foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apiVersion: v1alpha1 | ||
kind: NetworkDefaultActionConfig | ||
ingress: block | ||
--- | ||
apiVersion: v1alpha1 | ||
kind: NetworkRuleConfig | ||
name: test | ||
portSelector: | ||
ports: | ||
- 53 | ||
- 8000-9000 | ||
protocol: udp | ||
ingress: | ||
- subnet: 192.168.0.0/16 | ||
except: 192.168.0.3/32 | ||
- subnet: 2001::/16 | ||
--- | ||
apiVersion: v1alpha1 | ||
kind: NetworkRuleConfig | ||
name: www | ||
portSelector: | ||
ports: | ||
- 80 | ||
protocol: tcp | ||
ingress: | ||
- subnet: 192.168.0.0/16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package registry_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/config/config" | ||
"github.com/siderolabs/talos/pkg/machinery/config/internal/registry" | ||
) | ||
|
||
type mockDocument struct { | ||
kind, version string | ||
} | ||
|
||
func (d mockDocument) Clone() config.Document { | ||
return d | ||
} | ||
|
||
func (d mockDocument) Kind() string { | ||
return d.kind | ||
} | ||
|
||
func (d mockDocument) APIVersion() string { | ||
return d.version | ||
} | ||
|
||
func mockFactory(kind, version string) registry.NewDocumentFunc { | ||
return func(requestedVersion string) config.Document { | ||
if requestedVersion == version { | ||
return mockDocument{kind, version} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func TestRegistry(t *testing.T) { | ||
r := registry.NewRegistry() | ||
|
||
// register document types | ||
r.Register("kind1", mockFactory("kind1", "v1alpha1")) | ||
r.Register("kind2", mockFactory("kind2", "v1alpha1")) | ||
|
||
// register duplicate kind | ||
assert.Panics(t, func() { | ||
r.Register("kind1", mockFactory("kind1", "v1alpha3")) | ||
}) | ||
|
||
// attempt to get unregistered kind | ||
_, err := r.New("unknownKind", "unknownVersion") | ||
require.Error(t, err) | ||
assert.ErrorIs(t, err, registry.ErrNotRegistered) | ||
assert.EqualError(t, err, "\"unknownKind\" \"unknownVersion\": not registered") | ||
|
||
// successful creation of documents | ||
d, err := r.New("kind1", "v1alpha1") | ||
require.NoError(t, err) | ||
assert.Equal(t, "kind1", d.Kind()) | ||
assert.Equal(t, "v1alpha1", d.APIVersion()) | ||
|
||
d, err = r.New("kind2", "v1alpha1") | ||
require.NoError(t, err) | ||
assert.Equal(t, "kind2", d.Kind()) | ||
assert.Equal(t, "v1alpha1", d.APIVersion()) | ||
|
||
// attempt get registered kind, but wrong version | ||
_, err = r.New("kind1", "unknownVersion") | ||
require.Error(t, err) | ||
assert.ErrorIs(t, err, registry.ErrNotRegistered) | ||
assert.EqualError(t, err, "\"kind1\" \"unknownVersion\": not registered") | ||
} |