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.security.visibility; 019 020import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME; 021import static org.junit.Assert.assertArrayEquals; 022import static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertTrue; 024 025import java.io.IOException; 026import java.security.PrivilegedExceptionAction; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.CellScanner; 030import org.apache.hadoop.hbase.CellUtil; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.HBaseConfiguration; 033import org.apache.hadoop.hbase.HBaseTestingUtility; 034import org.apache.hadoop.hbase.HColumnDescriptor; 035import org.apache.hadoop.hbase.HConstants; 036import org.apache.hadoop.hbase.HTableDescriptor; 037import org.apache.hadoop.hbase.Tag; 038import org.apache.hadoop.hbase.client.Admin; 039import org.apache.hadoop.hbase.client.Connection; 040import org.apache.hadoop.hbase.client.ConnectionFactory; 041import org.apache.hadoop.hbase.client.Get; 042import org.apache.hadoop.hbase.client.Result; 043import org.apache.hadoop.hbase.client.Table; 044import org.apache.hadoop.hbase.codec.KeyValueCodecWithTags; 045import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; 046import org.apache.hadoop.hbase.replication.ReplicationPeerConfig; 047import org.apache.hadoop.hbase.security.User; 048import org.apache.hadoop.hbase.testclassification.MediumTests; 049import org.apache.hadoop.hbase.testclassification.SecurityTests; 050import org.apache.hadoop.hbase.util.Bytes; 051import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 052import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 053import org.junit.Before; 054import org.junit.ClassRule; 055import org.junit.experimental.categories.Category; 056import org.slf4j.Logger; 057import org.slf4j.LoggerFactory; 058 059@Category({ SecurityTests.class, MediumTests.class }) 060public class TestVisibilityLabelReplicationWithExpAsString extends TestVisibilityLabelsReplication { 061 062 @ClassRule 063 public static final HBaseClassTestRule CLASS_RULE = 064 HBaseClassTestRule.forClass(TestVisibilityLabelReplicationWithExpAsString.class); 065 066 private static final Logger LOG = LoggerFactory 067 .getLogger(TestVisibilityLabelReplicationWithExpAsString.class); 068 069 @Override 070 @Before 071 public void setup() throws Exception { 072 expected[0] = 4; 073 expected[1] = 6; 074 expected[2] = 4; 075 expected[3] = 0; 076 expected[3] = 3; 077 expectedVisString[0] = "(\"public\"&\"secret\"&\"topsecret\")|(\"confidential\"&\"topsecret\")"; 078 expectedVisString[1] = "(\"private\"&\"public\")|(\"private\"&\"topsecret\")|" 079 + "(\"confidential\"&\"public\")|(\"confidential\"&\"topsecret\")"; 080 expectedVisString[2] = "(!\"topsecret\"&\"secret\")|(!\"topsecret\"&\"confidential\")"; 081 expectedVisString[3] = "(\"secret\"&\"" + COPYRIGHT + "\\\"" + ACCENT + "\\\\" + SECRET 082 + "\\\"" + "\u0027&\\\\" + "\")"; 083 // setup configuration 084 conf = HBaseConfiguration.create(); 085 conf.setInt("hfile.format.version", 3); 086 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1"); 087 conf.setInt("replication.source.size.capacity", 10240); 088 conf.setLong("replication.source.sleepforretries", 100); 089 conf.setInt("hbase.regionserver.maxlogs", 10); 090 conf.setLong("hbase.master.logcleaner.ttl", 10); 091 conf.setInt("zookeeper.recovery.retry", 1); 092 conf.setInt("zookeeper.recovery.retry.intervalmill", 10); 093 conf.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100); 094 conf.setInt("replication.stats.thread.period.seconds", 5); 095 conf.setBoolean("hbase.tests.use.shortcircuit.reads", false); 096 setVisibilityLabelServiceImpl(conf, ExpAsStringVisibilityLabelServiceImpl.class); 097 conf.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName()); 098 VisibilityTestUtil.enableVisiblityLabels(conf); 099 conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, 100 VisibilityReplication.class.getName()); 101 conf.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY, 102 SimpleCP.class.getName()); 103 // Have to reset conf1 in case zk cluster location different 104 // than default 105 conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class, 106 ScanLabelGenerator.class); 107 conf.set("hbase.superuser", "admin"); 108 conf.set("hbase.superuser", User.getCurrent().getShortName()); 109 SUPERUSER = User.createUserForTesting(conf, User.getCurrent().getShortName(), 110 new String[] { "supergroup" }); 111 User.createUserForTesting(conf, 112 User.getCurrent().getShortName(), new String[] { "supergroup" }); 113 USER1 = User.createUserForTesting(conf, "user1", new String[] {}); 114 TEST_UTIL = new HBaseTestingUtility(conf); 115 TEST_UTIL.startMiniZKCluster(); 116 MiniZooKeeperCluster miniZK = TEST_UTIL.getZkCluster(); 117 zkw1 = new ZKWatcher(conf, "cluster1", null, true); 118 119 // Base conf2 on conf1 so it gets the right zk cluster. 120 conf1 = HBaseConfiguration.create(conf); 121 conf1.setInt("hfile.format.version", 3); 122 conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2"); 123 conf1.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6); 124 conf1.setBoolean("hbase.tests.use.shortcircuit.reads", false); 125 conf1.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName()); 126 conf1.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY, 127 TestCoprocessorForTagsAtSink.class.getName()); 128 setVisibilityLabelServiceImpl(conf1, ExpAsStringVisibilityLabelServiceImpl.class); 129 TEST_UTIL1 = new HBaseTestingUtility(conf1); 130 TEST_UTIL1.setZkCluster(miniZK); 131 zkw2 = new ZKWatcher(conf1, "cluster2", null, true); 132 133 TEST_UTIL.startMiniCluster(1); 134 // Wait for the labels table to become available 135 TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000); 136 TEST_UTIL1.startMiniCluster(1); 137 admin = TEST_UTIL.getAdmin(); 138 139 ReplicationPeerConfig rpc = new ReplicationPeerConfig(); 140 rpc.setClusterKey(TEST_UTIL1.getClusterKey()); 141 admin.addReplicationPeer("2", rpc); 142 143 HTableDescriptor table = new HTableDescriptor(TABLE_NAME); 144 HColumnDescriptor desc = new HColumnDescriptor(fam); 145 desc.setScope(HConstants.REPLICATION_SCOPE_GLOBAL); 146 table.addFamily(desc); 147 try (Admin hBaseAdmin = TEST_UTIL.getAdmin()) { 148 hBaseAdmin.createTable(table); 149 } 150 try (Admin hBaseAdmin1 = TEST_UTIL1.getAdmin()){ 151 hBaseAdmin1.createTable(table); 152 } 153 addLabels(); 154 setAuths(conf); 155 setAuths(conf1); 156 } 157 158 protected static void setVisibilityLabelServiceImpl(Configuration conf, Class clazz) { 159 conf.setClass(VisibilityLabelServiceManager.VISIBILITY_LABEL_SERVICE_CLASS, 160 clazz, VisibilityLabelService.class); 161 } 162 163 @Override 164 protected void verifyGet(final byte[] row, final String visString, final int expected, 165 final boolean nullExpected, final String... auths) throws IOException, 166 InterruptedException { 167 PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() { 168 169 @Override 170 public Void run() throws Exception { 171 try (Connection connection = ConnectionFactory.createConnection(conf1); 172 Table table2 = connection.getTable(TABLE_NAME)) { 173 CellScanner cellScanner; 174 Cell current; 175 Get get = new Get(row); 176 get.setAuthorizations(new Authorizations(auths)); 177 Result result = table2.get(get); 178 cellScanner = result.cellScanner(); 179 boolean advance = cellScanner.advance(); 180 if (nullExpected) { 181 assertTrue(!advance); 182 return null; 183 } 184 current = cellScanner.current(); 185 assertArrayEquals(CellUtil.cloneRow(current), row); 186 assertEquals(expected, TestCoprocessorForTagsAtSink.tags.size()); 187 boolean foundNonVisTag = false; 188 for(Tag t : TestCoprocessorForTagsAtSink.tags) { 189 if(t.getType() == NON_VIS_TAG_TYPE) { 190 assertEquals(TEMP, Bytes.toString(Tag.cloneValue(t))); 191 foundNonVisTag = true; 192 break; 193 } 194 } 195 doAssert(row, visString); 196 assertTrue(foundNonVisTag); 197 return null; 198 } 199 } 200 }; 201 USER1.runAs(scanAction); 202 } 203}