View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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      // Create a JKECS store containing a test secret key
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      // Create the test directory
63      String dataDir = TEST_UTIL.getDataTestDir().toString();
64      new File(dataDir).mkdirs();
65      // Write the keystore file
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      // Write the password file
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 }