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