|
| 1 | +/** |
| 2 | + * |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.apache.hadoop.hbase.replication; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.function.Supplier; |
| 27 | +import org.apache.hadoop.conf.Configuration; |
| 28 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 29 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 30 | +import org.apache.hadoop.hbase.client.Admin; |
| 31 | +import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; |
| 32 | +import org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication; |
| 33 | +import org.apache.hadoop.hbase.security.HBaseKerberosUtils; |
| 34 | +import org.apache.hadoop.hbase.security.access.AccessController; |
| 35 | +import org.apache.hadoop.hbase.security.access.SecureTestUtil; |
| 36 | +import org.apache.hadoop.hbase.security.token.AuthenticationTokenIdentifier; |
| 37 | +import org.apache.hadoop.hbase.security.token.TokenProvider; |
| 38 | +import org.apache.hadoop.hbase.security.visibility.VisibilityTestUtil; |
| 39 | +import org.apache.hadoop.hbase.testclassification.LargeTests; |
| 40 | +import org.apache.hadoop.hbase.testclassification.ReplicationTests; |
| 41 | +import org.apache.hadoop.hbase.zookeeper.ZKClusterId; |
| 42 | +import org.apache.hadoop.hbase.zookeeper.ZKConfig; |
| 43 | +import org.apache.hadoop.io.Text; |
| 44 | +import org.apache.hadoop.mapreduce.Job; |
| 45 | +import org.apache.hadoop.minikdc.MiniKdc; |
| 46 | +import org.apache.hadoop.security.Credentials; |
| 47 | +import org.apache.hadoop.security.UserGroupInformation; |
| 48 | +import org.apache.hadoop.security.token.Token; |
| 49 | +import org.apache.hadoop.security.token.TokenIdentifier; |
| 50 | +import org.junit.AfterClass; |
| 51 | +import org.junit.BeforeClass; |
| 52 | +import org.junit.ClassRule; |
| 53 | +import org.junit.Test; |
| 54 | +import org.junit.experimental.categories.Category; |
| 55 | +import org.junit.runner.RunWith; |
| 56 | +import org.junit.runners.Parameterized; |
| 57 | +import org.junit.runners.Parameterized.Parameter; |
| 58 | +import org.junit.runners.Parameterized.Parameters; |
| 59 | + |
| 60 | +@Category({ ReplicationTests.class, LargeTests.class }) |
| 61 | +@RunWith(Parameterized.class) |
| 62 | +public class TestVerifyReplicationSecureClusterCredentials { |
| 63 | + @ClassRule |
| 64 | + public static final HBaseClassTestRule CLASS_RULE = |
| 65 | + HBaseClassTestRule.forClass(TestVerifyReplicationSecureClusterCredentials.class); |
| 66 | + |
| 67 | + private static MiniKdc KDC; |
| 68 | + private static final HBaseTestingUtil UTIL1 = new HBaseTestingUtil(); |
| 69 | + private static final HBaseTestingUtil UTIL2 = new HBaseTestingUtil(); |
| 70 | + |
| 71 | + private static final File KEYTAB_FILE = |
| 72 | + new File(UTIL1.getDataTestDir("keytab").toUri().getPath()); |
| 73 | + |
| 74 | + private static final String LOCALHOST = "localhost"; |
| 75 | + private static String CLUSTER_PRINCIPAL; |
| 76 | + private static String FULL_USER_PRINCIPAL; |
| 77 | + private static String HTTP_PRINCIPAL; |
| 78 | + |
| 79 | + private static void setUpKdcServer() throws Exception { |
| 80 | + KDC = UTIL1.setupMiniKdc(KEYTAB_FILE); |
| 81 | + String username = UserGroupInformation.getLoginUser().getShortUserName(); |
| 82 | + String userPrincipal = username + '/' + LOCALHOST; |
| 83 | + CLUSTER_PRINCIPAL = userPrincipal; |
| 84 | + FULL_USER_PRINCIPAL = userPrincipal + '@' + KDC.getRealm(); |
| 85 | + HTTP_PRINCIPAL = "HTTP/" + LOCALHOST; |
| 86 | + KDC.createPrincipal(KEYTAB_FILE, CLUSTER_PRINCIPAL, HTTP_PRINCIPAL); |
| 87 | + } |
| 88 | + |
| 89 | + private static void setupCluster(HBaseTestingUtil util) throws Exception { |
| 90 | + Configuration conf = util.getConfiguration(); |
| 91 | + |
| 92 | + SecureTestUtil.enableSecurity(conf); |
| 93 | + VisibilityTestUtil.enableVisiblityLabels(conf); |
| 94 | + SecureTestUtil.verifyConfiguration(conf); |
| 95 | + |
| 96 | + conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, |
| 97 | + AccessController.class.getName() + ',' + TokenProvider.class.getName()); |
| 98 | + |
| 99 | + HBaseKerberosUtils.setSecuredConfiguration(conf, |
| 100 | + CLUSTER_PRINCIPAL + '@' + KDC.getRealm(), HTTP_PRINCIPAL + '@' + KDC.getRealm()); |
| 101 | + |
| 102 | + util.startMiniCluster(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets the security firstly for getting the correct default realm. |
| 107 | + */ |
| 108 | + @BeforeClass |
| 109 | + public static void beforeClass() throws Exception { |
| 110 | + setUpKdcServer(); |
| 111 | + setupCluster(UTIL1); |
| 112 | + setupCluster(UTIL2); |
| 113 | + |
| 114 | + try (Admin admin = UTIL1.getAdmin()) { |
| 115 | + admin.addReplicationPeer("1", ReplicationPeerConfig.newBuilder() |
| 116 | + .setClusterKey(ZKConfig.getZooKeeperClusterKey(UTIL2.getConfiguration())) |
| 117 | + .putConfiguration(HBaseKerberosUtils.KRB_PRINCIPAL, |
| 118 | + UTIL2.getConfiguration().get(HBaseKerberosUtils.KRB_PRINCIPAL)) |
| 119 | + .putConfiguration(HBaseKerberosUtils.MASTER_KRB_PRINCIPAL, |
| 120 | + UTIL2.getConfiguration().get(HBaseKerberosUtils.MASTER_KRB_PRINCIPAL)) |
| 121 | + .build()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @AfterClass |
| 126 | + public static void cleanup() throws IOException { |
| 127 | + UTIL1.shutdownMiniCluster(); |
| 128 | + UTIL2.shutdownMiniCluster(); |
| 129 | + } |
| 130 | + |
| 131 | + @Parameters |
| 132 | + public static Collection<Supplier<String>> peer() { |
| 133 | + return Arrays.asList( |
| 134 | + () -> "1", |
| 135 | + () -> ZKConfig.getZooKeeperClusterKey(UTIL2.getConfiguration()) |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + @Parameter |
| 140 | + public Supplier<String> peer; |
| 141 | + |
| 142 | + @Test |
| 143 | + @SuppressWarnings("unchecked") |
| 144 | + public void testJobCredentials() throws Exception { |
| 145 | + Job job = new VerifyReplication().createSubmittableJob( |
| 146 | + new Configuration(UTIL1.getConfiguration()), |
| 147 | + new String[] { |
| 148 | + peer.get(), |
| 149 | + "table" |
| 150 | + }); |
| 151 | + |
| 152 | + Credentials credentials = job.getCredentials(); |
| 153 | + Collection<Token<? extends TokenIdentifier>> tokens = credentials.getAllTokens(); |
| 154 | + assertEquals(2, tokens.size()); |
| 155 | + |
| 156 | + String clusterId1 = ZKClusterId.readClusterIdZNode(UTIL1.getZooKeeperWatcher()); |
| 157 | + Token<AuthenticationTokenIdentifier> tokenForCluster1 = |
| 158 | + (Token<AuthenticationTokenIdentifier>) credentials.getToken(new Text(clusterId1)); |
| 159 | + assertEquals(FULL_USER_PRINCIPAL, tokenForCluster1.decodeIdentifier().getUsername()); |
| 160 | + |
| 161 | + String clusterId2 = ZKClusterId.readClusterIdZNode(UTIL2.getZooKeeperWatcher()); |
| 162 | + Token<AuthenticationTokenIdentifier> tokenForCluster2 = |
| 163 | + (Token<AuthenticationTokenIdentifier>) credentials.getToken(new Text(clusterId2)); |
| 164 | + assertEquals(FULL_USER_PRINCIPAL, tokenForCluster2.decodeIdentifier().getUsername()); |
| 165 | + } |
| 166 | +} |
0 commit comments