1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import static org.junit.Assert.*;
21
22 import java.security.Key;
23 import java.security.SecureRandom;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.crypto.spec.SecretKeySpec;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.HColumnDescriptor;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.HTableDescriptor;
37 import org.apache.hadoop.hbase.testclassification.MediumTests;
38 import org.apache.hadoop.hbase.TableName;
39 import org.apache.hadoop.hbase.Waiter.Predicate;
40 import org.apache.hadoop.hbase.client.HTable;
41 import org.apache.hadoop.hbase.client.Put;
42 import org.apache.hadoop.hbase.client.Table;
43 import org.apache.hadoop.hbase.io.crypto.Encryption;
44 import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
45 import org.apache.hadoop.hbase.io.crypto.aes.AES;
46 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
47 import org.apache.hadoop.hbase.io.hfile.HFile;
48 import org.apache.hadoop.hbase.security.EncryptionUtil;
49 import org.apache.hadoop.hbase.security.User;
50 import org.apache.hadoop.hbase.util.Bytes;
51
52 import org.junit.AfterClass;
53 import org.junit.BeforeClass;
54 import org.junit.Test;
55 import org.junit.experimental.categories.Category;
56
57 @Category(MediumTests.class)
58 public class TestEncryptionKeyRotation {
59 private static final Log LOG = LogFactory.getLog(TestEncryptionKeyRotation.class);
60 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
61 private static final Configuration conf = TEST_UTIL.getConfiguration();
62 private static final Key initialCFKey;
63 private static final Key secondCFKey;
64 static {
65
66 SecureRandom rng = new SecureRandom();
67 byte[] keyBytes = new byte[AES.KEY_LENGTH];
68 rng.nextBytes(keyBytes);
69 String algorithm =
70 conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
71 initialCFKey = new SecretKeySpec(keyBytes, algorithm);
72 rng.nextBytes(keyBytes);
73 secondCFKey = new SecretKeySpec(keyBytes, algorithm);
74 }
75
76 @BeforeClass
77 public static void setUp() throws Exception {
78 conf.setInt("hfile.format.version", 3);
79 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
80 conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
81
82 conf.setBoolean("hbase.online.schema.update.enable", true);
83
84
85 TEST_UTIL.startMiniCluster(1);
86 }
87
88 @AfterClass
89 public static void tearDown() throws Exception {
90 TEST_UTIL.shutdownMiniCluster();
91 }
92
93 @Test
94 public void testCFKeyRotation() throws Exception {
95
96 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("default",
97 "testCFKeyRotation"));
98 HColumnDescriptor hcd = new HColumnDescriptor("cf");
99 String algorithm =
100 conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
101 hcd.setEncryptionType(algorithm);
102 hcd.setEncryptionKey(EncryptionUtil.wrapKey(conf, "hbase", initialCFKey));
103 htd.addFamily(hcd);
104
105
106 createTableAndFlush(htd);
107
108
109 final List<Path> initialPaths = findStorefilePaths(htd.getTableName());
110 assertTrue(initialPaths.size() > 0);
111 for (Path path: initialPaths) {
112 assertTrue("Store file " + path + " has incorrect key",
113 Bytes.equals(initialCFKey.getEncoded(), extractHFileKey(path)));
114 }
115
116
117 hcd = htd.getFamily(Bytes.toBytes("cf"));
118 hcd.setEncryptionKey(EncryptionUtil.wrapKey(conf,
119 conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, User.getCurrent().getShortName()),
120 secondCFKey));
121 TEST_UTIL.getHBaseAdmin().modifyColumn(htd.getTableName(), hcd);
122 Thread.sleep(5000);
123
124
125 TEST_UTIL.getHBaseAdmin().majorCompact(htd.getTableName());
126 TEST_UTIL.waitFor(30000, 1000, true, new Predicate<Exception>() {
127 @Override
128 public boolean evaluate() throws Exception {
129
130
131 boolean found = false;
132 for (Path path: initialPaths) {
133 found = TEST_UTIL.getTestFileSystem().exists(path);
134 if (found) {
135 LOG.info("Found " + path);
136 break;
137 }
138 }
139 return !found;
140 }
141 });
142
143
144 List<Path> pathsAfterCompaction = findStorefilePaths(htd.getTableName());
145 assertTrue(pathsAfterCompaction.size() > 0);
146 for (Path path: pathsAfterCompaction) {
147 assertFalse("Store file " + path + " retains initial key",
148 Bytes.equals(initialCFKey.getEncoded(), extractHFileKey(path)));
149 assertTrue("Store file " + path + " has incorrect key",
150 Bytes.equals(secondCFKey.getEncoded(), extractHFileKey(path)));
151 }
152 }
153
154 @Test
155 public void testMasterKeyRotation() throws Exception {
156
157 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("default",
158 "testMasterKeyRotation"));
159 HColumnDescriptor hcd = new HColumnDescriptor("cf");
160 String algorithm =
161 conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
162 hcd.setEncryptionType(algorithm);
163 hcd.setEncryptionKey(EncryptionUtil.wrapKey(conf, "hbase", initialCFKey));
164 htd.addFamily(hcd);
165
166
167 createTableAndFlush(htd);
168
169
170 List<Path> storeFilePaths = findStorefilePaths(htd.getTableName());
171 assertTrue(storeFilePaths.size() > 0);
172 for (Path path: storeFilePaths) {
173 assertTrue("Store file " + path + " has incorrect key",
174 Bytes.equals(initialCFKey.getEncoded(), extractHFileKey(path)));
175 }
176
177
178 TEST_UTIL.shutdownMiniHBaseCluster();
179
180
181 conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "other");
182 conf.set(HConstants.CRYPTO_MASTERKEY_ALTERNATE_NAME_CONF_KEY, "hbase");
183
184
185 TEST_UTIL.startMiniHBaseCluster(1, 1);
186
187 TEST_UTIL.waitTableAvailable(htd.getName(), 5000);
188
189 storeFilePaths = findStorefilePaths(htd.getTableName());
190 assertTrue(storeFilePaths.size() > 0);
191 for (Path path: storeFilePaths) {
192 assertTrue("Store file " + path + " has incorrect key",
193 Bytes.equals(initialCFKey.getEncoded(), extractHFileKey(path)));
194 }
195 }
196
197 private static List<Path> findStorefilePaths(TableName tableName) throws Exception {
198 List<Path> paths = new ArrayList<Path>();
199 for (HRegion region:
200 TEST_UTIL.getRSForFirstRegionInTable(tableName).getOnlineRegions(tableName)) {
201 for (Store store: region.getStores().values()) {
202 for (StoreFile storefile: store.getStorefiles()) {
203 paths.add(storefile.getPath());
204 }
205 }
206 }
207 return paths;
208 }
209
210 private void createTableAndFlush(HTableDescriptor htd) throws Exception {
211 HColumnDescriptor hcd = htd.getFamilies().iterator().next();
212
213 TEST_UTIL.getHBaseAdmin().createTable(htd);
214 TEST_UTIL.waitTableAvailable(htd.getName(), 5000);
215
216 Table table = new HTable(conf, htd.getTableName());
217 try {
218 table.put(new Put(Bytes.toBytes("testrow"))
219 .add(hcd.getName(), Bytes.toBytes("q"), Bytes.toBytes("value")));
220 } finally {
221 table.close();
222 }
223 TEST_UTIL.getHBaseAdmin().flush(htd.getTableName());
224 }
225
226 private static byte[] extractHFileKey(Path path) throws Exception {
227 HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), path,
228 new CacheConfig(conf), conf);
229 try {
230 reader.loadFileInfo();
231 Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext();
232 assertNotNull("Reader has a null crypto context", cryptoContext);
233 Key key = cryptoContext.getKey();
234 assertNotNull("Crypto context has no key", key);
235 return key.getEncoded();
236 } finally {
237 reader.close();
238 }
239 }
240
241 }