harden managedFields decoding

This commit is contained in:
Kevin Wiesmueller 2021-03-01 19:58:56 +01:00
parent ff5a0ccb36
commit 470ad03d07
2 changed files with 86 additions and 0 deletions

View File

@ -101,6 +101,14 @@ func DecodeManagedFields(encodedManagedFields []metav1.ManagedFieldsEntry) (Mana
managed.times = make(map[string]*metav1.Time, len(encodedManagedFields))
for i, encodedVersionedSet := range encodedManagedFields {
switch encodedVersionedSet.Operation {
case metav1.ManagedFieldsOperationApply, metav1.ManagedFieldsOperationUpdate:
default:
return nil, fmt.Errorf("operation must be `Apply` or `Update`")
}
if len(encodedVersionedSet.APIVersion) < 1 {
return nil, fmt.Errorf("apiVersion must not be empty")
}
switch encodedVersionedSet.FieldsType {
case "FieldsV1":
// Valid case.

View File

@ -72,6 +72,84 @@ func TestHasFieldsType(t *testing.T) {
}
}
// TestHasAPIVersion makes sure that we fail if we don't have an
// APIVersion set.
func TestHasAPIVersion(t *testing.T) {
var unmarshaled []metav1.ManagedFieldsEntry
if err := yaml.Unmarshal([]byte(`- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:field: {}
manager: foo
operation: Apply
`), &unmarshaled); err != nil {
t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
}
if _, err := DecodeManagedFields(unmarshaled); err != nil {
t.Fatalf("did not expect decoding error but got: %v", err)
}
// Missing apiVersion.
unmarshaled = nil
if err := yaml.Unmarshal([]byte(`- fieldsType: FieldsV1
fieldsV1:
f:field: {}
manager: foo
operation: Apply
`), &unmarshaled); err != nil {
t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
}
if _, err := DecodeManagedFields(unmarshaled); err == nil {
t.Fatal("Expect decoding error but got none")
}
}
// TestHasOperation makes sure that we fail if we don't have an
// Operation set properly.
func TestHasOperation(t *testing.T) {
var unmarshaled []metav1.ManagedFieldsEntry
if err := yaml.Unmarshal([]byte(`- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:field: {}
manager: foo
operation: Apply
`), &unmarshaled); err != nil {
t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
}
if _, err := DecodeManagedFields(unmarshaled); err != nil {
t.Fatalf("did not expect decoding error but got: %v", err)
}
// Invalid operation.
if err := yaml.Unmarshal([]byte(`- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:field: {}
manager: foo
operation: Invalid
`), &unmarshaled); err != nil {
t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
}
if _, err := DecodeManagedFields(unmarshaled); err == nil {
t.Fatal("Expect decoding error but got none")
}
// Missing operation.
unmarshaled = nil
if err := yaml.Unmarshal([]byte(`- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:field: {}
manager: foo
`), &unmarshaled); err != nil {
t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
}
if _, err := DecodeManagedFields(unmarshaled); err == nil {
t.Fatal("Expect decoding error but got none")
}
}
// TestRoundTripManagedFields will roundtrip ManagedFields from the wire format
// (api format) to the format used by sigs.k8s.io/structured-merge-diff and back
func TestRoundTripManagedFields(t *testing.T) {