001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io.crypto; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.security.Key; 028import java.util.Arrays; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseConfiguration; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.io.crypto.aes.AES; 033import org.apache.hadoop.hbase.testclassification.MiscTests; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.junit.jupiter.api.Tag; 036import org.junit.jupiter.api.Test; 037 038@Tag(MiscTests.TAG) 039@Tag(SmallTests.TAG) 040public class TestCipherProvider { 041 042 public static class MyCipherProvider implements CipherProvider { 043 private Configuration conf; 044 045 @Override 046 public Configuration getConf() { 047 return conf; 048 } 049 050 @Override 051 public void setConf(Configuration conf) { 052 this.conf = conf; 053 } 054 055 @Override 056 public String getName() { 057 return MyCipherProvider.class.getName(); 058 } 059 060 @Override 061 public String[] getSupportedCiphers() { 062 return new String[] { "TEST" }; 063 } 064 065 @Override 066 public Cipher getCipher(String name) { 067 if (name.equals("TEST")) { 068 return new Cipher(this) { 069 @Override 070 public String getName() { 071 return "TEST"; 072 } 073 074 @Override 075 public int getKeyLength() { 076 return 0; 077 } 078 079 @Override 080 public int getIvLength() { 081 return 0; 082 } 083 084 @Override 085 public Key getRandomKey() { 086 return null; 087 } 088 089 @Override 090 public Encryptor getEncryptor() { 091 return null; 092 } 093 094 @Override 095 public Decryptor getDecryptor() { 096 return null; 097 } 098 099 @Override 100 public OutputStream createEncryptionStream(OutputStream out, Context context, byte[] iv) 101 throws IOException { 102 return null; 103 } 104 105 @Override 106 public OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor) 107 throws IOException { 108 return null; 109 } 110 111 @Override 112 public InputStream createDecryptionStream(InputStream in, Context context, byte[] iv) 113 throws IOException { 114 return null; 115 } 116 117 @Override 118 public InputStream createDecryptionStream(InputStream in, Decryptor decryptor) 119 throws IOException { 120 return null; 121 } 122 }; 123 } 124 return null; 125 } 126 } 127 128 @Test 129 public void testCustomProvider() { 130 Configuration conf = HBaseConfiguration.create(); 131 conf.set(HConstants.CRYPTO_CIPHERPROVIDER_CONF_KEY, MyCipherProvider.class.getName()); 132 CipherProvider provider = Encryption.getCipherProvider(conf); 133 assertTrue(provider instanceof MyCipherProvider); 134 assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains("TEST")); 135 Cipher a = Encryption.getCipher(conf, "TEST"); 136 assertNotNull(a); 137 assertTrue(a.getProvider() instanceof MyCipherProvider); 138 assertEquals("TEST", a.getName()); 139 assertEquals(0, a.getKeyLength()); 140 } 141 142 @Test 143 public void testDefaultProvider() { 144 Configuration conf = HBaseConfiguration.create(); 145 CipherProvider provider = Encryption.getCipherProvider(conf); 146 assertTrue(provider instanceof DefaultCipherProvider); 147 String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES); 148 assertTrue(Arrays.asList(provider.getSupportedCiphers()).contains(algorithm)); 149 Cipher a = Encryption.getCipher(conf, algorithm); 150 assertNotNull(a); 151 assertTrue(a.getProvider() instanceof DefaultCipherProvider); 152 assertEquals(a.getName(), algorithm); 153 assertEquals(AES.KEY_LENGTH, a.getKeyLength()); 154 } 155 156}