How to implement row level access control in Lucene
Join the DZone community and get the full member experience.
Join For FreeIn the below example there are two fields:
DATA: Which contains any data that you want your users to be able to search. NOTE: You can have as many data fields as you like.
ACL_FIELD: The field used to determine what users have access to this document. Note: You can have as many access control fields as you like.
All you have to do is built the access control query for each user and submit your user's query unchanged.
public class TestIndexerSearcher {
public static void main(String[] args) throws Exception {
Directory directory = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(directory, new StandardAnalyzer());
indexWriter.addDocument(buildDocument("DATA:sametoken","ACL_FIELD:access"));
indexWriter.addDocument(buildDocument("DATA:sametoken","ACL_FIELD:noaccess"));
indexWriter.optimize();
indexWriter.close();
IndexSearcher indexSearcher = new IndexSearcher(directory);
QueryParser parser = new QueryParser("DATA", new StandardAnalyzer());
Query query = parser.parse("sametoken");
//This is all you have to add to your existing code.
Filter aclFilter = applyAccessControl(new TermQuery(
new Term("ACL_FIELD","access")));
Hits hits = indexSearcher.search(query, aclFilter);
System.out.println("Hits[" + hits.length() + "]");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println("DATA [" + doc.get("DATA") +
"] ACL_FIELD [" + doc.get("ACL_FIELD") + "]");
}
indexSearcher.close();
}
private static Filter applyAccessControl(Query aclQuery) {
return new CachedQueryFilter(aclQuery.toString(),
new QueryWrapperFilter(aclQuery));
}
private static Document buildDocument(String... fieldInfo) {
Document document = new Document();
for (int i = 0; i < fieldInfo.length; i++) {
String[] split = fieldInfo[i].split(":");
String fieldName = split[0];
String fieldValue = split[1];
document.add(new Field(fieldName,fieldValue,
Field.Store.YES,Field.Index.TOKENIZED));
}
return document;
}
}
After you run this code, you will get a single hit, not the two that you would normally get if the access control filter wasn't in place.
public class CachedQueryFilter extends Filter {
private static final long serialVersionUID = 6797293376134753695L;
private Filter filter;
private String key;
private static transient Map<String, BitSetCache> filterCache =
new ConcurrentHashMap<String, BitSetCache>();
public CachedQueryFilter(String key, Filter filter) {
this.filter = filter;
this.key = key;
}
public BitSet bits(IndexReader reader) throws IOException {
BitSetCache cachedBitSet = (BitSetCache) filterCache.get(key);
if (cachedBitSet != null) {
BitSet bitSet = cachedBitSet.bitSet.get();
if (bitSet != null && cachedBitSet.indexReaderVersion == reader.getVersion()) {
return bitSet;
}
}
BitSet bits = filter.bits(reader);
BitSetCache bitSetCache = new BitSetCache();
bitSetCache.indexReaderVersion = reader.getVersion();
bitSetCache.bitSet = new SoftReference<BitSet>(bits);
filterCache.put(key, bitSetCache);
return bits;
}
private class BitSetCache {
long indexReaderVersion;
SoftReference<BitSet> bitSet;
}
}
There are two additional features that this query filter doesn't implements that you may want to consider.
1st - Provide per query locking around the bitset creation code. This
would allow multiple bitset creation calls to occur at once, but the
same access control query would block. Therefore we would only have to
build it once, even if multiple user queries with the same access
control hit the query filter at once.
2nd - Persist the bitsets. In the past I have used the same directory
as the index, but you may want to use a database, or something else.
From near @ infinity blog
Opinions expressed by DZone contributors are their own.
Comments