1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.crypto;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.net.URLEncoder;
25 import java.security.Key;
26 import java.security.KeyStore;
27 import java.security.MessageDigest;
28 import java.util.Properties;
29
30 import javax.crypto.spec.SecretKeySpec;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
35 import org.apache.hadoop.hbase.testclassification.SmallTests;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 @Category(SmallTests.class)
42 public class TestKeyStoreKeyProvider {
43
44 private static final Log LOG = LogFactory.getLog(TestKeyStoreKeyProvider.class);
45 static final HBaseCommonTestingUtility TEST_UTIL = new HBaseCommonTestingUtility();
46 static final String ALIAS = "test";
47 static final String PASSWORD = "password";
48
49 static byte[] KEY;
50 static File storeFile;
51 static File passwordFile;
52
53 @BeforeClass
54 public static void setUp() throws Exception {
55 KEY = MessageDigest.getInstance("SHA-256").digest(Bytes.toBytes(ALIAS));
56
57 KeyStore store = KeyStore.getInstance("JCEKS");
58 store.load(null, PASSWORD.toCharArray());
59 store.setEntry(ALIAS,
60 new KeyStore.SecretKeyEntry(new SecretKeySpec(KEY, "AES")),
61 new KeyStore.PasswordProtection(PASSWORD.toCharArray()));
62
63 String dataDir = TEST_UTIL.getDataTestDir().toString();
64 new File(dataDir).mkdirs();
65
66 storeFile = new File(dataDir, "keystore.jks");
67 FileOutputStream os = new FileOutputStream(storeFile);
68 try {
69 store.store(os, PASSWORD.toCharArray());
70 } finally {
71 os.close();
72 }
73
74 Properties p = new Properties();
75 p.setProperty(ALIAS, PASSWORD);
76 passwordFile = new File(dataDir, "keystore.pw");
77 os = new FileOutputStream(passwordFile);
78 try {
79 p.store(os, "");
80 } finally {
81 os.close();
82 }
83 }
84
85 @Test(timeout=30000)
86 public void testKeyStoreKeyProviderWithPassword() throws Exception {
87 KeyProvider provider = new KeyStoreKeyProvider();
88 provider.init("jceks://" + storeFile.toURI().getPath() + "?password=" + PASSWORD);
89 Key key = provider.getKey(ALIAS);
90 assertNotNull(key);
91 byte[] keyBytes = key.getEncoded();
92 assertEquals(keyBytes.length, KEY.length);
93 for (int i = 0; i < KEY.length; i++) {
94 assertEquals(keyBytes[i], KEY[i]);
95 }
96 }
97
98 @Test(timeout=30000)
99 public void testKeyStoreKeyProviderWithPasswordFile() throws Exception {
100 KeyProvider provider = new KeyStoreKeyProvider();
101 provider.init("jceks://" + storeFile.toURI().getPath() + "?passwordFile=" +
102 URLEncoder.encode(passwordFile.getAbsolutePath(), "UTF-8"));
103 Key key = provider.getKey(ALIAS);
104 assertNotNull(key);
105 byte[] keyBytes = key.getEncoded();
106 assertEquals(keyBytes.length, KEY.length);
107 for (int i = 0; i < KEY.length; i++) {
108 assertEquals(keyBytes[i], KEY[i]);
109 }
110 }
111 }