summaryrefslogtreecommitdiff
path: root/qpid/tools/src/java/qpid-qmf2-tools/src/main/java/org/apache/qpid/qmf2/tools/ConnectionAudit.java
blob: 7f6109cb7a474362ddbbe9c66ca7bb43d0a82157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 *
 * 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.qpid.qmf2.tools;

// JMS Imports
import javax.jms.Connection;

// Misc Imports
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

// For DOM parsing the whitelist
import org.w3c.dom.*;
import javax.xml.parsers.*;

// QMF2 Imports
import org.apache.qpid.qmf2.common.ObjectId;
import org.apache.qpid.qmf2.common.QmfEvent;
import org.apache.qpid.qmf2.common.QmfEventListener;
import org.apache.qpid.qmf2.common.QmfException;
import org.apache.qpid.qmf2.common.WorkItem;
import org.apache.qpid.qmf2.console.Agent;
import org.apache.qpid.qmf2.console.AgentRestartedWorkItem;
import org.apache.qpid.qmf2.console.Console;
import org.apache.qpid.qmf2.console.EventReceivedWorkItem;
import org.apache.qpid.qmf2.console.QmfConsoleData;
import org.apache.qpid.qmf2.util.ConnectionHelper;
import org.apache.qpid.qmf2.util.GetOpt;

/**
 * Audits connections to one or more Qpid message brokers.
 * <pre>
 * Exchange and Queue names are checked against a whitelist and if no match is found an alert is generated.
 * 
 * If no broker-addr is supplied, ConnectionAudit connects to 'localhost:5672'.
 * 
 * [broker-addr] syntax:
 * 
 * [username/password@] hostname
 * ip-address [:&lt;port&gt;]
 * 
 * Examples:
 * 
 * $ ConnectionAudit localhost:5672
 * $ ConnectionAudit 10.1.1.7:10000
 * $ ConnectionAudit guest/guest@broker-host:10000
 * 
 * Options:
 *   -h, --help            show this help message and exit
 *   --sasl-mechanism=&lt;mech&gt;
 *                         SASL mechanism for authentication (e.g. EXTERNAL,
 *                         ANONYMOUS, PLAIN, CRAM-MD5, DIGEST-MD5, GSSAPI). SASL
 *                         automatically picks the most secure available
 *                         mechanism - use this option to override.
 *   --whitelist=&lt;whitelist XML document&gt;
 *                         The fully qualified name of the whitelist XML file,
 *                         default is ./whitelist.xml
 * 
 * </pre>
 * An example whitelist is illustrated below, note that in this example the exchanges associated with management
 * have been whitelisted to remove spurious alerts caused by the temporary management queues.
 * <pre>
 *&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 *&lt;whitelist&gt;
 *    &lt;exchangeWhitelist&gt;
 *        &lt;exchange&gt;qmf.default.topic&lt;/exchange&gt;
 *        &lt;exchange&gt;qmf.default.direct&lt;/exchange&gt;
 *        &lt;exchange&gt;qpid.management&lt;/exchange&gt;
 *        &lt;exchange&gt;amq.direct&lt;/exchange&gt;
 *        &lt;exchange&gt;&lt;/exchange&gt;
 *    &lt;/exchangeWhitelist&gt;
 *    &lt;queueWhitelist&gt;
 *        &lt;queue&gt;testqueue&lt;/queue&gt;
 *    &lt;/queueWhitelist&gt;
 *&lt;/whitelist&gt;
 * </pre>

 * @author Fraser Adams
 */
public final class ConnectionAudit implements QmfEventListener
{
    private static final String _usage =
    "Usage: ConnectionAudit [options] [broker-addr]...\n";

    private static final String _description =
    "Audits connections to one or more Qpid message brokers.\n" +
    "Exchange and Queue names are checked against a whitelist and if no match is found an alert is generated.\n" +
    "\n" +
    "If no broker-addr is supplied, ConnectionAudit connects to 'localhost:5672'.\n" +
    "\n" +
    "[broker-addr] syntax:\n" +
    "\n" +
    "[username/password@] hostname\n" +
    "ip-address [:<port>]\n" +
    "\n" +
    "Examples:\n" +
    "\n" +
    "$ ConnectionAudit localhost:5672\n" +
    "$ ConnectionAudit 10.1.1.7:10000\n" +
    "$ ConnectionAudit guest/guest@broker-host:10000\n";

    private static final String _options =
    "Options:\n" +
    "  -h, --help            show this help message and exit\n" +
    "  --sasl-mechanism=<mech>\n" +
    "                        SASL mechanism for authentication (e.g. EXTERNAL,\n" +
    "                        ANONYMOUS, PLAIN, CRAM-MD5, DIGEST-MD5, GSSAPI). SASL\n" +
    "                        automatically picks the most secure available\n" +
    "                        mechanism - use this option to override.\n" +
    "  --whitelist=<whitelist XML document>\n" +
    "                        The fully qualified name of the whitelist XML file,\n" +
    "                        default is ./whitelist.xml\n";


    private final String _url;
    private final String _whitelist;
    private long _whitelistLastModified = 0;
    private Console _console;

    // The sets to be used as the whitelists.
    private Set<String> _exchangeWhitelist = new HashSet<String>();
    private Set<String> _queueWhitelist = new HashSet<String>();

    /**
     * Basic constructor. Creates JMS Session, Initialises Destinations, Producers &amp; Consumers and starts connection.
     * @param url the connection URL.
     * @param connectionOptions the options String to pass to ConnectionHelper.
     * @param whitelist the path name of the whitelist XML file.
     */
    public ConnectionAudit(final String url, final String connectionOptions, final String whitelist)
    {
        System.out.println("Connecting to " + url);
        _url = url;
        _whitelist = whitelist;
        try
        {
            Connection connection = ConnectionHelper.createConnection(url, connectionOptions);        
            _console = new Console(this);
            _console.addConnection(connection);
            checkExistingSubscriptions();
        }
        catch (QmfException qmfe)
        {
            System.err.println ("QmfException " + qmfe.getMessage() + " caught in ConnectionAudit constructor");
        }
    }

    /**
     * When we start up we need to check any subscriptions that already exist against the whitelist.
     * Subsequent checks are made only when we receive new subscribe events.
     */
    private void checkExistingSubscriptions()
    {
        readWhitelist();
        List<QmfConsoleData> subscriptions = _console.getObjects("org.apache.qpid.broker", "subscription");
        for (QmfConsoleData subscription : subscriptions)
        {
            QmfConsoleData queue = dereference(subscription.getRefValue("queueRef"));
            QmfConsoleData session = dereference(subscription.getRefValue("sessionRef"));
            QmfConsoleData connection = dereference(session.getRefValue("connectionRef"));
            
            String queueName = queue.getStringValue("name");
            String address = connection.getStringValue("address");
            String timestamp = new Date(subscription.getCreateTime()/1000000l).toString();
            validateQueue(queueName, address, timestamp);
        }
    }

    /**
     * Dereferences an ObjectId returning a QmfConsoleData.
     * @param ref the ObjectId to be dereferenced.
     * @return the dereferenced QmfConsoleData object or null if the object can't be found.
     */
    private QmfConsoleData dereference(final ObjectId ref)
    {
        List<QmfConsoleData> data = _console.getObjects(ref);
        if (data.size() == 1)
        {
            return data.get(0);
        }
        return null;
    }

    /**
     * Looks up the exchange and binding information from the supplied queuename then calls the main validateQueue()
     * @param queueName the name of the queue that we want to check against the whitelists.
     * @param exchangeName the name of the exchange that the queue we want to check against the whitelists is bound to.
     * @param binding the binding associating queue "queueName" with exchange "exchangeName".
     * @param address the connection address information for the subscription.
     * @param timestamp the timestamp of the subscription.
     */
    private void validateQueue(final String queueName, String exchangeName, final QmfConsoleData binding,
                               final String address, final String timestamp)
    {
        if (_exchangeWhitelist.contains(exchangeName))
        { // Check exchangeName against the exchangeWhitelist and if it's in there we simply return.
            return;
        }

        if (_queueWhitelist.contains(queueName))
        { // Check queueName against the queueWhitelist and if it's in there we simply return.
            return;
        }

        if (exchangeName.equals(""))
        { // Make exchangeName render more prettily if necessary.
            exchangeName = "''";
        }

        String bindingKey = binding.getStringValue("bindingKey");
        Map arguments = (Map)binding.getValue("arguments");
        if (arguments.isEmpty())
        {
            System.out.printf("%s ALERT ConnectionAudit.validateQueue() validation failed for queue: %s with binding[%s] => %s from address: %s with connection timestamp %s\n\n", new Date().toString(), queueName, bindingKey, exchangeName, address, timestamp);
        }
        else
        { // If there are binding arguments then it's a headers exchange so display accordimgly.
            System.out.printf("%s ALERT ConnectionAudit.validateQueue() validation failed for queue: %s with binding[%s] => %s %s from address: %s with connection timestamp %s\n\n", new Date().toString(), queueName, bindingKey, exchangeName, arguments, address, timestamp);
        }
    }

    /**
     * Looks up the exchange and binding information from the supplied queuename then calls the main validateQueue()
     * @param queueName the name of the queue that we want to check against the whitelists.
     * @param address the connection address information for the subscription.
     * @param timestamp the timestamp of the subscription.
     */
    private void validateQueue(final String queueName, final String address, final String timestamp)
    {
        ObjectId queueId = null;
        List<QmfConsoleData> queues = _console.getObjects("org.apache.qpid.broker", "queue");
        for (QmfConsoleData queue : queues)
        { // We first have to find the ObjectId of the queue called queueName.
            if (queue.getStringValue("name").equals(queueName))
            {
                queueId = queue.getObjectId();
                break;
            }
        }

        if (queueId == null)
        {
            System.out.printf("%s ERROR ConnectionAudit.validateQueue() %s reference couldn't be found\n",
                              new Date().toString(), queueName);
        }
        else
        { // If we've got the queue's ObjectId we then find the binding that references it.
            List<QmfConsoleData> bindings = _console.getObjects("org.apache.qpid.broker", "binding");
            for (QmfConsoleData binding : bindings)
            {
                ObjectId queueRef = binding.getRefValue("queueRef");
                if (queueRef.equals(queueId))
                { // We've found a binding that matches queue queueName so look up the associated exchange and validate.
                    QmfConsoleData exchange = dereference(binding.getRefValue("exchangeRef"));
                    String exchangeName = exchange.getStringValue("name");
                    validateQueue(queueName, exchangeName, binding, address, timestamp);
                }
            }
        }
    }

    /**
     * Handles WorkItems delivered by the Console.
     * <p>
     * If we receive an EventReceivedWorkItem check if it is a subscribe event. If it is we check if the whitelist has 
     * changed, and if it has we re-read it. We then extract the queue name, exchange name, binding, connection address
     * and timestamp and validate with the whitelsist.
     * <p>
     * If we receive an AgentRestartedWorkItem we revalidate all subscriptions as it's possible that a client connection
     * could have been made to the broker before ConnectionAudit has successfully re-established its own connections.
     * @param wi a QMF2 WorkItem object
     */
    public void onEvent(final WorkItem wi)
    {
        if (wi instanceof EventReceivedWorkItem)
        {
            EventReceivedWorkItem item = (EventReceivedWorkItem)wi;
            QmfEvent event = item.getEvent();
            String className = event.getSchemaClassId().getClassName();
            if (className.equals("subscribe"))
            {
                readWhitelist();
                String queueName = event.getStringValue("qName");
                String address = event.getStringValue("rhost");
                String timestamp = new Date(event.getTimestamp()/1000000l).toString();
                validateQueue(queueName, address, timestamp);
            }
        }
        else if (wi instanceof AgentRestartedWorkItem)
        {
            checkExistingSubscriptions();
        }
    }

    /**
     * This method first checks if the whitelist file exists, if not it clears the sets used as whitelists
     * so that no whitelisting is applied. If the whitelist file does exist it is parsed by a DOM parser.
     * <p>
     * We look for all exchange and queue elements and populate the respective whitelist sets with their
     * contents. Note that we check the whitelist file update time to avoid reading it if it hasn't been changed
     */
    private void readWhitelist()
    {
        File file = new File(_whitelist);
        if (file.exists())
        {
            long mtime = file.lastModified();
            if (mtime != _whitelistLastModified)
            {
                _whitelistLastModified = mtime;
                _exchangeWhitelist.clear();
                _queueWhitelist.clear();

                try
                {
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = factory.newDocumentBuilder();
                    Document doc = docBuilder.parse(file);
    
                    Element whitelist = doc.getDocumentElement();
                    if (whitelist.getNodeName().equals("whitelist"))
                    {
                        NodeList children = whitelist.getChildNodes();
                        for (int i = 0; i < children.getLength(); i++)
                        {
                            Node child = children.item(i);
                            if (child.getNodeName().equals("exchangeWhitelist"))
                            {
                                NodeList exchanges = child.getChildNodes();
                                for (int j = 0; j < exchanges.getLength(); j++)
                                {
                                    Node node = exchanges.item(j);
                                    if (node.getNodeName().equals("exchange"))
                                    {
                                        if (node.hasChildNodes())
                                        {
                                            String exchange = node.getFirstChild().getNodeValue();
                                            _exchangeWhitelist.add(exchange);
                                        }
                                        else
                                        {
                                            _exchangeWhitelist.add("");
                                        }
                                    }
                                }
                            }
                            else if (child.getNodeName().equals("queueWhitelist"))
                            {
                                NodeList queues = child.getChildNodes();
                                for (int j = 0; j < queues.getLength(); j++)
                                {
                                    Node node = queues.item(j);
                                    if (node.getNodeName().equals("queue"))
                                    {
                                        if (node.hasChildNodes())
                                        {
                                            String queue = node.getFirstChild().getNodeValue();
                                            _queueWhitelist.add(queue);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                { // Failed to parse correctly.
                    System.out.println("Exception " + e + " while reading " + _whitelist);
                    System.out.println(new Date().toString() + " WARN ConnectionAudit.readWhitelist() " + 
                                       _whitelist + " failed: " + e.getMessage());
                    return;
                }
            }
        }
        else 
        { // If whitelist file doesn't exist log a warning and clear the whitelists.
            System.out.println(new Date().toString() + " WARN ConnectionAudit.readWhitelist() " + 
                               _whitelist + " doesn't exist");
            _exchangeWhitelist.clear();
            _queueWhitelist.clear();
        }
    } // End of readWhitelist()

    /**
     * Runs ConnectionAudit.
     * @param args the command line arguments.
     */
    public static void main(final String[] args)
    {
        String logLevel = System.getProperty("amqj.logging.level");
        logLevel = (logLevel == null) ? "FATAL" : logLevel; // Set default log level to FATAL rather than DEBUG.
        System.setProperty("amqj.logging.level", logLevel);

        String[] longOpts = {"help", "whitelist=", "sasl-mechanism="};
        try
        {
            String connectionOptions = "{reconnect: true}";
            String whitelist = "./whitelist.xml";
            GetOpt getopt = new GetOpt(args, "h", longOpts);
            List<String[]> optList = getopt.getOptList();
            String[] cargs = {};
            cargs = getopt.getEncArgs().toArray(cargs);
            for (String[] opt : optList)
            {
                if (opt[0].equals("-h") || opt[0].equals("--help"))
                {
                    System.out.println(_usage);
                    System.out.println(_description);
                    System.out.println(_options);
                    System.exit(1);
                }
                else if (opt[0].equals("--whitelist"))
                {
                    whitelist = opt[1];
                }
                else if (opt[0].equals("--sasl-mechanism"))
                {
                    connectionOptions = "{reconnect: true, sasl_mechs: " + opt[1] + "}";
                }
            }

            int nargs = cargs.length;
            if (nargs == 0)
            {
                cargs = new String[] {"localhost"};
            }

            for (String url : cargs)
            {
                ConnectionAudit eventPrinter = new ConnectionAudit(url, connectionOptions, whitelist);
            }
        }
        catch (IllegalArgumentException e)
        {
            System.out.println(_usage);
            System.exit(1);
        }

        try
        {   // Block here
            Thread.currentThread().join();
        }
        catch (InterruptedException ie)
        {
        }
    }
}