Log hello message as confidential
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / NetconfXMLToHelloMessageDecoder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.nettyutil.handler;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufUtil;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import org.opendaylight.netconf.api.messages.HelloMessage;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
24 import org.opendaylight.netconf.api.messages.NetconfMessage;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
26 import org.opendaylight.odlparent.logging.markers.Markers;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.xml.sax.SAXException;
31
32 /**
33  * Customized NetconfXMLToMessageDecoder that reads additional header with
34  * session metadata from
35  * {@link HelloMessage}*
36  * This handler should be replaced in pipeline by regular message handler as last step of negotiation.
37  * It serves as a message barrier and halts all non-hello netconf messages.
38  * Netconf messages after hello should be processed once the negotiation succeeded.
39  *
40  */
41 public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
42     private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
43
44     private static final List<byte[]> POSSIBLE_ENDS = List.of(
45             new byte[] { ']', '\n' },
46             new byte[] { ']', '\r', '\n' });
47     private static final List<byte[]> POSSIBLE_STARTS = List.of(
48             new byte[] { '[' },
49             new byte[] { '\r', '\n', '[' },
50             new byte[] { '\n', '[' });
51
52     // State variables do not have to by synchronized
53     // Netty uses always the same (1) thread per pipeline
54     // We use instance of this per pipeline
55     private final List<NetconfMessage> nonHelloMessages = new ArrayList<>();
56     private boolean helloReceived = false;
57
58     @Override
59     @VisibleForTesting
60     public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
61             throws IOException, SAXException {
62         if (in.readableBytes() == 0) {
63             LOG.debug("No more content in incoming buffer.");
64             return;
65         }
66
67         in.markReaderIndex();
68         try {
69             if (LOG.isTraceEnabled()) {
70                 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
71             }
72
73             byte[] bytes = new byte[in.readableBytes()];
74             in.readBytes(bytes);
75
76             logMessage(bytes);
77
78             // Extract bytes containing header with additional metadata
79             String additionalHeader = null;
80             if (startsWithAdditionalHeader(bytes)) {
81                 // Auth information containing username, ip address... extracted for monitoring
82                 int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
83                 if (endOfAuthHeader > -1) {
84                     byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader);
85                     additionalHeader = additionalHeaderToString(additionalHeaderBytes);
86                     bytes = Arrays.copyOfRange(bytes, endOfAuthHeader, bytes.length);
87                 }
88             }
89
90             Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
91
92             final NetconfMessage message = getNetconfMessage(additionalHeader, doc);
93             if (message instanceof HelloMessage) {
94                 if (helloReceived) {
95                     throw new IllegalStateException("Multiple hello messages received, unexpected hello: " + message);
96                 }
97                 out.add(message);
98                 helloReceived = true;
99             } else if (helloReceived) {
100                 // Non hello message, suspend the message and insert into cache
101                 LOG.debug("Netconf message received during negotiation, caching {}", message);
102                 nonHelloMessages.add(message);
103             } else {
104                 throw new IllegalStateException("Hello message not received, instead received: " + message);
105             }
106         } finally {
107             in.discardReadBytes();
108         }
109     }
110
111     private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) {
112         NetconfMessage msg = new NetconfMessage(doc);
113         if (HelloMessage.isHelloMessage(msg)) {
114             if (additionalHeader != null) {
115                 return new HelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
116             } else {
117                 return new HelloMessage(doc);
118             }
119         }
120
121         return msg;
122     }
123
124     private static int getAdditionalHeaderEndIndex(final byte[] bytes) {
125         for (byte[] possibleEnd : POSSIBLE_ENDS) {
126             int idx = findByteSequence(bytes, possibleEnd);
127
128             if (idx != -1) {
129                 return idx + possibleEnd.length;
130             }
131         }
132
133         return -1;
134     }
135
136     private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
137         if (bytes.length < sequence.length) {
138             throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
139         }
140         if (bytes.length == sequence.length) {
141             if (Arrays.equals(bytes, sequence)) {
142                 return 0;
143             } else {
144                 return -1;
145             }
146         }
147         int index = 0;
148         for (int i = 0; i < bytes.length; i++) {
149             if (bytes[i] == sequence[index]) {
150                 index++;
151                 if (index == sequence.length) {
152                     return i - index + 1;
153                 }
154             } else {
155                 index = 0;
156             }
157         }
158         return -1;
159     }
160
161     private static void logMessage(final byte[] bytes) {
162         if (LOG.isDebugEnabled()) {
163             LOG.debug(Markers.confidential(), "Parsing message \n{}",
164                 StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString());
165         }
166     }
167
168     private static boolean startsWithAdditionalHeader(final byte[] bytes) {
169         for (byte[] possibleStart : POSSIBLE_STARTS) {
170             int index = 0;
171             for (byte b : possibleStart) {
172                 if (bytes[index++] != b) {
173                     break;
174                 }
175
176                 if (index == possibleStart.length) {
177                     return true;
178                 }
179             }
180         }
181
182         return false;
183     }
184
185     private static String additionalHeaderToString(final byte[] bytes) {
186         return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
187     }
188
189     /**
190      * Get netconf messages received during negotiation.
191      *
192      * @return Collection of NetconfMessages that were not hello, but were received during negotiation.
193      */
194     public Iterable<NetconfMessage> getPostHelloNetconfMessages() {
195         return nonHelloMessages;
196     }
197 }