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