|
| 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.hdfs.server.datanode.web; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.File; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.net.InetSocketAddress; |
| 24 | +import java.net.URL; |
| 25 | +import java.net.URLConnection; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collection; |
| 28 | + |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.fs.FileUtil; |
| 31 | +import org.apache.hadoop.hdfs.DFSConfigKeys; |
| 32 | +import org.apache.hadoop.hdfs.web.URLConnectionFactory; |
| 33 | +import org.apache.hadoop.http.HttpConfig; |
| 34 | +import org.apache.hadoop.http.HttpConfig.Policy; |
| 35 | +import org.apache.hadoop.net.NetUtils; |
| 36 | +import org.apache.hadoop.security.ssl.KeyStoreTestUtil; |
| 37 | +import org.apache.hadoop.test.GenericTestUtils; |
| 38 | +import org.junit.AfterClass; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.BeforeClass; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | +import org.junit.runners.Parameterized; |
| 44 | +import org.junit.runners.Parameterized.Parameters; |
| 45 | + |
| 46 | +@RunWith(value = Parameterized.class) |
| 47 | +public class TestDatanodeHttpServer { |
| 48 | + private static final String BASEDIR = GenericTestUtils |
| 49 | + .getTempPath(TestDatanodeHttpServer.class.getSimpleName()); |
| 50 | + private static String keystoresDir; |
| 51 | + private static String sslConfDir; |
| 52 | + private static Configuration conf; |
| 53 | + private static URLConnectionFactory connectionFactory; |
| 54 | + |
| 55 | + @Parameters |
| 56 | + public static Collection<Object[]> policy() { |
| 57 | + Object[][] params = new Object[][] {{HttpConfig.Policy.HTTP_ONLY}, |
| 58 | + {HttpConfig.Policy.HTTPS_ONLY}, {HttpConfig.Policy.HTTP_AND_HTTPS}}; |
| 59 | + return Arrays.asList(params); |
| 60 | + } |
| 61 | + |
| 62 | + private final HttpConfig.Policy policy; |
| 63 | + |
| 64 | + public TestDatanodeHttpServer(Policy policy) { |
| 65 | + super(); |
| 66 | + this.policy = policy; |
| 67 | + } |
| 68 | + |
| 69 | + @BeforeClass |
| 70 | + public static void setUp() throws Exception { |
| 71 | + File base = new File(BASEDIR); |
| 72 | + FileUtil.fullyDelete(base); |
| 73 | + base.mkdirs(); |
| 74 | + conf = new Configuration(); |
| 75 | + keystoresDir = new File(BASEDIR).getAbsolutePath(); |
| 76 | + sslConfDir = KeyStoreTestUtil.getClasspathDir(TestDatanodeHttpServer.class); |
| 77 | + KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false); |
| 78 | + connectionFactory = URLConnectionFactory |
| 79 | + .newDefaultURLConnectionFactory(conf); |
| 80 | + conf.set(DFSConfigKeys.DFS_CLIENT_HTTPS_KEYSTORE_RESOURCE_KEY, |
| 81 | + KeyStoreTestUtil.getClientSSLConfigFileName()); |
| 82 | + conf.set(DFSConfigKeys.DFS_SERVER_HTTPS_KEYSTORE_RESOURCE_KEY, |
| 83 | + KeyStoreTestUtil.getServerSSLConfigFileName()); |
| 84 | + } |
| 85 | + |
| 86 | + @AfterClass |
| 87 | + public static void tearDown() throws Exception { |
| 88 | + FileUtil.fullyDelete(new File(BASEDIR)); |
| 89 | + KeyStoreTestUtil.cleanupSSLConfig(keystoresDir, sslConfDir); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testHttpPolicy() throws Exception { |
| 94 | + conf.set(DFSConfigKeys.DFS_HTTP_POLICY_KEY, policy.name()); |
| 95 | + conf.set(DFSConfigKeys.DFS_DATANODE_HTTP_ADDRESS_KEY, "localhost:0"); |
| 96 | + conf.set(DFSConfigKeys.DFS_DATANODE_HTTPS_ADDRESS_KEY, "localhost:0"); |
| 97 | + |
| 98 | + DatanodeHttpServer server = null; |
| 99 | + try { |
| 100 | + server = new DatanodeHttpServer(conf, null, null); |
| 101 | + server.start(); |
| 102 | + |
| 103 | + Assert.assertTrue(implies(policy.isHttpEnabled(), |
| 104 | + canAccess("http", server.getHttpAddress()))); |
| 105 | + Assert.assertTrue(implies(!policy.isHttpEnabled(), |
| 106 | + server.getHttpAddress() == null)); |
| 107 | + |
| 108 | + Assert.assertTrue(implies(policy.isHttpsEnabled(), |
| 109 | + canAccess("https", server.getHttpsAddress()))); |
| 110 | + Assert.assertTrue(implies(!policy.isHttpsEnabled(), |
| 111 | + server.getHttpsAddress() == null)); |
| 112 | + |
| 113 | + } finally { |
| 114 | + if (server != null) { |
| 115 | + server.close(); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static boolean canAccess(String scheme, InetSocketAddress addr) { |
| 121 | + if (addr == null) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + try { |
| 125 | + URL url = new URL(scheme + "://" + NetUtils.getHostPortString(addr)); |
| 126 | + URLConnection conn = connectionFactory.openConnection(url); |
| 127 | + conn.connect(); |
| 128 | + Assert.assertTrue(conn instanceof java.net.HttpURLConnection); |
| 129 | + java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) conn; |
| 130 | + if (httpConn.getResponseCode() != 200) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + StringBuilder builder = new StringBuilder(); |
| 135 | + InputStreamReader responseReader = new InputStreamReader((conn.getInputStream())); |
| 136 | + try (BufferedReader reader = new BufferedReader(responseReader)) { |
| 137 | + String output; |
| 138 | + while ((output = reader.readLine()) != null) { |
| 139 | + builder.append(output); |
| 140 | + } |
| 141 | + } |
| 142 | + return builder.toString().contains("Hadoop Administration"); |
| 143 | + } catch (Exception e) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static boolean implies(boolean a, boolean b) { |
| 149 | + return !a || b; |
| 150 | + } |
| 151 | +} |
0 commit comments