From 6b9108cf9c51ac0d8dd62d97d13bd4e0d43c51b2 Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Thu, 12 Nov 2026 09:24:29 -0300 Subject: [PATCH 1/2] Read the private key file when is served by WEBrick When has extra_conf, in_prometheus serves the metrics with WEBrick and loads private_key_path with OpenSSL::PKey.read. That call was given the path instead of the file content since bc7c2ba, so it always raised "Could not parse PKey" and the plugin failed to start. Read the file before parsing it, as is done for certificate_path. Signed-off-by: Lucas Barros --- lib/fluent/plugin/in_prometheus.rb | 2 +- spec/fluent/plugin/in_prometheus_spec.rb | 44 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_prometheus.rb b/lib/fluent/plugin/in_prometheus.rb index 9c4f5fc..d827bfb 100644 --- a/lib/fluent/plugin/in_prometheus.rb +++ b/lib/fluent/plugin/in_prometheus.rb @@ -178,7 +178,7 @@ def start_webrick end if @ssl['private_key_path'] - key = OpenSSL::PKey.read(@ssl['private_key_path']) + key = OpenSSL::PKey.read(File.read(@ssl['private_key_path'])) ssl_config[:SSLPrivateKey] = key end diff --git a/spec/fluent/plugin/in_prometheus_spec.rb b/spec/fluent/plugin/in_prometheus_spec.rb index a0c4447..5368b16 100644 --- a/spec/fluent/plugin/in_prometheus_spec.rb +++ b/spec/fluent/plugin/in_prometheus_spec.rb @@ -4,6 +4,8 @@ require 'net/http' require 'zlib' +require 'tmpdir' +require 'fileutils' describe Fluent::Plugin::PrometheusInput do CONFIG = %[ @@ -157,6 +159,48 @@ end end + context 'when extra_conf is used with certificate_path and private_key_path' do + let(:tmpdir) { Dir.mktmpdir } + let(:key) { OpenSSL::PKey::RSA.new(2048) } + let(:cert) do + cert = OpenSSL::X509::Certificate.new + cert.version = 2 + cert.serial = 1 + cert.subject = cert.issuer = OpenSSL::X509::Name.parse('/CN=localhost') + cert.public_key = key.public_key + cert.not_before = Time.now - 60 + cert.not_after = Time.now + 3600 + cert.sign(key, OpenSSL::Digest::SHA256.new) + cert + end + let(:cert_path) { File.join(tmpdir, 'cert.pem').tap { |path| File.write(path, cert.to_pem) } } + let(:private_key_path) { File.join(tmpdir, 'key.pem').tap { |path| File.write(path, key.to_pem) } } + let(:config) do + %[ + @type prometheus + bind 127.0.0.1 + + enable true + certificate_path #{cert_path} + private_key_path #{private_key_path} + extra_conf { "SSLVerifyClient": 0 } + + ] + end + + after { FileUtils.rm_rf(tmpdir) } + + it 'serves the given certificate' do + driver.run(timeout: 1) do + Net::HTTP.start('127.0.0.1', port, verify_mode: OpenSSL::SSL::VERIFY_NONE, use_ssl: true) do |http| + res = http.request(Net::HTTP::Get.new('/metrics')) + expect(res.code).to eq('200') + expect(http.peer_cert.to_der).to eq(cert.to_der) + end + end + end + end + context 'cert_path and private_key_path combination' do let(:config) do %[ From a0154162654501c394a002927f6269d4d391b536 Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Fri, 13 Nov 2026 21:18:05 -0300 Subject: [PATCH 2/2] Do not include private_key_path in the error when the file cannot be read When private_key_path holds the PEM text itself, File.read raised an error whose message had the whole key, and fluentd logged it. Signed-off-by: Lucas Barros --- lib/fluent/plugin/in_prometheus.rb | 7 ++++++- spec/fluent/plugin/in_prometheus_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/fluent/plugin/in_prometheus.rb b/lib/fluent/plugin/in_prometheus.rb index d827bfb..aacca56 100644 --- a/lib/fluent/plugin/in_prometheus.rb +++ b/lib/fluent/plugin/in_prometheus.rb @@ -178,7 +178,12 @@ def start_webrick end if @ssl['private_key_path'] - key = OpenSSL::PKey.read(File.read(@ssl['private_key_path'])) + begin + key_contents = File.read(@ssl['private_key_path']) + rescue SystemCallError => e + raise Fluent::ConfigError, "failed to read private_key_path (#{e.class})", cause: nil + end + key = OpenSSL::PKey.read(key_contents) ssl_config[:SSLPrivateKey] = key end diff --git a/spec/fluent/plugin/in_prometheus_spec.rb b/spec/fluent/plugin/in_prometheus_spec.rb index 5368b16..6b74945 100644 --- a/spec/fluent/plugin/in_prometheus_spec.rb +++ b/spec/fluent/plugin/in_prometheus_spec.rb @@ -199,6 +199,14 @@ end end end + + context 'when private_key_path holds the key itself instead of a path' do + let(:private_key_path) { %["#{key.to_pem.gsub("\n", '\n')}"] } + + it 'raises an error that does not include the key' do + expect { driver.run(timeout: 1) }.to raise_error(Fluent::ConfigError, /\Afailed to read private_key_path \(Errno::\w+\)\z/) { |e| expect(e.cause).to be_nil } + end + end end context 'cert_path and private_key_path combination' do