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  
19  package org.apache.hadoop.hbase.regionserver.wal;
20  
21  import java.io.IOException;
22  import java.security.Key;
23  import java.security.SecureRandom;
24  
25  import javax.crypto.spec.SecretKeySpec;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.util.ByteStringer;
29  import org.apache.hadoop.hbase.util.EncryptionTest;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.io.crypto.Cipher;
36  import org.apache.hadoop.hbase.io.crypto.Encryption;
37  import org.apache.hadoop.hbase.io.crypto.Encryptor;
38  import org.apache.hadoop.hbase.protobuf.generated.WALProtos.WALHeader;
39  import org.apache.hadoop.hbase.security.EncryptionUtil;
40  import org.apache.hadoop.hbase.security.User;
41  
42  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
43  public class SecureProtobufLogWriter extends ProtobufLogWriter {
44  
45    private static final Log LOG = LogFactory.getLog(SecureProtobufLogWriter.class);
46    private Encryptor encryptor = null;
47  
48    @Override
49    protected WALHeader buildWALHeader(Configuration conf, WALHeader.Builder builder)
50        throws IOException {
51      builder.setWriterClsName(SecureProtobufLogWriter.class.getSimpleName());
52      if (conf.getBoolean(HConstants.ENABLE_WAL_ENCRYPTION, false)) {
53        EncryptionTest.testKeyProvider(conf);
54        EncryptionTest.testCipherProvider(conf);
55  
56        // Get an instance of our cipher
57        final String cipherName =
58            conf.get(HConstants.CRYPTO_WAL_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
59        Cipher cipher = Encryption.getCipher(conf, cipherName);
60        if (cipher == null) {
61          throw new RuntimeException("Cipher '" + cipherName + "' is not available");
62        }
63  
64        // Generate an encryption key for this WAL
65        SecureRandom rng = new SecureRandom();
66        byte[] keyBytes = new byte[cipher.getKeyLength()];
67        rng.nextBytes(keyBytes);
68        Key key = new SecretKeySpec(keyBytes, cipher.getName());
69        builder.setEncryptionKey(ByteStringer.wrap(EncryptionUtil.wrapKey(conf,
70            conf.get(HConstants.CRYPTO_WAL_KEY_NAME_CONF_KEY,
71                conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY,
72                    User.getCurrent().getShortName())),
73            key)));
74  
75        // Set up the encryptor
76        encryptor = cipher.getEncryptor();
77        encryptor.setKey(key);
78  
79        if (LOG.isTraceEnabled()) {
80          LOG.trace("Initialized secure protobuf WAL: cipher=" + cipher.getName());
81        }
82      }
83      builder.setCellCodecClsName(SecureWALCellCodec.class.getName());
84      return super.buildWALHeader(conf, builder);
85    }
86  
87    @Override
88    protected void initAfterHeader(boolean doCompress) throws IOException {
89      if (conf.getBoolean(HConstants.ENABLE_WAL_ENCRYPTION, false) && encryptor != null) {
90        WALCellCodec codec = SecureWALCellCodec.getCodec(this.conf, encryptor);
91        this.cellEncoder = codec.getEncoder(this.output);
92        // We do not support compression
93        this.compressionContext = null;
94      } else {
95        super.initAfterHeader(doCompress);
96      }
97    }
98  
99  }