Review Board 1.7.22


Fix for FLUME-2142

Review Request #13264 - Created Aug. 4, 2013 and updated

Ashish Paliwal
FLUME-2142
Reviewers
Flume
flume-git
1. Negative test where non-https client fails to connect to https source
2. Test that events are available on the channel after being written to https
Test case code is added
flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java
Revision 6c9fd86 New Change
[20] 39 lines
[+20]
40

    
   
40

   
41
import javax.net.ssl.*;
41
import javax.net.ssl.*;
42
import javax.servlet.http.HttpServletResponse;
42
import javax.servlet.http.HttpServletResponse;
43
import java.io.IOException;
43
import java.io.IOException;
44
import java.lang.reflect.Type;
44
import java.lang.reflect.Type;

    
   
45
import java.net.HttpURLConnection;
45
import java.net.ServerSocket;
46
import java.net.ServerSocket;
46
import java.net.URL;
47
import java.net.URL;
47
import java.security.SecureRandom;
48
import java.security.SecureRandom;
48
import java.security.cert.CertificateException;
49
import java.security.cert.CertificateException;
49
import java.util.ArrayList;
50
import java.util.ArrayList;
[+20] [20] 31 lines
[+20] [+] public static void setUpClass() throws Exception {
81

    
   
82

   
82
    source = new HTTPSource();
83
    source = new HTTPSource();
83
    channel = new MemoryChannel();
84
    channel = new MemoryChannel();
84

    
   
85

   
85
    httpsSource = new HTTPSource();
86
    httpsSource = new HTTPSource();
86
//    httpsChannel = new MemoryChannel();
87
    httpsSource.setName("HTTPS Source");
87

    
   
88

   
88
    Context ctx = new Context();
89
    Context ctx = new Context();
89
    ctx.put("capacity", "100");
90
    ctx.put("capacity", "100");
90
    Configurables.configure(channel, ctx);
91
    Configurables.configure(channel, ctx);
91
//    Configurables.configure(httpsChannel, ctx);

   
92

    
   
92

   
93
    List<Channel> channels = new ArrayList<Channel>(1);
93
    List<Channel> channels = new ArrayList<Channel>(1);
94
    channels.add(channel);
94
    channels.add(channel);
95

    
   
95

   
96
    ChannelSelector rcs = new ReplicatingChannelSelector();
96
    ChannelSelector rcs = new ReplicatingChannelSelector();
97
    rcs.setChannels(channels);
97
    rcs.setChannels(channels);
98

    
   
98

   
99
    source.setChannelProcessor(new ChannelProcessor(rcs));
99
    source.setChannelProcessor(new ChannelProcessor(rcs));
100

    
   
100

   
101
    channel.start();
101
    channel.start();
102

    
   
102

   
103
    // Channel for HTTPS source

   
104
//    List<Channel> sslChannels = new ArrayList<Channel>(1);

   
105
//    channels.add(httpsChannel);

   
106
//

   
107
//    ChannelSelector sslRcs = new ReplicatingChannelSelector();

   
108
//    rcs.setChannels(sslChannels);

   
109

    
   

   
110
    httpsSource.setChannelProcessor(new ChannelProcessor(rcs));
103
    httpsSource.setChannelProcessor(new ChannelProcessor(rcs));
111
//    httpsChannel.start();

   
112

    
   
104

   
113
    // HTTP context
105
    // HTTP context
114
    Context context = new Context();
106
    Context context = new Context();
115

    
   
107

   
116
    context.put("port", String.valueOf(selectedPort));
108
    context.put("port", String.valueOf(selectedPort));
[+20] [20] 16 lines
[+20] public static void setUpClass() throws Exception {
133
  @AfterClass
125
  @AfterClass
134
  public static void tearDownClass() throws Exception {
126
  public static void tearDownClass() throws Exception {
135
    source.stop();
127
    source.stop();
136
    channel.stop();
128
    channel.stop();
137
    httpsSource.stop();
129
    httpsSource.stop();
138
//    httpsChannel.stop();

   
139
  }
130
  }
140

    
   
131

   
141
  @Before
132
  @Before
142
  public void setUp() {
133
  public void setUp() {
143
    httpClient = new DefaultHttpClient();
134
    httpClient = new DefaultHttpClient();
[+20] [20] 166 lines
[+20] [+] public void testHttps() throws Exception {
310
    for (int i = 0; i < 10; i++) {
301
    for (int i = 0; i < 10; i++) {
311
      Map<String, String> input = Maps.newHashMap();
302
      Map<String, String> input = Maps.newHashMap();
312
      for (int j = 0; j < 10; j++) {
303
      for (int j = 0; j < 10; j++) {
313
        input.put(String.valueOf(i) + String.valueOf(j), String.valueOf(i));
304
        input.put(String.valueOf(i) + String.valueOf(j), String.valueOf(i));
314
      }
305
      }

    
   
306
      input.put("MsgNum", String.valueOf(i));
315
      JSONEvent e = new JSONEvent();
307
      JSONEvent e = new JSONEvent();
316
      e.setHeaders(input);
308
      e.setHeaders(input);
317
      e.setBody(String.valueOf(rand.nextGaussian()).getBytes("UTF-8"));
309
      e.setBody(String.valueOf(rand.nextGaussian()).getBytes("UTF-8"));
318
      events.add(e);
310
      events.add(e);
319
    }
311
    }
[+20] [20] 38 lines
[+20] [+] public boolean verify(String arg0, SSLSession arg1) {
358
      httpsURLConnection.setRequestMethod("POST");
350
      httpsURLConnection.setRequestMethod("POST");
359
      httpsURLConnection.getOutputStream().write(json.getBytes());
351
      httpsURLConnection.getOutputStream().write(json.getBytes());
360

    
   
352

   
361
      int statusCode = httpsURLConnection.getResponseCode();
353
      int statusCode = httpsURLConnection.getResponseCode();
362
      Assert.assertEquals(200, statusCode);
354
      Assert.assertEquals(200, statusCode);

    
   
355

   

    
   
356
      Transaction transaction = channel.getTransaction();

    
   
357
      transaction.begin();

    
   
358
      for(int i = 0; i < 10; i++) {

    
   
359
        Event e = channel.take();

    
   
360
        Assert.assertNotNull(e);

    
   
361
        Assert.assertEquals(String.valueOf(i), e.getHeaders().get("MsgNum"));

    
   
362
      }

    
   
363

   

    
   
364
    transaction.commit();

    
   
365
    transaction.close();
363
    } catch (Exception exception) {
366
    } catch (Exception exception) {
364
      Assert.fail("Exception not expected");
367
      Assert.fail("Exception not expected");
365
      exception.printStackTrace();

   
366
    } finally {
368
    } finally {
367
      httpsURLConnection.disconnect();
369
      httpsURLConnection.disconnect();
368
    }
370
    }
369
  }
371
  }
370

    
   
372

   

    
   
373
  @Test

    
   
374
  public void testHttpsSourceNonHttpsClient() throws Exception {

    
   
375
    Type listType = new TypeToken<List<JSONEvent>>() {

    
   
376
    }.getType();

    
   
377
    List<JSONEvent> events = Lists.newArrayList();

    
   
378
    Random rand = new Random();

    
   
379
    for (int i = 0; i < 10; i++) {

    
   
380
        Map<String, String> input = Maps.newHashMap();

    
   
381
        for (int j = 0; j < 10; j++) {

    
   
382
            input.put(String.valueOf(i) + String.valueOf(j), String.valueOf(i));

    
   
383
        }

    
   
384
        input.put("MsgNum", String.valueOf(i));

    
   
385
        JSONEvent e = new JSONEvent();

    
   
386
        e.setHeaders(input);

    
   
387
        e.setBody(String.valueOf(rand.nextGaussian()).getBytes("UTF-8"));

    
   
388
        events.add(e);

    
   
389
    }

    
   
390
    Gson gson = new Gson();

    
   
391
    String json = gson.toJson(events, listType);

    
   
392
    HttpURLConnection httpURLConnection = null;

    
   
393
    try {

    
   
394
        URL url = new URL("http://0.0.0.0:" + sslPort);

    
   
395
        httpURLConnection = (HttpURLConnection) url.openConnection();

    
   
396
        httpURLConnection.setDoInput(true);

    
   
397
        httpURLConnection.setDoOutput(true);

    
   
398
        httpURLConnection.setRequestMethod("POST");

    
   
399
        httpURLConnection.getOutputStream().write(json.getBytes());

    
   
400
        httpURLConnection.getResponseCode();

    
   
401

   

    
   
402
        Assert.fail("HTTP Client cannot connect to HTTPS source");

    
   
403
    } catch (Exception exception) {

    
   
404
        Assert.assertTrue("Exception expected", true);

    
   
405
    } finally {

    
   
406
        httpURLConnection.disconnect();

    
   
407
    }

    
   
408
  }

    
   
409

   
371
  private void takeWithEncoding(String encoding, int n, List<JSONEvent> events)
410
  private void takeWithEncoding(String encoding, int n, List<JSONEvent> events)
372
          throws Exception{
411
          throws Exception{
373
    Transaction tx = channel.getTransaction();
412
    Transaction tx = channel.getTransaction();
374
    tx.begin();
413
    tx.begin();
375
    Event e = null;
414
    Event e = null;
[+20] [20] 35 lines
  1. flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java: Loading...