Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protosanitizer: fix panic if enum value is not found #188

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion protosanitizer/protosanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func stripSingleValue(field protoreflect.FieldDescriptor, v protoreflect.Value)
case protoreflect.MessageKind:
return stripMessage(v.Message())
case protoreflect.EnumKind:
return field.Enum().Values().ByNumber(v.Enum()).Name()
desc := field.Enum().Values().ByNumber(v.Enum())
if desc == nil {
return v.Enum()
}
return desc.Name()
default:
return v.Interface()
}
Expand Down
8 changes: 8 additions & 0 deletions protosanitizer/protosanitizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ func TestStripSecrets(t *testing.T) {
{createVolumeFuture,
`{"capacity_range":{"required_bytes":1024},"maybe_secret_map":{"1":{"array_secret":"***stripped***"},"2":{"array_secret":"***stripped***"}},"name":"foo","new_secret_int":"***stripped***","seecreets":"***stripped***","volume_capabilities":[{"array_secret":"***stripped***","mount":{"fs_type":"ext4"}},{"array_secret":"***stripped***"}],"volume_content_source":{"nested_secret_field":"***stripped***","volume":{"oneof_secret_field":"***stripped***","volume_id":"abc"}}}`,
},
{&csi.CreateVolumeRequest{
VolumeCapabilities: []*csi.VolumeCapability{{
AccessMode: &csi.VolumeCapability_AccessMode{
// Test for unknown enum value
Mode: csi.VolumeCapability_AccessMode_Mode(12345),
},
}},
}, `{"volume_capabilities":[{"access_mode":{"mode":12345}}]}`},
}

// Message from revised spec as received by a sidecar based on the current spec.
Expand Down