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.replication.ReplicationPeerConfig;
51  import org.apache.hadoop.hbase.security.User;
52  import org.apache.hadoop.hbase.security.visibility.VisibilityController.VisibilityReplication;
53  import org.apache.hadoop.hbase.testclassification.MediumTests;
54  import org.junit.experimental.categories.Category;
55  import org.apache.hadoop.hbase.util.Bytes;
56  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
57  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
58  import org.junit.Before;
59  
60  @Category(MediumTests.class)
61  public class TestVisibilityLabelReplicationWithExpAsString extends TestVisibilityLabelsReplication {
62    private static final Log LOG = LogFactory
63        .getLog(TestVisibilityLabelReplicationWithExpAsString.class);
64  
65    @Override
66    @Before
67    public void setup() throws Exception {
68      expected[0] = 4;
69      expected[1] = 6;
70      expected[2] = 4;
71      expected[3] = 0;
72      expected[3] = 3;
73      expectedVisString[0] = "(\"public\"&\"secret\"&\"topsecret\")|(\"confidential\"&\"topsecret\")";
74      expectedVisString[1] = "(\"private\"&\"public\")|(\"private\"&\"topsecret\")|"
75          + "(\"confidential\"&\"public\")|(\"confidential\"&\"topsecret\")";
76      expectedVisString[2] = "(!\"topsecret\"&\"secret\")|(!\"topsecret\"&\"confidential\")";
77      expectedVisString[3] = "(\"secret\"&\"" + COPYRIGHT + "\\\"" + ACCENT + "\\\\" + SECRET
78          + "\\\"" + "\u0027&\\\\" + "\")";
79      // setup configuration
80      conf = HBaseConfiguration.create();
81      conf.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false);
82      conf.setBoolean("hbase.online.schema.update.enable", true);
83      conf.setInt("hfile.format.version", 3);
84      conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
85      conf.setInt("replication.source.size.capacity", 10240);
86      conf.setLong("replication.source.sleepforretries", 100);
87      conf.setInt("hbase.regionserver.maxlogs", 10);
88      conf.setLong("hbase.master.logcleaner.ttl", 10);
89      conf.setInt("zookeeper.recovery.retry", 1);
90      conf.setInt("zookeeper.recovery.retry.intervalmill", 10);
91      conf.setBoolean("dfs.support.append", true);
92      conf.setLong(HConstants.THREAD_WAKE_FREQUENCY, 100);
93      conf.setInt("replication.stats.thread.period.seconds", 5);
94      conf.setBoolean("hbase.tests.use.shortcircuit.reads", false);
95      setVisibilityLabelServiceImpl(conf, ExpAsStringVisibilityLabelServiceImpl.class);
96      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
97      conf.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName());
98      VisibilityTestUtil.enableVisiblityLabels(conf);
99      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 ZooKeeperWatcher(conf, "cluster1", null, true);
118     replicationAdmin = new ReplicationAdmin(conf);
119 
120     // Base conf2 on conf1 so it gets the right zk cluster.
121     conf1 = HBaseConfiguration.create(conf);
122     conf1.setInt("hfile.format.version", 3);
123     conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
124     conf1.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
125     conf1.setBoolean("dfs.support.append", true);
126     conf1.setBoolean("hbase.tests.use.shortcircuit.reads", false);
127     conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
128     conf1.setStrings(HConstants.REPLICATION_CODEC_CONF_KEY, KeyValueCodecWithTags.class.getName());
129     conf1.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY,
130         TestCoprocessorForTagsAtSink.class.getName());
131     setVisibilityLabelServiceImpl(conf1, ExpAsStringVisibilityLabelServiceImpl.class);
132     TEST_UTIL1 = new HBaseTestingUtility(conf1);
133     TEST_UTIL1.setZkCluster(miniZK);
134     zkw2 = new ZooKeeperWatcher(conf1, "cluster2", null, true);
135     ReplicationPeerConfig rpc = new ReplicationPeerConfig();
136     rpc.setClusterKey(TEST_UTIL1.getClusterKey());
137     replicationAdmin.addPeer("2", rpc);
138 
139     TEST_UTIL.startMiniCluster(1);
140     // Wait for the labels table to become available
141     TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);
142     TEST_UTIL1.startMiniCluster(1);
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 (HBaseAdmin hBaseAdmin = TEST_UTIL.getHBaseAdmin()) {
148       hBaseAdmin.createTable(table);
149     }
150     try (HBaseAdmin hBaseAdmin1 = TEST_UTIL1.getHBaseAdmin()){
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       public Void run() throws Exception {
169         try (Connection connection = ConnectionFactory.createConnection(conf1);
170              Table table2 = connection.getTable(TableName.valueOf(TABLE_NAME))) {
171           CellScanner cellScanner;
172           Cell current;
173           Get get = new Get(row);
174           get.setAuthorizations(new Authorizations(auths));
175           Result result = table2.get(get);
176           cellScanner = result.cellScanner();
177           boolean advance = cellScanner.advance();
178           if (nullExpected) {
179             assertTrue(!advance);
180             return null;
181           }
182           current = cellScanner.current();
183           assertArrayEquals(CellUtil.cloneRow(current), row);
184           assertEquals(expected, TestCoprocessorForTagsAtSink.tags.size());
185           boolean foundNonVisTag = false;
186           for(Tag t : TestCoprocessorForTagsAtSink.tags) {
187             if(t.getType() == NON_VIS_TAG_TYPE) {
188               assertEquals(TEMP, Bytes.toString(t.getValue()));
189               foundNonVisTag = true;
190               break;
191             }
192           }
193           doAssert(row, visString);
194           assertTrue(foundNonVisTag);
195           return null;
196         }
197       }
198     };
199     USER1.runAs(scanAction);
200   }
201 }