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.regionserver.MultiVersionConcurrencyControl;
43  import org.apache.hadoop.hbase.testclassification.MediumTests;
44  import org.apache.hadoop.hbase.TableName;
45  import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
46  import org.apache.hadoop.hbase.util.Bytes;
47  import org.apache.hadoop.hbase.util.FSUtils;
48  import org.apache.log4j.Level;
49  
50  // imports for things that haven't moved from regionserver.wal yet.
51  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
52  import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogReader;
53  import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogWriter;
54  
55  import org.junit.BeforeClass;
56  import org.junit.Test;
57  import org.junit.experimental.categories.Category;
58  
59  @Category(MediumTests.class)
60  public class TestSecureWAL {
61    private static final Log LOG = LogFactory.getLog(TestSecureWAL.class);
62    static {
63      ((Log4JLogger)LogFactory.getLog("org.apache.hadoop.hbase.regionserver.wal"))
64        .getLogger().setLevel(Level.ALL);
65    };
66    static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
67  
68    @BeforeClass
69    public static void setUpBeforeClass() throws Exception {
70      Configuration conf = TEST_UTIL.getConfiguration();
71      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
72      conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
73      conf.setClass("hbase.regionserver.hlog.reader.impl", SecureProtobufLogReader.class,
74        WAL.Reader.class);
75      conf.setClass("hbase.regionserver.hlog.writer.impl", SecureProtobufLogWriter.class,
76        WALProvider.Writer.class);
77      conf.setBoolean(HConstants.ENABLE_WAL_ENCRYPTION, true);
78      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
79    }
80  
81    @Test
82    public void testSecureWAL() throws Exception {
83      TableName tableName = TableName.valueOf("TestSecureWAL");
84      HTableDescriptor htd = new HTableDescriptor(tableName);
85      htd.addFamily(new HColumnDescriptor(tableName.getName()));
86      HRegionInfo regioninfo = new HRegionInfo(tableName,
87        HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW, false);
88      final int total = 10;
89      final byte[] row = Bytes.toBytes("row");
90      final byte[] family = Bytes.toBytes("family");
91      final byte[] value = Bytes.toBytes("Test value");
92      FileSystem fs = TEST_UTIL.getTestFileSystem();
93      final WALFactory wals = new WALFactory(TEST_UTIL.getConfiguration(), null, "TestSecureWAL");
94  
95      // Write the WAL
96      final WAL wal =
97          wals.getWAL(regioninfo.getEncodedNameAsBytes(), regioninfo.getTable().getNamespace());
98  
99      MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl();
100     for (int i = 0; i < total; i++) {
101       WALEdit kvs = new WALEdit();
102       kvs.add(new KeyValue(row, family, Bytes.toBytes(i), value));
103       wal.append(htd, regioninfo, new WALKey(regioninfo.getEncodedNameAsBytes(), tableName,
104           System.currentTimeMillis(), mvcc), kvs, true);
105     }
106     wal.sync();
107     final Path walPath = DefaultWALProvider.getCurrentFileName(wal);
108     wals.shutdown();
109 
110     // Insure edits are not plaintext
111     long length = fs.getFileStatus(walPath).getLen();
112     FSDataInputStream in = fs.open(walPath);
113     byte[] fileData = new byte[(int)length];
114     IOUtils.readFully(in, fileData);
115     in.close();
116     assertFalse("Cells appear to be plaintext", Bytes.contains(fileData, value));
117 
118     // Confirm the WAL can be read back
119     WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath);
120     int count = 0;
121     WAL.Entry entry = new WAL.Entry();
122     while (reader.next(entry) != null) {
123       count++;
124       List<Cell> cells = entry.getEdit().getCells();
125       assertTrue("Should be one KV per WALEdit", cells.size() == 1);
126       for (Cell cell: cells) {
127         byte[] thisRow = cell.getRow();
128         assertTrue("Incorrect row", Bytes.equals(thisRow, row));
129         byte[] thisFamily = cell.getFamily();
130         assertTrue("Incorrect family", Bytes.equals(thisFamily, family));
131         byte[] thisValue = cell.getValue();
132         assertTrue("Incorrect value", Bytes.equals(thisValue, value));
133       }
134     }
135     assertEquals("Should have read back as many KVs as written", total, count);
136     reader.close();
137   }
138 
139 }