|
| 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.http; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.net.URL; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.List; |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.fs.Path; |
| 31 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 32 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 33 | +import org.apache.hadoop.hbase.HConstants; |
| 34 | +import org.apache.hadoop.hbase.LocalHBaseCluster; |
| 35 | +import org.apache.hadoop.hbase.ServerName; |
| 36 | +import org.apache.hadoop.hbase.master.HMaster; |
| 37 | +import org.apache.hadoop.hbase.master.ServerManager; |
| 38 | +import org.apache.hadoop.hbase.testclassification.MiscTests; |
| 39 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 40 | +import org.apache.hadoop.hbase.util.CommonFSUtils; |
| 41 | +import org.apache.hadoop.hbase.util.TestServerHttpUtils; |
| 42 | +import org.junit.AfterClass; |
| 43 | +import org.junit.BeforeClass; |
| 44 | +import org.junit.ClassRule; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.experimental.categories.Category; |
| 47 | + |
| 48 | +/** |
| 49 | + * This class performs tests that ensure sensitive config values found in the HBase UI's Debug Dump |
| 50 | + * are redacted. A config property name must follow one of the regex patterns specified in |
| 51 | + * hadoop.security.sensitive-config-keys in order to have its value redacted. |
| 52 | + */ |
| 53 | +@Category({ MiscTests.class, SmallTests.class }) |
| 54 | +public class TestDebugDumpRedaction { |
| 55 | + private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); |
| 56 | + private static final String XML_CONFIGURATION_START_TAG = "<configuration>"; |
| 57 | + private static final String XML_CONFIGURATION_END_TAG = "</configuration>"; |
| 58 | + private static final int SUBSTRING_OFFSET = XML_CONFIGURATION_END_TAG.length(); |
| 59 | + private static final String PLAIN_TEXT_UTF8 = "text/plain;charset=utf-8"; |
| 60 | + private static final String REDACTED = "******"; |
| 61 | + private static final List<String> SENSITIVE_CONF_PROPERTIES = |
| 62 | + List.of("hbase.zookeeper.property.ssl.trustStore.password", "ssl.client.truststore.password", |
| 63 | + "hbase.rpc.tls.truststore.password", "ssl.server.keystore.password", |
| 64 | + "fs.s3a.server-side-encryption.key", "fs.s3a.encryption.algorithm", "fs.s3a.encryption.key", |
| 65 | + "fs.s3a.secret.key", "fs.s3a.important.secret.key", "fs.s3a.session.key", |
| 66 | + "fs.s3a.secret.session.key", "fs.s3a.session.token", "fs.s3a.secret.session.token", |
| 67 | + "fs.azure.account.key.importantKey", "fs.azure.oauth2.token", "fs.adl.oauth2.token", |
| 68 | + "fs.gs.encryption.sensitive", "fs.gs.proxy.important", "fs.gs.auth.sensitive.info", |
| 69 | + "sensitive.credential", "oauth.important.secret", "oauth.important.password", |
| 70 | + "oauth.important.token"); |
| 71 | + private static LocalHBaseCluster CLUSTER; |
| 72 | + |
| 73 | + @ClassRule |
| 74 | + public static final HBaseClassTestRule CLASS_RULE = |
| 75 | + HBaseClassTestRule.forClass(TestDebugDumpRedaction.class); |
| 76 | + |
| 77 | + @BeforeClass |
| 78 | + public static void beforeClass() throws Exception { |
| 79 | + Configuration conf = UTIL.getConfiguration(); |
| 80 | + |
| 81 | + // Add various config properties with sensitive information that should be redacted |
| 82 | + // when the Debug Dump is performed in the UI. These properties are following the |
| 83 | + // regexes specified by the hadoop.security.sensitive-config-keys property. |
| 84 | + for (String property : SENSITIVE_CONF_PROPERTIES) { |
| 85 | + conf.set(property, "testPassword"); |
| 86 | + } |
| 87 | + |
| 88 | + UTIL.startMiniZKCluster(); |
| 89 | + |
| 90 | + UTIL.startMiniDFSCluster(1); |
| 91 | + Path rootdir = UTIL.getDataTestDirOnTestFS("TestDebugDumpServlet"); |
| 92 | + CommonFSUtils.setRootDir(conf, rootdir); |
| 93 | + |
| 94 | + // The info servers do not run in tests by default. |
| 95 | + // Set them to ephemeral ports so they will start |
| 96 | + // setup configuration |
| 97 | + conf.setInt(HConstants.MASTER_INFO_PORT, 0); |
| 98 | + conf.setInt(HConstants.REGIONSERVER_INFO_PORT, 0); |
| 99 | + |
| 100 | + CLUSTER = new LocalHBaseCluster(conf, 1); |
| 101 | + CLUSTER.startup(); |
| 102 | + CLUSTER.getActiveMaster().waitForMetaOnline(); |
| 103 | + } |
| 104 | + |
| 105 | + @AfterClass |
| 106 | + public static void afterClass() throws Exception { |
| 107 | + if (CLUSTER != null) { |
| 108 | + CLUSTER.shutdown(); |
| 109 | + CLUSTER.join(); |
| 110 | + } |
| 111 | + UTIL.shutdownMiniCluster(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testMasterPasswordsAreRedacted() throws IOException { |
| 116 | + URL debugDumpUrl = |
| 117 | + new URL(TestServerHttpUtils.getMasterInfoServerHostAndPort(CLUSTER) + "/dump"); |
| 118 | + String response = TestServerHttpUtils.getPageContent(debugDumpUrl, PLAIN_TEXT_UTF8); |
| 119 | + |
| 120 | + // Verify this is the master server's debug dump |
| 121 | + assertTrue( |
| 122 | + response.startsWith("Master status for " + CLUSTER.getActiveMaster().getServerName())); |
| 123 | + |
| 124 | + verifyDebugDumpResponseConfig(response); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testRegionServerPasswordsAreRedacted() throws IOException { |
| 129 | + HMaster master = CLUSTER.getActiveMaster(); |
| 130 | + |
| 131 | + ServerManager serverManager = master.getServerManager(); |
| 132 | + List<ServerName> onlineServersList = serverManager.getOnlineServersList(); |
| 133 | + |
| 134 | + assertEquals(1, onlineServersList.size()); |
| 135 | + |
| 136 | + ServerName regionServerName = onlineServersList.get(0); |
| 137 | + int regionServerInfoPort = master.getRegionServerInfoPort(regionServerName); |
| 138 | + String regionServerHostname = regionServerName.getHostname(); |
| 139 | + |
| 140 | + URL debugDumpUrl = |
| 141 | + new URL("http://" + regionServerHostname + ":" + regionServerInfoPort + "/dump"); |
| 142 | + String response = TestServerHttpUtils.getPageContent(debugDumpUrl, PLAIN_TEXT_UTF8); |
| 143 | + |
| 144 | + // Verify this is the region server's debug dump |
| 145 | + assertTrue(response.startsWith("RegionServer status for " + regionServerName)); |
| 146 | + |
| 147 | + verifyDebugDumpResponseConfig(response); |
| 148 | + } |
| 149 | + |
| 150 | + private void verifyDebugDumpResponseConfig(String response) throws IOException { |
| 151 | + // Grab the server's config from the Debug Dump. |
| 152 | + String xmlString = response.substring(response.indexOf(XML_CONFIGURATION_START_TAG), |
| 153 | + response.indexOf(XML_CONFIGURATION_END_TAG) + SUBSTRING_OFFSET); |
| 154 | + |
| 155 | + // Convert the XML string into an InputStream |
| 156 | + Configuration conf = new Configuration(false); |
| 157 | + try (InputStream is = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8))) { |
| 158 | + // Add the InputStream as a resource to the Configuration object |
| 159 | + conf.addResource(is, "DebugDumpXmlConfig"); |
| 160 | + } |
| 161 | + |
| 162 | + // Verify all sensitive properties have had their values redacted |
| 163 | + for (String property : SENSITIVE_CONF_PROPERTIES) { |
| 164 | + assertEquals(REDACTED, conf.get(property)); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments