Skip to content

Commit 785744e

Browse files
committed
[fix](fe) Reject authentication plugin metadata lookup failures
### What problem does this PR solve? Issue Number: N/A Related PR: #67286 Problem Summary: The directory plugin loader used null for both a genuinely absent API-version declaration and a failed defining-JAR lookup. With legacy authentication compatibility enabled, an unreadable JAR, malformed manifest, or unresolved factory could therefore be admitted as API 1.0. Propagate lookup failures as apiVersion load failures before construction and publication, retain missing defining-JAR locations instead of falling back to another copy of the class, and keep release-version display metadata best effort. Add deterministic loader tests for readable legacy plugins, explicit invalid declarations, corrupt JARs/manifests, missing defining JARs, and unavailable provenance. ### Release note Authentication plugins with unreadable or unresolved API-version metadata are rejected. Readable legacy plugins without a declaration continue to load with a warning. ### Check List (For Author) - Test: Unit Test - run-fe-ut.sh with CUSTOM_MVN scoping the reactor to fe-extension-loader and fe-authentication-handler: 59 tests passed, including six new loader tests; Checkstyle passed for all 10 selected reactor modules. Full build.sh --fe was stopped after incomplete native third-party dependencies triggered an unrelated dependency rebuild. - Behavior changed: Yes (failed API-version metadata verification cannot use legacy compatibility) - Does this need documentation: No
1 parent 4eb0fa0 commit 785744e

3 files changed

Lines changed: 151 additions & 16 deletions

File tree

‎fe/fe-extension-loader/src/main/java/org/apache/doris/extension/loader/DirectoryPluginRuntimeManager.java‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,18 @@ private PluginHandle<F> loadFromPluginDir(Path pluginDir, ClassLoader parent, Cl
283283
// Version check before construction, and before asSubclass(): a plugin built against a
284284
// different SPI major is precisely the one whose types may no longer line up, so it must be
285285
// answered with a diagnosis naming both versions rather than with a cast or linkage error.
286-
String declaredApiVersion = readManifestMainAttribute(
287-
discoveredClass, allJars, apiVersionGate.getManifestAttribute());
286+
String declaredApiVersion;
287+
try {
288+
declaredApiVersion = readManifestMainAttribute(
289+
discoveredClass, allJars, apiVersionGate.getManifestAttribute());
290+
} catch (IOException e) {
291+
throw new PluginLoadException(
292+
normalizedDir,
293+
LoadFailure.STAGE_API_VERSION,
294+
"Failed to read " + apiVersionGate.getManifestAttribute() + " for factory '"
295+
+ factoryClassName + "' in " + normalizedDir + ": " + e.getMessage(),
296+
e);
297+
}
288298
String rejection = apiVersionGate.rejectionReason(declaredApiVersion);
289299
if (rejection != null) {
290300
throw new PluginLoadException(
@@ -375,17 +385,16 @@ private interface ManifestReader {
375385
* source when available (covers layouts where the service descriptor sits in a root jar but the
376386
* implementation lives in lib/), otherwise the first candidate jar containing the class entry.
377387
*
378-
* <p>An unreadable jar degrades to null rather than throwing. For the display-only version that leaves
379-
* the inventory row's version empty; for the plugin API version it reads as "declared nothing", which
380-
* {@link ApiVersionGate} rejects — the fail-closed direction in both cases.
388+
* <p>Null means a readable defining jar genuinely omitted the requested datum. An unreadable or
389+
* unresolved defining jar throws: the API gate must not mistake failed verification for an absent
390+
* legacy declaration. Once the code source identifies a jar, another candidate cannot replace it.
381391
*/
382-
private String readFromDefiningJar(Class<?> factoryClass, List<Path> candidateJars, ManifestReader reader) {
392+
private String readFromDefiningJar(Class<?> factoryClass, List<Path> candidateJars, ManifestReader reader)
393+
throws IOException {
383394
Path definingJar = ManifestVersions.jarOf(factoryClass);
384395
if (definingJar != null) {
385396
try (JarFile jarFile = new JarFile(definingJar.toFile())) {
386397
return reader.read(jarFile);
387-
} catch (IOException ignored) {
388-
// Fall through to scanning the candidate jars.
389398
}
390399
}
391400
String classEntry = factoryClass.getName().replace('.', '/') + ".class";
@@ -395,23 +404,26 @@ private String readFromDefiningJar(Class<?> factoryClass, List<Path> candidateJa
395404
continue;
396405
}
397406
return reader.read(jarFile);
398-
} catch (IOException ignored) {
399-
// Fall through to the next candidate jar.
400407
}
401408
}
402-
return null;
409+
throw new IOException("Cannot locate defining jar for factory '" + factoryClass.getName() + "'");
403410
}
404411

405412
/** Implementation-Version, honoring the class's package section. Display-only metadata. */
406413
private String readImplementationVersion(Class<?> factoryClass, List<Path> candidateJars) {
407414
String packagePath = ManifestVersions.packagePathOf(factoryClass);
408-
return readFromDefiningJar(factoryClass, candidateJars,
409-
jarFile -> ManifestVersions.fromManifest(jarFile, packagePath));
415+
try {
416+
return readFromDefiningJar(factoryClass, candidateJars,
417+
jarFile -> ManifestVersions.fromManifest(jarFile, packagePath));
418+
} catch (IOException ignored) {
419+
// Display-only metadata may be unavailable without making a verified plugin unloadable.
420+
return null;
421+
}
410422
}
411423

412424
/** The plugin API version a jar declares, as a MANIFEST main attribute. Null when absent. */
413425
private String readManifestMainAttribute(Class<?> factoryClass, List<Path> candidateJars,
414-
String attributeName) {
426+
String attributeName) throws IOException {
415427
return readFromDefiningJar(factoryClass, candidateJars,
416428
jarFile -> ManifestVersions.mainAttribute(jarFile, attributeName));
417429
}

‎fe/fe-extension-loader/src/main/java/org/apache/doris/extension/loader/ManifestVersions.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ static String packagePathOf(Class<?> clazz) {
9191
return lastDot < 0 ? null : className.substring(0, lastDot).replace('.', '/') + "/";
9292
}
9393

94-
/** Jar file that defined the class per its protection domain, or null. */
94+
/**
95+
* Defining jar location from the protection domain, or null for class directories or unavailable
96+
* provenance. Keep the location even if the jar was removed after class loading, so callers report
97+
* the read failure instead of falling back to an unrelated candidate with the same class entry.
98+
*/
9599
static Path jarOf(Class<?> clazz) {
96100
try {
97101
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
98102
if (codeSource == null || codeSource.getLocation() == null) {
99103
return null;
100104
}
101105
Path path = Paths.get(codeSource.getLocation().toURI());
102-
return Files.isRegularFile(path) ? path : null;
106+
return Files.isDirectory(path) ? null : path;
103107
} catch (URISyntaxException | RuntimeException e) {
104108
return null;
105109
}

‎fe/fe-extension-loader/src/test/java/org/apache/doris/extension/loader/DirectoryPluginRuntimeManagerApiVersionTest.java‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@
2525
import org.junit.jupiter.api.io.TempDir;
2626

2727
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.nio.charset.StandardCharsets;
30+
import java.nio.file.Files;
2831
import java.nio.file.Path;
32+
import java.security.CodeSource;
33+
import java.security.ProtectionDomain;
34+
import java.security.cert.Certificate;
2935
import java.util.Arrays;
3036
import java.util.Collections;
37+
import java.util.jar.JarEntry;
38+
import java.util.jar.JarOutputStream;
3139

3240
/**
3341
* The gate as the directory loader applies it: what a real plugin jar declares in its MANIFEST decides
@@ -45,6 +53,8 @@
4553
class DirectoryPluginRuntimeManagerApiVersionTest {
4654

4755
private static final ApiVersionGate GATE = DirectoryPluginRuntimeManagerMetadataTest.TEST_GATE;
56+
private static final ApiVersionGate LEGACY_GATE = ApiVersionGate.forFamilyAllowingUnversioned(
57+
"test", DirectoryPluginRuntimeManagerApiVersionTest.class, "7.0");
4858

4959
@TempDir
5060
Path tempDir;
@@ -80,6 +90,70 @@ void testMalformedDeclarationIsRefused() throws IOException {
8090
assertRefused(report, "seven-point-two", "7.2");
8191
}
8292

93+
@Test
94+
void testReadableUnversionedLegacyPluginLoads() throws IOException {
95+
Path root = tempDir.resolve("legacy");
96+
DirectoryPluginRuntimeManagerMetadataTest.createPluginJar(
97+
root.resolve("metadata-test").resolve("metadata-test.jar"),
98+
MetadataTestPluginFactory.class, "3.1.4", null);
99+
DirectoryPluginRuntimeManager<PluginFactory> manager = new DirectoryPluginRuntimeManager<>();
100+
LoadReport<PluginFactory> report = manager.loadAll(Collections.singletonList(root),
101+
Thread.currentThread().getContextClassLoader(), PluginFactory.class, null, LEGACY_GATE);
102+
103+
Assertions.assertEquals(1, report.getSuccesses().size(), () -> describe(report));
104+
Assertions.assertEquals("3.1.4", report.getSuccesses().get(0).getVersion());
105+
manager.discard("metadata-test");
106+
}
107+
108+
@Test
109+
void testLegacyGateStillRejectsExplicitInvalidDeclarations() throws IOException {
110+
for (String version : Arrays.asList("", "seven-point-two", "6.0")) {
111+
Path root = tempDir.resolve("invalid-legacy-" + version);
112+
DirectoryPluginRuntimeManagerMetadataTest.createPluginJar(
113+
root.resolve("metadata-test").resolve("metadata-test.jar"),
114+
MetadataTestPluginFactory.class, "3.1.4", version);
115+
LoadReport<PluginFactory> report = new DirectoryPluginRuntimeManager<PluginFactory>().loadAll(
116+
Collections.singletonList(root), Thread.currentThread().getContextClassLoader(),
117+
PluginFactory.class, null, LEGACY_GATE);
118+
119+
assertRefused(report, "Doris-Test-Plugin-Api-Version", "7.2");
120+
}
121+
}
122+
123+
@Test
124+
void testUnreadableDefiningJarIsRefusedInsteadOfUsingAnotherCandidate() throws IOException {
125+
Path definingJar = tempDir.resolve("unreadable.jar");
126+
Files.write(definingJar, "not a jar".getBytes(StandardCharsets.UTF_8));
127+
128+
assertRefused(loadWithFactoryCodeSource(definingJar, true),
129+
"Failed to read Doris-Test-Plugin-Api-Version", "metadata-test");
130+
}
131+
132+
@Test
133+
void testUnreadableManifestIsRefusedAsApiVersionFailure() throws IOException {
134+
Path definingJar = tempDir.resolve("bad-manifest.jar");
135+
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(definingJar))) {
136+
jar.putNextEntry(new JarEntry("META-INF/MANIFEST.MF"));
137+
jar.write("invalid manifest header\n".getBytes(StandardCharsets.UTF_8));
138+
jar.closeEntry();
139+
}
140+
141+
assertRefused(loadWithFactoryCodeSource(definingJar, false),
142+
"Failed to read Doris-Test-Plugin-Api-Version", "invalid header");
143+
}
144+
145+
@Test
146+
void testMissingDefiningJarIsRefusedInsteadOfBeingTreatedAsLegacy() throws IOException {
147+
assertRefused(loadWithFactoryCodeSource(tempDir.resolve("missing.jar"), true),
148+
"Failed to read Doris-Test-Plugin-Api-Version", "missing.jar");
149+
}
150+
151+
@Test
152+
void testUnresolvedDefiningJarIsRefusedInsteadOfBeingTreatedAsLegacy() throws IOException {
153+
assertRefused(loadWithFactoryCodeSource(null, false),
154+
"Failed to read Doris-Test-Plugin-Api-Version", "Cannot locate defining jar");
155+
}
156+
83157
@Test
84158
void testRefusedPluginLeavesNoTraceAndRepeatsIdentically() throws IOException {
85159
Path root = tempDir.resolve("repeat");
@@ -127,6 +201,51 @@ private LoadReport<PluginFactory> loadWithDeclaredVersion(String rootName, Strin
127201
GATE);
128202
}
129203

204+
private LoadReport<PluginFactory> loadWithFactoryCodeSource(Path definingJar, boolean includeFactory)
205+
throws IOException {
206+
Path root = tempDir.resolve("lookup-failure");
207+
Path pluginJar = root.resolve("metadata-test").resolve("metadata-test.jar");
208+
if (includeFactory) {
209+
// This readable candidate must not hide a failure to read the actual defining jar.
210+
DirectoryPluginRuntimeManagerMetadataTest.createPluginJar(
211+
pluginJar, MetadataTestPluginFactory.class, "3.1.4", null);
212+
} else {
213+
Files.createDirectories(pluginJar.getParent());
214+
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(pluginJar))) {
215+
jar.putNextEntry(new JarEntry("META-INF/services/" + PluginFactory.class.getName()));
216+
jar.write((MetadataTestPluginFactory.class.getName() + "\n").getBytes(StandardCharsets.UTF_8));
217+
jar.closeEntry();
218+
}
219+
}
220+
// Define the factory before the separate manifest lookup, with controlled provenance. This makes
221+
// missing/unreadable metadata deterministic without racing filesystem mutation against loading.
222+
ClassLoader parent = new FactoryClassLoader(definingJar);
223+
ClassLoadingPolicy policy = new ClassLoadingPolicy(
224+
Collections.singletonList(MetadataTestPluginFactory.class.getName()));
225+
DirectoryPluginRuntimeManager<PluginFactory> manager = new DirectoryPluginRuntimeManager<>();
226+
LoadReport<PluginFactory> report = manager.loadAll(Collections.singletonList(root),
227+
parent, PluginFactory.class, policy, LEGACY_GATE);
228+
229+
Assertions.assertTrue(manager.list().isEmpty(), "failed verification must not publish a handle");
230+
Assertions.assertFalse(manager.get("metadata-test").isPresent());
231+
return report;
232+
}
233+
234+
private static class FactoryClassLoader extends ClassLoader {
235+
FactoryClassLoader(Path definingJar) throws IOException {
236+
super(Thread.currentThread().getContextClassLoader());
237+
String name = MetadataTestPluginFactory.class.getName();
238+
String classEntry = name.replace('.', '/') + ".class";
239+
try (InputStream in = MetadataTestPluginFactory.class.getResourceAsStream("/" + classEntry)) {
240+
Assertions.assertNotNull(in);
241+
byte[] bytes = in.readAllBytes();
242+
CodeSource source = definingJar == null ? null
243+
: new CodeSource(definingJar.toUri().toURL(), (Certificate[]) null);
244+
defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(source, null));
245+
}
246+
}
247+
}
248+
130249
private static void assertRefused(LoadReport<PluginFactory> report, String... mustMention) {
131250
Assertions.assertTrue(report.getSuccesses().isEmpty(),
132251
"an incompatible plugin must never be published: " + describe(report));

0 commit comments

Comments
 (0)