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.asyncfs;
019
020import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_BLOCK_ACCESS_TOKEN_ENABLE_KEY;
021import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_CLIENT_SOCKET_TIMEOUT_KEY;
022import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_HTTPS_ADDRESS_KEY;
023import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_KERBEROS_PRINCIPAL_KEY;
024import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_KEYTAB_FILE_KEY;
025import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATA_ENCRYPTION_ALGORITHM_KEY;
026import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_ENCRYPT_DATA_TRANSFER_CIPHER_SUITES_KEY;
027import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_ENCRYPT_DATA_TRANSFER_KEY;
028import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_HTTP_POLICY_KEY;
029import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_HTTPS_ADDRESS_KEY;
030import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_KERBEROS_PRINCIPAL_KEY;
031import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY;
032import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY;
033
034import java.io.File;
035import java.io.IOException;
036import java.lang.reflect.Method;
037import java.net.URI;
038import java.util.ArrayList;
039import java.util.Arrays;
040import java.util.List;
041import java.util.concurrent.ExecutionException;
042import org.apache.commons.lang3.StringUtils;
043import org.apache.hadoop.conf.Configuration;
044import org.apache.hadoop.crypto.CipherSuite;
045import org.apache.hadoop.crypto.key.KeyProvider;
046import org.apache.hadoop.crypto.key.KeyProviderFactory;
047import org.apache.hadoop.fs.Path;
048import org.apache.hadoop.hbase.HBaseClassTestRule;
049import org.apache.hadoop.hbase.HBaseTestingUtility;
050import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
051import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
052import org.apache.hadoop.hbase.security.token.TestGenerateDelegationToken;
053import org.apache.hadoop.hbase.testclassification.LargeTests;
054import org.apache.hadoop.hbase.testclassification.MiscTests;
055import org.apache.hadoop.hdfs.DistributedFileSystem;
056import org.apache.hadoop.http.HttpConfig;
057import org.apache.hadoop.minikdc.MiniKdc;
058import org.apache.hadoop.security.UserGroupInformation;
059import org.junit.After;
060import org.junit.AfterClass;
061import org.junit.Before;
062import org.junit.BeforeClass;
063import org.junit.ClassRule;
064import org.junit.Rule;
065import org.junit.Test;
066import org.junit.Ignore;
067import org.junit.experimental.categories.Category;
068import org.junit.rules.TestName;
069import org.junit.runner.RunWith;
070import org.junit.runners.Parameterized;
071import org.junit.runners.Parameterized.Parameter;
072import org.junit.runners.Parameterized.Parameters;
073
074import org.apache.hbase.thirdparty.io.netty.channel.Channel;
075import org.apache.hbase.thirdparty.io.netty.channel.EventLoop;
076import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
077import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
078import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
079
080@RunWith(Parameterized.class)
081@Category({ MiscTests.class, LargeTests.class })
082@Ignore
083public class TestSaslFanOutOneBlockAsyncDFSOutput {
084
085  @ClassRule
086  public static final HBaseClassTestRule CLASS_RULE =
087      HBaseClassTestRule.forClass(TestSaslFanOutOneBlockAsyncDFSOutput.class);
088
089  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
090
091  private static DistributedFileSystem FS;
092
093  private static EventLoopGroup EVENT_LOOP_GROUP;
094
095  private static Class<? extends Channel> CHANNEL_CLASS;
096
097  private static int READ_TIMEOUT_MS = 200000;
098
099  private static final File KEYTAB_FILE =
100    new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath());
101
102  private static MiniKdc KDC;
103
104  private static String HOST = "localhost";
105
106  private static String USERNAME;
107
108  private static String PRINCIPAL;
109
110  private static String HTTP_PRINCIPAL;
111
112  private static String TEST_KEY_NAME = "test_key";
113
114  @Rule
115  public TestName name = new TestName();
116
117  @Parameter(0)
118  public String protection;
119
120  @Parameter(1)
121  public String encryptionAlgorithm;
122
123  @Parameter(2)
124  public String cipherSuite;
125
126  @Parameters(name = "{index}: protection={0}, encryption={1}, cipherSuite={2}")
127  public static Iterable<Object[]> data() {
128    List<Object[]> params = new ArrayList<>();
129    for (String protection : Arrays.asList("authentication", "integrity", "privacy")) {
130      for (String encryptionAlgorithm : Arrays.asList("", "3des", "rc4")) {
131        for (String cipherSuite : Arrays.asList("", CipherSuite.AES_CTR_NOPADDING.getName())) {
132          params.add(new Object[] { protection, encryptionAlgorithm, cipherSuite });
133        }
134      }
135    }
136    return params;
137  }
138
139  private static void setHdfsSecuredConfiguration(Configuration conf) throws Exception {
140    conf.set(DFS_NAMENODE_KERBEROS_PRINCIPAL_KEY, PRINCIPAL + "@" + KDC.getRealm());
141    conf.set(DFS_NAMENODE_KEYTAB_FILE_KEY, KEYTAB_FILE.getAbsolutePath());
142    conf.set(DFS_DATANODE_KERBEROS_PRINCIPAL_KEY, PRINCIPAL + "@" + KDC.getRealm());
143    conf.set(DFS_DATANODE_KEYTAB_FILE_KEY, KEYTAB_FILE.getAbsolutePath());
144    conf.set(DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY, HTTP_PRINCIPAL + "@" + KDC.getRealm());
145    conf.setBoolean(DFS_BLOCK_ACCESS_TOKEN_ENABLE_KEY, true);
146    conf.set(DFS_HTTP_POLICY_KEY, HttpConfig.Policy.HTTPS_ONLY.name());
147    conf.set(DFS_NAMENODE_HTTPS_ADDRESS_KEY, "localhost:0");
148    conf.set(DFS_DATANODE_HTTPS_ADDRESS_KEY, "localhost:0");
149
150    File keystoresDir = new File(TEST_UTIL.getDataTestDir("keystore").toUri().getPath());
151    keystoresDir.mkdirs();
152    String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestGenerateDelegationToken.class);
153    KeyStoreTestUtil.setupSSLConfig(keystoresDir.getAbsolutePath(), sslConfDir, conf, false);
154
155    conf.setBoolean("ignore.secure.ports.for.testing", true);
156  }
157
158  private static void setUpKeyProvider(Configuration conf) throws Exception {
159    URI keyProviderUri =
160      new URI("jceks://file" + TEST_UTIL.getDataTestDir("test.jks").toUri().toString());
161    conf.set("dfs.encryption.key.provider.uri", keyProviderUri.toString());
162    KeyProvider keyProvider = KeyProviderFactory.get(keyProviderUri, conf);
163    keyProvider.createKey(TEST_KEY_NAME, KeyProvider.options(conf));
164    keyProvider.flush();
165    keyProvider.close();
166  }
167
168  @BeforeClass
169  public static void setUpBeforeClass() throws Exception {
170    EVENT_LOOP_GROUP = new NioEventLoopGroup();
171    CHANNEL_CLASS = NioSocketChannel.class;
172    TEST_UTIL.getConfiguration().setInt(DFS_CLIENT_SOCKET_TIMEOUT_KEY, READ_TIMEOUT_MS);
173    KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
174    USERNAME = UserGroupInformation.getLoginUser().getShortUserName();
175    PRINCIPAL = USERNAME + "/" + HOST;
176    HTTP_PRINCIPAL = "HTTP/" + HOST;
177    KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, HTTP_PRINCIPAL);
178
179    setUpKeyProvider(TEST_UTIL.getConfiguration());
180    setHdfsSecuredConfiguration(TEST_UTIL.getConfiguration());
181    HBaseKerberosUtils.setPrincipalForTesting(PRINCIPAL + "@" + KDC.getRealm());
182    HBaseKerberosUtils.setSecuredConfiguration(TEST_UTIL.getConfiguration());
183    UserGroupInformation.setConfiguration(TEST_UTIL.getConfiguration());
184  }
185
186  @AfterClass
187  public static void tearDownAfterClass() throws IOException, InterruptedException {
188    if (EVENT_LOOP_GROUP != null) {
189      EVENT_LOOP_GROUP.shutdownGracefully().sync();
190    }
191    if (KDC != null) {
192      KDC.stop();
193    }
194  }
195
196  private Path testDirOnTestFs;
197
198  private Path entryptionTestDirOnTestFs;
199
200  private void createEncryptionZone() throws Exception {
201    Method method =
202      DistributedFileSystem.class.getMethod("createEncryptionZone", Path.class, String.class);
203    method.invoke(FS, entryptionTestDirOnTestFs, TEST_KEY_NAME);
204  }
205
206  @Before
207  public void setUp() throws Exception {
208    TEST_UTIL.getConfiguration().set("dfs.data.transfer.protection", protection);
209    if (StringUtils.isBlank(encryptionAlgorithm) && StringUtils.isBlank(cipherSuite)) {
210      TEST_UTIL.getConfiguration().setBoolean(DFS_ENCRYPT_DATA_TRANSFER_KEY, false);
211    } else {
212      TEST_UTIL.getConfiguration().setBoolean(DFS_ENCRYPT_DATA_TRANSFER_KEY, true);
213    }
214    if (StringUtils.isBlank(encryptionAlgorithm)) {
215      TEST_UTIL.getConfiguration().unset(DFS_DATA_ENCRYPTION_ALGORITHM_KEY);
216    } else {
217      TEST_UTIL.getConfiguration().set(DFS_DATA_ENCRYPTION_ALGORITHM_KEY, encryptionAlgorithm);
218    }
219    if (StringUtils.isBlank(cipherSuite)) {
220      TEST_UTIL.getConfiguration().unset(DFS_ENCRYPT_DATA_TRANSFER_CIPHER_SUITES_KEY);
221    } else {
222      TEST_UTIL.getConfiguration().set(DFS_ENCRYPT_DATA_TRANSFER_CIPHER_SUITES_KEY, cipherSuite);
223    }
224
225    TEST_UTIL.startMiniDFSCluster(3);
226    FS = TEST_UTIL.getDFSCluster().getFileSystem();
227    testDirOnTestFs = new Path("/" + name.getMethodName().replaceAll("[^0-9a-zA-Z]", "_"));
228    FS.mkdirs(testDirOnTestFs);
229    entryptionTestDirOnTestFs = new Path("/" + testDirOnTestFs.getName() + "_enc");
230    FS.mkdirs(entryptionTestDirOnTestFs);
231    createEncryptionZone();
232  }
233
234  @After
235  public void tearDown() throws IOException {
236    TEST_UTIL.shutdownMiniDFSCluster();
237  }
238
239  private Path getTestFile() {
240    return new Path(testDirOnTestFs, "test");
241  }
242
243  private Path getEncryptionTestFile() {
244    return new Path(entryptionTestDirOnTestFs, "test");
245  }
246
247  private void test(Path file) throws IOException, InterruptedException, ExecutionException {
248    EventLoop eventLoop = EVENT_LOOP_GROUP.next();
249    FanOutOneBlockAsyncDFSOutput out = FanOutOneBlockAsyncDFSOutputHelper.createOutput(FS, file,
250      true, false, (short) 3, FS.getDefaultBlockSize(), eventLoop, CHANNEL_CLASS);
251    TestFanOutOneBlockAsyncDFSOutput.writeAndVerify(FS, file, out);
252  }
253
254  @Test
255  public void test() throws IOException, InterruptedException, ExecutionException {
256    test(getTestFile());
257    test(getEncryptionTestFile());
258  }
259}