Skip to content

Commit

Permalink
fix(kuma-cp): guard the nil version in metadata (#3969)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dyszkiewicz <jakub.dyszkiewicz@gmail.com>
(cherry picked from commit 7f5ae2b)
  • Loading branch information
jakubdyszkiewicz authored and mergify-bot committed Mar 3, 2022
1 parent 1c846d7 commit f078cdc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/core/xds/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func (m *DataplaneMetadata) GetVersion() *mesh_proto.Version {
}

func DataplaneMetadataFromXdsMetadata(xdsMetadata *structpb.Struct) *DataplaneMetadata {
// Be extra careful here about nil checks since xdsMetadata is a "user" input.
// Even if we know that something should not be nil since we are generating metadata,
// the DiscoveryRequest can still be crafted manually to crash the CP.
metadata := DataplaneMetadata{}
if xdsMetadata == nil {
return &metadata
Expand Down Expand Up @@ -172,7 +175,9 @@ func DataplaneMetadataFromXdsMetadata(xdsMetadata *structpb.Struct) *DataplaneMe
for field, val := range value.GetStructValue().GetFields() {
if strings.HasPrefix(field, FieldPrefixDependenciesVersion) {
dependencyName := strings.TrimPrefix(field, FieldPrefixDependenciesVersion+".")
metadata.Version.Dependencies[dependencyName] = val.GetStringValue()
if metadata.GetVersion().GetDependencies() != nil {
metadata.Version.Dependencies[dependencyName] = val.GetStringValue()
}
} else {
dynamicMetadata[field] = val.GetStringValue()
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/core/xds/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ var _ = Describe("DataplaneMetadataFromXdsMetadata", func() {
EmptyDNSPort: 8001,
},
}),
Entry("should ignore dependencies version provided through metadata if version is not set at all", testCase{
node: &structpb.Struct{
Fields: map[string]*structpb.Value{
"dynamicMetadata": {
Kind: &structpb.Value_StructValue{
StructValue: &structpb.Struct{
Fields: map[string]*structpb.Value{
"version.dependencies.coredns": {
Kind: &structpb.Value_StringValue{
StringValue: "8000",
},
},
},
},
},
},
},
},
expected: xds.DataplaneMetadata{
DynamicMetadata: map[string]string{},
},
}),
)

It("should parse version", func() { // this has to be separate test because Equal does not work on proto
Expand Down

0 comments on commit f078cdc

Please sign in to comment.