tabish121 commented on code in PR #5908:
URL: https://github.com/apache/activemq-artemis/pull/5908#discussion_r2337553736


##########
artemis-server/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/HAProxyMessageHandler.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.activemq.artemis.core.remoting.impl.netty;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.handler.codec.haproxy.HAProxyMessage;
+
+import static 
org.apache.activemq.artemis.utils.ProxyProtocolUtil.PROXY_PROTOCOL_DESTINATION_ADDRESS;
+import static 
org.apache.activemq.artemis.utils.ProxyProtocolUtil.PROXY_PROTOCOL_SOURCE_ADDRESS;
+import static 
org.apache.activemq.artemis.utils.ProxyProtocolUtil.PROXY_PROTOCOL_VERSION;
+
+public class HAProxyMessageHandler extends ChannelInboundHandlerAdapter {

Review Comment:
   As I understand things the case where an HA Proxy frontend is expected this 
handler should not pass through any bytes if the remote has not sent the proxy 
header first so I'd expect it would throw on the current path where it checks 
the skip bytes tracking.  No I get that HAProxyDecoder should have already 
failed but it seems at this level we should just build in precautions that 
protect against any expected cases that might arise from future code changes.  
   
   We could also just add the HAProxyDecoder into the pipeline here and not 
have it managed in the NettyAcceptor code which also adds this type into the 
pipeline making this fully self contained. Possibly something along the lines 
of the following:
   
   ```
   public class HAProxyMessageHandler extends ChannelInboundHandlerAdapter {
   
      private static final String HA_PROXY_DECODER_ID = 
"ARTEMIS_HA_PROXY_DECODER_INSTANCE";
   
      @Override
      public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
          ctx.pipeline().addFirst(HA_PROXY_DECODER_ID, new 
HAProxyMessageDecoder());
      }
   
      @Override
      public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
         ctx.pipeline().remove(HA_PROXY_DECODER_ID);
      }
   
      @Override
      public void channelRead(ChannelHandlerContext ctx, Object msg) throws 
Exception {
         if (msg instanceof HAProxyMessage haProxyMessage) {
            
ctx.channel().attr(PROXY_PROTOCOL_SOURCE_ADDRESS).set(haProxyMessage.sourceAddress()
 + ":" + Integer.toString(haProxyMessage.sourcePort()));
            
ctx.channel().attr(PROXY_PROTOCOL_DESTINATION_ADDRESS).set(haProxyMessage.destinationAddress()
 + ":" + Integer.toString(haProxyMessage.destinationPort()));
            
ctx.channel().attr(PROXY_PROTOCOL_VERSION).set(haProxyMessage.protocolVersion().toString());
   
            // once we get the HAProxyMessage and set the proper attributes our 
job is done
            ctx.pipeline().remove(this);
   
            // slice off the proxy-related bytes that have already been read so 
other protocol handlers don't choke on them
            ctx.fireChannelRead(((ByteBuf) msg).slice());
         } else {
            throw new IOException("Should not processes anything other than 
HAProxy messages in this handler");
         }
      }
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to