-
Notifications
You must be signed in to change notification settings - Fork 992
Validate supported fields for xDS #6736
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
Changes from 1 commit
fe32d03
4a74337
78aa0fb
6346707
63557af
27c4958
88c95b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.linecorp.armeria.xds.api.StrictXdsValidatorIndex |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright 2025 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 com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
|
||
| import io.envoyproxy.pgv.ReflectiveValidatorIndex; | ||
| import io.envoyproxy.pgv.ValidationException; | ||
| import io.envoyproxy.pgv.ValidatorIndex; | ||
|
|
||
| /** | ||
| * Validates xDS protobuf messages using pgv (protoc-gen-validate) structural validation. | ||
| */ | ||
| @UnstableApi | ||
| public final class PgvValidator { | ||
|
|
||
| private static final PgvValidator INSTANCE = new PgvValidator(); | ||
|
|
||
| private final ValidatorIndex delegate = new ReflectiveValidatorIndex(); | ||
|
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. static? |
||
|
|
||
| private PgvValidator() {} | ||
|
|
||
| /** | ||
| * Returns the singleton {@link PgvValidator} instance. | ||
| */ | ||
| public static PgvValidator of() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| /** | ||
| * Validates the given message using pgv structural validation. | ||
| * | ||
| * @throws IllegalArgumentException if validation fails | ||
| */ | ||
| public void assertValid(Object message) { | ||
| try { | ||
| delegate.validatorFor(message).assertValid(message); | ||
| } catch (ValidationException e) { | ||
| throw new IllegalArgumentException(e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2025 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 java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import com.google.protobuf.Message; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.UnstableApi; | ||
| import com.linecorp.armeria.xds.validator.XdsValidatorIndex; | ||
|
|
||
| /** | ||
| * A strict {@link XdsValidatorIndex} that composes pgv (protoc-gen-validate) structural validation | ||
| * with supported-field validation. All unsupported field violations are collected and reported | ||
| * together in a single {@link IllegalArgumentException}. | ||
|
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. Optional) What do you think of introducing a dedicated exception type to identify the validation error more easily.
Contributor
Author
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. Note that exceptions don't really impact actually request flow, so I don't really expect users to react based on the exceptions. Updated anyways. |
||
| * | ||
| * <p>This validator is intended for use in test environments via SPI to catch unsupported | ||
| * field usage early. Its {@link #priority()} is {@code 1}, which is higher than | ||
| * {@link DefaultXdsValidatorIndex} ({@code 0}), so it wins SPI selection when present | ||
| * on the classpath. | ||
| */ | ||
| @UnstableApi | ||
| public final class StrictXdsValidatorIndex implements XdsValidatorIndex { | ||
|
|
||
| private final PgvValidator pgvValidator = PgvValidator.of(); | ||
|
|
||
| @Override | ||
| public void assertValid(Message message) { | ||
| pgvValidator.assertValid(message); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| final List<String> violations = new ArrayList<>(); | ||
| final SupportedFieldValidator validator = | ||
| SupportedFieldValidator.of((descriptorName, fieldPath, value) -> | ||
| violations.add(descriptorName + ": " + fieldPath)); | ||
| validator.validate(message); | ||
| if (!violations.isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
| "Unsupported xDS fields detected: " + String.join(", ", violations)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int priority() { | ||
| return 1; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.