From ed56dfeb487c12eecd3d3bec6e34cda95f4ba392 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Thu, 10 Apr 2025 13:08:46 +0900 Subject: [PATCH] http_server: stop fallback to WEBrick WEBrick is no longer recommended for production use. We don't need this feature because it was for supporting Ruby < 2.3. (#2447) Signed-off-by: Daijiro Fukuda --- fluentd.gemspec | 2 +- lib/fluent/plugin_helper/http_server.rb | 9 +- .../http_server/compat/server.rb | 92 ------------------- .../compat/ssl_context_extractor.rb | 52 ----------- .../http_server/compat/webrick_handler.rb | 58 ------------ 5 files changed, 2 insertions(+), 211 deletions(-) delete mode 100644 lib/fluent/plugin_helper/http_server/compat/server.rb delete mode 100644 lib/fluent/plugin_helper/http_server/compat/ssl_context_extractor.rb delete mode 100644 lib/fluent/plugin_helper/http_server/compat/webrick_handler.rb diff --git a/fluentd.gemspec b/fluentd.gemspec index 44af5531e1..03d476e18f 100644 --- a/fluentd.gemspec +++ b/fluentd.gemspec @@ -35,6 +35,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency("webrick", ["~> 1.4"]) gem.add_runtime_dependency("zstd-ruby", ["~> 1.5"]) gem.add_runtime_dependency("uri", '~> 1.0') + gem.add_runtime_dependency("async-http", "~> 0.86") # gems that aren't default gems as of Ruby 3.4 gem.add_runtime_dependency("base64", ["~> 0.2"]) @@ -68,7 +69,6 @@ Gem::Specification.new do |gem| gem.add_development_dependency("test-unit", ["~> 3.3"]) gem.add_development_dependency("test-unit-rr", ["~> 1.0"]) gem.add_development_dependency("oj", [">= 2.14", "< 4"]) - gem.add_development_dependency("async-http", "~> 0.86") gem.add_development_dependency("console", "~> 1.30") gem.add_development_dependency("aws-sigv4", ["~> 1.8"]) gem.add_development_dependency("aws-sdk-core", ["~> 3.191"]) diff --git a/lib/fluent/plugin_helper/http_server.rb b/lib/fluent/plugin_helper/http_server.rb index 9a7892cbbb..e609e29a7e 100644 --- a/lib/fluent/plugin_helper/http_server.rb +++ b/lib/fluent/plugin_helper/http_server.rb @@ -14,16 +14,9 @@ # limitations under the License. # -begin - require 'async' - require 'fluent/plugin_helper/http_server/server' -rescue LoadError => _ - require 'fluent/plugin_helper/http_server/compat/server' - Fluent::PluginHelper::HttpServer::Server = Fluent::PluginHelper::HttpServer::Compat::Server -end - require 'fluent/plugin_helper/thread' require 'fluent/plugin_helper/server' # For Server::ServerTransportParams +require 'fluent/plugin_helper/http_server/server' require 'fluent/plugin_helper/http_server/ssl_context_builder' module Fluent diff --git a/lib/fluent/plugin_helper/http_server/compat/server.rb b/lib/fluent/plugin_helper/http_server/compat/server.rb deleted file mode 100644 index 56c3281a9a..0000000000 --- a/lib/fluent/plugin_helper/http_server/compat/server.rb +++ /dev/null @@ -1,92 +0,0 @@ -# -# Fluentd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require 'fluent/plugin_helper/http_server/methods' -require 'fluent/plugin_helper/http_server/compat/webrick_handler' -require 'fluent/plugin_helper/http_server/compat/ssl_context_extractor' - -module Fluent - module PluginHelper - module HttpServer - module Compat - class Server - # @param logger [Logger] - # @param default_app [Object] ignored option. only for compat - # @param tls_context [OpenSSL::SSL::SSLContext] - def initialize(addr:, port:, logger:, default_app: nil, tls_context: nil) - @addr = addr - @port = port - @logger = logger - - config = { - BindAddress: @addr, - Port: @port, - Logger: WEBrick::Log.new(STDERR, WEBrick::Log::FATAL), - AccessLog: [], - } - if tls_context - require 'webrick/https' - @logger.warn('Webrick ignores given TLS version') - tls_opt = Fluent::PluginHelper::HttpServer::Compat::SSLContextExtractor.extract(tls_context) - config = tls_opt.merge(**config) - end - - @server = WEBrick::HTTPServer.new(config) - - # @example ["/example.json", :get, handler object] - @methods = [] - - if block_given? - yield(self) - end - end - - def start(notify = nil) - build_handler - notify.push(:ready) - @logger.debug('Start webrick HTTP server listening') - @server.start - end - - def stop - @server.shutdown - @server.stop - end - - HttpServer::Methods::ALL.map { |e| e.downcase.to_sym }.each do |name| - define_method(name) do |path, app = nil, &block| - if (block && app) || (!block && !app) - raise 'You must specify either app or block in the same time' - end - - # Do not build a handler class here to able to handle multiple methods for single path. - @methods << [path, name, app || block] - end - end - - private - - def build_handler - @methods.group_by(&:first).each do |(path, rest)| - klass = Fluent::PluginHelper::HttpServer::Compat::WebrickHandler.build(**Hash[rest.map { |e| [e[1], e[2]] }]) - @server.mount(path, klass) - end - end - end - end - end - end -end diff --git a/lib/fluent/plugin_helper/http_server/compat/ssl_context_extractor.rb b/lib/fluent/plugin_helper/http_server/compat/ssl_context_extractor.rb deleted file mode 100644 index 320f52d373..0000000000 --- a/lib/fluent/plugin_helper/http_server/compat/ssl_context_extractor.rb +++ /dev/null @@ -1,52 +0,0 @@ -# -# Fluentd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -module Fluent - module PluginHelper - module HttpServer - module Compat - # This class converts OpenSSL::SSL::SSLContext to Webrick SSL Config because webrick does not have interface to pass OpenSSL::SSL::SSLContext directory - # https://github.com/ruby/webrick/blob/v1.6.0/lib/webrick/ssl.rb#L67-L88 - class SSLContextExtractor - - # - # memo: https://github.com/ruby/webrick/blob/v1.6.0/lib/webrick/ssl.rb#L180-L205 - # @param ctx [OpenSSL::SSL::SSLContext] - def self.extract(ctx) - { - SSLEnable: true, - SSLPrivateKey: ctx.key, - SSLCertificate: ctx.cert, - SSLClientCA: ctx.client_ca, - SSLExtraChainCert: ctx.extra_chain_cert, - SSLCACertificateFile: ctx.ca_file, - SSLCACertificatePath: ctx.ca_path, - SSLCertificateStore: ctx.cert_store, - SSLTmpDhCallback: ctx.tmp_dh_callback, - SSLVerifyClient: ctx.verify_mode, - SSLVerifyDepth: ctx.verify_depth, - SSLVerifyCallback: ctx.verify_callback, - SSLServerNameCallback: ctx.servername_cb, - SSLTimeout: ctx.timeout, - SSLOptions: ctx.options, - SSLCiphers: ctx.ciphers, - } - end - end - end - end - end -end diff --git a/lib/fluent/plugin_helper/http_server/compat/webrick_handler.rb b/lib/fluent/plugin_helper/http_server/compat/webrick_handler.rb deleted file mode 100644 index c3b2f00582..0000000000 --- a/lib/fluent/plugin_helper/http_server/compat/webrick_handler.rb +++ /dev/null @@ -1,58 +0,0 @@ -# -# Fluentd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require 'webrick' -require 'json' - -module Fluent - module PluginHelper - module HttpServer - module Compat - class WebrickHandler - # **opt is enough. but I wrote a signature explicitly for readability - def self.build(get: nil, head: nil, post: nil, put: nil, patch: nil, delete: nil, connect: nil, options: nil, trace: nil) - opt = { get: get, head: head, post: post, put: put, patch: patch, delete: delete, connect: connect, options: options, trace: trace } - - Class.new(WEBrick::HTTPServlet::AbstractServlet) do - HttpServer::Methods::ALL.each do |name| - define_method("do_#{name}") do |request, response| - code, headers, body = - if request.path_info != '' - render_json(404, 'message' => 'Not found') - else - begin - opt[name.downcase.to_sym].call(request) - rescue => _ - render_json(500, 'message' => 'Something went wrong') - end - end - - response.status = code - headers.each { |k, v| response[k] = v } - response.body = body - end - end - - def render_json(code, obj) - [code, { 'Content-Type' => 'application/json' }, obj.to_json] - end - end - end - end - end - end - end -end