From 9b9b7b18308dbbe6d7b7eb7a2aa53d8dbd0f8095 Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 7 Jul 2021 13:44:53 +0200 Subject: [PATCH] Refactor: simplify config_hash generation --- .../execution/AbstractPipelineExt.java | 23 +++++++++++++------ .../execution/JavaBasePipelineExt.java | 3 +-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/logstash-core/src/main/java/org/logstash/execution/AbstractPipelineExt.java b/logstash-core/src/main/java/org/logstash/execution/AbstractPipelineExt.java index d08385be801..7b77c8368ee 100644 --- a/logstash-core/src/main/java/org/logstash/execution/AbstractPipelineExt.java +++ b/logstash-core/src/main/java/org/logstash/execution/AbstractPipelineExt.java @@ -46,6 +46,7 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; +import org.jruby.util.ByteList; import org.logstash.RubyUtil; import org.logstash.ackedqueue.QueueFactoryExt; import org.logstash.ackedqueue.ext.JRubyAckedQueueExt; @@ -148,19 +149,14 @@ public AbstractPipelineExt(final Ruby runtime, final RubyClass metaClass) { @JRubyMethod public final AbstractPipelineExt initialize(final ThreadContext context, final IRubyObject pipelineConfig, final IRubyObject namespacedMetric, - final IRubyObject rubyLogger) - throws NoSuchAlgorithmException { + final IRubyObject rubyLogger) { reporter = new PipelineReporterExt( context.runtime, RubyUtil.PIPELINE_REPORTER_CLASS).initialize(context, rubyLogger, this ); pipelineSettings = pipelineConfig; configString = (RubyString) pipelineSettings.callMethod(context, "config_string"); configParts = pipelineSettings.toJava(PipelineConfig.class).getConfigParts(); - configHash = context.runtime.newString( - Hex.encodeHexString( - MessageDigest.getInstance("SHA1").digest(configString.getBytes()) - ) - ); + configHash = context.runtime.newString(Hex.encodeHexString(digestString(configString))); settings = pipelineSettings.callMethod(context, "settings"); final IRubyObject id = getSetting(context, "pipeline.id"); if (id.isNil()) { @@ -191,6 +187,19 @@ public final AbstractPipelineExt initialize(final ThreadContext context, return this; } + private static byte[] digestString(final RubyString content) { + final MessageDigest digest; + try { + digest = MessageDigest.getInstance("SHA1"); + } catch (NoSuchAlgorithmException ex) { + throw new AssertionError(ex); + } + + final ByteList bytes = content.getByteList(); + digest.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize()); + return digest.digest(); + } + /** * queue opening needs to happen out of the the initialize method because the * AbstractPipeline is used for pipeline config validation and the queue diff --git a/logstash-core/src/main/java/org/logstash/execution/JavaBasePipelineExt.java b/logstash-core/src/main/java/org/logstash/execution/JavaBasePipelineExt.java index f860aebbf63..3e401858730 100644 --- a/logstash-core/src/main/java/org/logstash/execution/JavaBasePipelineExt.java +++ b/logstash-core/src/main/java/org/logstash/execution/JavaBasePipelineExt.java @@ -67,8 +67,7 @@ public JavaBasePipelineExt(final Ruby runtime, final RubyClass metaClass) { } @JRubyMethod(required = 4) - public JavaBasePipelineExt initialize(final ThreadContext context, final IRubyObject[] args) - throws IncompleteSourceWithMetadataException, NoSuchAlgorithmException { + public JavaBasePipelineExt initialize(final ThreadContext context, final IRubyObject[] args) { initialize(context, args[0], args[1], args[2]); lirExecution = new CompiledPipeline( lir,