Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ tasks.register("copyPluginTestAlias", Copy) {
}
}

processResources.dependsOn(copyPluginAlias)
tasks.findByPath(':logstash-core:processResources').dependsOn(copyPluginAlias)
tasks.findByPath(':logstash-core:processTestResources').dependsOn(copyPluginTestAlias)


Expand All @@ -261,6 +261,13 @@ clean {
delete "${projectDir}/qa/integration/.bundle"
delete "${projectDir}/build/licenseReportFolders.txt"
delete "${projectDir}/build/rubyDependencies.csv"

delete "${projectDir}/lib/pluginmanager/plugin_aliases.yml"
delete "${projectDir}/spec/unit/plugin_manager/plugin_aliases.yml"
delete "${projectDir}/logstash-core/build/resources/test/org/logstash/plugins/plugin_aliases.yml"
delete "${projectDir}/logstash-core/build/resources/main/org/logstash/plugins/plugin_aliases.yml"
delete "${projectDir}/logstash-core/src/test/resources/org/logstash/plugins/plugin_aliases.yml"
delete "${projectDir}/logstash-core/src/main/resources/org/logstash/plugins/plugin_aliases.yml"
}

def assemblyDeps = [downloadAndInstallJRuby, assemble] + subprojects.collect {
Expand Down
49 changes: 36 additions & 13 deletions logstash-core/src/main/java/org/logstash/plugins/AliasRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.apache.logging.log4j.Logger;
import org.logstash.plugins.PluginLookup.PluginType;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -22,7 +25,7 @@ public class AliasRegistry {

private static final Logger LOGGER = LogManager.getLogger(AliasRegistry.class);

private final static class PluginCoordinate {
final static class PluginCoordinate {
private final PluginType type;
private final String name;

Expand All @@ -47,16 +50,20 @@ public int hashCode() {
PluginCoordinate withName(String name) {
return new PluginCoordinate(this.type, name);
}

@Override
public String toString() {
return "PluginCoordinate{type=" + type + ", name='" + name + "'}";
}

public String fullName() {
return "logstash-" + type.rubyLabel().toString().toLowerCase() + "-" + name;
}
}

private static class YamlWithChecksum {

static YamlWithChecksum load(final String filePath) {
final InputStream in = YamlWithChecksum.class.getClassLoader().getResourceAsStream(filePath);
if (in == null) {
throw new IllegalArgumentException("Can't find aliases yml definition file in jar resources: " + filePath);
}

private static YamlWithChecksum load(InputStream in) {
try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name())) {
// read the header line
final String header = scanner.nextLine();
Expand Down Expand Up @@ -98,17 +105,33 @@ private String computeHashFromContent() {
}
}

private static class AliasYamlLoader {
static class AliasYamlLoader {

private Map<PluginCoordinate, String> loadAliasesDefinitions() {
final YamlWithChecksum aliasYml;
Map<PluginCoordinate, String> loadAliasesDefinitions(Path yamlPath) {
final FileInputStream in;
try {
aliasYml = YamlWithChecksum.load("org/logstash/plugins/plugin_aliases.yml");
} catch (IllegalArgumentException badSyntaxExcp) {
LOGGER.warn("Malformed yaml file", badSyntaxExcp);
in = new FileInputStream(yamlPath.toFile());
} catch (FileNotFoundException e) {
LOGGER.warn("Can't find aliases yml definition file in in path: " + yamlPath, e);
return Collections.emptyMap();
}

return loadAliasesDefinitionsFromInputStream(in);
}

Map<PluginCoordinate, String> loadAliasesDefinitions() {
final String filePath = "org/logstash/plugins/plugin_aliases.yml";
final InputStream in = AliasYamlLoader.class.getClassLoader().getResourceAsStream(filePath);
if (in == null) {
LOGGER.warn("Malformed yaml file in yml definition file in jar resources: {}", filePath);
return Collections.emptyMap();
}

return loadAliasesDefinitionsFromInputStream(in);
}

private Map<PluginCoordinate, String> loadAliasesDefinitionsFromInputStream(InputStream in) {
final YamlWithChecksum aliasYml = YamlWithChecksum.load(in);
final String calculatedHash = aliasYml.computeHashFromContent();
if (!calculatedHash.equals(aliasYml.checksumHash)) {
LOGGER.warn("Bad checksum value, expected {} but found {}", calculatedHash, aliasYml.checksumHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import org.junit.Test;
import org.logstash.plugins.PluginLookup.PluginType;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import static org.junit.Assert.*;

public class AliasRegistryTest {
Expand All @@ -18,4 +25,22 @@ public void testLoadAliasesFromYAML() {
assertEquals("aliased_filter should be the alias for json filter",
"json", sut.originalFromAlias(PluginType.FILTER, "aliased_filter"));
}

@Test
public void testProductionConfigAliasesGemsExists() throws IOException {
final Path currentPath = Paths.get("./src/main/resources/org/logstash/plugins/plugin_aliases.yml").toAbsolutePath();
final AliasRegistry.AliasYamlLoader aliasLoader = new AliasRegistry.AliasYamlLoader();
final Map<AliasRegistry.PluginCoordinate, String> aliasesDefinitions = aliasLoader.loadAliasesDefinitions(currentPath);

for (AliasRegistry.PluginCoordinate alias : aliasesDefinitions.keySet()) {
final String gemName = alias.fullName();
URL url = new URL("https://rubygems.org/api/v1/gems/" + gemName +".json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");

final String errorMsg = "Aliased plugin " + gemName + "specified in " + currentPath + " MUST be published on RubyGems";
assertEquals(errorMsg, 200, connection.getResponseCode());
}
}
}