Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite Middleware without sinatra #16

Merged
merged 2 commits into from
Jul 26, 2024
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
21 changes: 15 additions & 6 deletions lib/puma/acme/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
module Puma
module Acme
# ACME challenge response handler for HTTP-01 challenges.
class Middleware < Sinatra::Base
class Middleware
def initialize(app, manager:)
@app = app
@manager = manager
end

super(app)
def call(env)
dup._call(env)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dup?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because rack middleware isn't threadsafe, and ivars leak across threads.

end

get '/.well-known/acme-challenge/:token' do
if (token = params[:token]).nil?
def _call(env)
if (token = token(env)).nil?
return @app.call(env)
end

if (answer = @manager.answer(type: CHALLENGE_TYPE, token: token)).nil?
return @app.call(env)
end

content_type 'text/plain'
answer.value
[200, {'content-type' => 'text/plain;charset=utf-8'}, [answer.value]]
end

private

def token(env)
path = ::Rack::Request.new(env).path_info
prefix = '/.well-known/acme-challenge/'
path[prefix.size..-1] if path.start_with?(prefix)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions puma-acme.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'minitest', '~> 5.14'
s.add_development_dependency 'minitest-mock_expectations', '~> 1.2'
s.add_development_dependency 'r509', '~> 1.0'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'vcr', '~> 6.1'
s.add_development_dependency 'webmock', '~> 3.19'
Expand Down
57 changes: 57 additions & 0 deletions test/puma/acme/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require_relative '../.././test_helper'
require "rack/test"

module Puma
module Acme
class MiddlewareTest < Minitest::Test
include ::Rack::Test::Methods

def test_it_returns_the_manager_answer_value_when_present
get '/.well-known/acme-challenge/good-token'
assert_equal '42', last_response.body
assert_equal 'text/plain;charset=utf-8', last_response.headers['content-type']
end

def test_it_passes_through_if_manager_provides_no_answer
get '/.well-known/acme-challenge/unknown-token'
assert_equal 'request not handled by middleware', last_response.body
end

def test_it_passes_unrecognized_requests_through
get '/'
assert_equal 'request not handled by middleware', last_response.body

get '/any-other/path'
assert_equal 'request not handled by middleware', last_response.body
end

class App
def call(env)
[200, {}, ['request not handled by middleware']]
end
end

class FakeManager
class Answer
def initialize(value)
@value = value
end
attr_reader :value
end

def answer(type:, token:)
Answer.new('42') if token == 'good-token'
end
end

def app
Puma::Acme::Middleware.new(
App.new,
manager: FakeManager.new
)
end
end
end
end
Loading