|
| 1 | +<!-- |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +or more contributor license agreements. See the NOTICE file |
| 4 | +distributed with this work for additional information |
| 5 | +regarding copyright ownership. The ASF licenses this file |
| 6 | +to you under the Apache License, Version 2.0 (the |
| 7 | +"License"); you may not use this file except in compliance |
| 8 | +with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, |
| 13 | +software distributed under the License is distributed on an |
| 14 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +KIND, either express or implied. See the License for the |
| 16 | +specific language governing permissions and limitations |
| 17 | +under the License. |
| 18 | +--> |
| 19 | + |
| 20 | +# Optional plugin configuration contracts |
| 21 | + |
| 22 | +`shenyu-common` provides `ConfigurationContractProvider` and |
| 23 | +`ConfigurationContractRegistry`. A contract is keyed by the stable plugin name, |
| 24 | +one existing storage scope (`PLUGIN`, `SELECTOR`, `RULE`), and a contract version. |
| 25 | +It does not change `PluginData.config`, selector/rule handle strings, plugin |
| 26 | +names, synchronization envelopes, or the existing `PluginHandle` form metadata. |
| 27 | +Its version is not a fleet configuration revision. |
| 28 | + |
| 29 | +## Registration and inspection |
| 30 | + |
| 31 | +Put a pure provider in a lightweight shared contract artifact and register its |
| 32 | +class in `META-INF/services/org.apache.shenyu.common.config.contract.ConfigurationContractProvider`. |
| 33 | +The shared registry discovers these providers with Java ServiceLoader from the |
| 34 | +contract API's classloader. Applications can also register trusted providers |
| 35 | +explicitly during startup. It rejects duplicate plugin/scope/version keys and |
| 36 | +limits the catalog to 1024 registrations. Provider construction and validation |
| 37 | +must not initialize a request plugin, an SDK client, perform remote calls, or |
| 38 | +modify configuration/caches. This is a trusted extension contract, not a Java |
| 39 | +sandbox. Admin must install the lightweight artifact, never a gateway plugin JAR |
| 40 | +or its SDK implementation to obtain a validator. |
| 41 | + |
| 42 | +`registry.catalog()` returns immutable, deterministically ordered metadata. |
| 43 | +Admin exposes the same catalog at `GET /plugin-template/config-contracts`, using |
| 44 | +the existing `system:plugin:edit` permission. It contains field descriptions and |
| 45 | +effective defaults, never configured values. Existing form metadata remains |
| 46 | +available for unconverted plugins and forms; the endpoint does not generate or |
| 47 | +replace forms. |
| 48 | + |
| 49 | +```java |
| 50 | +ConfigurationValidation result = ConfigurationContractRegistry.shared() |
| 51 | + .validate("agentGateway", ConfigurationScope.PLUGIN, "1", originalJson); |
| 52 | +``` |
| 53 | + |
| 54 | +Treat `VALID`, `INVALID`, `UNREGISTERED`, and `UNSUPPORTED_VERSION` distinctly. |
| 55 | +`UNREGISTERED` preserves the legacy path but is not a validation/readiness claim. |
| 56 | +An explicitly unsupported version requires an availability/compatibility |
| 57 | +decision, never implicit success. The existing versionless production adapter |
| 58 | +selects version `1`; introducing another wire/version-selection policy needs a |
| 59 | +separate compatibility change. A provider may be installed without capability |
| 60 | +descriptors, a graph executor or the reactive test kit. |
| 61 | + |
| 62 | +## Structural checks and semantics |
| 63 | + |
| 64 | +Field descriptors record JSON type, required/null behavior, effective default, |
| 65 | +unit, sensitivity, optional inclusive numeric bounds and optional closed string |
| 66 | +enumerations. SPI names must not be turned into closed built-in enums. |
| 67 | +`structuralValidation=true` opts into strict JSON syntax/duplicate/trailing-token |
| 68 | +checks, declared field types, required/null checks, integral/range constraints, |
| 69 | +and enumerations. Dotted declared paths can describe nested object fields; |
| 70 | +unknown-field rejection currently applies to the root object's names. Nested |
| 71 | +unknown-field policy and cross-field rules belong to the semantic provider. |
| 72 | +Defaults describe execution and are never inserted into stored input. |
| 73 | + |
| 74 | +`structuralValidation=false` leaves coercion, syntax and null compatibility to |
| 75 | +the adopted existing parser. `UnknownFields` explicitly describes allow, |
| 76 | +root-level reject, or semantic-validator ownership. This lets a legacy parser |
| 77 | +keep its accepted input set without globally making other plugins strict. |
| 78 | +Custom semantic checks run after successful optional structural checks. They |
| 79 | +return stable `ConfigurationViolation` codes and declared paths, not exception |
| 80 | +messages or configuration values. Errors are capped at 16; paths at 128 |
| 81 | +characters. Parser/provider exceptions become `PROVIDER_FAILURE` without their |
| 82 | +message/cause. Unknown supplied field names are reported at `$`, so a secret |
| 83 | +used as a key cannot leak through diagnostics. Sensitivity is descriptive |
| 84 | +metadata for tooling, not permission to expose other configured values. |
| 85 | + |
| 86 | +Runtime dictionary/SPI, credential and endpoint availability remain binding |
| 87 | +checks. `RUNTIME_BINDING_UNAVAILABLE` is a separate diagnostic code available |
| 88 | +to adapters. A successful shared schema/semantic check does not establish that |
| 89 | +an extension or credential exists on a particular gateway, that resources were |
| 90 | +prepared, or that a candidate became active. |
| 91 | + |
| 92 | +## Operation ownership and initial production adapter |
| 93 | + |
| 94 | +Validation returns facts and never clears, retains, replaces or deletes a cache. |
| 95 | +Each descriptor documents operation semantics; the owning data handler continues |
| 96 | +to implement them. Missing input, explicit JSON null, blank clearing, `{}`, |
| 97 | +invalid update, disable and removal are separate cases. Do not copy another |
| 98 | +plugin's clearing or eviction policy merely because both use this registry. |
| 99 | + |
| 100 | +The first production adapter delegates Agent Gateway plugin configuration to |
| 101 | +its existing `AgentGatewayAggregationConfig.parsePluginConfig` implementation. |
| 102 | +Missing/blank/empty-object input keeps local-only behavior. Literal JSON null, |
| 103 | +unknown fields and malformed aggregation objects remain invalid. The generic |
| 104 | +Admin validation entry uses the registry for create/update/import, including |
| 105 | +persisted plugin identity on update. Gateway catalog reload validates before |
| 106 | +credential resolution/resource preparation and then retains its existing parser |
| 107 | +and lifecycle. Invalid candidates leave the previous directory active; removal |
| 108 | +and disable remain lifecycle operations. No request-path work is added. |
| 109 | + |
| 110 | +`META-INF/shenyu/config-contract-fixtures/agentGateway-plugin-v1.tsv` is the |
| 111 | +versioned fixture corpus packaged with the contract artifact. The common parser/ |
| 112 | +registry, Admin validator, and real gateway catalog-reload tests consume it. |
| 113 | +`@missing` and `@blank` distinguish missing values from literal JSON null. |
| 114 | +Production service tests also check invalid create, persisted-identity update |
| 115 | +and import rejection before writes/events. Generic fixtures exercise all three |
| 116 | +scopes, defaults/units, type/range/enum/null policies, immutable metadata, |
| 117 | +duplicates, unknown versions, legacy fallback and bounded secret-free failures. |
| 118 | + |
| 119 | +Use the repository Maven wrapper to run the focused reactor: |
| 120 | + |
| 121 | +```text |
| 122 | +./mvnw -B -ntp -pl shenyu-admin,shenyu-plugin/shenyu-plugin-agent-gateway -am verify -Dtest=ConfigurationContractRegistryTest,AgentGatewayAggregationConfigTest,AgentGatewayPluginConfigValidatorTest,PluginServiceTest,PluginControllerTest,ManagedRemoteMcpCatalogTest,HttpLongPollingDataChangedListenerTest -Dsurefire.failIfNoSpecifiedTests=false -Djacoco.skip=true -DskipRemoteResources=true -Dmaven.javadoc.skip=true |
| 123 | +``` |
0 commit comments