|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.test.feature_aware; |
| 8 | + |
| 9 | +import org.elasticsearch.cluster.ClusterState; |
| 10 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 11 | +import org.elasticsearch.common.SuppressForbidden; |
| 12 | +import org.elasticsearch.common.io.PathUtils; |
| 13 | +import org.elasticsearch.persistent.PersistentTaskParams; |
| 14 | +import org.elasticsearch.xpack.core.XPackPlugin; |
| 15 | +import org.objectweb.asm.ClassReader; |
| 16 | + |
| 17 | +import java.io.FileNotFoundException; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.nio.file.FileVisitResult; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.nio.file.SimpleFileVisitor; |
| 25 | +import java.nio.file.attribute.BasicFileAttributes; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.TreeSet; |
| 31 | +import java.util.function.Consumer; |
| 32 | + |
| 33 | +/** |
| 34 | + * Used in the featureAwareCheck to check for classes in X-Pack that implement customs but do not extend the appropriate marker interface. |
| 35 | + */ |
| 36 | +public final class FeatureAwareCheck { |
| 37 | + |
| 38 | + /** |
| 39 | + * Check the class directories specified by the arguments for classes in X-Pack that implement customs but do not extend the appropriate |
| 40 | + * marker interface that provides a mix-in implementation of {@link ClusterState.FeatureAware#getRequiredFeature()}. |
| 41 | + * |
| 42 | + * @param args the class directories to check |
| 43 | + * @throws IOException if an I/O exception is walking the class directories |
| 44 | + */ |
| 45 | + public static void main(final String[] args) throws IOException { |
| 46 | + systemOutPrintln("checking for custom violations"); |
| 47 | + final List<FeatureAwareViolation> violations = new ArrayList<>(); |
| 48 | + checkDirectories(violations::add, args); |
| 49 | + if (violations.isEmpty()) { |
| 50 | + systemOutPrintln("no custom violations found"); |
| 51 | + } else { |
| 52 | + violations.forEach(violation -> |
| 53 | + systemOutPrintln( |
| 54 | + "class [" + violation.name + "] implements" |
| 55 | + + " [" + violation.interfaceName + " but does not implement" |
| 56 | + + " [" + violation.expectedInterfaceName + "]") |
| 57 | + ); |
| 58 | + throw new IllegalStateException( |
| 59 | + "found custom" + (violations.size() == 1 ? "" : "s") + " in X-Pack not extending appropriate X-Pack mix-in"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @SuppressForbidden(reason = "System.out#println") |
| 64 | + private static void systemOutPrintln(final String s) { |
| 65 | + System.out.println(s); |
| 66 | + } |
| 67 | + |
| 68 | + private static void checkDirectories( |
| 69 | + final Consumer<FeatureAwareViolation> callback, |
| 70 | + final String... classDirectories) throws IOException { |
| 71 | + for (final String classDirectory : classDirectories) { |
| 72 | + final Path root = pathsGet(classDirectory); |
| 73 | + if (Files.isDirectory(root)) { |
| 74 | + Files.walkFileTree(root, new SimpleFileVisitor<Path>() { |
| 75 | + @Override |
| 76 | + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { |
| 77 | + if (Files.isRegularFile(file) && file.getFileName().toString().endsWith(".class")) { |
| 78 | + try (InputStream in = Files.newInputStream(file)) { |
| 79 | + checkClass(in, callback); |
| 80 | + } |
| 81 | + } |
| 82 | + return super.visitFile(file, attrs); |
| 83 | + } |
| 84 | + }); |
| 85 | + } else { |
| 86 | + throw new FileNotFoundException("class directory [" + classDirectory + "] should exist"); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @SuppressForbidden(reason = "Paths#get") |
| 92 | + private static Path pathsGet(final String pathString) { |
| 93 | + return Paths.get(pathString); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Represents a feature-aware violation. |
| 98 | + */ |
| 99 | + static class FeatureAwareViolation { |
| 100 | + |
| 101 | + final String name; |
| 102 | + final String interfaceName; |
| 103 | + final String expectedInterfaceName; |
| 104 | + |
| 105 | + /** |
| 106 | + * Constructs a representation of a feature-aware violation. |
| 107 | + * |
| 108 | + * @param name the name of the custom class |
| 109 | + * @param interfaceName the name of the feature-aware interface |
| 110 | + * @param expectedInterfaceName the name of the expected mix-in class |
| 111 | + */ |
| 112 | + FeatureAwareViolation(final String name, final String interfaceName, final String expectedInterfaceName) { |
| 113 | + this.name = name; |
| 114 | + this.interfaceName = interfaceName; |
| 115 | + this.expectedInterfaceName = expectedInterfaceName; |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Loads a class from the specified input stream and checks that if it implements a feature-aware custom then it extends the appropriate |
| 122 | + * mix-in interface from X-Pack. If the class does not, then the specified callback is invoked. |
| 123 | + * |
| 124 | + * @param in the input stream |
| 125 | + * @param callback the callback to invoke |
| 126 | + * @throws IOException if an I/O exception occurs loading the class hierarchy |
| 127 | + */ |
| 128 | + static void checkClass(final InputStream in, final Consumer<FeatureAwareViolation> callback) throws IOException { |
| 129 | + // the class format only reports declared interfaces so we have to walk the hierarchy looking for all interfaces |
| 130 | + final List<String> interfaces = new ArrayList<>(); |
| 131 | + ClassReader cr = new ClassReader(in); |
| 132 | + final String name = cr.getClassName(); |
| 133 | + do { |
| 134 | + interfaces.addAll(Arrays.asList(cr.getInterfaces())); |
| 135 | + final String superName = cr.getSuperName(); |
| 136 | + if ("java/lang/Object".equals(superName)) { |
| 137 | + break; |
| 138 | + } |
| 139 | + cr = new ClassReader(superName); |
| 140 | + } while (true); |
| 141 | + checkClass(name, interfaces, callback); |
| 142 | + } |
| 143 | + |
| 144 | + private static void checkClass( |
| 145 | + final String name, |
| 146 | + final List<String> interfaces, |
| 147 | + final Consumer<FeatureAwareViolation> callback) { |
| 148 | + checkCustomForClass(ClusterState.Custom.class, XPackPlugin.XPackClusterStateCustom.class, name, interfaces, callback); |
| 149 | + checkCustomForClass(MetaData.Custom.class, XPackPlugin.XPackMetaDataCustom.class, name, interfaces, callback); |
| 150 | + checkCustomForClass(PersistentTaskParams.class, XPackPlugin.XPackPersistentTaskParams.class, name, interfaces, callback); |
| 151 | + } |
| 152 | + |
| 153 | + private static void checkCustomForClass( |
| 154 | + final Class<? extends ClusterState.FeatureAware> interfaceToCheck, |
| 155 | + final Class<? extends ClusterState.FeatureAware> expectedInterface, |
| 156 | + final String name, |
| 157 | + final List<String> interfaces, |
| 158 | + final Consumer<FeatureAwareViolation> callback) { |
| 159 | + final Set<String> interfaceSet = new TreeSet<>(interfaces); |
| 160 | + final String interfaceToCheckName = formatClassName(interfaceToCheck); |
| 161 | + final String expectedXPackInterfaceName = formatClassName(expectedInterface); |
| 162 | + if (interfaceSet.contains(interfaceToCheckName) |
| 163 | + && name.equals(expectedXPackInterfaceName) == false |
| 164 | + && interfaceSet.contains(expectedXPackInterfaceName) == false) { |
| 165 | + assert name.startsWith("org/elasticsearch/license") || name.startsWith("org/elasticsearch/xpack"); |
| 166 | + callback.accept(new FeatureAwareViolation(name, interfaceToCheckName, expectedXPackInterfaceName)); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Format the specified class to a name in the ASM format replacing all dots in the class name with forward-slashes. |
| 172 | + * |
| 173 | + * @param clazz the class whose name to format |
| 174 | + * @return the formatted class name |
| 175 | + */ |
| 176 | + static String formatClassName(final Class<?> clazz) { |
| 177 | + return clazz.getName().replace(".", "/"); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments