View Javadoc

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.security.visibility;
19  
20  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
21  import static org.junit.Assert.assertArrayEquals;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  import java.security.PrivilegedExceptionAction;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Cell;
32  import org.apache.hadoop.hbase.CellScanner;
33  import org.apache.hadoop.hbase.CellUtil;
34  import org.apache.hadoop.hbase.HBaseConfiguration;
35  import org.apache.hadoop.hbase.HBaseTestingUtility;
36  import org.apache.hadoop.hbase.HColumnDescriptor;
37  import org.apache.hadoop.hbase.HConstants;
38  import org.apache.hadoop.hbase.HTableDescriptor;
39  import org.apache.hadoop.hbase.TableName;
40  import org.apache.hadoop.hbase.Tag;
41  import org.apache.hadoop.hbase.client.Connection;
42  import org.apache.hadoop.hbase.client.ConnectionFactory;
43  import org.apache.hadoop.hbase.client.Get;
44  import org.apache.hadoop.hbase.client.HBaseAdmin;
45  import org.apache.hadoop.hbase.client.Result;
46  import org.apache.hadoop.hbase.client.Table;
47  import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
48  import org.apache.hadoop.hbase.codec.KeyValueCodecWithTags;
49  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
50  import org.apache.hadoop.hbase.security.User;
51  import org.apache.hadoop.hbase.security.visibility.VisibilityController.VisibilityReplication;
52  import org.apache.hadoop.hbase.testclassification.MediumTests;
53  import org.junit.experimental.categories.Category;
54  import org.apache.hadoop.hbase.util.Bytes;
55  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
56  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
57  import org.junit.Before;
58  
59  @Category(MediumTests.class)
60  public class TestVisibilityLabelReplicationWithExpAsString extends TestVisibilityLabelsReplication {
61    private static final Log LOG = LogFactory
62        .getLog(TestVisibilityLabelReplicationWithExpAsString.class);
63  
64    @Override
65    @Before
66    public void setup() throws Exception {
67      expected[0] = 4;
68      expected[1] = 6;
69      expected[2] = 4;
70      expected[3] = 0;
71      expected[3] = 3;
72      expectedVisString[0] = "(\"public\"&\"secret\"&\"topsecret\")|(\"confidential\"&\"topsecret\")";
73      expectedVisString[1] = "(\"private\"&\"public\")|(\"private\"&\"topsecret\")|"
74          + "(\"confidential\"&\"public\")|(\"confidential\"&\"topsecret\")";
75      expectedVisString[2] = "(!\"topsecret\"&\"secret\")|(!\"topsecret\"&\"confidential\")";
76      expectedVisString[3] = "(\"secret\"&\"" + COPYRIGHT + "\\\"" + ACCENT + "\\\\" + SECRET
77          + "\\\"" + "\u0027&\\\\" + "\")";
78      // setup configuration
79      conf = HBaseConfiguration.create();
80      conf.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false);
81      conf.setBoolean("hbase.online.schema.update.enable", true);
82      conf.setInt("hfile.format.version", 3);
83      conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
84      conf.setInt("replication.source.size.capacity", 10240);
85      conf.setLong("replication.source.sleepforretries", 100);
86      conf.setInt("hbase.regionserver.maxlogs", 10);
87      conf.setLong("hbase.master.logcleaner.ttl", 10);
88      conf.setInt("zookeeper.recovery.retry", 1);
89      conf.setInt("zookeeper.recovery.retry.intervalmill", 10);
90      conf.setBoolean("dfs.support.append", true);
91      conf.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100);
92      conf.setInt("replication.stats.thread.period.seconds", 5);
93      conf.setBoolean("hbase.tests.use.shortcircuit.reads", false);
94      setVisibilityLabelServiceImpl(conf, ExpAsStringVisibilityLabelServiceImpl.class);
95      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
96      conf.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName());
97      VisibilityTestUtil.enableVisiblityLabels(conf);
98      conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
99          VisibilityReplication.class.getName());
100     conf.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY,
101         SimpleCP.class.getName());
102     // Have to reset conf1 in case zk cluster location different
103     // than default
104     conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS, SimpleScanLabelGenerator.class,
105         ScanLabelGenerator.class);
106     conf.set("hbase.superuser", "admin");
107     conf.set("hbase.superuser", User.getCurrent().getShortName());
108     SUPERUSER = User.createUserForTesting(conf, User.getCurrent().getShortName(),
109         new String[] { "supergroup" });
110     User.createUserForTesting(conf,
111         User.getCurrent().getShortName(), new String[] { "supergroup" });
112     USER1 = User.createUserForTesting(conf, "user1", new String[] {});
113     TEST_UTIL = new HBaseTestingUtility(conf);
114     TEST_UTIL.startMiniZKCluster();
115     MiniZooKeeperCluster miniZK = TEST_UTIL.getZkCluster();
116     zkw1 = new ZooKeeperWatcher(conf, "cluster1", null, true);
117     replicationAdmin = new ReplicationAdmin(conf);
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("dfs.support.append", true);
125     conf1.setBoolean("hbase.tests.use.shortcircuit.reads", false);
126     conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
127     conf1.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName());
128     conf1.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY,
129         TestCoprocessorForTagsAtSink.class.getName());
130     setVisibilityLabelServiceImpl(conf1, ExpAsStringVisibilityLabelServiceImpl.class);
131     TEST_UTIL1 = new HBaseTestingUtility(conf1);
132     TEST_UTIL1.setZkCluster(miniZK);
133     zkw2 = new ZooKeeperWatcher(conf1, "cluster2", null, true);
134     replicationAdmin.addPeer("2", TEST_UTIL1.getClusterKey());
135 
136     TEST_UTIL.startMiniCluster(1);
137     // Wait for the labels table to become available
138     TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);
139     TEST_UTIL1.startMiniCluster(1);
140     HTableDescriptor table = new HTableDescriptor(TABLE_NAME);
141     HColumnDescriptor desc = new HColumnDescriptor(fam);
142     desc.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
143     table.addFamily(desc);
144     try (HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin()) {
145       hBaseAdmin.createTable(table);
146     }
147     try (HBaseAdmin hBaseAdmin1 = TEST_UTIL1.getHBaseAdmin()){
148       hBaseAdmin1.createTable(table);
149     }
150     addLabels();
151     setAuths(conf);
152     setAuths(conf1);
153   }
154 
155   protected static void setVisibilityLabelServiceImpl(Configuration conf, Class clazz) {
156     conf.setClass(VisibilityLabelServiceManager.VISIBILITY_LABEL_SERVICE_CLASS,
157         clazz, VisibilityLabelService.class);
158   }
159 
160   @Override
161   protected void verifyGet(final byte[] row, final String visString, final int expected,
162       final boolean nullExpected, final String... auths) throws IOException,
163       InterruptedException {
164     PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
165       public Void run() throws Exception {
166         try (Connection connection = ConnectionFactory.createConnection(conf1);
167              Table table2 = connection.getTable(TableName.valueOf(TABLE_NAME))) {
168           CellScanner cellScanner;
169           Cell current;
170           Get get = new Get(row);
171           get.setAuthorizations(new Authorizations(auths));
172           Result result = table2.get(get);
173           cellScanner = result.cellScanner();
174           boolean advance = cellScanner.advance();
175           if (nullExpected) {
176             assertTrue(!advance);
177             return null;
178           }
179           current = cellScanner.current();
180           assertArrayEquals(CellUtil.cloneRow(current), row);
181           assertEquals(expected, TestCoprocessorForTagsAtSink.tags.size());
182           boolean foundNonVisTag = false;
183           for(Tag t : TestCoprocessorForTagsAtSink.tags) {
184             if(t.getType() == NON_VIS_TAG_TYPE) {
185               assertEquals(TEMP, Bytes.toString(t.getValue()));
186               foundNonVisTag = true;
187               break;
188             }
189           }
190           doAssert(row, visString);
191           assertTrue(foundNonVisTag);
192           return null;
193         }
194       }
195     };
196     USER1.runAs(scanAction);
197   }
198 }