View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.wal;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.List;
25  import java.util.concurrent.atomic.AtomicLong;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.commons.logging.impl.Log4JLogger;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.FSDataInputStream;
33  import org.apache.hadoop.fs.FileSystem;
34  import org.apache.hadoop.fs.Path;
35  import org.apache.hadoop.hbase.Cell;
36  import org.apache.hadoop.hbase.HBaseTestingUtility;
37  import org.apache.hadoop.hbase.HColumnDescriptor;
38  import org.apache.hadoop.hbase.HConstants;
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.HTableDescriptor;
41  import org.apache.hadoop.hbase.KeyValue;
42  import org.apache.hadoop.hbase.testclassification.MediumTests;
43  import org.apache.hadoop.hbase.TableName;
44  import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
45  import org.apache.hadoop.hbase.util.Bytes;
46  import org.apache.hadoop.hbase.util.FSUtils;
47  import org.apache.log4j.Level;
48  
49  // imports for things that haven't moved from regionserver.wal yet.
50  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
51  import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogReader;
52  import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogWriter;
53  
54  import org.junit.BeforeClass;
55  import org.junit.Test;
56  import org.junit.experimental.categories.Category;
57  
58  @Category(MediumTests.class)
59  public class TestSecureWAL {
60    static final Log LOG = LogFactory.getLog(TestSecureWAL.class);
61    static {
62      ((Log4JLogger)LogFactory.getLog("org.apache.hadoop.hbase.regionserver.wal"))
63        .getLogger().setLevel(Level.ALL);
64    };
65    static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
66  
67    @BeforeClass
68    public static void setUpBeforeClass() throws Exception {
69      Configuration conf = TEST_UTIL.getConfiguration();
70      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
71      conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
72      conf.setClass("hbase.regionserver.hlog.reader.impl", SecureProtobufLogReader.class,
73        WAL.Reader.class);
74      conf.setClass("hbase.regionserver.hlog.writer.impl", SecureProtobufLogWriter.class,
75        WALProvider.Writer.class);
76      conf.setBoolean(HConstants.ENABLE_WAL_ENCRYPTION, true);
77      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
78    }
79  
80    @Test
81    public void testSecureWAL() throws Exception {
82      TableName tableName = TableName.valueOf("TestSecureWAL");
83      HTableDescriptor htd = new HTableDescriptor(tableName);
84      htd.addFamily(new HColumnDescriptor(tableName.getName()));
85      HRegionInfo regioninfo = new HRegionInfo(tableName,
86        HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, false);
87      final int total = 10;
88      final byte[] row = Bytes.toBytes("row");
89      final byte[] family = Bytes.toBytes("family");
90      final byte[] value = Bytes.toBytes("Test value");
91      FileSystem fs = TEST_UTIL.getTestFileSystem();
92      final WALFactory wals = new WALFactory(TEST_UTIL.getConfiguration(), null, "TestSecureWAL");
93      final AtomicLong sequenceId = new AtomicLong(1);
94  
95      // Write the WAL
96      final WAL wal = wals.getWAL(regioninfo.getEncodedNameAsBytes());
97  
98      for (int i = 0; i < total; i++) {
99        WALEdit kvs = new WALEdit();
100       kvs.add(new KeyValue(row, family, Bytes.toBytes(i), value));
101       wal.append(htd, regioninfo, new WALKey(regioninfo.getEncodedNameAsBytes(), tableName,
102           System.currentTimeMillis()), kvs, sequenceId, true, null);
103     }
104     wal.sync();
105     final Path walPath = DefaultWALProvider.getCurrentFileName(wal);
106     wals.shutdown();
107 
108     // Insure edits are not plaintext
109     long length = fs.getFileStatus(walPath).getLen();
110     FSDataInputStream in = fs.open(walPath);
111     byte[] fileData = new byte[(int)length];
112     IOUtils.readFully(in, fileData);
113     in.close();
114     assertFalse("Cells appear to be plaintext", Bytes.contains(fileData, value));
115 
116     // Confirm the WAL can be read back
117     WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath);
118     int count = 0;
119     WAL.Entry entry = new WAL.Entry();
120     while (reader.next(entry) != null) {
121       count++;
122       List<Cell> cells = entry.getEdit().getCells();
123       assertTrue("Should be one KV per WALEdit", cells.size() == 1);
124       for (Cell cell: cells) {
125         byte[] thisRow = cell.getRow();
126         assertTrue("Incorrect row", Bytes.equals(thisRow, row));
127         byte[] thisFamily = cell.getFamily();
128         assertTrue("Incorrect family", Bytes.equals(thisFamily, family));
129         byte[] thisValue = cell.getValue();
130         assertTrue("Incorrect value", Bytes.equals(thisValue, value));
131       }
132     }
133     assertEquals("Should have read back as many KVs as written", total, count);
134     reader.close();
135   }
136 
137 }