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      reader.loadFileInfo();
078      Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext();
079      assertNotNull("Reader has a null crypto context", cryptoContext);
080      Key key = cryptoContext.getKey();
081      if (key == null) {
082        return null;
083      }
084      return key.getEncoded();
085    } finally {
086      reader.close();
087    }
088  }
089
090  @BeforeClass
091  public static void setUp() throws Exception {
092    conf.setInt("hfile.format.version", 3);
093    conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
094    conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
095
096    // Create the table schema
097    // Specify an encryption algorithm without a key
098    htd = new HTableDescriptor(TableName.valueOf("default", "TestEncryptionRandomKeying"));
099    HColumnDescriptor hcd = new HColumnDescriptor("cf");
100    String algorithm =
101        conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
102    hcd.setEncryptionType(algorithm);
103    htd.addFamily(hcd);
104
105    // Start the minicluster
106    TEST_UTIL.startMiniCluster(1);
107
108    // Create the test table
109    TEST_UTIL.getAdmin().createTable(htd);
110    TEST_UTIL.waitTableAvailable(htd.getTableName(), 5000);
111
112    // Create a store file
113    Table table = TEST_UTIL.getConnection().getTable(htd.getTableName());
114    try {
115      table.put(new Put(Bytes.toBytes("testrow"))
116              .addColumn(hcd.getName(), Bytes.toBytes("q"), Bytes.toBytes("value")));
117    } finally {
118      table.close();
119    }
120    TEST_UTIL.getAdmin().flush(htd.getTableName());
121  }
122
123  @AfterClass
124  public static void tearDown() throws Exception {
125    TEST_UTIL.shutdownMiniCluster();
126  }
127
128  @Test
129  public void testRandomKeying() throws Exception {
130    // Verify we have store file(s) with a random key
131    final List<Path> initialPaths = findStorefilePaths(htd.getTableName());
132    assertTrue(initialPaths.size() > 0);
133    for (Path path: initialPaths) {
134      assertNotNull("Store file " + path + " is not encrypted", extractHFileKey(path));
135    }
136  }
137
138}