Skip to content

Commit

Permalink
swap dataformat-yaml with snakeyaml (elastic#15599) (elastic#15606)
Browse files Browse the repository at this point in the history
This removes the dependency on jackson's dataformat-yaml. Since there's only a single place where this library is used in core: to load the plugin alias definition, the code can be replaced by the underlying snakeyaml.

Co-authored-by: Andrea Selva <[email protected]>
(cherry picked from commit 93d8a9d)

Co-authored-by: João Duarte <[email protected]>
  • Loading branch information
github-actions[bot] and jsvd authored Nov 22, 2023
1 parent 57dc14c commit b7c31f3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion logstash-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ dependencies {
api "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
implementation 'org.codehaus.janino:janino:3.1.0'
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:${jacksonVersion}"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}"
implementation group: 'org.yaml', name: 'snakeyaml', version: '2.2'
implementation group: 'com.google.guava', name: 'guava', version: '32.1.2-jre'
implementation('com.google.googlejavaformat:google-java-format:1.15.0') {
exclude group: 'com.google.guava', module: 'guava'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.logstash.plugins;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.logstash.plugins.PluginLookup.PluginType;
import org.logstash.plugins.aliases.AliasDocumentReplace;
import org.logstash.plugins.aliases.AliasPlugin;

import java.io.FileInputStream;
Expand All @@ -24,6 +22,9 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Scanner;
import java.util.stream.Collectors;

import org.yaml.snakeyaml.Yaml;

public class AliasRegistry {

Expand Down Expand Up @@ -100,13 +101,59 @@ private YamlWithChecksum(final String yamlContents, final String checksumHash) {

@SuppressWarnings("unchecked")
private Map<PluginType, List<AliasPlugin>> decodeYaml() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
return mapper.readValue(yamlContents, new TypeReference<Map<PluginType, List<AliasPlugin>>>() {});
Yaml yaml = new Yaml();
Map<String, Object> yamlMap = yaml.load(yamlContents);

// Convert the loaded YAML content to the expected type structure
return convertYamlMapToObject(yamlMap);
}

private String computeHashFromContent() {
return DigestUtils.sha256Hex(yamlContents);
}

@SuppressWarnings("unchecked")
private Map<PluginType, List<AliasPlugin>> convertYamlMapToObject(Map<String, Object> yamlMap) {
Map<PluginType, List<AliasPlugin>> result = new HashMap<>();

for (Map.Entry<String, Object> entry : yamlMap.entrySet()) {
PluginType pluginType = PluginType.valueOf(entry.getKey().toUpperCase());
List<Map<String, Object>> aliasList = (List<Map<String, Object>>) entry.getValue();

List<AliasPlugin> aliasPlugins = aliasList.stream()
.map(YamlWithChecksum::convertToAliasPluginDefinition)
.collect(Collectors.toList());

result.put(pluginType, aliasPlugins);
}
return result;
}

@SuppressWarnings("unchecked")
private static AliasPlugin convertToAliasPluginDefinition(Map<String, Object> aliasEntry) {
String aliasName = (String) aliasEntry.get("alias");
String from = (String) aliasEntry.get("from");

List<Map<String, String>> docs = (List<Map<String, String>>) aliasEntry.get("docs");
List<AliasDocumentReplace> replaceRules = convertToDocumentationReplacementRules(docs);

return new AliasPlugin(aliasName, from, replaceRules);
}

private static List<AliasDocumentReplace> convertToDocumentationReplacementRules(List<Map<String, String>> docs) {
if (docs == null) {
return Collections.emptyList();
}
return docs.stream()
.map(YamlWithChecksum::convertSingleDocReplacementRule)
.collect(Collectors.toList());
}

private static AliasDocumentReplace convertSingleDocReplacementRule(Map<String, String> doc) {
String replace = doc.get("replace");
String with = doc.get("with");
return new AliasDocumentReplace(replace, with);
}
}

static class AliasYamlLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class AliasDocumentReplace {
@Nonnull
private String with;

public AliasDocumentReplace(String replace, String with) {
this.replace = replace;
this.with = with;
}

public String getReplace() {
return this.replace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ public class AliasPlugin {
* Name of the aliased plugin.
*/
@Nonnull
@JsonProperty("alias")
private String aliasName;
private final String aliasName;

/**
* The plugin name where aliased plugin made from.
*/
@Nonnull
private String from;
private final String from;

/**
* List of replace entries when transforming artifact doc to aliased plugin doc.
*/
@Nullable
@JsonProperty("docs")
private List<AliasDocumentReplace> docHeaderReplaces;

public AliasPlugin(String aliasName, String from, List<AliasDocumentReplace> docHeaderReplaces) {
this.aliasName = aliasName;
this.from = from;
this.docHeaderReplaces = docHeaderReplaces;
}

@Nonnull
public String getAliasName() {
return aliasName;
Expand Down

0 comments on commit b7c31f3

Please sign in to comment.