001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertTrue;
022
023import java.security.Key;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HColumnDescriptor;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.HTableDescriptor;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.io.crypto.Encryption;
037import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
038import org.apache.hadoop.hbase.io.hfile.CacheConfig;
039import org.apache.hadoop.hbase.io.hfile.HFile;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.testclassification.RegionServerTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.junit.AfterClass;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048
049@Category({ RegionServerTests.class, MediumTests.class })
050public class TestEncryptionRandomKeying {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestEncryptionRandomKeying.class);
055
056  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
057  private static Configuration conf = TEST_UTIL.getConfiguration();
058  private static HTableDescriptor htd;
059
060  private static List<Path> findStorefilePaths(TableName tableName) throws Exception {
061    List<Path> paths = new ArrayList<>();
062    for (Region region : TEST_UTIL.getRSForFirstRegionInTable(tableName)
063      .getRegions(htd.getTableName())) {
064      for (HStore store : ((HRegion) region).getStores()) {
065        for (HStoreFile storefile : store.getStorefiles()) {
066          paths.add(storefile.getPath());
067        }
068      }
069    }
070    return paths;
071  }
072
073  private static byte[] extractHFileKey(Path path) throws Exception {
074    HFile.Reader reader =
075      HFile.createReader(TEST_UTIL.getTestFileSystem(), path, new CacheConfig(conf), true, conf);
076    try {
077      Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext();
078      assertNotNull("Reader has a null crypto context", cryptoContext);
079      Key key = cryptoContext.getKey();
080      if (key == null) {
081        return null;
082      }
083      return key.getEncoded();
084    } finally {
085      reader.close();
086    }
087  }
088
089  @BeforeClass
090  public static void setUp() throws Exception {
091    conf.setInt("hfile.format.version", 3);
092    conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
093    conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
094
095    // Create the table schema
096    // Specify an encryption algorithm without a key
097    htd = new HTableDescriptor(TableName.valueOf("default", "TestEncryptionRandomKeying"));
098    HColumnDescriptor hcd = new HColumnDescriptor("cf");
099    String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
100    hcd.setEncryptionType(algorithm);
101    htd.addFamily(hcd);
102
103    // Start the minicluster
104    TEST_UTIL.startMiniCluster(1);
105
106    // Create the test table
107    TEST_UTIL.getAdmin().createTable(htd);
108    TEST_UTIL.waitTableAvailable(htd.getTableName(), 5000);
109
110    // Create a store file
111    Table table = TEST_UTIL.getConnection().getTable(htd.getTableName());
112    try {
113      table.put(new Put(Bytes.toBytes("testrow")).addColumn(hcd.getName(), Bytes.toBytes("q"),
114        Bytes.toBytes("value")));
115    } finally {
116      table.close();
117    }
118    TEST_UTIL.getAdmin().flush(htd.getTableName());
119  }
120
121  @AfterClass
122  public static void tearDown() throws Exception {
123    TEST_UTIL.shutdownMiniCluster();
124  }
125
126  @Test
127  public void testRandomKeying() throws Exception {
128    // Verify we have store file(s) with a random key
129    final List<Path> initialPaths = findStorefilePaths(htd.getTableName());
130    assertTrue(initialPaths.size() > 0);
131    for (Path path : initialPaths) {
132      assertNotNull("Store file " + path + " is not encrypted", extractHFileKey(path));
133    }
134  }
135
136}