View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.Waiter.Predicate;
24  import org.apache.hadoop.hbase.client.HBaseAdmin;
25  import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
26  import org.apache.hadoop.hbase.io.hfile.HFile;
27  import org.apache.hadoop.hbase.io.hfile.HFileReaderV3;
28  import org.apache.hadoop.hbase.io.hfile.HFileWriterV3;
29  import org.apache.hadoop.hbase.regionserver.wal.HLog;
30  import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogReader;
31  import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogWriter;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.util.ToolRunner;
34  import org.apache.log4j.Level;
35  import org.apache.log4j.Logger;
36  
37  import org.junit.Before;
38  import org.junit.experimental.categories.Category;
39  
40  @Category(IntegrationTests.class)
41  public class IntegrationTestIngestWithEncryption extends IntegrationTestIngest {
42  
43    static {
44      // These log level changes are only useful when running on a localhost
45      // cluster.
46      Logger.getLogger(HFileReaderV3.class).setLevel(Level.TRACE);
47      Logger.getLogger(HFileWriterV3.class).setLevel(Level.TRACE);
48      Logger.getLogger(SecureProtobufLogReader.class).setLevel(Level.TRACE);
49      Logger.getLogger(SecureProtobufLogWriter.class).setLevel(Level.TRACE);
50    }
51  
52    @Override
53    public void setUpCluster() throws Exception {
54      util = getTestingUtil(null);
55      Configuration conf = util.getConfiguration();
56      conf.setInt(HFile.FORMAT_VERSION_KEY, 3);
57      if (!util.isDistributedCluster()) {
58        // Inject the test key provider and WAL alternative if running on a
59        // localhost cluster; otherwise, whether or not the schema change below
60        // takes effect depends on the distributed cluster site configuration.
61        conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
62        conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
63        conf.setClass("hbase.regionserver.hlog.reader.impl", SecureProtobufLogReader.class,
64          HLog.Reader.class);
65        conf.setClass("hbase.regionserver.hlog.writer.impl", SecureProtobufLogWriter.class,
66          HLog.Writer.class);
67        conf.setBoolean(HConstants.ENABLE_WAL_ENCRYPTION, true);
68      }
69      super.setUpCluster();
70    }
71  
72    @Before
73    @Override
74    public void setUp() throws Exception {
75      // Initialize the cluster. This invokes LoadTestTool -init_only, which
76      // will create the test table, appropriately pre-split
77      super.setUp();
78  
79      // Update the test table schema so HFiles from this point will be written with
80      // encryption features enabled.
81      final HBaseAdmin admin = util.getHBaseAdmin();
82      HTableDescriptor tableDescriptor =
83          new HTableDescriptor(admin.getTableDescriptor(Bytes.toBytes(getTablename())));
84      for (HColumnDescriptor columnDescriptor: tableDescriptor.getColumnFamilies()) {
85        columnDescriptor.setEncryptionType("AES");
86        LOG.info("Updating CF schema for " + getTablename() + "." +
87          columnDescriptor.getNameAsString());
88        admin.disableTable(getTablename());
89        admin.modifyColumn(getTablename(), columnDescriptor);
90        admin.enableTable(getTablename());
91        util.waitFor(30000, 1000, true, new Predicate<IOException>() {
92          @Override
93          public boolean evaluate() throws IOException {
94            return admin.isTableAvailable(getTablename());
95          }
96        });
97      }
98    }
99  
100   public static void main(String[] args) throws Exception {
101     Configuration conf = HBaseConfiguration.create();
102     IntegrationTestingUtility.setUseDistributedCluster(conf);
103     int ret = ToolRunner.run(conf, new IntegrationTestIngestWithEncryption(), args);
104     System.exit(ret);
105   }
106 }