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    private 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  
94      // Write the WAL
95      final WAL wal = wals.getWAL(regioninfo.getEncodedNameAsBytes());
96  
97      for (int i = 0; i < total; i++) {
98        WALEdit kvs = new WALEdit();
99        kvs.add(new KeyValue(row, family, Bytes.toBytes(i), value));
100       wal.append(htd, regioninfo, new WALKey(regioninfo.getEncodedNameAsBytes(), tableName,
101           System.currentTimeMillis()), kvs, true);
102     }
103     wal.sync();
104     final Path walPath = DefaultWALProvider.getCurrentFileName(wal);
105     wals.shutdown();
106 
107     // Insure edits are not plaintext
108     long length = fs.getFileStatus(walPath).getLen();
109     FSDataInputStream in = fs.open(walPath);
110     byte[] fileData = new byte[(int)length];
111     IOUtils.readFully(in, fileData);
112     in.close();
113     assertFalse("Cells appear to be plaintext", Bytes.contains(fileData, value));
114 
115     // Confirm the WAL can be read back
116     WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath);
117     int count = 0;
118     WAL.Entry entry = new WAL.Entry();
119     while (reader.next(entry) != null) {
120       count++;
121       List<Cell> cells = entry.getEdit().getCells();
122       assertTrue("Should be one KV per WALEdit", cells.size() == 1);
123       for (Cell cell: cells) {
124         byte[] thisRow = cell.getRow();
125         assertTrue("Incorrect row", Bytes.equals(thisRow, row));
126         byte[] thisFamily = cell.getFamily();
127         assertTrue("Incorrect family", Bytes.equals(thisFamily, family));
128         byte[] thisValue = cell.getValue();
129         assertTrue("Incorrect value", Bytes.equals(thisValue, value));
130       }
131     }
132     assertEquals("Should have read back as many KVs as written", total, count);
133     reader.close();
134   }
135 
136 }