Skip to content

Commit

Permalink
Prevent MQTT entities to be bound to AMQP exchanges
Browse files Browse the repository at this point in the history
This is a temporary "hack" that can be removed when MQTT Exchanges and
Queues no longer inherit from AMQP entitites.
  • Loading branch information
spuun committed Dec 23, 2024
1 parent 68a5494 commit 49aa426
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lavinmq/amqp/exchange/exchange.cr
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ module LavinMQ
notify_observers(ExchangeEvent::Deleted)
end

# This outer macro will add a finished macro hook to all inherited classes
# in LavinMQ::AMQP namespace.
macro inherited
{% if @type.name.starts_with?("LavinMQ::AMQP::") %}
# This macro will find the "bind" method of classes inhering from this class
# and redfine them to raise AccessRefused exception if the first argument
# isn't a type in LavinMQ::AMQP namespace.
#
# TODO remove this when LavinMQ::MQTT::Session no longer inherit from
# LavinMQ::AMQP::Queue and LavinMQ::MQTT::Exchange no longer inherit from
# lavinMQ::AMQP::Exchange
macro finished
\{% if (m = @type.methods.find(&.name.== "bind")) %}
def bind(\{{ m.args.map(&.id).join(",").id}}) : Bool
unless \{{m.args[0].name.id}}.class.name.starts_with?("LavinMQ::AMQP::")
raise AccessRefused.new(self)
end
\{{ m.body }}
end
\{% end %}
end
{% end %}
end

def bind(destination : LavinMQ::Destination, routing_key, headers = nil) : Bool
raise AccessRefused.new(self)
end
Expand Down

0 comments on commit 49aa426

Please sign in to comment.