accumulo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] ctubbsii closed pull request #43: ACCUMULO-3970
Date Thu, 01 Jan 1970 00:00:00 GMT
ctubbsii closed pull request #43: ACCUMULO-3970
URL: https://github.com/apache/accumulo/pull/43
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/accumulo/core/conf/Property.java b/core/src/main/java/org/apache/accumulo/core/conf/Property.java
index ef4d87734e..2ba98287dd 100644
--- a/core/src/main/java/org/apache/accumulo/core/conf/Property.java
+++ b/core/src/main/java/org/apache/accumulo/core/conf/Property.java
@@ -512,6 +512,9 @@
   @Experimental
   TABLE_VOLUME_CHOOSER("table.volume.chooser", "org.apache.accumulo.server.fs.RandomVolumeChooser",
PropertyType.CLASSNAME,
       "The class that will be used to select which volume will be used to create new files
for this table."),
+  @Experimental
+  TABLE_VTI_CLASS("table.vti.class", "", PropertyType.STRING, "The class that will be used
to transform key-value pairs"
+      + " to different visibilities at scan-time.\nThe class must be a subclass of VisibilityTransformingIterator"),
 
   // VFS ClassLoader properties
   VFS_CLASSLOADER_SYSTEM_CLASSPATH_PROPERTY(AccumuloVFSClassLoader.VFS_CLASSLOADER_SYSTEM_CLASSPATH_PROPERTY,
"", PropertyType.STRING,
diff --git a/core/src/main/java/org/apache/accumulo/core/iterators/system/VisibilityTransformingIterator.java
b/core/src/main/java/org/apache/accumulo/core/iterators/system/VisibilityTransformingIterator.java
new file mode 100644
index 0000000000..ef707a8b9e
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/iterators/system/VisibilityTransformingIterator.java
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.accumulo.core.iterators.system;
+
+import org.apache.accumulo.core.data.ArrayByteSequence;
+import org.apache.accumulo.core.data.ByteSequence;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.PartialKey;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.iterators.IteratorEnvironment;
+import org.apache.accumulo.core.iterators.SortedKeyValueIterator;
+import org.apache.accumulo.core.security.ColumnVisibility;
+import org.apache.hadoop.io.Text;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.TreeMap;
+
+public abstract class VisibilityTransformingIterator implements SortedKeyValueIterator<Key,Value>
{
+
+  private static final Logger logger = LoggerFactory.getLogger(VisibilityTransformingIterator.class);
+
+  private SortedKeyValueIterator<Key,Value> source;
+  private LinkedList<Map.Entry<Key,Value>> sourceKvPairs = new LinkedList<>();
+  private NavigableMap<Key,Value> vtiKvPairs = new TreeMap<>();
+
+  private Map.Entry<Key,Value> topEntry;
+  private Range seekRange;
+
+  private final Text rowHolder = new Text();
+  private final Text cfHolder = new Text();
+  private final Text cqHolder = new Text();
+
+  @Override
+  public void init(SortedKeyValueIterator<Key,Value> source, Map<String,String>
options, IteratorEnvironment env) throws IOException {
+    this.source = source;
+  }
+
+  @Override
+  public boolean hasTop() {
+    return topEntry != null;
+  }
+
+  @Override
+  public Key getTopKey() {
+    return topEntry.getKey();
+  }
+
+  @Override
+  public Value getTopValue() {
+    return topEntry.getValue();
+  }
+
+  @Override
+  public void next() throws IOException {
+    if (sourceKvPairs.isEmpty() && vtiKvPairs.isEmpty()) {
+      consumeSource();
+    }
+    setTop();
+  }
+
+  private void consumeSource() throws IOException {
+    if (!source.hasTop()) {
+      return;
+    }
+    Key sourceTop = source.getTopKey();
+    Key nextKey = sourceTop.followingKey(PartialKey.ROW_COLFAM_COLQUAL);
+    while (source.hasTop() && source.getTopKey().compareTo(nextKey) < 0 &&
seekRange.contains(source.getTopKey())) {
+      sourceKvPairs.add(new AbstractMap.SimpleImmutableEntry<>(source.getTopKey(),
source.getTopValue()));
+      source.next();
+    }
+    for (Map.Entry<Key,Value> sourceKv : sourceKvPairs) {
+      Key sourceKey = sourceKv.getKey();
+      for (Map.Entry<ColumnVisibility,Value> transformed : transformVisibility(sourceKey,
sourceKv.getValue())) {
+        Key transformedKey = replaceVisibility(sourceKey, transformed.getKey());
+        if (seekRange.contains(transformedKey)) {
+          vtiKvPairs.put(transformedKey, transformed.getValue());
+        }
+      }
+    }
+    setTop();
+  }
+
+  @Override
+  public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive)
throws IOException {
+    seekRange = range;
+    Range adjusted = range;
+    if (!range.isInfiniteStartKey()) {
+      // The start key of the range might be a transformed key, that is, it might not be
present in the source
+      // iterator. Seek to the specified (row, cf, cq) and start transforming from there,
throwing out anything
+      // prior to the specified start key. Complicated a little bit by the fact that visibility
sorts before
+      // timestamp -- we might have to throw out a bunch of different versions of the key
before we start
+      // producing useful data.
+      Key startKey = range.getStartKey();
+      startKey = new Key(startKey.getRow(rowHolder), startKey.getColumnFamily(cfHolder),
startKey.getColumnQualifier(cqHolder));
+      adjusted = new Range(startKey, range.getEndKey());
+    }
+    source.seek(adjusted, columnFamilies, inclusive);
+    consumeSource();
+    if (!adjusted.equals(range)) {
+      while (getTopKey().compareTo(range.getStartKey()) < 0) {
+        next();
+      }
+    }
+  }
+
+  private void setTop() {
+    topEntry = null;
+    if (vtiKvPairs.isEmpty() && sourceKvPairs.isEmpty()) {
+      // exhausted.
+      return;
+    }
+    if (vtiKvPairs.isEmpty()) {
+      topEntry = sourceKvPairs.removeFirst();
+    } else if (sourceKvPairs.isEmpty()) {
+      topEntry = vtiKvPairs.pollFirstEntry();
+    } else {
+      Map.Entry<Key,Value> sourceTop = sourceKvPairs.getFirst();
+      Map.Entry<Key,Value> vtiTop = vtiKvPairs.firstEntry();
+      int cmp = sourceTop.getKey().compareTo(vtiTop.getKey());
+      if (cmp < 0) {
+        topEntry = sourceKvPairs.removeFirst();
+      } else if (cmp > 0) {
+        topEntry = vtiKvPairs.pollFirstEntry();
+      } else {
+        logger.info("Transform key " + vtiTop.getKey() + " also exists in source");
+        topEntry = sourceKvPairs.removeFirst();
+        vtiKvPairs.pollFirstEntry();
+      }
+    }
+  }
+
+  private Key replaceVisibility(Key key, ColumnVisibility newVis) {
+    return new Key(key.getRow(rowHolder), key.getColumnFamily(cfHolder), key.getColumnQualifier(cqHolder),
newVis, key.getTimestamp());
+  }
+
+  public static ColumnVisibility replaceTerm(ColumnVisibility vis, String oldTerm, String
newTerm) {
+    newTerm = ColumnVisibility.quote(newTerm);
+    ByteSequence oldTermBs = new ArrayByteSequence(oldTerm.getBytes());
+    ColumnVisibility.Node root = vis.getParseTree();
+    byte[] expression = vis.getExpression();
+    StringBuilder out = new StringBuilder();
+    stringify(newTerm, oldTermBs, root, expression, out);
+    return new ColumnVisibility(out.toString());
+  }
+
+  private static void stringify(String newTerm, ByteSequence oldTermBs, ColumnVisibility.Node
root, byte[] expression, StringBuilder out) {
+    if (root.getType() == ColumnVisibility.NodeType.TERM) {
+      ByteSequence termBs = root.getTerm(expression);
+      if (termBs.compareTo(oldTermBs) == 0) {
+        out.append(newTerm);
+      } else {
+        out.append(termBs);
+      }
+    } else {
+      String sep = "";
+      for (ColumnVisibility.Node c : root.getChildren()) {
+        out.append(sep);
+        boolean parens = (c.getType() != ColumnVisibility.NodeType.TERM && root.getType()
!= c.getType());
+        if (parens)
+          out.append("(");
+        stringify(newTerm, oldTermBs, c, expression, out);
+        if (parens)
+          out.append(")");
+        sep = root.getType() == ColumnVisibility.NodeType.AND ? "&" : "|";
+      }
+    }
+  }
+
+  protected abstract Collection<? extends Map.Entry<ColumnVisibility,Value>>
transformVisibility(Key key, Value value);
+
+  @Override
+  public SortedKeyValueIterator<Key,Value> deepCopy(IteratorEnvironment env) {
+    VisibilityTransformingIterator vtiIter;
+    try {
+      vtiIter = getClass().newInstance();
+    } catch (Exception e) {
+      throw new RuntimeException("Subclasses of VisibilityTransformingIterator must define
a no-arg constructor", e);
+    }
+    vtiIter.source = source.deepCopy(env);
+    return vtiIter;
+  }
+}
diff --git a/server/tserver/src/main/java/org/apache/accumulo/tserver/tablet/ScanDataSource.java
b/server/tserver/src/main/java/org/apache/accumulo/tserver/tablet/ScanDataSource.java
index 853714af08..3e0e001abb 100644
--- a/server/tserver/src/main/java/org/apache/accumulo/tserver/tablet/ScanDataSource.java
+++ b/server/tserver/src/main/java/org/apache/accumulo/tserver/tablet/ScanDataSource.java
@@ -19,11 +19,13 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.accumulo.core.conf.Property;
 import org.apache.accumulo.core.data.Column;
 import org.apache.accumulo.core.data.Key;
 import org.apache.accumulo.core.data.Value;
@@ -41,6 +43,7 @@
 import org.apache.accumulo.core.iterators.system.SourceSwitchingIterator.DataSource;
 import org.apache.accumulo.core.iterators.system.StatsIterator;
 import org.apache.accumulo.core.iterators.system.VisibilityFilter;
+import org.apache.accumulo.core.iterators.system.VisibilityTransformingIterator;
 import org.apache.accumulo.core.metadata.schema.DataFileValue;
 import org.apache.accumulo.core.security.Authorizations;
 import org.apache.accumulo.core.util.Pair;
@@ -170,12 +173,28 @@ public boolean isCurrent() {
 
     ColumnQualifierFilter colFilter = new ColumnQualifierFilter(cfsi, options.getColumnSet());
 
-    VisibilityFilter visFilter = new VisibilityFilter(colFilter, options.getAuthorizations(),
options.getDefaultLabels());
+    SortedKeyValueIterator<Key,Value> visParent = insertVTI(colFilter, iterEnv);
+    VisibilityFilter visFilter = new VisibilityFilter(visParent, options.getAuthorizations(),
options.getDefaultLabels());
 
     return iterEnv.getTopLevelIterator(IteratorUtil.loadIterators(IteratorScope.scan, visFilter,
tablet.getExtent(), tablet.getTableConfiguration(),
         options.getSsiList(), options.getSsio(), iterEnv));
   }
 
+  private SortedKeyValueIterator<Key,Value> insertVTI(final SortedKeyValueIterator<Key,Value>
parent, IteratorEnvironment iterEnv) throws IOException {
+    String vtiClass = tablet.getTableConfiguration().get(Property.TABLE_VTI_CLASS);
+    if (vtiClass != null && vtiClass.length() > 0) {
+      try {
+        VisibilityTransformingIterator vtiIter = (VisibilityTransformingIterator) Class.forName(vtiClass).newInstance();
+        vtiIter.init(parent, Collections.<String,String> emptyMap(), iterEnv);
+        return vtiIter;
+      } catch (Exception e) {
+        throw new IOException("Unable to create VisibilityTransformingIterator " + vtiClass,
e);
+      }
+    } else {
+      return parent;
+    }
+  }
+
   void close(boolean sawErrors) {
 
     if (memIters != null) {
diff --git a/test/src/test/java/org/apache/accumulo/test/vti/VisibilityTransformingIteratorTest.java
b/test/src/test/java/org/apache/accumulo/test/vti/VisibilityTransformingIteratorTest.java
new file mode 100644
index 0000000000..ac02bcbe47
--- /dev/null
+++ b/test/src/test/java/org/apache/accumulo/test/vti/VisibilityTransformingIteratorTest.java
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.accumulo.test.vti;
+
+import com.google.common.collect.Lists;
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+
+import org.apache.accumulo.core.client.BatchWriter;
+import org.apache.accumulo.core.client.BatchWriterConfig;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.Scanner;
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.iterators.system.VisibilityTransformingIterator;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.accumulo.core.security.ColumnVisibility;
+import org.apache.accumulo.minicluster.MiniAccumuloCluster;
+import org.apache.accumulo.minicluster.MiniAccumuloConfig;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class VisibilityTransformingIteratorTest {
+
+  private static final String VTI_TABLE = "vti";
+  private static final ColumnVisibility RAW_VIS = new ColumnVisibility("raw");
+  private static final Authorizations ALL_AUTHS = new Authorizations(HashingIterator.VIS_HASHED,
"raw");
+  private static final Authorizations HASHED_AUTHS = new Authorizations(HashingIterator.VIS_HASHED);
+
+  private static MiniAccumuloCluster mac;
+  private static Connector rootConn;
+
+  @BeforeClass
+  public static void setupMAC() throws Exception {
+    Path macPath = Files.createTempDirectory("mac");
+    System.out.println("MAC running at " + macPath);
+    MiniAccumuloConfig macCfg = new MiniAccumuloConfig(macPath.toFile(), "password");
+    macCfg.setNumTservers(1);
+    mac = new MiniAccumuloCluster(macCfg);
+    mac.start();
+    rootConn = mac.getConnector("root", "password");
+    rootConn.tableOperations().create(VTI_TABLE);
+    rootConn.tableOperations().setProperty("vti", Property.TABLE_VTI_CLASS.getKey(), HashingIterator.class.getName());
+    rootConn.securityOperations().changeUserAuthorizations("root", ALL_AUTHS);
+  }
+
+  @Test
+  public void testVti() throws Exception {
+    BatchWriter bw = rootConn.createBatchWriter(VTI_TABLE, new BatchWriterConfig());
+    Mutation m = new Mutation("r0");
+    m.put("cf0", "cq0", RAW_VIS, new Value("some bytes".getBytes()));
+    bw.addMutation(m);
+    bw.flush();
+
+    Scanner rawScan = rootConn.createScanner(VTI_TABLE, ALL_AUTHS);
+    rawScan.setRange(new Range());
+    List<Map.Entry<Key,Value>> allKvs = Lists.newArrayList(rawScan);
+    assertTrue("All scan must have 2 kv pairs", allKvs.size() == 2);
+    assertTrue("First value must be hashed", HashingIterator.isHashedValue(allKvs.get(0).getValue()));
+    assertFalse("Second value must not be hashed", HashingIterator.isHashedValue(allKvs.get(1).getValue()));
+    assertTrue("Hashed key must have hashed visibility", HashingIterator.isHashedVisibility(allKvs.get(0).getKey()));
+    assertTrue("Raw key must have raw visibility", RAW_VIS.equals(allKvs.get(1).getKey().getColumnVisibilityParsed()));
+    rawScan.close();
+
+    rawScan = rootConn.createScanner(VTI_TABLE, HASHED_AUTHS);
+    rawScan.setRange(new Range());
+    allKvs = Lists.newArrayList(rawScan);
+    assertTrue("Hashed scan must have 1 kv pairs", allKvs.size() == 1);
+    assertTrue("First value must be hashed", HashingIterator.isHashedValue(allKvs.get(0).getValue()));
+    assertTrue("Hashed key must have hashed visibility", HashingIterator.isHashedVisibility(allKvs.get(0).getKey()));
+    rawScan.close();
+  }
+
+  @Before
+  public void clearVtiTable() throws Exception {
+    rootConn.tableOperations().deleteRows(VTI_TABLE, null, null);
+  }
+
+  @AfterClass
+  public static void tearDownMAC() throws Exception {
+    mac.stop();
+  }
+
+  public static class HashingIterator extends VisibilityTransformingIterator {
+
+    private static final String VIS_HASHED = "hashed";
+
+    private static final byte[] HDR = "sha1:".getBytes();
+    private static final HashFunction SHA1 = Hashing.sha1();
+
+    private final byte[] out = new byte[HDR.length + SHA1.bits() / 8];
+
+    public HashingIterator() {
+      System.arraycopy(HDR, 0, out, 0, HDR.length);
+    }
+
+    @Override
+    protected Collection<? extends Map.Entry<ColumnVisibility,Value>> transformVisibility(Key
key, Value value) {
+      byte[] hash = SHA1.hashBytes(value.get()).asBytes();
+      System.arraycopy(hash, 0, out, HDR.length, hash.length);
+      return Collections.singletonList(new AbstractMap.SimpleImmutableEntry<>(replaceTerm(key.getColumnVisibilityParsed(),
"raw", VIS_HASHED), new Value(out)));
+    }
+
+    static boolean isHashedValue(Value v) {
+      byte[] b = v.get();
+      for (int i = 0; i < HDR.length; i++) {
+        if (b[i] != HDR[i]) {
+          return false;
+        }
+      }
+      return true;
+    }
+
+    static boolean isHashedVisibility(Key k) {
+      return VIS_HASHED.equals(k.getColumnVisibility().toString());
+    }
+  }
+
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message