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:
063        TEST_UTIL.getRSForFirstRegionInTable(tableName).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 = HFile.createReader(TEST_UTIL.getTestFileSystem(), path,
075      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 =
100        conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
101    hcd.setEncryptionType(algorithm);
102    htd.addFamily(hcd);
103
104    // Start the minicluster
105    TEST_UTIL.startMiniCluster(1);
106
107    // Create the test table
108    TEST_UTIL.getAdmin().createTable(htd);
109    TEST_UTIL.waitTableAvailable(htd.getTableName(), 5000);
110
111    // Create a store file
112    Table table = TEST_UTIL.getConnection().getTable(htd.getTableName());
113    try {
114      table.put(new Put(Bytes.toBytes("testrow"))
115              .addColumn(hcd.getName(), Bytes.toBytes("q"), Bytes.toBytes("value")));
116    } finally {
117      table.close();
118    }
119    TEST_UTIL.getAdmin().flush(htd.getTableName());
120  }
121
122  @AfterClass
123  public static void tearDown() throws Exception {
124    TEST_UTIL.shutdownMiniCluster();
125  }
126
127  @Test
128  public void testRandomKeying() throws Exception {
129    // Verify we have store file(s) with a random key
130    final List<Path> initialPaths = findStorefilePaths(htd.getTableName());
131    assertTrue(initialPaths.size() > 0);
132    for (Path path: initialPaths) {
133      assertNotNull("Store file " + path + " is not encrypted", extractHFileKey(path));
134    }
135  }
136
137}