-
Notifications
You must be signed in to change notification settings - Fork 992
Introduce SupportedFieldValidator
#6722
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
Merged
+638
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
xds-api/src/main/java/com/linecorp/armeria/xds/api/IgnoreUnsupportedFieldHandler.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright 2026 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.xds.api; | ||
|
|
||
| enum IgnoreUnsupportedFieldHandler implements UnsupportedFieldHandler { | ||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public void handle(String descriptorName, String fieldPath, Object value) { | ||
| // no-op | ||
| } | ||
| } |
183 changes: 183 additions & 0 deletions
183
xds-api/src/main/java/com/linecorp/armeria/xds/api/SupportedFieldValidator.java
This file contains hidden or 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,183 @@ | ||
| /* | ||
| * Copyright 2026 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.xds.api; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.google.protobuf.Descriptors; | ||
| import com.google.protobuf.Descriptors.EnumValueDescriptor; | ||
| import com.google.protobuf.Descriptors.FieldDescriptor; | ||
| import com.google.protobuf.Message; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
|
||
| /** | ||
| * Validates protobuf messages against the {@code (armeria.xds.supported)} field annotation. | ||
| * Any set field that lacks the annotation is reported as an unsupported field violation. | ||
| * The validator walks recursively into supported message-typed fields. | ||
| * | ||
| * <p>Currently, supported fields are annotated inline on each field declaration in the proto files, e.g.: | ||
| * <pre>{@code | ||
| * string exact = 1 [(armeria.xds.supported) = true]; | ||
| * }</pre> | ||
| */ | ||
| @UnstableApi | ||
| public final class SupportedFieldValidator { | ||
|
|
||
| static final Logger unsupportedLogger = LoggerFactory.getLogger("com.linecorp.armeria.xds.unsupported"); | ||
| private static final SupportedFieldValidator DEFAULT = new SupportedFieldValidator( | ||
| UnsupportedFieldHandler.warn()); | ||
| private static final SupportedFieldValidator NOOP = new SupportedFieldValidator( | ||
| UnsupportedFieldHandler.ignore()); | ||
|
|
||
| private final ConcurrentMap<Descriptors.Descriptor, Set<FieldDescriptor>> supportedFieldsCache = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| private final UnsupportedFieldHandler handler; | ||
|
|
||
| private SupportedFieldValidator(UnsupportedFieldHandler handler) { | ||
| this.handler = requireNonNull(handler, "handler"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a {@link SupportedFieldValidator} with the default {@link UnsupportedFieldHandler#warn()} | ||
| * handler. | ||
| */ | ||
| public static SupportedFieldValidator of() { | ||
| return DEFAULT; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a {@link SupportedFieldValidator} with the specified {@link UnsupportedFieldHandler}. | ||
| */ | ||
| public static SupportedFieldValidator of(UnsupportedFieldHandler handler) { | ||
| requireNonNull(handler, "handler"); | ||
| if (handler == IgnoreUnsupportedFieldHandler.INSTANCE) { | ||
| return NOOP; | ||
| } | ||
| return new SupportedFieldValidator(handler); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a no-op validator that does not perform any validation. | ||
| */ | ||
| public static SupportedFieldValidator noop() { | ||
| return NOOP; | ||
| } | ||
|
|
||
| /** | ||
| * Validates the message, calling the handler directly for each unsupported field found. | ||
| * If the handler is the {@link UnsupportedFieldHandler#ignore()} sentinel, returns immediately | ||
| * to skip recursion cost. | ||
| */ | ||
| public void validate(Message message) { | ||
| requireNonNull(message, "message"); | ||
| if (handler == IgnoreUnsupportedFieldHandler.INSTANCE) { | ||
| return; | ||
| } | ||
| final String descriptorName = message.getDescriptorForType().getFullName(); | ||
| doValidate(message, descriptorName, "$"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private void doValidate(Message message, String descriptorName, String path) { | ||
| if (unsupportedPackage(message.getDescriptorForType().getFile().getPackage())) { | ||
| return; | ||
| } | ||
| final Descriptors.Descriptor descriptor = message.getDescriptorForType(); | ||
| final Set<FieldDescriptor> supported = supportedFields(descriptor); | ||
|
|
||
| for (Map.Entry<FieldDescriptor, Object> entry : message.getAllFields().entrySet()) { | ||
| final FieldDescriptor fd = entry.getKey(); | ||
| final Object value = entry.getValue(); | ||
| final String fieldPath = path + '.' + fd.getJsonName(); | ||
|
|
||
| if (!supported.contains(fd)) { | ||
| handler.handle(descriptorName, fieldPath, value); | ||
| continue; | ||
| } | ||
|
|
||
| // Field is supported — check enum values and recurse into nested messages. | ||
| if (fd.isMapField()) { | ||
| final FieldDescriptor valueField = fd.getMessageType().findFieldByNumber(2); | ||
| if (valueField != null) { | ||
| final List<Message> mapEntries = (List<Message>) value; | ||
| for (int i = 0; i < mapEntries.size(); i++) { | ||
| validateFieldValue(valueField, mapEntries.get(i).getField(valueField), | ||
| descriptorName, fieldPath + '[' + i + "].value"); | ||
| } | ||
| } | ||
| } else if (fd.isRepeated()) { | ||
| final List<?> elements = (List<?>) value; | ||
| for (int i = 0; i < elements.size(); i++) { | ||
| validateFieldValue(fd, elements.get(i), descriptorName, | ||
| fieldPath + '[' + i + ']'); | ||
| } | ||
| } else { | ||
| validateFieldValue(fd, value, descriptorName, fieldPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void validateFieldValue(FieldDescriptor fd, Object value, | ||
| String descriptorName, String fieldPath) { | ||
| if (fd.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { | ||
| if (value instanceof Message) { | ||
| doValidate((Message) value, descriptorName, fieldPath); | ||
| } | ||
| } else if (fd.getJavaType() == FieldDescriptor.JavaType.ENUM) { | ||
| if (!unsupportedPackage(fd.getEnumType().getFile().getPackage()) && | ||
| value instanceof EnumValueDescriptor) { | ||
| final EnumValueDescriptor ev = (EnumValueDescriptor) value; | ||
| if (unsupportedEnumValue(ev)) { | ||
| handler.handle(descriptorName, fieldPath, ev); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean unsupportedEnumValue(EnumValueDescriptor ev) { | ||
| return !ev.getOptions().getExtension(SupportedFieldProto.supportedValue); | ||
| } | ||
|
|
||
| private static boolean unsupportedPackage(String pkg) { | ||
| return !(pkg.startsWith("envoy.") || pkg.startsWith("xds.") || pkg.startsWith("armeria.")); | ||
| } | ||
|
|
||
| private Set<FieldDescriptor> supportedFields(Descriptors.Descriptor descriptor) { | ||
| return supportedFieldsCache.computeIfAbsent(descriptor, d -> { | ||
| final Set<FieldDescriptor> result = new HashSet<>(); | ||
| for (FieldDescriptor fd : d.getFields()) { | ||
| if (fd.getOptions().getExtension(SupportedFieldProto.supported)) { | ||
| result.add(fd); | ||
| } | ||
| } | ||
| return Collections.unmodifiableSet(result); | ||
| }); | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
xds-api/src/main/java/com/linecorp/armeria/xds/api/UnsupportedFieldHandler.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright 2026 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.xds.api; | ||
|
|
||
| import static com.linecorp.armeria.xds.api.SupportedFieldValidator.unsupportedLogger; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
|
||
| /** | ||
| * A handler that is invoked when unsupported xDS fields are detected in a protobuf message. | ||
| * Unsupported fields are those not annotated with {@code (armeria.xds.supported) = true}. | ||
| */ | ||
| @UnstableApi | ||
| @FunctionalInterface | ||
| public interface UnsupportedFieldHandler { | ||
|
|
||
| /** | ||
| * Called when an unsupported field is detected. | ||
| * | ||
| * @param descriptorName the full name of the root message being validated | ||
| * (e.g., {@code "envoy.config.cluster.v3.Cluster"}) | ||
| * @param fieldPath the JSON path of the unsupported field (e.g., {@code "$.edsConfig.serviceName"}) | ||
| * @param value the raw value of the unsupported field | ||
| */ | ||
| void handle(String descriptorName, String fieldPath, Object value); | ||
|
|
||
| /** | ||
| * Returns a composed handler that first invokes this handler, then the {@code after} handler. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted that |
||
| */ | ||
| default UnsupportedFieldHandler andThen(UnsupportedFieldHandler after) { | ||
| requireNonNull(after, "after"); | ||
| return (descriptorName, fieldPath, value) -> { | ||
| handle(descriptorName, fieldPath, value); | ||
| after.handle(descriptorName, fieldPath, value); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a handler that logs a warning for each unsupported field path. | ||
| */ | ||
| static UnsupportedFieldHandler warn() { | ||
| return (descriptorName, fieldPath, value) -> | ||
| unsupportedLogger.warn("Unsupported xDS field detected in {}: {}", descriptorName, fieldPath); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a handler that throws an {@link IllegalArgumentException} on the first unsupported field. | ||
| */ | ||
| static UnsupportedFieldHandler reject() { | ||
| return (descriptorName, fieldPath, value) -> { | ||
| throw new IllegalArgumentException( | ||
| "Unsupported xDS field detected in " + descriptorName + ": " + fieldPath); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a handler that silently ignores unsupported fields. | ||
| */ | ||
| static UnsupportedFieldHandler ignore() { | ||
| return IgnoreUnsupportedFieldHandler.INSTANCE; | ||
| } | ||
| } | ||
This file contains hidden or 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,16 @@ | ||
| syntax = "proto3"; | ||
| package armeria.xds; | ||
|
|
||
| option java_package = "com.linecorp.armeria.xds.api"; | ||
| option java_outer_classname = "SupportedFieldProto"; | ||
|
|
||
| import "google/protobuf/descriptor.proto"; | ||
|
|
||
| extend google.protobuf.FieldOptions { | ||
| optional bool supported = 50000; | ||
| } | ||
|
|
||
| extend google.protobuf.EnumValueOptions { | ||
| // Distinct name from the FieldOptions extension to avoid Java codegen name collision. | ||
| optional bool supported_value = 50000; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chance that a user wants to customize this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be possible if users want to validate their custom proto, but we can always create an extension point later on rather than prematurely adding APIs.